Skip to main content

wasmtime_c_api/
global.rs

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