From 963ddd26a89490458e31d9d553dffafe5e350e96 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Mon, 29 Jan 2024 22:38:06 +0100 Subject: chore: add more examples, refactoring Signed-off-by: Henry Gressmann --- examples/archive.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/archive.rs (limited to 'examples/archive.rs') diff --git a/examples/archive.rs b/examples/archive.rs new file mode 100644 index 0000000..7c93205 --- /dev/null +++ b/examples/archive.rs @@ -0,0 +1,29 @@ +use color_eyre::eyre::Result; +use tinywasm::{parser::Parser, types::TinyWasmModule, Module, Store}; + +const WASM: &str = r#" +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (export "add" (func $add))) +"#; + +fn main() -> Result<()> { + let wasm = wat::parse_str(WASM).expect("failed to parse wat"); + let module = Parser::default().parse_module_bytes(&wasm)?; + let twasm = module.serialize_twasm(); + + // now, you could e.g. write twasm to a file called `add.twasm` + // and load it later in a different program + + 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")?; + + assert_eq!(add.call(&mut store, (1, 2))?, 3); + + Ok(()) +} -- cgit v1.3.1