summaryrefslogtreecommitdiff
path: root/examples/exceptions.rs
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2026-08-24 15:47:05 +0200
committerGitHub <noreply@github.com>2026-08-24 15:47:05 +0200
commit7b9b3ec681e68280399d9c67e01433c2d3acb6c2 (patch)
treeb6000a9755d50fe1ba6f93de6ed784deb179ae49 /examples/exceptions.rs
parent3e2e964f70e6128b891fa3b43f73a821493bd7bc (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/exceptions.rs')
-rw-r--r--examples/exceptions.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/exceptions.rs b/examples/exceptions.rs
new file mode 100644
index 0000000..7c2f88e
--- /dev/null
+++ b/examples/exceptions.rs
@@ -0,0 +1,28 @@
+use anyhow::{Result, bail};
+use tinywasm::{Error, ModuleInstance, Store, WasmValue};
+
+const WASM: &str = r#"
+(module
+ (tag $failure (export "failure") (param i32))
+ (func (export "fail") (param i32)
+ local.get 0
+ throw $failure))
+"#;
+
+fn main() -> Result<()> {
+ let wasm = wat::parse_str(WASM)?;
+ let module = tinywasm::parse_bytes(&wasm)?;
+ let mut store = Store::default();
+ let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
+ let fail = instance.func::<i32, ()>(&store, "fail")?;
+
+ // Uncaught guest exceptions cross the host boundary as owned ExnRef handles.
+ let Err(Error::Exception(exception)) = fail.call(&mut store, 42) else {
+ bail!("expected a guest exception");
+ };
+
+ // The handle exposes the exception tag and typed payload values.
+ assert_eq!(exception.tag(&store)?, instance.tag("failure")?);
+ assert_eq!(exception.fields(&mut store)?, [WasmValue::I32(42)]);
+ Ok(())
+}