Path: blob/main/crates/test-programs/src/bin/async_poll_synchronous.rs
1693 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "poll",4async: false,5});67use super::Component;8export!(Component);9}1011use {12bindings::{exports::local::local::run::Guest, local::local::ready},13test_programs::async_::{14EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED, subtask_drop, waitable_join, waitable_set_drop,15waitable_set_new, waitable_set_poll,16},17};1819fn async_when_ready() -> u32 {20#[cfg(not(target_arch = "wasm32"))]21{22unreachable!()23}2425#[cfg(target_arch = "wasm32")]26{27#[link(wasm_import_module = "local:local/ready")]28unsafe extern "C" {29#[link_name = "[async-lower][async]when-ready"]30fn call_when_ready() -> u32;31}32unsafe { call_when_ready() }33}34}3536struct Component;3738impl Guest for Component {39fn run() {40unsafe {41ready::set_ready(false);4243let set = waitable_set_new();4445assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));4647let result = async_when_ready();48let status = result & 0xf;49let call = result >> 4;50assert!(status != STATUS_RETURNED);51waitable_join(call, set);5253assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));5455ready::set_ready(true);5657let (event, task, code) = waitable_set_poll(set);58assert_eq!(event, EVENT_SUBTASK);59assert_eq!(call, task);60assert_eq!(code, STATUS_RETURNED);6162subtask_drop(task);6364assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));6566assert_eq!(async_when_ready(), STATUS_RETURNED);6768assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));6970waitable_set_drop(set);71}72}73}7475// Unused function; required since this file is built as a `bin`:76fn main() {}777879