diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2025-02-12 19:13:23 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2025-02-12 19:13:23 +0100 |
| commit | d6493c277eb1104aaf72a8e0e6959ce3d1cc4932 (patch) | |
| tree | a82452aeb44be6b79d15fc83ddfcbd12a965520a /examples/wasm-rust.rs | |
| parent | 7e668f9e321c941f38395e47a29501dce0fc81a9 (diff) | |
fix: fix build, improve error handling
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/wasm-rust.rs')
| -rw-r--r-- | examples/wasm-rust.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 429bdcb..95769f4 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -33,6 +33,7 @@ fn main() -> Result<()> { println!("Available examples:"); println!(" hello"); println!(" printi32"); + println!(" host_fn"); println!(" fibonacci - calculate fibonacci(30)"); println!(" tinywasm - run printi32 inside of tinywasm inside of itself"); println!(" argon2id - run argon2id(1000, 2, 1)"); @@ -46,6 +47,7 @@ fn main() -> Result<()> { "tinywasm" => tinywasm()?, "tinywasm_no_std" => tinywasm_no_std()?, "argon2id" => argon2id()?, + "host_fn" => host_fn()?, "all" => { println!("Running all examples"); println!("\nhello.wasm:"); @@ -60,6 +62,8 @@ fn main() -> Result<()> { tinywasm_no_std()?; println!("argon2id.wasm:"); argon2id()?; + println!("\nhost_fn.wasm:"); + host_fn()?; } _ => {} } @@ -126,6 +130,26 @@ fn hello() -> Result<()> { Ok(()) } +fn host_fn() -> Result<()> { + let module = Module::parse_file("./examples/rust/out/host_fn.wasm")?; + let mut store = Store::default(); + 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) + }), + )?; + + let instance = module.instantiate(&mut store, Some(imports))?; + let host_fn = instance.exported_func::<(), i32>(&store, "foo")?; + assert_eq!(host_fn.call(&mut store, ())?, 3); + Ok(()) +} + fn printi32() -> Result<()> { let module = Module::parse_file("./examples/rust/out/print.opt.wasm")?; let mut store = Store::default(); |
