1use crate::{
2 wasm_extern_t,
3 wasm_functype_t,
4 wasm_store_t,
5 wasm_trap_t,
6 wasm_val_t,
7 wasm_val_vec_t,
8};
9use alloc::{boxed::Box, string::String, vec, vec::Vec};
10use core::{any::Any, ffi::c_void, hint, iter, ptr, str};
11use wasmi::{Error, Extern, Func, Ref, Val};
12
13#[cfg(feature = "std")]
14use core::panic::AssertUnwindSafe;
15
16#[derive(Clone)]
20#[repr(transparent)]
21pub struct wasm_func_t {
22 inner: wasm_extern_t,
23}
24
25wasmi_c_api_macros::declare_ref!(wasm_func_t);
26
27pub type wasm_func_callback_t = extern "C" fn(
29 params: *const wasm_val_vec_t,
30 results: *mut wasm_val_vec_t,
31) -> Option<Box<wasm_trap_t>>;
32
33pub type wasm_func_callback_with_env_t = extern "C" fn(
35 env: *mut c_void,
36 params: *const wasm_val_vec_t,
37 results: *mut wasm_val_vec_t,
38) -> Option<Box<wasm_trap_t>>;
39
40impl wasm_func_t {
41 pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_func_t> {
42 match &e.which {
43 Extern::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
44 _ => None,
45 }
46 }
47
48 pub(crate) fn try_from_mut(e: &mut wasm_extern_t) -> Option<&mut wasm_func_t> {
49 match &mut e.which {
50 Extern::Func(_) => Some(unsafe { &mut *(e as *mut _ as *mut _) }),
51 _ => None,
52 }
53 }
54
55 pub(crate) fn func(&self) -> Func {
57 match self.inner.which {
58 Extern::Func(f) => f,
59 _ => unsafe { hint::unreachable_unchecked() },
60 }
61 }
62}
63
64unsafe fn create_function(
76 store: &mut wasm_store_t,
77 ty: &wasm_functype_t,
78 func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>>
79 + Send
80 + Sync
81 + 'static,
82) -> Box<wasm_func_t> {
83 let ty = ty.ty().ty.clone();
84 let func = Func::new(
85 store.inner.context_mut(),
86 ty,
87 move |_caller, params, results| {
88 let params: wasm_val_vec_t = params
89 .iter()
90 .cloned()
91 .map(wasm_val_t::from)
92 .collect::<Box<[_]>>()
93 .into();
94 let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into();
95 if let Some(trap) = func(¶ms, &mut out_results) {
96 return Err(trap.error);
97 }
98 results
99 .iter_mut()
100 .zip(out_results.as_slice())
101 .for_each(|(result, out_results)| {
102 *result = out_results.to_val();
103 });
104 Ok(())
105 },
106 );
107 Box::new(wasm_func_t {
108 inner: wasm_extern_t {
109 store: store.inner.clone(),
110 which: func.into(),
111 },
112 })
113}
114
115#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
126#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
127pub unsafe extern "C" fn wasm_func_new(
128 store: &mut wasm_store_t,
129 ty: &wasm_functype_t,
130 callback: wasm_func_callback_t,
131) -> Box<wasm_func_t> {
132 create_function(store, ty, move |params, results| callback(params, results))
133}
134
135#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
147#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
148pub unsafe extern "C" fn wasm_func_new_with_env(
149 store: &mut wasm_store_t,
150 ty: &wasm_functype_t,
151 callback: wasm_func_callback_with_env_t,
152 data: *mut c_void,
153 finalizer: Option<extern "C" fn(arg1: *mut c_void)>,
154) -> Box<wasm_func_t> {
155 let finalizer = crate::ForeignData { data, finalizer };
156 create_function(store, ty, move |params, results| {
157 let _ = &finalizer; callback(finalizer.data, params, results)
159 })
160}
161
162fn prepare_params_and_results(
166 dst: &mut Vec<Val>,
167 params: impl ExactSizeIterator<Item = Val>,
168 len_results: usize,
169) -> (&[Val], &mut [Val]) {
170 debug_assert!(dst.is_empty());
171 let len_params = params.len();
172 dst.reserve(len_params + len_results);
173 dst.extend(params);
174 dst.extend(iter::repeat_n(Val::FuncRef(<Ref<Func>>::Null), len_results));
175 let (params, results) = dst.split_at_mut(len_params);
176 (params, results)
177}
178
179#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
191#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
192pub unsafe extern "C" fn wasm_func_call(
193 func: &mut wasm_func_t,
194 params: *const wasm_val_vec_t,
195 results: *mut wasm_val_vec_t,
196) -> *mut wasm_trap_t {
197 let f = func.func();
198 let results = (*results).as_uninit_slice();
199 let params = (*params).as_slice();
200 let mut dst = Vec::new();
201 let (wt_params, wt_results) =
202 prepare_params_and_results(&mut dst, params.iter().map(|i| i.to_val()), results.len());
203
204 let result = {
205 #[cfg(feature = "std")]
206 {
207 std::panic::catch_unwind(AssertUnwindSafe(|| {
212 f.call(func.inner.store.context_mut(), wt_params, wt_results)
213 }))
214 }
215 #[cfg(not(feature = "std"))]
216 {
217 Ok(f.call(func.inner.store.context_mut(), wt_params, wt_results))
218 }
219 };
220 match result {
221 Ok(Ok(())) => {
222 for (slot, val) in results.iter_mut().zip(wt_results.iter().cloned()) {
223 crate::initialize(slot, wasm_val_t::from(val));
224 }
225 ptr::null_mut()
226 }
227 Ok(Err(err)) => Box::into_raw(Box::new(wasm_trap_t::new(err))),
228 Err(panic) => {
229 let err = error_from_panic(panic);
230 let trap = Box::new(wasm_trap_t::new(err));
231 Box::into_raw(trap)
232 }
233 }
234}
235
236fn error_from_panic(panic: Box<dyn Any + Send>) -> Error {
238 if let Some(msg) = panic.downcast_ref::<String>() {
239 Error::new(msg.clone())
240 } else if let Some(msg) = panic.downcast_ref::<&'static str>() {
241 Error::new(*msg)
242 } else {
243 Error::new("panic happened on the Rust side")
244 }
245}
246
247#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
256#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
257pub unsafe extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
258 Box::new(wasm_functype_t::new(f.func().ty(f.inner.store.context())))
259}
260
261#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
272#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
273pub unsafe extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
274 f.func().ty(f.inner.store.context()).params().len()
275}
276
277#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
288#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
289pub unsafe extern "C" fn wasm_func_result_arity(f: &wasm_func_t) -> usize {
290 f.func().ty(f.inner.store.context()).results().len()
291}
292
293#[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_func_as_extern(f: &mut wasm_func_t) -> &mut wasm_extern_t {
297 &mut f.inner
298}
299
300#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
302#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
303pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {
304 &f.inner
305}