MCPcopy Index your code
hub / github.com/explodingcamera/tinywasm / from

Method from

crates/tinywasm/src/func.rs:226–238  ·  view source on GitHub ↗

Create a new typed host function import. ## Example ```rust # fn main() -> tinywasm::Result<()> { # use tinywasm::{HostFunction, Imports, ModuleInstance, Store}; # let wasm = wat::parse_str(r#" # (module # (import "host" "add_one" (func $add_one (param i32) (result i32))) # (func (export "call") (param i32) (result i32) # local.get 0 # call $add_one)) # "#).expect(

(store: &mut Store, func: impl Fn(FuncContext<'_>, P) -> Result<R> + 'static)

Source from the content-addressed store, hash-verified

224 /// # }
225 /// ```
226 pub fn from<P, R>(store: &mut Store, func: impl Fn(FuncContext<'_>, P) -> Result<R> + 'static) -> Function
227 where
228 P: FromWasmValues + ToWasmTypes,
229 R: IntoWasmValues + ToWasmTypes,
230 {
231 let inner_func = move |ctx: FuncContext<'_>, args: &[WasmValue]| -> Result<Vec<WasmValue>> {
232 Ok(func(ctx, P::from_wasm_values(args)?)?.into_wasm_values())
233 };
234
235 let ty = Arc::new(tinywasm_types::FuncType::new(&P::wasm_types(), &R::wasm_types()));
236 let addr = store.add_func(FunctionInstance::Host(Rc::new(Self { func: Box::new(inner_func), ty: ty.clone() })));
237 Function { item: crate::StoreItem::new(store.id(), addr), module_addr: 0, addr, ty }
238 }
239}
240
241pub(crate) type HostFuncInner = Box<dyn Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>>>;

Callers

nothing calls this directly

Calls 3

into_wasm_valuesMethod · 0.80
add_funcMethod · 0.80
idMethod · 0.45

Tested by

no test coverage detected