Skip to main content

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::Ref;
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: Ref,
31}
32
33wasmi_c_api_macros::declare_own!(wasm_ref_t);
34
35impl wasm_ref_t {
36    /// Creates a new boxed [`wasm_ref_t`] from the given [`Ref`].
37    pub(crate) fn new(r: Ref) -> Option<Box<wasm_ref_t>> {
38        if r.is_null() || !r.is_func() {
39            None
40        } else {
41            Some(Box::new(wasm_ref_t { inner: r }))
42        }
43    }
44}
45
46/// Copies the [`wasm_ref_t`] and returns the copied reference.
47///
48/// Returns `None` if `r` was `None`.
49#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
50#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
51pub extern "C" fn wasm_ref_copy(r: Option<&wasm_ref_t>) -> Option<Box<wasm_ref_t>> {
52    r.map(|r| Box::new(r.clone()))
53}
54
55/// Returns `true` if both [`wasm_ref_t`] references are referencing the same objects.
56///
57/// # Note
58///
59/// This API is unsupported and will panic upon use.
60#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
61#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
62pub extern "C" fn wasm_ref_same(_a: Option<&wasm_ref_t>, _b: Option<&wasm_ref_t>) -> bool {
63    // In Wasmi we require a store to determine whether these are the same
64    // reference or not and therefore we cannot support this Wasm C API.
65    unimplemented!("wasm_ref_same")
66}
67
68/// Returns the host information of the [`wasm_ref_t`].
69///
70/// # Note
71///
72/// This API is unsupported and always returns a `null` pointer.
73#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
74#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
75pub extern "C" fn wasm_ref_get_host_info(_ref: Option<&wasm_ref_t>) -> *mut c_void {
76    ptr::null_mut()
77}
78
79/// Sets the host information of the [`wasm_ref_t`] to `info`.
80///
81/// # Note
82///
83/// This API is unsupported and will panic upon use.
84#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
85#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
86pub extern "C" fn wasm_ref_set_host_info(_ref: Option<&wasm_ref_t>, _info: *mut c_void) {
87    unimplemented!("wasm_ref_set_host_info")
88}
89
90/// Sets the host information of the [`wasm_ref_t`] to `info` with the associated `finalizer`.
91///
92/// The finalizer is run when deleting the [`wasm_ref_t`].
93///
94/// # Note
95///
96/// This API is unsupported and will panic upon use.
97#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
98#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
99pub extern "C" fn wasm_ref_set_host_info_with_finalizer(
100    _ref: Option<&wasm_ref_t>,
101    _info: *mut c_void,
102    _finalizer: Option<extern "C" fn(*mut c_void)>,
103) {
104    unimplemented!("wasm_ref_set_host_info_with_finalizer")
105}
106
107/// Returns the [`wasm_ref_t`] as shared [`wasm_extern_t`] if possible or otherwise returns `None`.
108///
109/// # Note
110///
111/// This API is unsupported and will panic upon use.
112#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
113#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
114pub extern "C" fn wasm_ref_as_extern(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_extern_t> {
115    unimplemented!("wasm_ref_as_extern")
116}
117
118/// Returns the [`wasm_ref_t`] as mutable [`wasm_extern_t`] if possible or otherwise returns `None`.
119///
120/// # Note
121///
122/// This API is unsupported and will panic upon use.
123#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
124#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
125pub extern "C" fn wasm_ref_as_extern_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_extern_t> {
126    unimplemented!("wasm_ref_as_extern_const")
127}
128
129/// Returns the [`wasm_ref_t`] as shared [`wasm_foreign_t`] if possible or otherwise returns `None`.
130///
131/// # Note
132///
133/// This API is unsupported and will panic upon use.
134#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
135#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
136pub extern "C" fn wasm_ref_as_foreign(
137    _ref: Option<&mut wasm_ref_t>,
138) -> Option<&mut wasm_foreign_t> {
139    unimplemented!("wasm_ref_as_foreign")
140}
141
142/// Returns the [`wasm_ref_t`] as mutable [`wasm_foreign_t`] if possible or otherwise returns `None`.
143///
144/// # Note
145///
146/// This API is unsupported and will panic upon use.
147#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
148#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
149pub extern "C" fn wasm_ref_as_foreign_const(
150    _ref: Option<&wasm_ref_t>,
151) -> Option<&crate::wasm_foreign_t> {
152    unimplemented!("wasm_ref_as_foreign_const")
153}
154
155/// Returns the [`wasm_ref_t`] as shared [`wasm_func_t`] if possible or otherwise returns `None`.
156///
157/// # Note
158///
159/// This API is unsupported and will panic upon use.
160#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
161#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
162pub extern "C" fn wasm_ref_as_func(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_func_t> {
163    unimplemented!("wasm_ref_as_func")
164}
165
166/// Returns the [`wasm_ref_t`] as mutable [`wasm_func_t`] if possible or otherwise returns `None`.
167///
168/// # Note
169///
170/// This API is unsupported and will panic upon use.
171#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
172#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
173pub extern "C" fn wasm_ref_as_func_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_func_t> {
174    unimplemented!("wasm_ref_as_func_const")
175}
176
177/// Returns the [`wasm_ref_t`] as shared [`wasm_global_t`] if possible or otherwise returns `None`.
178///
179/// # Note
180///
181/// This API is unsupported and will panic upon use.
182#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
183#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
184pub extern "C" fn wasm_ref_as_global(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_global_t> {
185    unimplemented!("wasm_ref_as_global")
186}
187
188/// Returns the [`wasm_ref_t`] as mutable [`wasm_global_t`] if possible or otherwise returns `None`.
189///
190/// # Note
191///
192/// This API is unsupported and will panic upon use.
193#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
194#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
195pub extern "C" fn wasm_ref_as_global_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_global_t> {
196    unimplemented!("wasm_ref_as_global_const")
197}
198
199/// Returns the [`wasm_ref_t`] as shared [`wasm_instance_t`] if possible or otherwise returns `None`.
200///
201/// # Note
202///
203/// This API is unsupported and will panic upon use.
204#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
205#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
206pub extern "C" fn wasm_ref_as_instance(
207    _ref: Option<&mut wasm_ref_t>,
208) -> Option<&mut wasm_instance_t> {
209    unimplemented!("wasm_ref_as_instance")
210}
211
212/// Returns the [`wasm_ref_t`] as mutable [`wasm_instance_t`] if possible or otherwise returns `None`.
213///
214/// # Note
215///
216/// This API is unsupported and will panic upon use.
217#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
218#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
219pub extern "C" fn wasm_ref_as_instance_const(
220    _ref: Option<&wasm_ref_t>,
221) -> Option<&wasm_instance_t> {
222    unimplemented!("wasm_ref_as_instance_const")
223}
224
225/// Returns the [`wasm_ref_t`] as shared [`wasm_memory_t`] if possible or otherwise returns `None`.
226///
227/// # Note
228///
229/// This API is unsupported and will panic upon use.
230#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
231#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
232pub extern "C" fn wasm_ref_as_memory(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_memory_t> {
233    unimplemented!("wasm_ref_as_memory")
234}
235
236/// Returns the [`wasm_ref_t`] as mutable [`wasm_memory_t`] if possible or otherwise returns `None`.
237///
238/// # Note
239///
240/// This API is unsupported and will panic upon use.
241#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
242#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
243pub extern "C" fn wasm_ref_as_memory_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_memory_t> {
244    unimplemented!("wasm_ref_as_memory_const")
245}
246
247/// Returns the [`wasm_ref_t`] as shared [`wasm_module_t`] if possible or otherwise returns `None`.
248///
249/// # Note
250///
251/// This API is unsupported and will panic upon use.
252#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
253#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
254pub extern "C" fn wasm_ref_as_module(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_module_t> {
255    unimplemented!("wasm_ref_as_module")
256}
257
258/// Returns the [`wasm_ref_t`] as mutable [`wasm_module_t`] if possible or otherwise returns `None`.
259///
260/// # Note
261///
262/// This API is unsupported and will panic upon use.
263#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
264#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
265pub extern "C" fn wasm_ref_as_module_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_module_t> {
266    unimplemented!("wasm_ref_as_module_const")
267}
268
269/// Returns the [`wasm_ref_t`] as shared [`wasm_table_t`] if possible or otherwise returns `None`.
270///
271/// # Note
272///
273/// This API is unsupported and will panic upon use.
274#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
275#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
276pub extern "C" fn wasm_ref_as_table(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_table_t> {
277    unimplemented!("wasm_ref_as_table")
278}
279
280/// Returns the [`wasm_ref_t`] as mutable [`wasm_table_t`] if possible or otherwise returns `None`.
281///
282/// # Note
283///
284/// This API is unsupported and will panic upon use.
285#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
286#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
287pub extern "C" fn wasm_ref_as_table_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_table_t> {
288    unimplemented!("wasm_ref_as_table_const")
289}
290
291/// Returns the [`wasm_ref_t`] as shared [`wasm_trap_t`] if possible or otherwise returns `None`.
292///
293/// # Note
294///
295/// This API is unsupported and will panic upon use.
296#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
297#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
298pub extern "C" fn wasm_ref_as_trap(_ref: Option<&mut wasm_ref_t>) -> Option<&mut wasm_trap_t> {
299    unimplemented!("wasm_ref_as_trap")
300}
301
302/// Returns the [`wasm_ref_t`] as mutable [`wasm_trap_t`] if possible or otherwise returns `None`.
303///
304/// # Note
305///
306/// This API is unsupported and will panic upon use.
307#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
308#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
309pub extern "C" fn wasm_ref_as_trap_const(_ref: Option<&wasm_ref_t>) -> Option<&wasm_trap_t> {
310    unimplemented!("wasm_ref_as_trap_const")
311}