wasmi_c_api/
extern.rs

1use crate::{
2    wasm_externkind_t,
3    wasm_externtype_t,
4    wasm_func_t,
5    wasm_global_t,
6    wasm_memory_t,
7    wasm_table_t,
8    WasmStoreRef,
9};
10use alloc::boxed::Box;
11use wasmi::Extern;
12
13/// A Wasm external reference.
14///
15/// Wraps [`Extern`].
16#[derive(Clone)]
17pub struct wasm_extern_t {
18    pub(crate) store: WasmStoreRef,
19    pub(crate) which: Extern,
20}
21
22wasmi_c_api_macros::declare_ref!(wasm_extern_t);
23
24/// Returns the [`wasm_extern_kind`] of the [`wasm_extern_t`].
25#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
26#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
27pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
28    match e.which {
29        Extern::Func(_) => wasm_externkind_t::WASM_EXTERN_FUNC,
30        Extern::Global(_) => wasm_externkind_t::WASM_EXTERN_GLOBAL,
31        Extern::Table(_) => wasm_externkind_t::WASM_EXTERN_TABLE,
32        Extern::Memory(_) => wasm_externkind_t::WASM_EXTERN_MEMORY,
33    }
34}
35
36/// Returns the [`wasm_externtype_t`] of the [`wasm_extern_t`].
37///
38/// # Safety
39///
40/// It is the caller's responsibility not to alias the [`wasm_extern_t`]
41/// with its underlying, internal [`WasmStoreRef`].
42#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
43#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
44pub unsafe extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externtype_t> {
45    Box::new(wasm_externtype_t::from_extern_type(
46        e.which.ty(e.store.context()),
47    ))
48}
49
50/// Returns the [`wasm_extern_t`] as reference to mutable [`wasm_func_t`] if possible.
51///
52/// Returns `None` if `e` is not a [`wasm_func_t`].
53#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
54#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
55pub extern "C" fn wasm_extern_as_func(e: &mut wasm_extern_t) -> Option<&mut wasm_func_t> {
56    wasm_func_t::try_from_mut(e)
57}
58
59/// Returns the [`wasm_extern_t`] as reference to shared [`wasm_func_t`] if possible.
60///
61/// Returns `None` if `e` is not a [`wasm_func_t`].
62#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
63#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
64pub extern "C" fn wasm_extern_as_func_const(e: &wasm_extern_t) -> Option<&wasm_func_t> {
65    wasm_func_t::try_from(e)
66}
67
68/// Returns the [`wasm_extern_t`] as reference to mutable [`wasm_global_t`] if possible.
69///
70/// Returns `None` if `e` is not a [`wasm_global_t`].
71#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
72#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
73pub extern "C" fn wasm_extern_as_global(e: &mut wasm_extern_t) -> Option<&mut wasm_global_t> {
74    wasm_global_t::try_from_mut(e)
75}
76
77/// Returns the [`wasm_extern_t`] as reference to shared [`wasm_global_t`] if possible.
78///
79/// Returns `None` if `e` is not a [`wasm_global_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_extern_as_global_const(e: &wasm_extern_t) -> Option<&wasm_global_t> {
83    wasm_global_t::try_from(e)
84}
85
86/// Returns the [`wasm_extern_t`] as reference to mutable [`wasm_table_t`] if possible.
87///
88/// Returns `None` if `e` is not a [`wasm_table_t`].
89#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
90#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
91pub extern "C" fn wasm_extern_as_table(e: &mut wasm_extern_t) -> Option<&mut wasm_table_t> {
92    wasm_table_t::try_from_mut(e)
93}
94
95/// Returns the [`wasm_extern_t`] as reference to shared [`wasm_table_t`] if possible.
96///
97/// Returns `None` if `e` is not a [`wasm_table_t`].
98#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
99#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
100pub extern "C" fn wasm_extern_as_table_const(e: &wasm_extern_t) -> Option<&wasm_table_t> {
101    wasm_table_t::try_from(e)
102}
103
104/// Returns the [`wasm_extern_t`] as reference to mutable [`wasm_memory_t`] if possible.
105///
106/// Returns `None` if `e` is not a [`wasm_memory_t`].
107#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
108#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
109pub extern "C" fn wasm_extern_as_memory(e: &mut wasm_extern_t) -> Option<&mut wasm_memory_t> {
110    wasm_memory_t::try_from_mut(e)
111}
112
113/// Returns the [`wasm_extern_t`] as reference to shared [`wasm_memory_t`] if possible.
114///
115/// Returns `None` if `e` is not a [`wasm_memory_t`].
116#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
117#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
118pub extern "C" fn wasm_extern_as_memory_const(e: &wasm_extern_t) -> Option<&wasm_memory_t> {
119    wasm_memory_t::try_from(e)
120}