Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/test-programs/src/bin/async_yield_caller_cancel.rs
3050 views
1
mod bindings {
2
wit_bindgen::generate!({
3
path: "../misc/component-async-tests/wit",
4
world: "yield-caller",
5
});
6
7
use super::Component;
8
export!(Component);
9
}
10
11
use {
12
bindings::{exports::local::local::run::Guest, local::local::continue_},
13
test_programs::async_::{STATUS_RETURNED, STATUS_STARTED, subtask_cancel},
14
};
15
16
#[cfg(target_arch = "wasm32")]
17
#[link(wasm_import_module = "local:local/run")]
18
unsafe extern "C" {
19
#[link_name = "[async-lower]run"]
20
fn run() -> u32;
21
}
22
#[cfg(not(target_arch = "wasm32"))]
23
unsafe extern "C" fn run() -> u32 {
24
unreachable!()
25
}
26
27
struct Component;
28
29
impl Guest for Component {
30
async fn run() {
31
continue_::set_continue(true);
32
33
unsafe {
34
let status = run();
35
let waitable = status >> 4;
36
let status = status & 0xF;
37
assert_eq!(status, STATUS_STARTED);
38
39
// Here we assume the following:
40
//
41
// - Wasmtime will deliver a cancel event to the callee before returning
42
// from `subtask_cancel`.
43
//
44
// - The callee will immediately return as soon as it receives the
45
// event.
46
let status = subtask_cancel(waitable);
47
assert_eq!(status, STATUS_RETURNED);
48
}
49
}
50
}
51
52
// Unused function; required since this file is built as a `bin`:
53
fn main() {}
54
55