Skip to content

Commit

Permalink
Remove the poll_list workarounds that are no longer needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Sep 26, 2023
1 parent 461e45c commit 129db82
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ fn main() {
if args == &["correct"] {
let stdin: streams::InputStream = stdin::get_stdin();
let stdin_pollable = stdin.subscribe();
let ready = poll_list(&[&stdin_pollable]);
let ready = poll::poll_list(&[&stdin_pollable]);
assert_eq!(ready, &[0]);
drop(stdin_pollable);
drop(stdin);
} else if args == &["trap"] {
let stdin: streams::InputStream = stdin::get_stdin();
let stdin_pollable = stdin.subscribe();
let ready = poll_list(&[&stdin_pollable]);
let ready = poll::poll_list(&[&stdin_pollable]);
assert_eq!(ready, &[0]);
drop(stdin);
unreachable!(
Expand All @@ -26,76 +26,3 @@ fn main() {
panic!("bad value for args: expected `[\"correct\"]` or `[\"trap\"]`, got {args:?}")
}
}

/// FIXME: This is a copy of the `poll_list` bindings generated with a
/// wit-bindgen with this fix:
/// <https://github.com/bytecodealliance/wit-bindgen/pull/670>
///
/// One that PR is in a published release, delete this code and use the
/// bindings.
///
/// Poll for completion on a set of pollables.
///
/// This function takes a list of pollables, which identify I/O sources of
/// interest, and waits until one or more of the events is ready for I/O.
///
/// The result `list<u32>` contains one or more indices of handles in the
/// argument list that is ready for I/O.
///
/// If the list contains more elements than can be indexed with a `u32`
/// value, this function traps.
///
/// A timeout can be implemented by adding a pollable from the
/// wasi-clocks API to the list.
///
/// This function does not return a `result`; polling in itself does not
/// do any I/O so it doesn't fail. If any of the I/O sources identified by
/// the pollables has an error, it is indicated by marking the source as
/// being reaedy for I/O.
pub fn poll_list(in_: &[&poll::Pollable]) -> wit_bindgen::rt::vec::Vec<u32> {
#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, string::String, vec::Vec};
unsafe {
#[repr(align(4))]
struct RetArea([u8; 8]);
let mut ret_area = ::core::mem::MaybeUninit::<RetArea>::uninit();
let vec0 = in_;
let len0 = vec0.len() as i32;
let layout0 = alloc::Layout::from_size_align_unchecked(vec0.len() * 4, 4);
let result0 = if layout0.size() != 0 {
let ptr = alloc::alloc(layout0);
if ptr.is_null() {
alloc::handle_alloc_error(layout0);
}
ptr
} else {
::core::ptr::null_mut()
};
for (i, e) in vec0.into_iter().enumerate() {
let base = result0 as i32 + (i as i32) * 4;
{
*((base + 0) as *mut i32) = (e).handle() as i32;
}
}
let ptr1 = ret_area.as_mut_ptr() as i32;
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "wasi:io/poll")]
extern "C" {
#[link_name = "poll-list"]
fn wit_import(_: i32, _: i32, _: i32);
}

#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: i32, _: i32) {
unreachable!()
}
wit_import(result0 as i32, len0, ptr1);
let l2 = *((ptr1 + 0) as *const i32);
let l3 = *((ptr1 + 4) as *const i32);
let len4 = l3 as usize;
if layout0.size() != 0 {
alloc::dealloc(result0, layout0);
}
Vec::from_raw_parts(l2 as *mut _, len4, len4)
}
}
77 changes: 2 additions & 75 deletions crates/test-programs/reactor-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Guest for T {
for s in STATE.iter() {
let mut out = s.as_bytes();
while !out.is_empty() {
crate::poll_list(&[&pollable]);
poll::poll_list(&[&pollable]);
let n = match o.check_write() {
Ok(n) => n,
Err(_) => return Err(()),
Expand All @@ -53,7 +53,7 @@ impl Guest for T {
Err(_) => return Err(()),
}

crate::poll_list(&[&pollable]);
poll::poll_list(&[&pollable]);
match o.check_write() {
Ok(_) => {}
Err(_) => return Err(()),
Expand All @@ -66,76 +66,3 @@ impl Guest for T {
format!("{stat:?}")
}
}

/// FIXME: This is a copy of the `poll_list` bindings generated with a
/// wit-bindgen with this fix:
/// <https://github.com/bytecodealliance/wit-bindgen/pull/670>
///
/// One that PR is in a published release, delete this code and use the
/// bindings.
///
/// Poll for completion on a set of pollables.
///
/// This function takes a list of pollables, which identify I/O sources of
/// interest, and waits until one or more of the events is ready for I/O.
///
/// The result `list<u32>` contains one or more indices of handles in the
/// argument list that is ready for I/O.
///
/// If the list contains more elements than can be indexed with a `u32`
/// value, this function traps.
///
/// A timeout can be implemented by adding a pollable from the
/// wasi-clocks API to the list.
///
/// This function does not return a `result`; polling in itself does not
/// do any I/O so it doesn't fail. If any of the I/O sources identified by
/// the pollables has an error, it is indicated by marking the source as
/// being reaedy for I/O.
pub fn poll_list(in_: &[&poll::Pollable]) -> wit_bindgen::rt::vec::Vec<u32> {
#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, string::String, vec::Vec};
unsafe {
#[repr(align(4))]
struct RetArea([u8; 8]);
let mut ret_area = ::core::mem::MaybeUninit::<RetArea>::uninit();
let vec0 = in_;
let len0 = vec0.len() as i32;
let layout0 = alloc::Layout::from_size_align_unchecked(vec0.len() * 4, 4);
let result0 = if layout0.size() != 0 {
let ptr = alloc::alloc(layout0);
if ptr.is_null() {
alloc::handle_alloc_error(layout0);
}
ptr
} else {
::core::ptr::null_mut()
};
for (i, e) in vec0.into_iter().enumerate() {
let base = result0 as i32 + (i as i32) * 4;
{
*((base + 0) as *mut i32) = (e).handle() as i32;
}
}
let ptr1 = ret_area.as_mut_ptr() as i32;
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "wasi:io/poll")]
extern "C" {
#[link_name = "poll-list"]
fn wit_import(_: i32, _: i32, _: i32);
}

#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: i32, _: i32) {
unreachable!()
}
wit_import(result0 as i32, len0, ptr1);
let l2 = *((ptr1 + 0) as *const i32);
let l3 = *((ptr1 + 4) as *const i32);
let len4 = l3 as usize;
if layout0.size() != 0 {
alloc::dealloc(result0, layout0);
}
Vec::from_raw_parts(l2 as *mut _, len4, len4)
}
}
36 changes: 16 additions & 20 deletions crates/test-programs/wasi-http-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ pub async fn request(
.map_err(|_| anyhow!("outgoing request write failed"))?;

if let Some(mut buf) = body {
let request_body = http_types::outgoing_body_write(outgoing_body)
let request_body = outgoing_body
.write()
.map_err(|_| anyhow!("outgoing request write failed"))?;

let pollable = streams::subscribe_to_output_stream(request_body);
let pollable = request_body.subscribe();
while !buf.is_empty() {
test_programs::poll_list(&[pollable]);
poll::poll_list(&[&pollable]);

let permit = match streams::check_write(request_body) {
let permit = match request_body.check_write() {
Ok(n) => n,
Err(_) => anyhow::bail!("output stream error"),
};
Expand All @@ -92,39 +93,37 @@ pub async fn request(
let (chunk, rest) = buf.split_at(len);
buf = rest;

match streams::write(request_body, chunk) {
match request_body.write(chunk) {
Err(_) => anyhow::bail!("output stream error"),
_ => {}
}
}

match streams::flush(request_body) {
match request_body.flush() {
Err(_) => anyhow::bail!("output stream error"),
_ => {}
}

test_programs::poll_oneoff(&[pollable]);
poll::poll_list(&[&pollable]);

match streams::check_write(request_body) {
match request_body.check_write() {
Ok(_) => {}
Err(_) => anyhow::bail!("output stream error"),
};

streams::drop_output_stream(request_body);
}

let future_response = outgoing_handler::handle(request, None)?;

// TODO: The current implementation requires this drop after the request is sent.
// The ownership semantics are unclear in wasi-http we should clarify exactly what is
// supposed to happen here.
http_types::drop_outgoing_body(outgoing_body);
drop(outgoing_body);

let incoming_response = match http_types::future_incoming_response_get(future_response) {
Some(result) => result.map_err(|_| anyhow!("incoming response errored"))?,
None => {
let pollable = http_types::listen_to_future_incoming_response(future_response);
let _ = test_programs::poll_oneoff(&[pollable]);
let _ = poll::poll_list(&[&pollable]);
http_types::future_incoming_response_get(future_response)
.expect("incoming response available")
.map_err(|_| anyhow!("incoming response errored"))?
Expand All @@ -147,15 +146,16 @@ pub async fn request(

http_types::drop_incoming_response(incoming_response);

let input_stream = http_types::incoming_body_stream(incoming_body).unwrap();
let input_stream_pollable = streams::subscribe_to_input_stream(input_stream);
let input_stream = incoming_body.stream().unwrap();
let input_stream_pollable = input_stream.subscribe();

let mut body = Vec::new();
let mut eof = streams::StreamStatus::Open;
while eof != streams::StreamStatus::Ended {
poll::poll_oneoff(&[input_stream_pollable]);
poll::poll_list(&[&input_stream_pollable]);

let (mut body_chunk, stream_status) = streams::read(input_stream, 1024 * 1024)
let (mut body_chunk, stream_status) = input_stream
.read(1024 * 1024)
.map_err(|_| anyhow!("input_stream read failed"))?;

eof = stream_status;
Expand All @@ -165,10 +165,6 @@ pub async fn request(
}
}

poll::drop_pollable(input_stream_pollable);
streams::drop_input_stream(input_stream);
http_types::drop_incoming_body(incoming_body);

Ok(Response {
status,
headers,
Expand Down
75 changes: 1 addition & 74 deletions crates/test-programs/wasi-sockets-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn write(output: &streams::OutputStream, mut bytes: &[u8]) -> (usize, stream
let pollable = output.subscribe();

while !bytes.is_empty() {
crate::poll_list(&[&pollable]);
poll::poll_list(&[&pollable]);

let permit = match output.check_write() {
Ok(n) => n,
Expand Down Expand Up @@ -116,76 +116,3 @@ pub fn example_body(net: tcp::Network, sock: tcp::TcpSocket, family: network::Ip
// Check that we sent and recieved our message!
assert_eq!(data, second_message); // Not guaranteed to work but should work in practice.
}

/// FIXME: This is a copy of the `poll_list` bindings generated with a
/// wit-bindgen with this fix:
/// <https://github.com/bytecodealliance/wit-bindgen/pull/670>
///
/// One that PR is in a published release, delete this code and use the
/// bindings.
///
/// Poll for completion on a set of pollables.
///
/// This function takes a list of pollables, which identify I/O sources of
/// interest, and waits until one or more of the events is ready for I/O.
///
/// The result `list<u32>` contains one or more indices of handles in the
/// argument list that is ready for I/O.
///
/// If the list contains more elements than can be indexed with a `u32`
/// value, this function traps.
///
/// A timeout can be implemented by adding a pollable from the
/// wasi-clocks API to the list.
///
/// This function does not return a `result`; polling in itself does not
/// do any I/O so it doesn't fail. If any of the I/O sources identified by
/// the pollables has an error, it is indicated by marking the source as
/// being reaedy for I/O.
pub fn poll_list(in_: &[&poll::Pollable]) -> wit_bindgen::rt::vec::Vec<u32> {
#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, string::String, vec::Vec};
unsafe {
#[repr(align(4))]
struct RetArea([u8; 8]);
let mut ret_area = ::core::mem::MaybeUninit::<RetArea>::uninit();
let vec0 = in_;
let len0 = vec0.len() as i32;
let layout0 = alloc::Layout::from_size_align_unchecked(vec0.len() * 4, 4);
let result0 = if layout0.size() != 0 {
let ptr = alloc::alloc(layout0);
if ptr.is_null() {
alloc::handle_alloc_error(layout0);
}
ptr
} else {
::core::ptr::null_mut()
};
for (i, e) in vec0.into_iter().enumerate() {
let base = result0 as i32 + (i as i32) * 4;
{
*((base + 0) as *mut i32) = (e).handle() as i32;
}
}
let ptr1 = ret_area.as_mut_ptr() as i32;
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "wasi:io/poll")]
extern "C" {
#[link_name = "poll-list"]
fn wit_import(_: i32, _: i32, _: i32);
}

#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: i32, _: i32) {
unreachable!()
}
wit_import(result0 as i32, len0, ptr1);
let l2 = *((ptr1 + 0) as *const i32);
let l3 = *((ptr1 + 4) as *const i32);
let len4 = l3 as usize;
if layout0.size() != 0 {
alloc::dealloc(result0, layout0);
}
Vec::from_raw_parts(l2 as *mut _, len4, len4)
}
}
Loading

0 comments on commit 129db82

Please sign in to comment.