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

Function call_wrapped_func

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

Source from the content-addressed store, hash-verified

9// Crate a synchronous Func, call it directly:
10#[test]
11fn call_wrapped_func() -> Result<(), Error> {
12 let mut store = Store::<State>::default();
13 store.call_hook(sync_call_hook);
14
15 fn verify(state: &State) {
16 // Calling this func will switch context into wasm, then back to host:
17 assert_eq!(state.context, vec![Context::Wasm, Context::Host]);
18
19 assert_eq!(state.calls_into_host, state.returns_from_host + 1);
20 assert_eq!(state.calls_into_wasm, state.returns_from_wasm + 1);
21 }
22
23 let mut funcs = Vec::new();
24 funcs.push(Func::wrap(
25 &mut store,
26 |caller: Caller<State>, a: i32, b: i64, c: f32, d: f64| {
27 verify(caller.data());
28
29 assert_eq!(a, 1);
30 assert_eq!(b, 2);
31 assert_eq!(c, 3.0);
32 assert_eq!(d, 4.0);
33 },
34 ));
35 let func_ty = FuncType::new(
36 store.engine(),
37 [ValType::I32, ValType::I64, ValType::F32, ValType::F64],
38 [],
39 );
40 funcs.push(Func::new(
41 &mut store,
42 func_ty,
43 |caller: Caller<State>, params, results| {
44 verify(caller.data());
45
46 assert_eq!(params.len(), 4);
47 assert_eq!(params[0].i32().unwrap(), 1);
48 assert_eq!(params[1].i64().unwrap(), 2);
49 assert_eq!(params[2].f32().unwrap(), 3.0);
50 assert_eq!(params[3].f64().unwrap(), 4.0);
51 assert_eq!(results.len(), 0);
52 Ok(())
53 },
54 ));
55 let func_ty = FuncType::new(
56 store.engine(),
57 [ValType::I32, ValType::I64, ValType::F32, ValType::F64],
58 [],
59 );
60 funcs.push(unsafe {
61 Func::new_unchecked(&mut store, func_ty, |caller: Caller<State>, space| {
62 verify(caller.data());
63
64 assert_eq!(space[0].assume_init_ref().get_i32(), 1i32);
65 assert_eq!(space[1].assume_init_ref().get_i64(), 2i64);
66 assert_eq!(space[2].assume_init_ref().get_f32(), 3.0f32.to_bits());
67 assert_eq!(space[3].assume_init_ref().get_f64(), 4.0f64.to_bits());
68 Ok(())

Callers

nothing calls this directly

Calls 12

OkFunction · 0.85
call_uncheckedMethod · 0.80
verifyFunction · 0.70
newFunction · 0.50
call_hookMethod · 0.45
pushMethod · 0.45
dataMethod · 0.45
engineMethod · 0.45
iterMethod · 0.45
callMethod · 0.45
to_rawMethod · 0.45
to_bitsMethod · 0.45

Tested by

no test coverage detected