()
| 116 | // Create an async Func, call it directly: |
| 117 | #[tokio::test] |
| 118 | async 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] |
nothing calls this directly
no test coverage detected