diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/archive.rs | 2 | ||||
| -rw-r--r-- | examples/funcref_callbacks.rs | 37 | ||||
| -rw-r--r-- | examples/linking.rs | 2 | ||||
| -rw-r--r-- | examples/resumable.rs | 2 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm.rs | 4 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm_no_std.rs | 4 | ||||
| -rw-r--r-- | examples/simple.rs | 2 | ||||
| -rw-r--r-- | examples/simple2.rs | 2 | ||||
| -rw-r--r-- | examples/wasm-rust.rs | 73 |
9 files changed, 56 insertions, 72 deletions
diff --git a/examples/archive.rs b/examples/archive.rs index 7025fba..f8d309c 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.func_typed::<(i32, i32), i32>(&store, "add")?; + let add = instance.func::<(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 6e1d3ef..f517e30 100644 --- a/examples/funcref_callbacks.rs +++ b/examples/funcref_callbacks.rs @@ -1,5 +1,5 @@ use eyre::Result; -use tinywasm::{Extern, FuncContext, Imports, Module, Store, types::FuncRef}; +use tinywasm::{FuncContext, HostFunction, Imports, Module, Store, types::FuncRef}; const LHS: i32 = 5; const RHS: i32 = 3; @@ -49,27 +49,21 @@ fn run_passed_funcref_example() -> Result<()> { let wasm = wat::parse_str(WASM).expect("failed to parse wat"); let module = Module::parse_bytes(&wasm)?; let mut store = Store::default(); - let mut imports = Imports::new(); - imports.define( - "host", - "call_this", - Extern::typed_func(|mut ctx: FuncContext<'_>, func_ref: FuncRef| -> tinywasm::Result<()> { + let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { 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_typed::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; + 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(()) - }), - )?; + }); - imports.define( - "host", - "mul", - Extern::typed_func(|_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { Ok(lhs * rhs) }), - )?; + let mut imports = Imports::new(); + imports.define("host", "call_this", call_this).define("host", "mul", mul); let instance = module.instantiate(&mut store, Some(imports))?; - let caller = instance.func_typed::<(), ()>(&store, "tell_host_to_call")?; + let caller = instance.func::<(), ()>(&store, "tell_host_to_call")?; caller.call(&mut store, ())?; @@ -113,21 +107,16 @@ fn run_returned_funcref_example() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - imports.define( - "host", - "mul", - Extern::typed_func(|_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { Ok(lhs * rhs) }), - )?; + let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { Ok(lhs * rhs) }); + imports.define("host", "mul", mul); let instance = module.instantiate(&mut store, Some(imports))?; - let (add_ref, sub_ref, mul_ref) = { - let get_funcrefs = instance.func_typed::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?; + let get_funcrefs = instance.func::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?; get_funcrefs.call(&mut store, ())? }; - let call_by_ref = instance.func_typed::<(FuncRef, i32, i32), i32>(&store, "call_binop_by_ref")?; - + let call_by_ref = instance.func::<(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 ce9748b..4da7fec 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -43,7 +43,7 @@ fn main() -> Result<()> { let import_instance = import_module.instantiate(&mut store, Some(imports))?; // Call the `main` function, which uses the imported `add` function. - let main = import_instance.func_typed::<(), i32>(&store, "main")?; + let main = import_instance.func::<(), i32>(&store, "main")?; assert_eq!(main.call(&mut store, ())?, 3); Ok(()) diff --git a/examples/resumable.rs b/examples/resumable.rs index f3ebc89..fef01dd 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.func_typed::<i32, i32>(&store, "count_down")?; + let count_down = instance.func::<i32, i32>(&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 e06cf81..7de0dbc 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -1,5 +1,5 @@ #![no_main] -use tinywasm::{Extern, FuncContext}; +use tinywasm::{FuncContext, HostFunction}; #[link(wasm_import_module = "env")] unsafe extern "C" { @@ -19,7 +19,7 @@ fn run() -> tinywasm::Result<()> { imports.define( "env", "printi32", - Extern::typed_func(|_: FuncContext<'_>, v: i32| { + HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { unsafe { printi32(v) } Ok(()) }), diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs index 9a032dd..5444280 100644 --- a/examples/rust/src/tinywasm_no_std.rs +++ b/examples/rust/src/tinywasm_no_std.rs @@ -1,7 +1,7 @@ #![no_main] #![no_std] use lol_alloc::{AssumeSingleThreaded, FreeListAllocator}; -use tinywasm::{Extern, FuncContext}; +use tinywasm::{FuncContext, HostFunction}; extern crate alloc; @@ -36,7 +36,7 @@ fn run() -> tinywasm::Result<()> { imports.define( "env", "printi32", - Extern::typed_func(|_: FuncContext<'_>, v: i32| { + HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { unsafe { printi32(v) } Ok(()) }), diff --git a/examples/simple.rs b/examples/simple.rs index 8371992..38839c9 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.func_typed::<(i32, i32), i32>(&store, "add")?; + let add = instance.func::<(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 66e85b3..f0d566e 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.func_typed::<(i32, i64), (i32, i64)>(&store, "return")?; + let add = instance.func::<(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 0ffa499..4386531 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -1,7 +1,7 @@ use std::hint::black_box; use eyre::{Result, eyre}; -use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store}; +use tinywasm::{FuncContext, HostFunction, Imports, Module, Store}; /// Examples of using WebAssembly compiled from Rust with tinywasm. /// @@ -76,10 +76,10 @@ fn tinywasm() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?; + imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _x: i32| Ok(()))); let instance = module.instantiate(&mut store, Some(black_box(imports)))?; - let hello = instance.func_typed::<(), ()>(&store, "hello")?; + let hello = instance.func::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; @@ -91,10 +91,10 @@ fn tinywasm_no_std() -> Result<()> { let mut store = Store::default(); let mut imports = Imports::new(); - imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?; + imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _x: i32| Ok(()))); let instance = module.instantiate(&mut store, Some(black_box(imports)))?; - let hello = instance.func_typed::<(), ()>(&store, "hello")?; + let hello = instance.func::<(), ()>(&store, "hello")?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; hello.call(&mut store, black_box(()))?; @@ -105,24 +105,22 @@ fn hello() -> Result<()> { let module = Module::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 mem = ctx.memory("memory")?; + let string = mem.load_string(ctx.store(), ptr as usize, len as usize)?; + println!("{string}"); + Ok(()) + }); + let mut imports = Imports::new(); - imports.define( - "env", - "print_utf8", - 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(()) - }), - )?; + imports.define("env", "print_utf8", print_utf8); let instance = module.instantiate(&mut store, Some(imports))?; - let arg_ptr = instance.func_typed::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; + let arg_ptr = instance.func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; let arg = b"world"; - instance.memory_mut(&mut store, "memory")?.store(arg_ptr as usize, arg.len(), arg)?; - let hello = instance.func_typed::<i32, ()>(&store, "hello")?; + instance.memory("memory")?.store(&mut store, arg_ptr as usize, arg.len(), arg)?; + let hello = instance.func::<i32, ()>(&store, "hello")?; hello.call(&mut store, arg.len() as i32)?; Ok(()) @@ -131,19 +129,18 @@ fn hello() -> Result<()> { fn host_fn() -> Result<()> { let module = Module::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)| { + assert_eq!(left, 1); + assert_eq!(right, 2); + Ok(left as i32 + right) + }); + let mut imports = Imports::new(); - imports.define( - "env", - "bar", - Extern::typed_func(|_: FuncContext<'_>, (left, right): (i64, i32)| { - assert_eq!(left, 1); - assert_eq!(right, 2); - Ok(left as i32 + right) - }), - )?; + imports.define("env", "bar", bar); let instance = module.instantiate(&mut store, Some(imports))?; - let host_fn = instance.func_typed::<(), i32>(&store, "foo")?; + let host_fn = instance.func::<(), i32>(&store, "foo")?; assert_eq!(host_fn.call(&mut store, ())?, 3); Ok(()) } @@ -152,18 +149,16 @@ fn printi32() -> Result<()> { let module = Module::parse_file("./examples/rust/out/print.opt.wasm")?; let mut store = Store::default(); + let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, x: i32| { + println!("{x}"); + Ok(()) + }); + let mut imports = Imports::new(); - imports.define( - "env", - "printi32", - Extern::typed_func(|_: FuncContext<'_>, x: i32| { - println!("{x}"); - Ok(()) - }), - )?; + imports.define("env", "printi32", printi32); let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.func_typed::<(i32, i32), ()>(&store, "add_and_print")?; + let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) @@ -174,7 +169,7 @@ fn fibonacci() -> Result<()> { let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let fibonacci = instance.func_typed::<i32, i32>(&store, "fibonacci_recursive")?; + let fibonacci = instance.func::<i32, i32>(&store, "fibonacci_recursive")?; let n = 26; let result = fibonacci.call(&mut store, n)?; println!("fibonacci({n}) = {result}"); @@ -187,7 +182,7 @@ fn argon2id() -> Result<()> { let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let argon2id = instance.func_typed::<(i32, i32, i32), i32>(&store, "argon2id")?; + let argon2id = instance.func::<(i32, i32, i32), i32>(&store, "argon2id")?; argon2id.call(&mut store, (1000, 2, 1))?; Ok(()) |
