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

Function call_wrapped_async_func

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

Source from the content-addressed store, hash-verified

116// Create an async Func, call it directly:
117#[tokio::test]
118async fn call_wrapped_async_func() -> Result<(), Error> {
119 let engine = Engine::default();
120 let mut store = Store::new(&engine, State::default());
121 store.call_hook(sync_call_hook);
122 let f = Func::wrap_async(
123 &mut store,
124 |caller: Caller<State>, (a, b, c, d): (i32, i64, f32, f64)| {
125 Box::new(async move {
126 // Calling this func will switch context into wasm, then back to host:
127 assert_eq!(caller.data().context, vec![Context::Wasm, Context::Host]);
128
129 assert_eq!(
130 caller.data().calls_into_host,
131 caller.data().returns_from_host + 1
132 );
133 assert_eq!(
134 caller.data().calls_into_wasm,
135 caller.data().returns_from_wasm + 1
136 );
137
138 assert_eq!(a, 1);
139 assert_eq!(b, 2);
140 assert_eq!(c, 3.0);
141 assert_eq!(d, 4.0);
142 })
143 },
144 );
145
146 f.call_async(
147 &mut store,
148 &[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()],
149 &mut [],
150 )
151 .await?;
152
153 // One switch from vm to host to call f, another in return from f.
154 assert_eq!(store.data().calls_into_host, 1);
155 assert_eq!(store.data().returns_from_host, 1);
156 assert_eq!(store.data().calls_into_wasm, 1);
157 assert_eq!(store.data().returns_from_wasm, 1);
158
159 f.typed::<(i32, i64, f32, f64), ()>(&store)?
160 .call_async(&mut store, (1, 2, 3.0, 4.0))
161 .await?;
162
163 assert_eq!(store.data().calls_into_host, 2);
164 assert_eq!(store.data().returns_from_host, 2);
165 assert_eq!(store.data().calls_into_wasm, 2);
166 assert_eq!(store.data().returns_from_wasm, 2);
167
168 Ok(())
169}
170
171// Use the Linker to define a sync func, call it through WebAssembly:
172#[test]

Callers

nothing calls this directly

Calls 4

OkFunction · 0.85
newFunction · 0.50
call_hookMethod · 0.45
call_asyncMethod · 0.45

Tested by

no test coverage detected