summaryrefslogtreecommitdiff
path: root/examples/linking.rs
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2025-01-14 17:41:29 +0100
committerHenry Gressmann <mail@henrygressmann.de>2025-01-14 17:41:29 +0100
commit40e570cf8bdd948c1e7e9dd85e5ac24d9a062d2a (patch)
tree64e9338f1c656fd9d81f51cd4f425359b2dbc6d1 /examples/linking.rs
parent1a64f4092af9304694e98f441b79f0a93ee1c194 (diff)
chore: update deps, spelling
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/linking.rs')
-rw-r--r--examples/linking.rs9
1 files changed, 9 insertions, 0 deletions
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(())
}