summaryrefslogtreecommitdiff
path: root/examples/reentrance.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/reentrance.rs')
-rw-r--r--examples/reentrance.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/examples/reentrance.rs b/examples/reentrance.rs
index 432fac7..cf57891 100644
--- a/examples/reentrance.rs
+++ b/examples/reentrance.rs
@@ -1,4 +1,4 @@
-use eyre::Result;
+use anyhow::Result;
use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store};
const WASM: &str = r#"
@@ -22,11 +22,12 @@ fn main() -> Result<()> {
let module = tinywasm::parse_bytes(&wasm)?;
let mut store = Store::default();
- let call_add_twice = HostFunction::from(&mut store, |mut ctx: FuncContext<'_>, value: i32| {
+ let call_add_twice = HostFunction::from(|mut ctx: FuncContext<'_>, value: i32| {
+ // FuncContext exposes the active module and its Store to the callback.
let add_one = ctx.module().func::<i32, i32>(ctx.store(), "add_one")?;
- // Use ctx.call for reentrant calls from host functions. Function::call
- // starts a root invocation and cannot preserve the active call stacks.
+ // Use ctx.call while a host callback has an active Wasm invocation.
+ // Function::call only starts top-level invocations.
let value = ctx.call(&add_one, value)?;
ctx.call(&add_one, value)
});
@@ -34,7 +35,7 @@ fn main() -> Result<()> {
let mut imports = Imports::new();
imports.define("host", "call_add_twice", call_add_twice);
- let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?;
+ let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?;
let run = instance.func::<i32, i32>(&store, "run")?;
assert_eq!(run.call(&mut store, 40)?, 52);