1024db5b4SJoel Dice mod bindings {
2024db5b4SJoel Dice wit_bindgen::generate!({
3024db5b4SJoel Dice path: "../misc/component-async-tests/wit",
4024db5b4SJoel Dice world: "synchronous-transmit-guest",
5024db5b4SJoel Dice });
6024db5b4SJoel Dice }
7024db5b4SJoel Dice
8024db5b4SJoel Dice use {
9024db5b4SJoel Dice std::mem,
10024db5b4SJoel Dice test_programs::async_::{
11024db5b4SJoel Dice CALLBACK_CODE_EXIT, CALLBACK_CODE_YIELD, COMPLETED, DROPPED, EVENT_NONE, context_get,
12024db5b4SJoel Dice context_set,
13024db5b4SJoel Dice },
14024db5b4SJoel Dice };
15024db5b4SJoel Dice
16024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
17024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
18024db5b4SJoel Dice unsafe extern "C" {
19*020727d0SAlex Crichton #[link_name = "[task-return]start"]
task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8)20024db5b4SJoel Dice fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8);
21024db5b4SJoel Dice }
22024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8)23024db5b4SJoel Dice unsafe extern "C" fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8) {
24024db5b4SJoel Dice unreachable!()
25024db5b4SJoel Dice }
26024db5b4SJoel Dice
27024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
28024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
29024db5b4SJoel Dice unsafe extern "C" {
30*020727d0SAlex Crichton #[link_name = "[stream-new-0]start"]
stream_new() -> u6431024db5b4SJoel Dice fn stream_new() -> u64;
32024db5b4SJoel Dice }
33024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
stream_new() -> u6434024db5b4SJoel Dice unsafe extern "C" fn stream_new() -> u64 {
35024db5b4SJoel Dice unreachable!()
36024db5b4SJoel Dice }
37024db5b4SJoel Dice
38024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
39024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
40024db5b4SJoel Dice unsafe extern "C" {
41*020727d0SAlex Crichton #[link_name = "[stream-write-0]start"]
stream_write(_: u32, _: *const u8, _: usize) -> u3242024db5b4SJoel Dice fn stream_write(_: u32, _: *const u8, _: usize) -> u32;
43024db5b4SJoel Dice }
44024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
stream_write(_: u32, _: *const u8, _: usize) -> u3245024db5b4SJoel Dice unsafe extern "C" fn stream_write(_: u32, _: *const u8, _: usize) -> u32 {
46024db5b4SJoel Dice unreachable!()
47024db5b4SJoel Dice }
48024db5b4SJoel Dice
49024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
50024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
51024db5b4SJoel Dice unsafe extern "C" {
52*020727d0SAlex Crichton #[link_name = "[stream-read-0]start"]
stream_read(_: u32, _: *mut u8, _: usize) -> u3253024db5b4SJoel Dice fn stream_read(_: u32, _: *mut u8, _: usize) -> u32;
54024db5b4SJoel Dice }
55024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
stream_read(_: u32, _: *mut u8, _: usize) -> u3256024db5b4SJoel Dice unsafe extern "C" fn stream_read(_: u32, _: *mut u8, _: usize) -> u32 {
57024db5b4SJoel Dice unreachable!()
58024db5b4SJoel Dice }
59024db5b4SJoel Dice
60024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
61024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
62024db5b4SJoel Dice unsafe extern "C" {
63*020727d0SAlex Crichton #[link_name = "[stream-drop-readable-0]start"]
stream_drop_readable(_: u32)64024db5b4SJoel Dice fn stream_drop_readable(_: u32);
65024db5b4SJoel Dice }
66024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
stream_drop_readable(_: u32)67024db5b4SJoel Dice unsafe extern "C" fn stream_drop_readable(_: u32) {
68024db5b4SJoel Dice unreachable!()
69024db5b4SJoel Dice }
70024db5b4SJoel Dice
71024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
72024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
73024db5b4SJoel Dice unsafe extern "C" {
74*020727d0SAlex Crichton #[link_name = "[stream-drop-writable-0]start"]
stream_drop_writable(_: u32)75024db5b4SJoel Dice fn stream_drop_writable(_: u32);
76024db5b4SJoel Dice }
77024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
stream_drop_writable(_: u32)78024db5b4SJoel Dice unsafe extern "C" fn stream_drop_writable(_: u32) {
79024db5b4SJoel Dice unreachable!()
80024db5b4SJoel Dice }
81024db5b4SJoel Dice
82024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
83024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
84024db5b4SJoel Dice unsafe extern "C" {
85*020727d0SAlex Crichton #[link_name = "[future-new-1]start"]
future_new() -> u6486024db5b4SJoel Dice fn future_new() -> u64;
87024db5b4SJoel Dice }
88024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
future_new() -> u6489024db5b4SJoel Dice unsafe extern "C" fn future_new() -> u64 {
90024db5b4SJoel Dice unreachable!()
91024db5b4SJoel Dice }
92024db5b4SJoel Dice
93024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
94024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
95024db5b4SJoel Dice unsafe extern "C" {
96*020727d0SAlex Crichton #[link_name = "[future-write-1]start"]
future_write(_: u32, _: *const u8) -> u3297024db5b4SJoel Dice fn future_write(_: u32, _: *const u8) -> u32;
98024db5b4SJoel Dice }
99024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
future_write(_: u32, _: *const u8) -> u32100024db5b4SJoel Dice unsafe extern "C" fn future_write(_: u32, _: *const u8) -> u32 {
101024db5b4SJoel Dice unreachable!()
102024db5b4SJoel Dice }
103024db5b4SJoel Dice
104024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
105024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
106024db5b4SJoel Dice unsafe extern "C" {
107*020727d0SAlex Crichton #[link_name = "[future-read-1]start"]
future_read(_: u32, _: *mut u8) -> u32108024db5b4SJoel Dice fn future_read(_: u32, _: *mut u8) -> u32;
109024db5b4SJoel Dice }
110024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
future_read(_: u32, _: *mut u8) -> u32111024db5b4SJoel Dice unsafe extern "C" fn future_read(_: u32, _: *mut u8) -> u32 {
112024db5b4SJoel Dice unreachable!()
113024db5b4SJoel Dice }
114024db5b4SJoel Dice
115024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
116024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
117024db5b4SJoel Dice unsafe extern "C" {
118*020727d0SAlex Crichton #[link_name = "[future-drop-readable-1]start"]
future_drop_readable(_: u32)119024db5b4SJoel Dice fn future_drop_readable(_: u32);
120024db5b4SJoel Dice }
121024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
future_drop_readable(_: u32)122024db5b4SJoel Dice unsafe extern "C" fn future_drop_readable(_: u32) {
123024db5b4SJoel Dice unreachable!()
124024db5b4SJoel Dice }
125024db5b4SJoel Dice
126024db5b4SJoel Dice #[cfg(target_arch = "wasm32")]
127024db5b4SJoel Dice #[link(wasm_import_module = "[export]local:local/synchronous-transmit")]
128024db5b4SJoel Dice unsafe extern "C" {
129*020727d0SAlex Crichton #[link_name = "[future-drop-writable-1]start"]
future_drop_writable(_: u32)130024db5b4SJoel Dice fn future_drop_writable(_: u32);
131024db5b4SJoel Dice }
132024db5b4SJoel Dice #[cfg(not(target_arch = "wasm32"))]
future_drop_writable(_: u32)133024db5b4SJoel Dice unsafe extern "C" fn future_drop_writable(_: u32) {
134024db5b4SJoel Dice unreachable!()
135024db5b4SJoel Dice }
136024db5b4SJoel Dice
137024db5b4SJoel Dice static STREAM_BYTES_TO_WRITE: &[u8] = &[1, 3, 5, 7, 11];
138024db5b4SJoel Dice static FUTURE_BYTE_TO_WRITE: u8 = 13;
139024db5b4SJoel Dice
140024db5b4SJoel Dice enum State {
141024db5b4SJoel Dice S0 {
142024db5b4SJoel Dice stream: u32,
143024db5b4SJoel Dice stream_expected: Vec<u8>,
144024db5b4SJoel Dice future: u32,
145024db5b4SJoel Dice future_expected: u8,
146024db5b4SJoel Dice },
147024db5b4SJoel Dice S1 {
148024db5b4SJoel Dice stream_tx: u32,
149024db5b4SJoel Dice stream: u32,
150024db5b4SJoel Dice stream_expected: Vec<u8>,
151024db5b4SJoel Dice future_tx: u32,
152024db5b4SJoel Dice future: u32,
153024db5b4SJoel Dice future_expected: u8,
154024db5b4SJoel Dice },
155024db5b4SJoel Dice }
156024db5b4SJoel Dice
157*020727d0SAlex Crichton #[unsafe(export_name = "[async-lift]local:local/synchronous-transmit#start")]
export_start( stream: u32, stream_expected: u32, stream_expected_len: u32, future: u32, future_expected: u8, ) -> u32158024db5b4SJoel Dice unsafe extern "C" fn export_start(
159024db5b4SJoel Dice stream: u32,
160024db5b4SJoel Dice stream_expected: u32,
161024db5b4SJoel Dice stream_expected_len: u32,
162024db5b4SJoel Dice future: u32,
163024db5b4SJoel Dice future_expected: u8,
164024db5b4SJoel Dice ) -> u32 {
165024db5b4SJoel Dice let stream_expected_len = usize::try_from(stream_expected_len).unwrap();
166024db5b4SJoel Dice
167024db5b4SJoel Dice unsafe {
168024db5b4SJoel Dice context_set(
169024db5b4SJoel Dice u32::try_from(Box::into_raw(Box::new(State::S0 {
170024db5b4SJoel Dice stream,
171024db5b4SJoel Dice stream_expected: Vec::from_raw_parts(
172024db5b4SJoel Dice stream_expected as usize as *mut u8,
173024db5b4SJoel Dice stream_expected_len,
174024db5b4SJoel Dice stream_expected_len,
175024db5b4SJoel Dice ),
176024db5b4SJoel Dice future,
177024db5b4SJoel Dice future_expected,
178024db5b4SJoel Dice })) as usize)
179024db5b4SJoel Dice .unwrap(),
180024db5b4SJoel Dice );
181024db5b4SJoel Dice
182024db5b4SJoel Dice callback_start(EVENT_NONE, 0, 0)
183024db5b4SJoel Dice }
184024db5b4SJoel Dice }
185024db5b4SJoel Dice
186*020727d0SAlex Crichton #[unsafe(export_name = "[callback][async-lift]local:local/synchronous-transmit#start")]
callback_start(event0: u32, _event1: u32, _event2: u32) -> u32187024db5b4SJoel Dice unsafe extern "C" fn callback_start(event0: u32, _event1: u32, _event2: u32) -> u32 {
188024db5b4SJoel Dice unsafe {
189024db5b4SJoel Dice let state = &mut *(usize::try_from(context_get()).unwrap() as *mut State);
190024db5b4SJoel Dice match state {
191024db5b4SJoel Dice &mut State::S0 {
192024db5b4SJoel Dice stream,
193024db5b4SJoel Dice ref mut stream_expected,
194024db5b4SJoel Dice future,
195024db5b4SJoel Dice future_expected,
196024db5b4SJoel Dice } => {
197024db5b4SJoel Dice assert_eq!(event0, EVENT_NONE);
198024db5b4SJoel Dice
199024db5b4SJoel Dice let pair = stream_new();
200024db5b4SJoel Dice let stream_tx = u32::try_from(pair >> 32).unwrap();
201024db5b4SJoel Dice let stream_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();
202024db5b4SJoel Dice
203024db5b4SJoel Dice let pair = future_new();
204024db5b4SJoel Dice let future_tx = u32::try_from(pair >> 32).unwrap();
205024db5b4SJoel Dice let future_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();
206024db5b4SJoel Dice
207024db5b4SJoel Dice task_return_start(
208024db5b4SJoel Dice stream_rx,
209024db5b4SJoel Dice STREAM_BYTES_TO_WRITE.as_ptr(),
210024db5b4SJoel Dice STREAM_BYTES_TO_WRITE.len(),
211024db5b4SJoel Dice future_rx,
212024db5b4SJoel Dice FUTURE_BYTE_TO_WRITE,
213024db5b4SJoel Dice );
214024db5b4SJoel Dice
215024db5b4SJoel Dice *state = State::S1 {
216024db5b4SJoel Dice stream_tx,
217024db5b4SJoel Dice stream,
218024db5b4SJoel Dice stream_expected: mem::take(stream_expected),
219024db5b4SJoel Dice future_tx,
220024db5b4SJoel Dice future,
221024db5b4SJoel Dice future_expected,
222024db5b4SJoel Dice };
223024db5b4SJoel Dice
224024db5b4SJoel Dice CALLBACK_CODE_YIELD
225024db5b4SJoel Dice }
226024db5b4SJoel Dice
227024db5b4SJoel Dice &mut State::S1 {
228024db5b4SJoel Dice stream_tx,
229024db5b4SJoel Dice stream,
230024db5b4SJoel Dice ref mut stream_expected,
231024db5b4SJoel Dice future_tx,
232024db5b4SJoel Dice future,
233024db5b4SJoel Dice future_expected,
234024db5b4SJoel Dice } => {
235024db5b4SJoel Dice // Now we synchronously read and write and expect that the
236024db5b4SJoel Dice // operations complete.
237024db5b4SJoel Dice
238024db5b4SJoel Dice let mut buffer = vec![0_u8; stream_expected.len()];
239024db5b4SJoel Dice let status = stream_read(stream, buffer.as_mut_ptr(), stream_expected.len());
240024db5b4SJoel Dice assert_eq!(
241024db5b4SJoel Dice status,
242024db5b4SJoel Dice DROPPED | u32::try_from(stream_expected.len() << 4).unwrap()
243024db5b4SJoel Dice );
244024db5b4SJoel Dice assert_eq!(&buffer[..], stream_expected);
245024db5b4SJoel Dice stream_drop_readable(stream);
246024db5b4SJoel Dice
247024db5b4SJoel Dice let status = stream_write(
248024db5b4SJoel Dice stream_tx,
249024db5b4SJoel Dice STREAM_BYTES_TO_WRITE.as_ptr(),
250024db5b4SJoel Dice STREAM_BYTES_TO_WRITE.len(),
251024db5b4SJoel Dice );
252024db5b4SJoel Dice assert_eq!(
253024db5b4SJoel Dice status,
254024db5b4SJoel Dice DROPPED | u32::try_from(STREAM_BYTES_TO_WRITE.len() << 4).unwrap()
255024db5b4SJoel Dice );
256024db5b4SJoel Dice stream_drop_writable(stream_tx);
257024db5b4SJoel Dice
258024db5b4SJoel Dice let received = &mut 0_u8;
259024db5b4SJoel Dice let status = future_read(future, received);
260024db5b4SJoel Dice assert_eq!(status, COMPLETED);
261024db5b4SJoel Dice assert_eq!(*received, future_expected);
262024db5b4SJoel Dice future_drop_readable(future);
263024db5b4SJoel Dice
264024db5b4SJoel Dice let status = future_write(future_tx, &FUTURE_BYTE_TO_WRITE);
265024db5b4SJoel Dice assert_eq!(status, COMPLETED);
266024db5b4SJoel Dice future_drop_writable(future_tx);
267024db5b4SJoel Dice
268024db5b4SJoel Dice CALLBACK_CODE_EXIT
269024db5b4SJoel Dice }
270024db5b4SJoel Dice }
271024db5b4SJoel Dice }
272024db5b4SJoel Dice }
273024db5b4SJoel Dice
274024db5b4SJoel Dice // Unused function; required since this file is built as a `bin`:
main()275024db5b4SJoel Dice fn main() {}
276