From d8d439e6401607fab18feb5025f3b91f135e3a50 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Fri, 26 Jan 2024 15:16:05 +0100 Subject: feat: add more examples, work on new public api Signed-off-by: Henry Gressmann --- examples/wasm-rust.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'examples/wasm-rust.rs') diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 3b8877e..3b26863 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -13,6 +13,7 @@ fn main() -> Result<()> { match args[1].as_str() { "hello" => hello()?, + "fibonacci" => fibonacci()?, "tinywasm" => tinywasm()?, _ => {} } @@ -36,7 +37,7 @@ fn tinywasm() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let hello = instance.typed_func::<(), ()>(&mut store, "hello")?; + let hello = instance.exported_func::<(), ()>(&mut store, "hello")?; hello.call(&mut store, ())?; Ok(()) @@ -58,8 +59,22 @@ fn hello() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } + +fn fibonacci() -> Result<()> { + const FIBONACCI_WASM: &[u8] = include_bytes!("./rust/out/fibonacci.wasm"); + let module = Module::parse_bytes(&FIBONACCI_WASM)?; + let mut store = Store::default(); + + let instance = module.instantiate(&mut store, None)?; + let fibonacci = instance.exported_func::(&mut store, "fibonacci")?; + let n = 30; + let result = fibonacci.call(&mut store, n)?; + println!("fibonacci({}) = {}", n, result); + + Ok(()) +} -- cgit v1.3.1