diff options
| author | Henry <mail@henrygressmann.de> | 2026-04-12 21:16:49 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-04-12 21:16:49 +0200 |
| commit | 80cf746b50513af77203f382f5f53bd036087358 (patch) | |
| tree | a9493ec0a69caba0dd82e568b6556340be306e0a /examples/wasm-rust.rs | |
| parent | d89729df921d39f81fe8a43990678853deb1edde (diff) | |
feat: remove lifetimes from runtime objects
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'examples/wasm-rust.rs')
| -rw-r--r-- | examples/wasm-rust.rs | 73 |
1 files changed, 34 insertions, 39 deletions
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(()) |
