wasmi_c_api/
ref.rs

1use crate::{
2    wasm_extern_t,
3    wasm_foreign_t,
4    wasm_func_t,
5    wasm_global_t,
6    wasm_instance_t,
7    wasm_memory_t,
8    wasm_module_t,
9    wasm_table_t,
10    wasm_trap_t,
11};
12use alloc::boxed::Box;
13use core::{ffi::c_void, ptr, unimplemented};
14use wasmi::{ExternRef, Func, Ref, Val};
15
16/// `*mut wasm_ref_t` is a reference type (`externref` or `funcref`) for the C API.
17///
18/// Because we do not have a uniform representation for `funcref`s and `externref`s,
19/// a `*mut wasm_ref_t` is morally a `Option<Box<Either<ExternRef, Func>>>`.
20///
21/// A null `*mut wasm_ref_t` is either a null `funcref` or a null `externref`
22/// depending on context (e.g. the table's element type that it is going into or
23/// coming out of).
24///
25/// Note: this is not `#[repr(C)]` because it is an opaque type in the header,
26/// and only ever referenced as `*mut wasm_ref_t`. This also lets us use a
27/// regular, non-`repr(C)` `enum` to define `WasmRef`.
28#[derive(Clone)]
29pub struct wasm_ref_t {
30    pub(crate) inner: WasmRef,
31}
32
33wasmi_c_api_macros::declare_own!(wasm_ref_t);
34
35#[derive(Clone)]
36pub(crate) enum WasmRef {
37    Func(Ref<Func>),
38    Extern(Ref<ExternRef>),
39}
40
41impl From<WasmRef> for Val {
42    fn from(value: WasmRef) -> Self {
43        match value {
44            WasmRef::Func(r) => Self::FuncRef(r),
45            WasmRef::Extern(r) => Self::ExternRef(r),
46        }
47    }
48}
49
50impl WasmRef {
51    /// Returns `true` if `self` is a Wasm function reference.
52    pub fn is_func(&self) -> bool {
53        matches!(self, Self::Func(_))
54    }
55
56    /// Returns `true` if `self` is a `null` reference.
57    pub fn is_null(&self) -> bool {
58        match self {
59            WasmRef::Func(r) => r.is_null(),
60            WasmRef::Extern(r) => r.is_null(),
61        }
62    }
63}
64
65impl wasm_ref_t {
66    /// Creates a new boxed [`wasm_ref_t`] from the given [`WasmRef`].
67    pub(crate) fn new(r: WasmRef) -> Option<Box<wasm_ref_t>> {
68        if r.is_null() || !r.is_func() {
69            None
70        } else {
71            Some(Box::new(wasm_ref_t { inner: r }))
72        }
73    }
74}
75
76/// Converts the [`wasm_ref_t`] into a Wasmi [`Val`].
77///
78/// # Note
79///
80/// This clones the [`wasm_ref_t`] if necessary and does not consume it.
81pub(crate) fn ref_to_val(r: &wasm_ref_t) -> Val {
82    match r.inner {
83        WasmRef::Func(r) => Val::FuncRef(r),
84        WasmRef::Extern(r) => Val::ExternRef(r),
85    }
86}
87
88/// Copies the [`wasm_ref_t`] and returns the copied reference.
89///
90/// Returns `None` if `r` was `None`.
91#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
92#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
93pub extern "C" fn wasm_ref_copy(r: Option<&wasm_ref_t>) -> Option<Box<wasm_ref_t>> {
94    r.map(|r| Box::new(r.clone()))
95}
96
97/// Returns `true` if both [`wasm_ref_t`] references are referencing the same objects.
98///
99/// # Note
100///
101/// This API is unsupported and will panic upon use.
102#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
103#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
104pub extern "C" fn wasm_ref_same(_a: Option<&wasm_ref_t>, _b: Option<&wasm_ref_t>) -> bool {
105    // In Wasmi we require a store to determine whether these are the same
106    // reference or not and therefore we cannot support this Wasm C API.
107    unimplemented!("wasm_ref_same")
108}
109
110/// Returns the host information of the [`wasm_ref_t`].
111///
112/// # Note
113///
114/// This API is unsupported and always returns a `null` pointer.
115#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
116#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
117pub extern "C" fn wasm_ref_get_host_info(_ref: Option<&wasm_ref_t>) -> *mut c_void {
118    ptr::null_mut()
119}
120
121/// Sets the host information of the [`wasm_ref_t`] to `info`.
122///
123/// # Note
124///
125/// This API is unsupported and will panic upon use.
126#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
127#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
128pub extern "C" fn wasm_ref_set_host_info(_ref: Option<&wasm_ref_t>, _info: *mut c_void) {
129    unimplemented!("wasm_ref_set_host_info")
130}
131
132/// Sets the host information of the [`wasm_ref_t`] to `info` with the associated `finalizer`.
133///
134/// The finalizer is run when deleting the [`wasm_ref_t`].
135///
136/// # Note
137///
138/// This API is unsupported and will panic upon use.
139#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
140#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
141pub extern "C" fn wasm_ref_set_host_info_with_finalizer(
142    _ref: Option<&wasm_ref_t>,
143    _info: *mut c_void,
144    _finalizer: Option<extern "C" fn(*mut c_void)>,
145) {
146    unimplemented!("wasm_ref_set_host_info_with_finalizer")
147}
148
149/// Returns the [`wasm_ref_t`] as shared [`wasm_extern_t`] if possible or otherwise returns `None`.
150///
151/// # Note
152///
153/// This API is unsupported and will panic upon use.
154#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
155#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
156pub extern "C" fn wasm_ref_as_extern(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_extern_t> {
157    unimplemented!("wasm_ref_as_extern")
158}
159
160/// Returns the [`wasm_ref_t`] as mutable [`wasm_extern_t`] if possible or otherwise returns `None`.
161///
162/// # Note
163///
164/// This API is unsupported and will panic upon use.
165#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
166#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
167pub extern "C" fn wasm_ref_as_extern_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_extern_t> {
168    unimplemented!("wasm_ref_as_extern_const")
169}
170
171/// Returns the [`wasm_ref_t`] as shared [`wasm_foreign_t`] if possible or otherwise returns `None`.
172///
173/// # Note
174///
175/// This API is unsupported and will panic upon use.
176#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
177#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
178pub extern "C" fn wasm_ref_as_foreign(
179    _ref: Option<&mut wasm_ref_t>,
180) -> Option<&mut wasm_foreign_t> {
181    unimplemented!("wasm_ref_as_foreign")
182}
183
184/// Returns the [`wasm_ref_t`] as mutable [`wasm_foreign_t`] if possible or otherwise returns `None`.
185///
186/// # Note
187///
188/// This API is unsupported and will panic upon use.
189#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
190#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
191pub extern "C" fn wasm_ref_as_foreign_const(
192    _ref: Option<&wasm_ref_t>,
193) -> Option<&crate::wasm_foreign_t> {
194    unimplemented!("wasm_ref_as_foreign_const")
195}
196
197/// Returns the [`wasm_ref_t`] as shared [`wasm_func_t`] if possible or otherwise returns `None`.
198///
199/// # Note
200///
201/// This API is unsupported and will panic upon use.
202#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
203#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
204pub extern "C" fn wasm_ref_as_func(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_func_t> {
205    unimplemented!("wasm_ref_as_func")
206}
207
208/// Returns the [`wasm_ref_t`] as mutable [`wasm_func_t`] if possible or otherwise returns `None`.
209///
210/// # Note
211///
212/// This API is unsupported and will panic upon use.
213#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
214#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
215pub extern "C" fn wasm_ref_as_func_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_func_t> {
216    unimplemented!("wasm_ref_as_func_const")
217}
218
219/// Returns the [`wasm_ref_t`] as shared [`wasm_global_t`] if possible or otherwise returns `None`.
220///
221/// # Note
222///
223/// This API is unsupported and will panic upon use.
224#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
225#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
226pub extern "C" fn wasm_ref_as_global(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_global_t> {
227    unimplemented!("wasm_ref_as_global")
228}
229
230/// Returns the [`wasm_ref_t`] as mutable [`wasm_global_t`] if possible or otherwise returns `None`.
231///
232/// # Note
233///
234/// This API is unsupported and will panic upon use.
235#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
236#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
237pub extern "C" fn wasm_ref_as_global_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_global_t> {
238    unimplemented!("wasm_ref_as_global_const")
239}
240
241/// Returns the [`wasm_ref_t`] as shared [`wasm_instance_t`] if possible or otherwise returns `None`.
242///
243/// # Note
244///
245/// This API is unsupported and will panic upon use.
246#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
247#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
248pub extern "C" fn wasm_ref_as_instance(
249    _ref: Option<&mut wasm_ref_t>,
250) -> Option<&mut wasm_instance_t> {
251    unimplemented!("wasm_ref_as_instance")
252}
253
254/// Returns the [`wasm_ref_t`] as mutable [`wasm_instance_t`] if possible or otherwise returns `None`.
255///
256/// # Note
257///
258/// This API is unsupported and will panic upon use.
259#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
260#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
261pub extern "C" fn wasm_ref_as_instance_const(
262    _ref: Option<&wasm_ref_t>,
263) -> Option<&wasm_instance_t> {
264    unimplemented!("wasm_ref_as_instance_const")
265}
266
267/// Returns the [`wasm_ref_t`] as shared [`wasm_memory_t`] if possible or otherwise returns `None`.
268///
269/// # Note
270///
271/// This API is unsupported and will panic upon use.
272#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
273#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
274pub extern "C" fn wasm_ref_as_memory(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_memory_t> {
275    unimplemented!("wasm_ref_as_memory")
276}
277
278/// Returns the [`wasm_ref_t`] as mutable [`wasm_memory_t`] if possible or otherwise returns `None`.
279///
280/// # Note
281///
282/// This API is unsupported and will panic upon use.
283#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
284#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
285pub extern "C" fn wasm_ref_as_memory_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_memory_t> {
286    unimplemented!("wasm_ref_as_memory_const")
287}
288
289/// Returns the [`wasm_ref_t`] as shared [`wasm_module_t`] if possible or otherwise returns `None`.
290///
291/// # Note
292///
293/// This API is unsupported and will panic upon use.
294#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
295#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
296pub extern "C" fn wasm_ref_as_module(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_module_t> {
297    unimplemented!("wasm_ref_as_module")
298}
299
300/// Returns the [`wasm_ref_t`] as mutable [`wasm_module_t`] if possible or otherwise returns `None`.
301///
302/// # Note
303///
304/// This API is unsupported and will panic upon use.
305#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
306#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
307pub extern "C" fn wasm_ref_as_module_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_module_t> {
308    unimplemented!("wasm_ref_as_module_const")
309}
310
311/// Returns the [`wasm_ref_t`] as shared [`wasm_table_t`] if possible or otherwise returns `None`.
312///
313/// # Note
314///
315/// This API is unsupported and will panic upon use.
316#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
317#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
318pub extern "C" fn wasm_ref_as_table(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_table_t> {
319    unimplemented!("wasm_ref_as_table")
320}
321
322/// Returns the [`wasm_ref_t`] as mutable [`wasm_table_t`] if possible or otherwise returns `None`.
323///
324/// # Note
325///
326/// This API is unsupported and will panic upon use.
327#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
328#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
329pub extern "C" fn wasm_ref_as_table_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_table_t> {
330    unimplemented!("wasm_ref_as_table_const")
331}
332
333/// Returns the [`wasm_ref_t`] as shared [`wasm_trap_t`] if possible or otherwise returns `None`.
334///
335/// # Note
336///
337/// This API is unsupported and will panic upon use.
338#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
339#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
340pub extern "C" fn wasm_ref_as_trap(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_trap_t> {
341    unimplemented!("wasm_ref_as_trap")
342}
343
344/// Returns the [`wasm_ref_t`] as mutable [`wasm_trap_t`] if possible or otherwise returns `None`.
345///
346/// # Note
347///
348/// This API is unsupported and will panic upon use.
349#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
350#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
351pub extern "C" fn wasm_ref_as_trap_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_trap_t> {
352    unimplemented!("wasm_ref_as_trap_const")
353}