From 40e570cf8bdd948c1e7e9dd85e5ac24d9a062d2a Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 14 Jan 2025 17:41:29 +0100 Subject: chore: update deps, spelling Signed-off-by: Henry Gressmann --- examples/linking.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'examples/linking.rs') diff --git a/examples/linking.rs b/examples/linking.rs index d215898..f94773a 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -1,6 +1,7 @@ use eyre::Result; use tinywasm::{Module, Store}; +// WebAssembly module defining and exporting an `add` function. const WASM_ADD: &str = r#" (module (func $add (param $lhs i32) (param $rhs i32) (result i32) @@ -10,6 +11,7 @@ const WASM_ADD: &str = r#" (export "add" (func $add))) "#; +// WebAssembly module importing an `add` function and using it. const WASM_IMPORT: &str = r#" (module (import "adder" "add" (func $add (param i32 i32) (result i32))) @@ -29,13 +31,20 @@ fn main() -> Result<()> { let import_module = Module::parse_bytes(&wasm_import)?; let mut store = Store::default(); + + // Instantiate the `add` module. let add_instance = add_module.instantiate(&mut store, None)?; + // Link the `adder` namespace to the `add` module's instance. let mut imports = tinywasm::Imports::new(); imports.link_module("adder", add_instance.id())?; + + // Instantiate the `import` module with the linked imports. let import_instance = import_module.instantiate(&mut store, Some(imports))?; + // Call the `main` function, which uses the imported `add` function. let main = import_instance.exported_func::<(), i32>(&store, "main")?; assert_eq!(main.call(&mut store, ())?, 3); + Ok(()) } -- cgit v1.3.1