()
| 237 | // Use the Linker to define an async func, call it through WebAssembly: |
| 238 | #[tokio::test] |
| 239 | async fn call_linked_func_async() -> Result<(), Error> { |
| 240 | let engine = Engine::default(); |
| 241 | let mut store = Store::new(&engine, State::default()); |
| 242 | store.call_hook(sync_call_hook); |
| 243 | |
| 244 | let f = Func::wrap_async( |
| 245 | &mut store, |
| 246 | |caller: Caller<State>, (a, b, c, d): (i32, i64, f32, f64)| { |
| 247 | Box::new(async move { |
| 248 | // Calling this func will switch context into wasm, then back to host: |
| 249 | assert_eq!(caller.data().context, vec![Context::Wasm, Context::Host]); |
| 250 | |
| 251 | assert_eq!( |
| 252 | caller.data().calls_into_host, |
| 253 | caller.data().returns_from_host + 1 |
| 254 | ); |
| 255 | assert_eq!( |
| 256 | caller.data().calls_into_wasm, |
| 257 | caller.data().returns_from_wasm + 1 |
| 258 | ); |
| 259 | assert_eq!(a, 1); |
| 260 | assert_eq!(b, 2); |
| 261 | assert_eq!(c, 3.0); |
| 262 | assert_eq!(d, 4.0); |
| 263 | }) |
| 264 | }, |
| 265 | ); |
| 266 | |
| 267 | let mut linker = Linker::new(&engine); |
| 268 | |
| 269 | linker.define(&mut store, "host", "f", f)?; |
| 270 | |
| 271 | let wat = r#" |
| 272 | (module |
| 273 | (import "host" "f" |
| 274 | (func $f (param i32) (param i64) (param f32) (param f64))) |
| 275 | (func (export "export") |
| 276 | (call $f (i32.const 1) (i64.const 2) (f32.const 3.0) (f64.const 4.0))) |
| 277 | ) |
| 278 | "#; |
| 279 | let module = Module::new(&engine, wat)?; |
| 280 | |
| 281 | let inst = linker.instantiate_async(&mut store, &module).await?; |
| 282 | let export = inst |
| 283 | .get_export(&mut store, "export") |
| 284 | .expect("get export") |
| 285 | .into_func() |
| 286 | .expect("export is func"); |
| 287 | |
| 288 | export.call_async(&mut store, &[], &mut []).await?; |
| 289 | |
| 290 | // One switch from vm to host to call f, another in return from f. |
| 291 | assert_eq!(store.data().calls_into_host, 1); |
| 292 | assert_eq!(store.data().returns_from_host, 1); |
| 293 | assert_eq!(store.data().calls_into_wasm, 1); |
| 294 | assert_eq!(store.data().returns_from_wasm, 1); |
| 295 | |
| 296 | export |
nothing calls this directly
no test coverage detected