From dcd8b152466ab023762e4b9b6fc50c99da0cae50 Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 12 Apr 2026 13:34:31 +0200 Subject: feat: rework public api, add missing export apis Signed-off-by: Henry --- examples/archive.rs | 2 +- examples/funcref_callbacks.rs | 10 ++++------ examples/linking.rs | 4 ++-- examples/resumable.rs | 2 +- examples/rust/src/tinywasm.rs | 2 +- examples/rust/src/tinywasm_no_std.rs | 2 +- examples/simple.rs | 2 +- examples/simple2.rs | 2 +- examples/wasm-rust.rs | 26 ++++++++++++-------------- 9 files changed, 24 insertions(+), 28 deletions(-) (limited to 'examples') diff --git a/examples/archive.rs b/examples/archive.rs index 7a1021b..7025fba 100644 --- a/examples/archive.rs +++ b/examples/archive.rs @@ -21,7 +21,7 @@ fn main() -> Result<()> { let module: Module = TinyWasmModule::from_twasm(&twasm)?.into(); let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let add = instance.exported_func::<(i32, i32), i32>(&store, "add")?; + let add = instance.func_typed::<(i32, i32), i32>(&store, "add")?; assert_eq!(add.call(&mut store, (1, 2))?, 3); diff --git a/examples/funcref_callbacks.rs b/examples/funcref_callbacks.rs index a460df8..6e1d3ef 100644 --- a/examples/funcref_callbacks.rs +++ b/examples/funcref_callbacks.rs @@ -56,8 +56,7 @@ fn run_passed_funcref_example() -> Result<()> { "call_this", Extern::typed_func(|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().exported_func::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; + let call_by_ref = ctx.module().func_typed::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; let _result = call_by_ref.call(ctx.store_mut(), (func_ref, LHS, RHS))?; Ok(()) }), @@ -70,7 +69,7 @@ fn run_passed_funcref_example() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let caller = instance.exported_func::<(), ()>(&store, "tell_host_to_call")?; + let caller = instance.func_typed::<(), ()>(&store, "tell_host_to_call")?; caller.call(&mut store, ())?; @@ -123,12 +122,11 @@ fn run_returned_funcref_example() -> Result<()> { let instance = module.instantiate(&mut store, Some(imports))?; let (add_ref, sub_ref, mul_ref) = { - let get_funcrefs = - instance.exported_func::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?; + let get_funcrefs = instance.func_typed::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?; get_funcrefs.call(&mut store, ())? }; - let call_by_ref = instance.exported_func::<(FuncRef, i32, i32), i32>(&store, "call_binop_by_ref")?; + let call_by_ref = instance.func_typed::<(FuncRef, i32, i32), i32>(&store, "call_binop_by_ref")?; for func_ref in [add_ref, sub_ref, mul_ref] { let _result = call_by_ref.call(&mut store, (func_ref, LHS, RHS))?; diff --git a/examples/linking.rs b/examples/linking.rs index f94773a..ce9748b 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -37,13 +37,13 @@ fn main() -> Result<()> { // Link the `adder` namespace to the `add` module's instance. let mut imports = tinywasm::Imports::new(); - imports.link_module("adder", add_instance.id())?; + imports.link_module("adder", add_instance)?; // Instantiate the `import` module with the linked imports. let import_instance = import_module.instantiate(&mut store, Some(imports))?; // Call the `main` function, which uses the imported `add` function. - let main = import_instance.exported_func::<(), i32>(&store, "main")?; + let main = import_instance.func_typed::<(), i32>(&store, "main")?; assert_eq!(main.call(&mut store, ())?, 3); Ok(()) diff --git a/examples/resumable.rs b/examples/resumable.rs index 6838e20..f3ebc89 100644 --- a/examples/resumable.rs +++ b/examples/resumable.rs @@ -27,7 +27,7 @@ fn main() -> Result<()> { let module = Module::parse_bytes(&wasm)?; let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let count_down = instance.exported_func::(&store, "count_down")?; + let count_down = instance.func_typed::(&store, "count_down")?; let mut execution = count_down.call_resumable(&mut store, 10_000)?; let fuel_per_round = 128; diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index 11d7637..e06cf81 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -26,7 +26,7 @@ fn run() -> tinywasm::Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print")?; + let add_and_print = instance.func_typed::<(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 6a31ff0..9a032dd 100644 --- a/examples/rust/src/tinywasm_no_std.rs +++ b/examples/rust/src/tinywasm_no_std.rs @@ -43,7 +43,7 @@ fn run() -> tinywasm::Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print")?; + let add_and_print = instance.func_typed::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } diff --git a/examples/simple.rs b/examples/simple.rs index e0842dd..8371992 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -15,7 +15,7 @@ fn main() -> Result<()> { let module = Module::parse_bytes(&wasm)?; let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let add = instance.exported_func::<(i32, i32), i32>(&store, "add")?; + let add = instance.func_typed::<(i32, i32), i32>(&store, "add")?; assert_eq!(add.call(&mut store, (1, 2))?, 3); Ok(()) diff --git a/examples/simple2.rs b/examples/simple2.rs index a3124f8..66e85b3 100644 --- a/examples/simple2.rs +++ b/examples/simple2.rs @@ -14,7 +14,7 @@ fn main() -> Result<()> { let module = Module::parse_bytes(&wasm)?; let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let add = instance.exported_func::<(i32, i64), (i32, i64)>(&store, "return")?; + let add = instance.func_typed::<(i32, i64), (i32, i64)>(&store, "return")?; assert_eq!(add.call(&mut store, (1, 2))?, (1, 2)); Ok(()) diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index d376130..0ffa499 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -79,7 +79,7 @@ fn tinywasm() -> Result<()> { imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?; let instance = module.instantiate(&mut store, Some(black_box(imports)))?; - let hello = instance.exported_func::<(), ()>(&store, "hello")?; + let hello = instance.func_typed::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; @@ -94,7 +94,7 @@ fn tinywasm_no_std() -> Result<()> { imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?; let instance = module.instantiate(&mut store, Some(black_box(imports)))?; - let hello = instance.exported_func::<(), ()>(&store, "hello")?; + let hello = instance.func_typed::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; @@ -109,22 +109,20 @@ fn hello() -> Result<()> { imports.define( "env", "print_utf8", - Extern::typed_func(|ctx: FuncContext<'_>, args: (i64, i32)| { - let mem = ctx.exported_memory("memory")?; - let ptr = args.0 as usize; - let len = args.1 as usize; - let string = mem.load_string(ptr, len)?; + Extern::typed_func(|ctx: FuncContext<'_>, (ptr, len): (i64, i32)| { + let mem = ctx.memory("memory")?; + let string = mem.load_string(ptr as usize, len as usize)?; println!("{string}"); Ok(()) }), )?; let instance = module.instantiate(&mut store, Some(imports))?; - let arg_ptr = instance.exported_func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; + let arg_ptr = instance.func_typed::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; let arg = b"world"; - instance.exported_memory_mut(&mut store, "memory")?.store(arg_ptr as usize, arg.len(), arg)?; - let hello = instance.exported_func::(&store, "hello")?; + instance.memory_mut(&mut store, "memory")?.store(arg_ptr as usize, arg.len(), arg)?; + let hello = instance.func_typed::(&store, "hello")?; hello.call(&mut store, arg.len() as i32)?; Ok(()) @@ -145,7 +143,7 @@ fn host_fn() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let host_fn = instance.exported_func::<(), i32>(&store, "foo")?; + let host_fn = instance.func_typed::<(), i32>(&store, "foo")?; assert_eq!(host_fn.call(&mut store, ())?, 3); Ok(()) } @@ -165,7 +163,7 @@ fn printi32() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print")?; + let add_and_print = instance.func_typed::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) @@ -176,7 +174,7 @@ fn fibonacci() -> Result<()> { let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let fibonacci = instance.exported_func::(&store, "fibonacci_recursive")?; + let fibonacci = instance.func_typed::(&store, "fibonacci_recursive")?; let n = 26; let result = fibonacci.call(&mut store, n)?; println!("fibonacci({n}) = {result}"); @@ -189,7 +187,7 @@ fn argon2id() -> Result<()> { let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let argon2id = instance.exported_func::<(i32, i32, i32), i32>(&store, "argon2id")?; + let argon2id = instance.func_typed::<(i32, i32, i32), i32>(&store, "argon2id")?; argon2id.call(&mut store, (1000, 2, 1))?; Ok(()) -- cgit v1.3.1