Path: blob/main/crates/test-programs/src/bin/async_synchronous_transmit.rs
3069 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "synchronous-transmit-guest",4});5}67use {8std::mem,9test_programs::async_::{10CALLBACK_CODE_EXIT, CALLBACK_CODE_YIELD, COMPLETED, DROPPED, EVENT_NONE, context_get,11context_set,12},13};1415#[cfg(target_arch = "wasm32")]16#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]17unsafe extern "C" {18#[link_name = "[task-return]start"]19fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8);20}21#[cfg(not(target_arch = "wasm32"))]22unsafe extern "C" fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8) {23unreachable!()24}2526#[cfg(target_arch = "wasm32")]27#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]28unsafe extern "C" {29#[link_name = "[stream-new-0]start"]30fn stream_new() -> u64;31}32#[cfg(not(target_arch = "wasm32"))]33unsafe extern "C" fn stream_new() -> u64 {34unreachable!()35}3637#[cfg(target_arch = "wasm32")]38#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]39unsafe extern "C" {40#[link_name = "[stream-write-0]start"]41fn stream_write(_: u32, _: *const u8, _: usize) -> u32;42}43#[cfg(not(target_arch = "wasm32"))]44unsafe extern "C" fn stream_write(_: u32, _: *const u8, _: usize) -> u32 {45unreachable!()46}4748#[cfg(target_arch = "wasm32")]49#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]50unsafe extern "C" {51#[link_name = "[stream-read-0]start"]52fn stream_read(_: u32, _: *mut u8, _: usize) -> u32;53}54#[cfg(not(target_arch = "wasm32"))]55unsafe extern "C" fn stream_read(_: u32, _: *mut u8, _: usize) -> u32 {56unreachable!()57}5859#[cfg(target_arch = "wasm32")]60#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]61unsafe extern "C" {62#[link_name = "[stream-drop-readable-0]start"]63fn stream_drop_readable(_: u32);64}65#[cfg(not(target_arch = "wasm32"))]66unsafe extern "C" fn stream_drop_readable(_: u32) {67unreachable!()68}6970#[cfg(target_arch = "wasm32")]71#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]72unsafe extern "C" {73#[link_name = "[stream-drop-writable-0]start"]74fn stream_drop_writable(_: u32);75}76#[cfg(not(target_arch = "wasm32"))]77unsafe extern "C" fn stream_drop_writable(_: u32) {78unreachable!()79}8081#[cfg(target_arch = "wasm32")]82#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]83unsafe extern "C" {84#[link_name = "[future-new-1]start"]85fn future_new() -> u64;86}87#[cfg(not(target_arch = "wasm32"))]88unsafe extern "C" fn future_new() -> u64 {89unreachable!()90}9192#[cfg(target_arch = "wasm32")]93#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]94unsafe extern "C" {95#[link_name = "[future-write-1]start"]96fn future_write(_: u32, _: *const u8) -> u32;97}98#[cfg(not(target_arch = "wasm32"))]99unsafe extern "C" fn future_write(_: u32, _: *const u8) -> u32 {100unreachable!()101}102103#[cfg(target_arch = "wasm32")]104#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]105unsafe extern "C" {106#[link_name = "[future-read-1]start"]107fn future_read(_: u32, _: *mut u8) -> u32;108}109#[cfg(not(target_arch = "wasm32"))]110unsafe extern "C" fn future_read(_: u32, _: *mut u8) -> u32 {111unreachable!()112}113114#[cfg(target_arch = "wasm32")]115#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]116unsafe extern "C" {117#[link_name = "[future-drop-readable-1]start"]118fn future_drop_readable(_: u32);119}120#[cfg(not(target_arch = "wasm32"))]121unsafe extern "C" fn future_drop_readable(_: u32) {122unreachable!()123}124125#[cfg(target_arch = "wasm32")]126#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]127unsafe extern "C" {128#[link_name = "[future-drop-writable-1]start"]129fn future_drop_writable(_: u32);130}131#[cfg(not(target_arch = "wasm32"))]132unsafe extern "C" fn future_drop_writable(_: u32) {133unreachable!()134}135136static STREAM_BYTES_TO_WRITE: &[u8] = &[1, 3, 5, 7, 11];137static FUTURE_BYTE_TO_WRITE: u8 = 13;138139enum State {140S0 {141stream: u32,142stream_expected: Vec<u8>,143future: u32,144future_expected: u8,145},146S1 {147stream_tx: u32,148stream: u32,149stream_expected: Vec<u8>,150future_tx: u32,151future: u32,152future_expected: u8,153},154}155156#[unsafe(export_name = "[async-lift]local:local/synchronous-transmit#start")]157unsafe extern "C" fn export_start(158stream: u32,159stream_expected: u32,160stream_expected_len: u32,161future: u32,162future_expected: u8,163) -> u32 {164let stream_expected_len = usize::try_from(stream_expected_len).unwrap();165166unsafe {167context_set(168u32::try_from(Box::into_raw(Box::new(State::S0 {169stream,170stream_expected: Vec::from_raw_parts(171stream_expected as usize as *mut u8,172stream_expected_len,173stream_expected_len,174),175future,176future_expected,177})) as usize)178.unwrap(),179);180181callback_start(EVENT_NONE, 0, 0)182}183}184185#[unsafe(export_name = "[callback][async-lift]local:local/synchronous-transmit#start")]186unsafe extern "C" fn callback_start(event0: u32, _event1: u32, _event2: u32) -> u32 {187unsafe {188let state = &mut *(usize::try_from(context_get()).unwrap() as *mut State);189match state {190&mut State::S0 {191stream,192ref mut stream_expected,193future,194future_expected,195} => {196assert_eq!(event0, EVENT_NONE);197198let pair = stream_new();199let stream_tx = u32::try_from(pair >> 32).unwrap();200let stream_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();201202let pair = future_new();203let future_tx = u32::try_from(pair >> 32).unwrap();204let future_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();205206task_return_start(207stream_rx,208STREAM_BYTES_TO_WRITE.as_ptr(),209STREAM_BYTES_TO_WRITE.len(),210future_rx,211FUTURE_BYTE_TO_WRITE,212);213214*state = State::S1 {215stream_tx,216stream,217stream_expected: mem::take(stream_expected),218future_tx,219future,220future_expected,221};222223CALLBACK_CODE_YIELD224}225226&mut State::S1 {227stream_tx,228stream,229ref mut stream_expected,230future_tx,231future,232future_expected,233} => {234// Now we synchronously read and write and expect that the235// operations complete.236237let mut buffer = vec![0_u8; stream_expected.len()];238let status = stream_read(stream, buffer.as_mut_ptr(), stream_expected.len());239assert_eq!(240status,241DROPPED | u32::try_from(stream_expected.len() << 4).unwrap()242);243assert_eq!(&buffer[..], stream_expected);244stream_drop_readable(stream);245246let status = stream_write(247stream_tx,248STREAM_BYTES_TO_WRITE.as_ptr(),249STREAM_BYTES_TO_WRITE.len(),250);251assert_eq!(252status,253DROPPED | u32::try_from(STREAM_BYTES_TO_WRITE.len() << 4).unwrap()254);255stream_drop_writable(stream_tx);256257let received = &mut 0_u8;258let status = future_read(future, received);259assert_eq!(status, COMPLETED);260assert_eq!(*received, future_expected);261future_drop_readable(future);262263let status = future_write(future_tx, &FUTURE_BYTE_TO_WRITE);264assert_eq!(status, COMPLETED);265future_drop_writable(future_tx);266267CALLBACK_CODE_EXIT268}269}270}271}272273// Unused function; required since this file is built as a `bin`:274fn main() {}275276277