Skip to content

Commit

Permalink
Rebase on the new wasi-sockets. (bytecodealliance#91)
Browse files Browse the repository at this point in the history
* Rebase on the new wasi-sockets.

This switches to using the wasi-sockets wit files from
WebAssembly/wasi-sockets#16. Many things are still stubbed out with
`todo!()` for now.

* Fix compilation on Windows.
  • Loading branch information
sunfishcode authored Feb 25, 2023
1 parent 107b07d commit a8295ae
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(unused_variables)] // TODO: remove this when more things are implemented

use crate::bindings::{
wasi_default_clocks, wasi_exit, wasi_filesystem, wasi_io, wasi_monotonic_clock, wasi_poll,
wasi_random, wasi_stderr, wasi_tcp, wasi_wall_clock,
wasi_default_clocks, wasi_exit, wasi_filesystem, wasi_io, wasi_monotonic_clock, wasi_network,
wasi_poll, wasi_random, wasi_stderr, wasi_tcp, wasi_wall_clock,
};
use core::arch::wasm32;
use core::cell::{Cell, RefCell, UnsafeCell};
Expand Down Expand Up @@ -1576,11 +1576,12 @@ impl Drop for Pollables {
}
}

impl From<wasi_tcp::Errno> for Errno {
fn from(error: wasi_tcp::Errno) -> Errno {
use wasi_tcp::Errno::*;

impl From<wasi_network::Error> for Errno {
fn from(error: wasi_network::Error) -> Errno {
match error {
wasi_network::Error::Unknown => unreachable!(), // TODO
wasi_network::Error::Again => ERRNO_AGAIN,
/* TODO
// Use a black box to prevent the optimizer from generating a
// lookup table, which would require a static initializer.
ConnectionAborted => black_box(ERRNO_CONNABORTED),
Expand All @@ -1591,6 +1592,7 @@ impl From<wasi_tcp::Errno> for Errno {
NetworkUnreachable => ERRNO_NETUNREACH,
Timedout => ERRNO_TIMEDOUT,
_ => unreachable!(),
*/
}
}
}
Expand Down Expand Up @@ -1802,22 +1804,25 @@ pub unsafe extern "C" fn poll_oneoff(
}
}
StreamType::Socket(connection) => {
match wasi_tcp::bytes_readable(*connection) {
Ok(result) => {
error = ERRNO_SUCCESS;
nbytes = result.0;
flags = if result.1 {
EVENTRWFLAGS_FD_READWRITE_HANGUP
} else {
0
};
}
Err(e) => {
error = e.into();
nbytes = 0;
flags = 0;
}
}
unreachable!() // TODO
/*
match wasi_tcp::bytes_readable(*connection) {
Ok(result) => {
error = ERRNO_SUCCESS;
nbytes = result.0;
flags = if result.1 {
EVENTRWFLAGS_FD_READWRITE_HANGUP
} else {
0
};
}
Err(e) => {
error = e.into();
nbytes = 0;
flags = 0;
}
}
*/
}
StreamType::EmptyStdin => {
error = ERRNO_SUCCESS;
Expand Down Expand Up @@ -1846,22 +1851,25 @@ pub unsafe extern "C" fn poll_oneoff(
flags = 0;
}
StreamType::Socket(connection) => {
match wasi_tcp::bytes_writable(connection) {
Ok(result) => {
error = ERRNO_SUCCESS;
nbytes = result.0;
flags = if result.1 {
EVENTRWFLAGS_FD_READWRITE_HANGUP
} else {
0
};
}
Err(e) => {
error = e.into();
nbytes = 0;
flags = 0;
}
}
unreachable!() // TODO
/*
match wasi_tcp::bytes_writable(connection) {
Ok(result) => {
error = ERRNO_SUCCESS;
nbytes = result.0;
flags = if result.1 {
EVENTRWFLAGS_FD_READWRITE_HANGUP
} else {
0
};
}
Err(e) => {
error = e.into();
nbytes = 0;
flags = 0;
}
}
*/
}
StreamType::EmptyStdin => {
error = ERRNO_BADF;
Expand Down Expand Up @@ -2193,7 +2201,7 @@ enum StreamType {
File(File),

/// Streaming data with a socket connection.
Socket(wasi_tcp::Connection),
Socket(wasi_tcp::TcpSocket),
}

impl Drop for Descriptor {
Expand Down Expand Up @@ -2574,7 +2582,7 @@ impl State {
}

#[allow(dead_code)] // until Socket is implemented
fn get_socket(&self, fd: Fd) -> Result<wasi_tcp::Connection, Errno> {
fn get_socket(&self, fd: Fd) -> Result<wasi_tcp::TcpSocket, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
type_: StreamType::Socket(socket),
Expand Down

0 comments on commit a8295ae

Please sign in to comment.