summaryrefslogtreecommitdiff
path: root/examples/basic.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic.rs')
-rw-r--r--examples/basic.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/examples/basic.rs b/examples/basic.rs
new file mode 100644
index 0000000..1f717c5
--- /dev/null
+++ b/examples/basic.rs
@@ -0,0 +1,26 @@
+use anyhow::Result;
+use tinywasm::{ModuleInstance, 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)?;
+ let module = tinywasm::parse_bytes(&wasm)?;
+
+ // Module is reusable, while Store owns the runtime state for this instance.
+ let mut store = Store::default();
+ let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
+
+ // Typed handles validate parameters and results. Use func_untyped for dynamic values.
+ let add = instance.func::<(i32, i32), i32>(&store, "add")?;
+
+ assert_eq!(add.call(&mut store, (1, 2))?, 3);
+ Ok(())
+}