diff options
Diffstat (limited to 'examples/references.rs')
| -rw-r--r-- | examples/references.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/references.rs b/examples/references.rs new file mode 100644 index 0000000..b02dd07 --- /dev/null +++ b/examples/references.rs @@ -0,0 +1,34 @@ +use anyhow::Result; +use tinywasm::{FuncRef, HostFunction, Imports, ModuleInstance, Store}; + +const WASM: &str = r#" +(module + (type $callback (func (param i32) (result i32))) + (import "host" "double" (func $double (type $callback))) + (elem declare func $double) + (func (export "callback") (result funcref) + ref.func $double) + (func (export "apply") (param funcref i32) (result i32) + local.get 1 + local.get 0 + ref.cast (ref $callback) + call_ref $callback)) +"#; + +fn main() -> Result<()> { + let mut store = Store::default(); + let mut imports = Imports::new(); + // Imports resolve reusable host definitions against each module's runtime types. + // HostFunction::instantiate is available when a standalone Function is needed. + imports.define("host", "double", HostFunction::from(|_, value: i32| -> tinywasm::Result<i32> { Ok(value * 2) })); + + let wasm = wat::parse_str(WASM)?; + let module = tinywasm::parse_bytes(&wasm)?; + let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; + // Option<FuncRef> maps nullable funcref. Bare FuncRef maps a non-null reference. + let callback = instance.func::<(), Option<FuncRef>>(&store, "callback")?.call(&mut store, ())?; + let apply = instance.func::<(Option<FuncRef>, i32), i32>(&store, "apply")?; + + assert_eq!(apply.call(&mut store, (callback, 21))?, 42); + Ok(()) +} |
