From 9893a51d2712f82a2ee57316a66bc26500eb3fdd Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 9 Aug 2026 16:11:57 +0200 Subject: feat: add reusable host imports and simplify function calls - make host definitions reusable across stores with gc-aware import linking - reorganize function calls, resumable execution, and typed value handling - expand typed tuples to arity 20 and update parser and store APIs Signed-off-by: Henry --- examples/doom/src/runtime.rs | 22 +++++++++++----------- examples/funcref_callbacks.rs | 21 ++++++++++----------- examples/linking.rs | 2 +- examples/reentrance.rs | 4 ++-- examples/rust/src/tinywasm.rs | 4 ++-- examples/rust/src/tinywasm_no_std.rs | 6 +++--- examples/wasm-rust.rs | 20 ++++++++++---------- 7 files changed, 39 insertions(+), 40 deletions(-) (limited to 'examples') diff --git a/examples/doom/src/runtime.rs b/examples/doom/src/runtime.rs index 980dc21..bee961c 100644 --- a/examples/doom/src/runtime.rs +++ b/examples/doom/src/runtime.rs @@ -30,7 +30,7 @@ impl Runtime { let mut store = Store::default(); let host_state = Rc::new(RefCell::new(HostState::new(wad_path))); let imports = build_imports(&mut store, host_state.clone()); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let wad_path_buf = instance.func::<(), i32>(&store, "tinywasm_doom_wad_path_buf")?; let init = instance.func::<(), ()>(&store, "tinywasm_doom_init")?; @@ -169,7 +169,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_open", - HostFunction::from(store, move |ctx: FuncContext<'_>, (filename_ptr, mode_ptr): (i32, i32)| { + HostFunction::from(move |ctx: FuncContext<'_>, (filename_ptr, mode_ptr): (i32, i32)| { let memory = ctx.memory("memory")?; let filename = memory.read_cstring_until_null(ctx.store(), filename_ptr as usize, 1024)?; let mode = memory.read_cstring_until_null(ctx.store(), mode_ptr as usize, 16)?; @@ -208,7 +208,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_close", - HostFunction::from(store, move |_ctx: FuncContext<'_>, handle: i32| { + HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { state.borrow_mut().files.remove(&handle); Ok(()) }), @@ -220,7 +220,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_read", - HostFunction::from(store, move |mut ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { + HostFunction::from(move |mut ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { let mut state = state.borrow_mut(); let Some(file) = state.files.get_mut(&handle) else { return Ok(0); @@ -238,7 +238,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_write", - HostFunction::from(store, move |ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { + HostFunction::from(move |ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { let data = ctx.memory("memory")?.read_vec(ctx.store(), buf_ptr as usize, count.max(0) as usize)?; let mut state = state.borrow_mut(); let Some(file) = state.files.get_mut(&handle) else { @@ -255,7 +255,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_seek", - HostFunction::from(store, move |_ctx: FuncContext<'_>, (handle, offset, origin): (i32, i32, i32)| { + HostFunction::from(move |_ctx: FuncContext<'_>, (handle, offset, origin): (i32, i32, i32)| { let seek_from = match origin { 0 => SeekFrom::Start(offset.max(0) as u64), 1 => SeekFrom::Current(offset as i64), @@ -277,7 +277,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_tell", - HostFunction::from(store, move |_ctx: FuncContext<'_>, handle: i32| { + HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { let mut state = state.borrow_mut(); let Some(file) = state.files.get_mut(&handle) else { return Ok(-1); @@ -293,7 +293,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_eof", - HostFunction::from(store, move |_ctx: FuncContext<'_>, handle: i32| { + HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { let mut state = state.borrow_mut(); let Some(file) = state.files.get_mut(&handle) else { return Ok(1); @@ -310,7 +310,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_gettime", - HostFunction::from(store, move |mut ctx: FuncContext<'_>, (sec_ptr, usec_ptr): (i32, i32)| { + HostFunction::from(move |mut ctx: FuncContext<'_>, (sec_ptr, usec_ptr): (i32, i32)| { let elapsed = state.borrow().start.elapsed(); let sec = elapsed.as_secs().min(i32::MAX as u64) as i32; let usec = elapsed.subsec_micros() as i32; @@ -327,7 +327,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_exit", - HostFunction::from(store, move |_ctx: FuncContext<'_>, code: i32| { + HostFunction::from(move |_ctx: FuncContext<'_>, code: i32| { state.borrow_mut().exit_code = Some(code); Ok(()) }), @@ -337,7 +337,7 @@ fn build_imports(store: &mut Store, state: Rc>) -> Imports { imports.define( IMPORT_MODULE, "host_print", - HostFunction::from(store, move |ctx: FuncContext<'_>, ptr: i32| { + HostFunction::from(move |ctx: FuncContext<'_>, ptr: i32| { let text = ctx.memory("memory")?.read_cstring_until_null(ctx.store(), ptr as usize, 4096)?; log::info!("guest: {}", text.to_string_lossy()); Ok(()) diff --git a/examples/funcref_callbacks.rs b/examples/funcref_callbacks.rs index 8ff9b49..5e21a6b 100644 --- a/examples/funcref_callbacks.rs +++ b/examples/funcref_callbacks.rs @@ -50,19 +50,18 @@ fn run_passed_funcref_example() -> Result<()> { let module = tinywasm::parse_bytes(&wasm)?; let mut store = Store::default(); - let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result { Ok(lhs * rhs) }); - let call_this = - HostFunction::from(&mut store, |mut ctx: FuncContext<'_>, func_ref: FuncRef| -> tinywasm::Result<()> { - // Host cannot call a funcref directly, so it routes through Wasm. - let call_by_ref = ctx.module().func::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; - let _result = call_by_ref.call(ctx.store_mut(), (func_ref, LHS, RHS))?; - Ok(()) - }); + let mul = HostFunction::from(|_, (lhs, rhs): (i32, i32)| -> tinywasm::Result { Ok(lhs * rhs) }); + let call_this = HostFunction::from(|mut ctx: FuncContext<'_>, func_ref: FuncRef| -> tinywasm::Result<()> { + // Host cannot call a funcref directly, so it routes through Wasm. + let call_by_ref = ctx.module().func::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; + let _result = call_by_ref.call(ctx.store_mut(), (func_ref, LHS, RHS))?; + Ok(()) + }); let mut imports = Imports::new(); imports.define("host", "call_this", call_this).define("host", "mul", mul); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let caller = instance.func::<(), ()>(&store, "tell_host_to_call")?; caller.call(&mut store, ())?; @@ -107,10 +106,10 @@ fn run_returned_funcref_example() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result { Ok(lhs * rhs) }); + let mul = HostFunction::from(|_, (lhs, rhs): (i32, i32)| -> tinywasm::Result { Ok(lhs * rhs) }); imports.define("host", "mul", mul); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let (add_ref, sub_ref, mul_ref) = { let get_funcrefs = instance.func::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?; get_funcrefs.call(&mut store, ())? diff --git a/examples/linking.rs b/examples/linking.rs index b3164ae..02cb88d 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -40,7 +40,7 @@ fn main() -> Result<()> { imports.link_module("adder", add_instance)?; // Instantiate the `import` module with the linked imports. - let import_instance = ModuleInstance::instantiate(&mut store, &import_module, Some(imports))?; + let import_instance = ModuleInstance::instantiate(&mut store, &import_module, Some(&imports))?; // Call the `main` function, which uses the imported `add` function. let main = import_instance.func::<(), i32>(&store, "main")?; diff --git a/examples/reentrance.rs b/examples/reentrance.rs index 432fac7..8a1988e 100644 --- a/examples/reentrance.rs +++ b/examples/reentrance.rs @@ -22,7 +22,7 @@ fn main() -> Result<()> { let module = tinywasm::parse_bytes(&wasm)?; let mut store = Store::default(); - let call_add_twice = HostFunction::from(&mut store, |mut ctx: FuncContext<'_>, value: i32| { + let call_add_twice = HostFunction::from(|mut ctx: FuncContext<'_>, value: i32| { let add_one = ctx.module().func::(ctx.store(), "add_one")?; // Use ctx.call for reentrant calls from host functions. Function::call @@ -34,7 +34,7 @@ fn main() -> Result<()> { let mut imports = Imports::new(); imports.define("host", "call_add_twice", call_add_twice); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let run = instance.func::(&store, "run")?; assert_eq!(run.call(&mut store, 40)?, 52); diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index 70de73e..b0a2e47 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -15,7 +15,7 @@ fn run() -> tinywasm::Result<()> { let module = tinywasm::parse_stream(&include_bytes!("./print.wasm")[..])?; let mut store = tinywasm::Store::default(); - let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { + let printi32 = HostFunction::from(|_: FuncContext<'_>, v: i32| { unsafe { printi32(v) } Ok(()) }); @@ -23,7 +23,7 @@ fn run() -> tinywasm::Result<()> { let mut imports = tinywasm::Imports::new(); imports.define("env", "printi32", printi32); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs index c1dd910..cbf10f4 100644 --- a/examples/rust/src/tinywasm_no_std.rs +++ b/examples/rust/src/tinywasm_no_std.rs @@ -28,17 +28,17 @@ fn run() -> tinywasm::Result<()> { let mut store = tinywasm::Store::default(); let mut imports = tinywasm::Imports::new(); - let res = tinywasm::parser::Parser::new().parse_module_bytes(include_bytes!("./print.wasm"))?; + let res = tinywasm::parser::Parser::default().parse_module_bytes(include_bytes!("./print.wasm"))?; let twasm = res.serialize_twasm()?; let module = tinywasm::parse_bytes(&twasm)?; - let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { + let printi32 = HostFunction::from(|_: FuncContext<'_>, v: i32| { unsafe { printi32(v) } Ok(()) }); imports.define("env", "printi32", printi32); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index e0b9968..e4af3cd 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -75,8 +75,8 @@ fn tinywasm() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _x: i32| Ok(()))); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(black_box(imports)))?; + imports.define("env", "printi32", HostFunction::from(|_: FuncContext<'_>, _x: i32| Ok(()))); + let instance = ModuleInstance::instantiate(&mut store, &module, Some(black_box(&imports)))?; let hello = instance.func::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; @@ -90,8 +90,8 @@ fn tinywasm_no_std() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _x: i32| Ok(()))); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(black_box(imports)))?; + imports.define("env", "printi32", HostFunction::from(|_: FuncContext<'_>, _x: i32| Ok(()))); + let instance = ModuleInstance::instantiate(&mut store, &module, Some(black_box(&imports)))?; let hello = instance.func::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; @@ -104,7 +104,7 @@ fn hello() -> Result<()> { let module = tinywasm::parse_file("./examples/rust/out/hello.opt.wasm")?; let mut store = Store::default(); - let print_utf8 = HostFunction::from(&mut store, |ctx: FuncContext<'_>, (ptr, len): (i64, i32)| { + let print_utf8 = HostFunction::from(|ctx: FuncContext<'_>, (ptr, len): (i64, i32)| { let mem = ctx.memory("memory")?; let string = mem.read_string(ctx.store(), ptr as usize, len as usize)?; println!("{string}"); @@ -114,7 +114,7 @@ fn hello() -> Result<()> { let mut imports = Imports::new(); imports.define("env", "print_utf8", print_utf8); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let arg_ptr = instance.func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; let arg = b"world"; @@ -129,7 +129,7 @@ fn host_fn() -> Result<()> { let module = tinywasm::parse_file("./examples/rust/out/host_fn.opt.wasm")?; let mut store = Store::default(); - let bar = HostFunction::from(&mut store, |_: FuncContext<'_>, (left, right): (i64, i32)| { + let bar = HostFunction::from(|_: FuncContext<'_>, (left, right): (i64, i32)| { assert_eq!(left, 1); assert_eq!(right, 2); Ok(left as i32 + right) @@ -138,7 +138,7 @@ fn host_fn() -> Result<()> { let mut imports = Imports::new(); imports.define("env", "bar", bar); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let host_fn = instance.func::<(), i32>(&store, "foo")?; assert_eq!(host_fn.call(&mut store, ())?, 3); Ok(()) @@ -148,7 +148,7 @@ fn printi32() -> Result<()> { let module = tinywasm::parse_file("./examples/rust/out/print.opt.wasm")?; let mut store = Store::default(); - let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, x: i32| { + let printi32 = HostFunction::from(|_: FuncContext<'_>, x: i32| { println!("{x}"); Ok(()) }); @@ -156,7 +156,7 @@ fn printi32() -> Result<()> { let mut imports = Imports::new(); imports.define("env", "printi32", printi32); - let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; -- cgit v1.3.1