wasmi_c_api/
global.rs

1use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t};
2use alloc::boxed::Box;
3use core::{hint, mem::MaybeUninit};
4use wasmi::{Extern, Global};
5
6/// A Wasm global variable.
7///
8/// Wraps [`Global`].
9#[derive(Clone)]
10#[repr(transparent)]
11pub struct wasm_global_t {
12    inner: wasm_extern_t,
13}
14
15wasmi_c_api_macros::declare_ref!(wasm_global_t);
16
17impl wasm_global_t {
18    pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_global_t> {
19        match &e.which {
20            Extern::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
21            _ => None,
22        }
23    }
24
25    pub(crate) fn try_from_mut(e: &mut wasm_extern_t) -> Option<&mut wasm_global_t> {
26        match &mut e.which {
27            Extern::Global(_) => Some(unsafe { &mut *(e as *mut _ as *mut _) }),
28            _ => None,
29        }
30    }
31
32    /// Returns the underlying [`Global`] of the [`wasm_global_t`].
33    fn global(&self) -> Global {
34        match self.inner.which {
35            Extern::Global(g) => g,
36            _ => unsafe { hint::unreachable_unchecked() },
37        }
38    }
39}
40
41/// Creates a new [`wasm_global_t`] from the given [`wasm_globaltype_t`] and [`wasm_val_t`].
42///
43/// Returns a `null` pointer if `ty` and `val` does not match.
44///
45/// Wraps [`Global::new`].
46///
47/// # Safety
48///
49/// It is the caller's responsibility not to alias the [`wasm_global_t`]
50/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
51#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
52#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
53pub unsafe extern "C" fn wasm_global_new(
54    store: &mut wasm_store_t,
55    ty: &wasm_globaltype_t,
56    val: &wasm_val_t,
57) -> Option<Box<wasm_global_t>> {
58    let val = val.to_val();
59    let ty = ty.ty().ty;
60    if val.ty() != ty.content() {
61        return None;
62    }
63    let global = Global::new(store.inner.context_mut(), val, ty.mutability());
64    Some(Box::new(wasm_global_t {
65        inner: wasm_extern_t {
66            store: store.inner.clone(),
67            which: global.into(),
68        },
69    }))
70}
71
72/// Returns the [`wasm_global_t`] as mutable reference to [`wasm_extern_t`].
73#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
74#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
75pub extern "C" fn wasm_global_as_extern(g: &mut wasm_global_t) -> &mut wasm_extern_t {
76    &mut g.inner
77}
78
79/// Returns the [`wasm_global_t`] as shared reference to [`wasm_extern_t`].
80#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
81#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
82pub extern "C" fn wasm_global_as_extern_const(g: &wasm_global_t) -> &wasm_extern_t {
83    &g.inner
84}
85
86/// Returns the [`wasm_globaltype_t`] of the [`wasm_global_t`].
87///
88/// Wraps [`Global::ty`].
89///
90/// # Safety
91///
92/// It is the caller's responsibility not to alias the [`wasm_global_t`]
93/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
94#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
95#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
96pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
97    let globaltype = g.global().ty(g.inner.store.context());
98    Box::new(wasm_globaltype_t::new(globaltype))
99}
100
101/// Returns the current value of the [`wasm_global_t`].
102///
103/// Wraps [`Global::get`].
104///
105/// # Safety
106///
107/// It is the caller's responsibility not to alias the [`wasm_global_t`]
108/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
109#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
110#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
111pub unsafe extern "C" fn wasm_global_get(g: &mut wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
112    let global = g.global();
113    crate::initialize(
114        out,
115        wasm_val_t::from(global.get(g.inner.store.context_mut())),
116    );
117}
118
119/// Sets the current value of the [`wasm_global_t`].
120///
121/// Wraps [`Global::set`].
122///
123/// # Safety
124///
125/// - It is the caller's responsibility that `val` matches the type of `g`.
126/// - It is the caller's responsibility not to alias the [`wasm_global_t`]
127///   with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
128#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
129#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
130pub unsafe extern "C" fn wasm_global_set(g: &mut wasm_global_t, val: &wasm_val_t) {
131    let global = g.global();
132    drop(global.set(g.inner.store.context_mut(), val.to_val()));
133}