MCPcopy Create free account
hub / github.com/explodingcamera/tinywasm / main

Function main

examples/reentrance.rs:20–43  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18"#;
19
20fn main() -> Result<()> {
21 let wasm = wat::parse_str(WASM)?;
22 let module = tinywasm::parse_bytes(&wasm)?;
23 let mut store = Store::default();
24
25 let call_add_twice = HostFunction::from(&mut store, |mut ctx: FuncContext<'_>, value: i32| {
26 let add_one = ctx.module().func::<i32, i32>(ctx.store(), "add_one")?;
27
28 // Use ctx.call for reentrant calls from host functions. Function::call
29 // starts a root invocation and cannot preserve the active call stacks.
30 let value = ctx.call(&add_one, value)?;
31 ctx.call(&add_one, value)
32 });
33
34 let mut imports = Imports::new();
35 imports.define("host", "call_add_twice", call_add_twice);
36
37 let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?;
38 let run = instance.func::<i32, i32>(&store, "run")?;
39
40 assert_eq!(run.call(&mut store, 40)?, 52);
41
42 Ok(())
43}

Callers

nothing calls this directly

Calls 5

parse_bytesFunction · 0.85
moduleMethod · 0.80
callMethod · 0.80
defineMethod · 0.80
storeMethod · 0.45

Tested by

no test coverage detected