xref: /wasmtime-44.0.1/crates/c-api/src/global.rs (revision bd2ea901)
1 use crate::{
2     handle_result, wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, wasmtime_error_t,
3     wasmtime_val_t, CStoreContext, CStoreContextMut,
4 };
5 use std::mem::MaybeUninit;
6 use wasmtime::{Extern, Global};
7 
8 #[derive(Clone)]
9 #[repr(transparent)]
10 pub struct wasm_global_t {
11     ext: wasm_extern_t,
12 }
13 
14 wasmtime_c_api_macros::declare_ref!(wasm_global_t);
15 
16 impl wasm_global_t {
17     pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_global_t> {
18         match &e.which {
19             Extern::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
20             _ => None,
21         }
22     }
23 
24     fn global(&self) -> Global {
25         match self.ext.which {
26             Extern::Global(g) => g,
27             _ => unsafe { std::hint::unreachable_unchecked() },
28         }
29     }
30 }
31 
32 #[no_mangle]
33 pub unsafe extern "C" fn wasm_global_new(
34     store: &mut wasm_store_t,
35     gt: &wasm_globaltype_t,
36     val: &wasm_val_t,
37 ) -> Option<Box<wasm_global_t>> {
38     match Global::new(store.store.context_mut(), gt.ty().ty.clone(), val.val()) {
39         Ok(global) => Some(Box::new(wasm_global_t {
40             ext: wasm_extern_t {
41                 store: store.store.clone(),
42                 which: global.into(),
43             },
44         })),
45         Err(_) => None,
46     }
47 }
48 
49 #[no_mangle]
50 pub extern "C" fn wasm_global_as_extern(g: &mut wasm_global_t) -> &mut wasm_extern_t {
51     &mut g.ext
52 }
53 
54 #[no_mangle]
55 pub extern "C" fn wasm_global_as_extern_const(g: &wasm_global_t) -> &wasm_extern_t {
56     &g.ext
57 }
58 
59 #[no_mangle]
60 pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
61     let globaltype = g.global().ty(&g.ext.store.context());
62     Box::new(wasm_globaltype_t::new(globaltype))
63 }
64 
65 #[no_mangle]
66 pub unsafe extern "C" fn wasm_global_get(g: &mut wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
67     let global = g.global();
68     crate::initialize(
69         out,
70         wasm_val_t::from_val(global.get(g.ext.store.context_mut())),
71     );
72 }
73 
74 #[no_mangle]
75 pub unsafe extern "C" fn wasm_global_set(g: &mut wasm_global_t, val: &wasm_val_t) {
76     let global = g.global();
77     drop(global.set(g.ext.store.context_mut(), val.val()));
78 }
79 
80 #[no_mangle]
81 pub unsafe extern "C" fn wasmtime_global_new(
82     mut store: CStoreContextMut<'_>,
83     gt: &wasm_globaltype_t,
84     val: &wasmtime_val_t,
85     ret: &mut Global,
86 ) -> Option<Box<wasmtime_error_t>> {
87     let val = val.to_val(&mut store);
88     let global = Global::new(store, gt.ty().ty.clone(), val);
89     handle_result(global, |global| {
90         *ret = global;
91     })
92 }
93 
94 #[no_mangle]
95 pub extern "C" fn wasmtime_global_type(
96     store: CStoreContext<'_>,
97     global: &Global,
98 ) -> Box<wasm_globaltype_t> {
99     Box::new(wasm_globaltype_t::new(global.ty(store)))
100 }
101 
102 #[no_mangle]
103 pub extern "C" fn wasmtime_global_get(
104     mut store: CStoreContextMut<'_>,
105     global: &Global,
106     val: &mut MaybeUninit<wasmtime_val_t>,
107 ) {
108     let gval = global.get(&mut store);
109     crate::initialize(val, wasmtime_val_t::from_val(store, gval))
110 }
111 
112 #[no_mangle]
113 pub unsafe extern "C" fn wasmtime_global_set(
114     mut store: CStoreContextMut<'_>,
115     global: &Global,
116     val: &wasmtime_val_t,
117 ) -> Option<Box<wasmtime_error_t>> {
118     let val = val.to_val(&mut store);
119     handle_result(global.set(store, val), |()| {})
120 }
121