MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / create_function

Function create_function

crates/c-api/src/func.rs:55–93  ·  view source on GitHub ↗
(
    store: &mut wasm_store_t,
    ty: &wasm_functype_t,
    func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>>
    + Send
    + Sync
    + 'static,
)

Source from the content-addressed store, hash-verified

53}
54
55unsafe fn create_function(
56 store: &mut wasm_store_t,
57 ty: &wasm_functype_t,
58 func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>>
59 + Send
60 + Sync
61 + 'static,
62) -> Box<wasm_func_t> {
63 let ty = ty.ty().ty(store.store.context().engine());
64 let func = Func::new(
65 store.store.context_mut(),
66 ty,
67 move |_caller, params, results| {
68 let params: wasm_val_vec_t = params
69 .iter()
70 .cloned()
71 .map(|p| wasm_val_t::from_val(p))
72 .collect::<Vec<_>>()
73 .into();
74 let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into();
75 let out = func(&params, &mut out_results);
76 if let Some(trap) = out {
77 return Err(trap.error);
78 }
79
80 let out_results = out_results.as_slice();
81 for i in 0..results.len() {
82 results[i] = out_results[i].val();
83 }
84 Ok(())
85 },
86 );
87 Box::new(wasm_func_t {
88 ext: wasm_extern_t {
89 store: store.store.clone(),
90 which: func.into(),
91 },
92 })
93}
94
95#[unsafe(no_mangle)]
96pub unsafe extern "C" fn wasm_func_new(

Callers 2

wasm_func_newFunction · 0.70
wasm_func_new_with_envFunction · 0.70

Calls 13

funcFunction · 0.85
OkFunction · 0.85
context_mutMethod · 0.80
valMethod · 0.80
newFunction · 0.50
tyMethod · 0.45
engineMethod · 0.45
contextMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
as_sliceMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected