-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathexceptions.rs
More file actions
28 lines (24 loc) · 957 Bytes
/
Copy pathexceptions.rs
File metadata and controls
28 lines (24 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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(())
}