Skip to main content

wasmtime_c_api/
ref.rs

1use crate::abort;
2use std::os::raw::c_void;
3use wasmtime::{Ref, Val};
4
5/// `*mut wasm_ref_t` is a reference type (`externref` or `funcref`), as seen by
6/// the C API. Because we do not have a uniform representation for `funcref`s
7/// and `externref`s, a `*mut wasm_ref_t` is morally a
8/// `Option<Box<Either<ExternRef, Func>>>`.
9///
10/// A null `*mut wasm_ref_t` is either a null `funcref` or a null `externref`
11/// depending on context (e.g. the table's element type that it is going into or
12/// coming out of).
13///
14/// Note: this is not `#[repr(C)]` because it is an opaque type in the header,
15/// and only ever referenced as `*mut wasm_ref_t`. This also lets us use a
16/// regular, non-`repr(C)` `enum` to define `WasmRefInner`.
17#[derive(Clone)]
18pub struct wasm_ref_t {
19    pub(crate) r: Ref,
20}
21
22wasmtime_c_api_macros::declare_own!(wasm_ref_t);
23
24impl wasm_ref_t {
25    pub(crate) fn new(r: Ref) -> Option<Box<wasm_ref_t>> {
26        if r.is_null() || !r.is_func() {
27            None
28        } else {
29            Some(Box::new(wasm_ref_t { r }))
30        }
31    }
32}
33
34pub(crate) fn ref_to_val(r: &wasm_ref_t) -> Val {
35    Val::from(r.r.clone())
36}
37
38#[unsafe(no_mangle)]
39pub extern "C" fn wasm_ref_copy(r: Option<&wasm_ref_t>) -> Option<Box<wasm_ref_t>> {
40    r.map(|r| Box::new(r.clone()))
41}
42
43#[unsafe(no_mangle)]
44pub extern "C" fn wasm_ref_same(_a: Option<&wasm_ref_t>, _b: Option<&wasm_ref_t>) -> bool {
45    // We need a store to determine whether these are the same reference or not.
46    abort("wasm_ref_same")
47}
48
49#[unsafe(no_mangle)]
50pub extern "C" fn wasm_ref_get_host_info(_ref: Option<&wasm_ref_t>) -> *mut c_void {
51    std::ptr::null_mut()
52}
53
54#[unsafe(no_mangle)]
55pub extern "C" fn wasm_ref_set_host_info(_ref: Option<&wasm_ref_t>, _info: *mut c_void) {
56    abort("wasm_ref_set_host_info")
57}
58
59#[unsafe(no_mangle)]
60pub extern "C" fn wasm_ref_set_host_info_with_finalizer(
61    _ref: Option<&wasm_ref_t>,
62    _info: *mut c_void,
63    _finalizer: Option<extern "C" fn(*mut c_void)>,
64) {
65    abort("wasm_ref_set_host_info_with_finalizer")
66}
67
68#[unsafe(no_mangle)]
69pub extern "C" fn wasm_ref_as_extern(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_extern_t> {
70    abort("wasm_ref_as_extern")
71}
72
73#[unsafe(no_mangle)]
74pub extern "C" fn wasm_ref_as_extern_const(
75    _ref: Option<&wasm_ref_t>,
76) -> Option<&crate::wasm_extern_t> {
77    abort("wasm_ref_as_extern_const")
78}
79
80#[unsafe(no_mangle)]
81pub extern "C" fn wasm_ref_as_foreign(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_foreign_t> {
82    abort("wasm_ref_as_foreign")
83}
84
85#[unsafe(no_mangle)]
86pub extern "C" fn wasm_ref_as_foreign_const(
87    _ref: Option<&wasm_ref_t>,
88) -> Option<&crate::wasm_foreign_t> {
89    abort("wasm_ref_as_foreign_const")
90}
91
92#[unsafe(no_mangle)]
93pub extern "C" fn wasm_ref_as_func(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_func_t> {
94    abort("wasm_ref_as_func")
95}
96
97#[unsafe(no_mangle)]
98pub extern "C" fn wasm_ref_as_func_const(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_func_t> {
99    abort("wasm_ref_as_func_const")
100}
101
102#[unsafe(no_mangle)]
103pub extern "C" fn wasm_ref_as_global(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_global_t> {
104    abort("wasm_ref_as_global")
105}
106
107#[unsafe(no_mangle)]
108pub extern "C" fn wasm_ref_as_global_const(
109    _ref: Option<&wasm_ref_t>,
110) -> Option<&crate::wasm_global_t> {
111    abort("wasm_ref_as_global_const")
112}
113
114#[unsafe(no_mangle)]
115pub extern "C" fn wasm_ref_as_instance(
116    _ref: Option<&wasm_ref_t>,
117) -> Option<&crate::wasm_instance_t> {
118    abort("wasm_ref_as_instance")
119}
120
121#[unsafe(no_mangle)]
122pub extern "C" fn wasm_ref_as_instance_const(
123    _ref: Option<&wasm_ref_t>,
124) -> Option<&crate::wasm_instance_t> {
125    abort("wasm_ref_as_instance_const")
126}
127
128#[unsafe(no_mangle)]
129pub extern "C" fn wasm_ref_as_memory(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_memory_t> {
130    abort("wasm_ref_as_memory")
131}
132
133#[unsafe(no_mangle)]
134pub extern "C" fn wasm_ref_as_memory_const(
135    _ref: Option<&wasm_ref_t>,
136) -> Option<&crate::wasm_memory_t> {
137    abort("wasm_ref_as_memory_const")
138}
139
140#[unsafe(no_mangle)]
141pub extern "C" fn wasm_ref_as_module(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_module_t> {
142    abort("wasm_ref_as_module")
143}
144
145#[unsafe(no_mangle)]
146pub extern "C" fn wasm_ref_as_module_const(
147    _ref: Option<&wasm_ref_t>,
148) -> Option<&crate::wasm_module_t> {
149    abort("wasm_ref_as_module_const")
150}
151
152#[unsafe(no_mangle)]
153pub extern "C" fn wasm_ref_as_table(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_table_t> {
154    abort("wasm_ref_as_table")
155}
156
157#[unsafe(no_mangle)]
158pub extern "C" fn wasm_ref_as_table_const(
159    _ref: Option<&wasm_ref_t>,
160) -> Option<&crate::wasm_table_t> {
161    abort("wasm_ref_as_table_const")
162}
163
164#[unsafe(no_mangle)]
165pub extern "C" fn wasm_ref_as_trap(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_trap_t> {
166    abort("wasm_ref_as_trap")
167}
168
169#[unsafe(no_mangle)]
170pub extern "C" fn wasm_ref_as_trap_const(_ref: Option<&wasm_ref_t>) -> Option<&crate::wasm_trap_t> {
171    abort("wasm_ref_as_trap_const")
172}
173
174#[derive(Clone)]
175#[repr(C)]
176pub struct wasm_foreign_t {}
177
178wasmtime_c_api_macros::declare_ref!(wasm_foreign_t);
179
180#[unsafe(no_mangle)]
181pub extern "C" fn wasm_foreign_new(_store: &crate::wasm_store_t) -> Box<wasm_foreign_t> {
182    abort("wasm_foreign_new")
183}