-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbasic.rs
More file actions
26 lines (21 loc) · 765 Bytes
/
Copy pathbasic.rs
File metadata and controls
26 lines (21 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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(())
}