()
| 546 | |
| 547 | #[tokio::test] |
| 548 | async fn basic_async_hook() -> Result<(), Error> { |
| 549 | struct HandlerR; |
| 550 | |
| 551 | #[async_trait::async_trait] |
| 552 | impl CallHookHandler<State> for HandlerR { |
| 553 | async fn handle_call_event( |
| 554 | &self, |
| 555 | ctx: StoreContextMut<'_, State>, |
| 556 | ch: CallHook, |
| 557 | ) -> Result<()> { |
| 558 | sync_call_hook(ctx, ch) |
| 559 | } |
| 560 | } |
| 561 | let engine = Engine::default(); |
| 562 | let mut store = Store::new(&engine, State::default()); |
| 563 | store.call_hook_async(HandlerR {}); |
| 564 | |
| 565 | assert_eq!(store.data().calls_into_host, 0); |
| 566 | assert_eq!(store.data().returns_from_host, 0); |
| 567 | assert_eq!(store.data().calls_into_wasm, 0); |
| 568 | assert_eq!(store.data().returns_from_wasm, 0); |
| 569 | |
| 570 | let mut linker = Linker::new(&engine); |
| 571 | |
| 572 | linker.func_wrap( |
| 573 | "host", |
| 574 | "f", |
| 575 | |caller: Caller<State>, a: i32, b: i64, c: f32, d: f64| { |
| 576 | // Calling this func will switch context into wasm, then back to host: |
| 577 | assert_eq!(caller.data().context, vec![Context::Wasm, Context::Host]); |
| 578 | |
| 579 | assert_eq!( |
| 580 | caller.data().calls_into_host, |
| 581 | caller.data().returns_from_host + 1 |
| 582 | ); |
| 583 | assert_eq!( |
| 584 | caller.data().calls_into_wasm, |
| 585 | caller.data().returns_from_wasm + 1 |
| 586 | ); |
| 587 | |
| 588 | assert_eq!(a, 1); |
| 589 | assert_eq!(b, 2); |
| 590 | assert_eq!(c, 3.0); |
| 591 | assert_eq!(d, 4.0); |
| 592 | }, |
| 593 | )?; |
| 594 | |
| 595 | let wat = r#" |
| 596 | (module |
| 597 | (import "host" "f" |
| 598 | (func $f (param i32) (param i64) (param f32) (param f64))) |
| 599 | (func (export "export") |
| 600 | (call $f (i32.const 1) (i64.const 2) (f32.const 3.0) (f64.const 4.0))) |
| 601 | ) |
| 602 | "#; |
| 603 | let module = Module::new(&engine, wat)?; |
| 604 | |
| 605 | let inst = linker.instantiate_async(&mut store, &module).await?; |
nothing calls this directly
no test coverage detected