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)
| 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 | |
| 241 | pub(crate) type HostFuncInner = Box<dyn Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>>>; |
nothing calls this directly
no test coverage detected