MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / recursion

Function recursion

tests/all/call_hook.rs:347–412  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

345
346#[test]
347fn recursion() -> Result<(), Error> {
348 // Make sure call hook behaves reasonably when called recursively
349
350 let engine = Engine::default();
351 let mut store = Store::new(&engine, State::default());
352 store.call_hook(sync_call_hook);
353 let mut linker = Linker::new(&engine);
354
355 linker.func_wrap("host", "f", |mut caller: Caller<State>, n: i32| {
356 assert_eq!(caller.data().context.last(), Some(&Context::Host));
357
358 assert_eq!(caller.data().calls_into_host, caller.data().calls_into_wasm);
359
360 // Recurse
361 if n > 0 {
362 caller
363 .get_export("export")
364 .expect("caller exports \"export\"")
365 .into_func()
366 .expect("export is a func")
367 .typed::<i32, ()>(&caller)
368 .expect("export typing")
369 .call(&mut caller, n - 1)
370 .unwrap()
371 }
372 })?;
373
374 let wat = r#"
375 (module
376 (import "host" "f"
377 (func $f (param i32)))
378 (func (export "export") (param i32)
379 (call $f (local.get 0)))
380 )
381 "#;
382 let module = Module::new(&engine, wat)?;
383
384 let inst = linker.instantiate(&mut store, &module)?;
385 let export = inst
386 .get_export(&mut store, "export")
387 .expect("get export")
388 .into_func()
389 .expect("export is func");
390
391 // Recursion depth:
392 let n: usize = 10;
393
394 export.call(&mut store, &[Val::I32(n as i32)], &mut [])?;
395
396 // Recurse down to 0: n+1 calls
397 assert_eq!(store.data().calls_into_host, n + 1);
398 assert_eq!(store.data().returns_from_host, n + 1);
399 assert_eq!(store.data().calls_into_wasm, n + 1);
400 assert_eq!(store.data().returns_from_wasm, n + 1);
401
402 export
403 .typed::<i32, ()>(&store)?
404 .call(&mut store, n as i32)?;

Callers

nothing calls this directly

Calls 10

OkFunction · 0.85
newFunction · 0.50
call_hookMethod · 0.45
func_wrapMethod · 0.45
unwrapMethod · 0.45
callMethod · 0.45
expectMethod · 0.45
into_funcMethod · 0.45
get_exportMethod · 0.45
instantiateMethod · 0.45

Tested by

no test coverage detected