diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2026-08-24 15:47:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-24 15:47:05 +0200 |
| commit | 7b9b3ec681e68280399d9c67e01433c2d3acb6c2 (patch) | |
| tree | b6000a9755d50fe1ba6f93de6ed784deb179ae49 /examples/linking.rs | |
| parent | 3e2e964f70e6128b891fa3b43f73a821493bd7bc (diff) | |
feat: add host typed reference / gc APIs (#54)
- add Store-aware handles for references, GC objects, and exceptions
- simplify host value conversion, rooting, and exception propagation
- update examples, tests, and documentation
- Remove deprecated APIs
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'examples/linking.rs')
| -rw-r--r-- | examples/linking.rs | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/examples/linking.rs b/examples/linking.rs index 2df7133..92c0b7d 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -1,7 +1,6 @@ use anyhow::Result; use tinywasm::{ModuleInstance, 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) @@ -11,7 +10,6 @@ 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))) @@ -24,25 +22,23 @@ const WASM_IMPORT: &str = r#" "#; fn main() -> Result<()> { - let wasm_add = wat::parse_str(WASM_ADD).expect("failed to parse wat"); - let wasm_import = wat::parse_str(WASM_IMPORT).expect("failed to parse wat"); + let wasm_add = wat::parse_str(WASM_ADD)?; + let wasm_import = wat::parse_str(WASM_IMPORT)?; let add_module = tinywasm::parse_bytes(&wasm_add)?; let import_module = tinywasm::parse_bytes(&wasm_import)?; let mut store = Store::default(); - // Instantiate the `add` module. let add_instance = ModuleInstance::instantiate(&mut store, &add_module, None)?; - // Link the `adder` namespace to the `add` module's instance. + // Imports can link a module namespace and define individual host items together. let mut imports = tinywasm::Imports::new(); imports.link_module("adder", add_instance)?; - // Instantiate the `import` module with the linked imports. let import_instance = ModuleInstance::instantiate(&mut store, &import_module, Some(&imports))?; - // Call the `main` function, which uses the imported `add` function. + // Calling `main` crosses the linked module boundary to `add`. let main = import_instance.func::<(), i32>(&store, "main")?; assert_eq!(main.call(&mut store, ())?, 3); |
