Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 6f0b2e8

Browse files
authored
sketch of poll_oneoff polyfill (#11)
This adds a `poll_oneoff` implementation to src/lib.rs. It's completely untested. I was planning on adding a host implementation and using that to test end-to-end, but that raised some tough questions about how much of the existing `wasi-common` scheduler(s) should be reused. I've decided to focus on other, more widely-used parts of WASI first, but I wanted to share the work I've already done here. Note that I've moved the clock- and socket-specific functions out of `wasi-poll` and into `wasi-clocks` and `wasi-tcp`, respectively. The latter is a new interface which will eventually contain functions and types resembling @npmccallum's https://github.com/npmccallum/wasi-snapshot-preview2#wasi-tcp proposal. Per discussion with @sunfishcode: - `wasi-tcp` includes an `error` enum type, intended to represent only socket-related errors. - It also includes a `socket` pseudo-handle type, distinct from `wasi-filesystem`'s `descriptor` type. These fine-grained types help move us away from the "everything is a file descriptor" and "all errors are errnos" approaches of Preview 1. If we decide `poll-oneoff` should be usable with files as well as sockets, we can add `subscribe-read` and `subscribe-write` functions to `wasi-filesystem` which accept file `descriptor`s. Likewise for any other pseudo-handle type from which we'd like to create futures. Signed-off-by: Joel Dice <joel.dice@fermyon.com> Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 2c96f4d commit 6f0b2e8

File tree

6 files changed

+340
-100
lines changed

6 files changed

+340
-100
lines changed

host/src/clocks.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ impl wasi_default_clocks::WasiDefaultClocks for WasiCtx {
6666
}
6767

6868
impl wasi_clocks::WasiClocks for WasiCtx {
69+
fn subscribe_wall_clock(
70+
&mut self,
71+
when: wasi_clocks::Datetime,
72+
absolute: bool,
73+
) -> anyhow::Result<wasi_clocks::WasiFuture> {
74+
drop((when, absolute));
75+
todo!()
76+
}
77+
78+
fn subscribe_monotonic_clock(
79+
&mut self,
80+
when: wasi_clocks::Instant,
81+
absolute: bool,
82+
) -> anyhow::Result<wasi_clocks::WasiFuture> {
83+
drop((when, absolute));
84+
todo!()
85+
}
86+
6987
fn monotonic_clock_now(
7088
&mut self,
7189
fd: wasi_clocks::MonotonicClock,

host/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod logging;
44
mod poll;
55
mod random;
66
mod table;
7+
mod tcp;
78
pub use table::Table;
89

910
wit_bindgen_host_wasmtime_rust::generate!({
@@ -21,6 +22,7 @@ pub fn add_to_linker<T>(
2122
wasi_logging::add_to_linker(l, f)?;
2223
wasi_poll::add_to_linker(l, f)?;
2324
wasi_random::add_to_linker(l, f)?;
25+
wasi_tcp::add_to_linker(l, f)?;
2426
Ok(())
2527
}
2628

host/src/poll.rs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,17 @@
11
#![allow(unused_variables)]
22

3-
use crate::{wasi_poll, WasiCtx};
3+
use crate::{
4+
wasi_poll::{WasiFuture, WasiPoll},
5+
WasiCtx,
6+
};
7+
use anyhow::Result;
48

5-
impl wasi_poll::WasiPoll for WasiCtx {
6-
fn drop_future(&mut self, future: wasi_poll::WasiFuture) -> anyhow::Result<()> {
9+
impl WasiPoll for WasiCtx {
10+
fn drop_future(&mut self, future: WasiFuture) -> Result<()> {
711
todo!()
812
}
913

10-
fn bytes_readable(
11-
&mut self,
12-
fd: wasi_poll::Descriptor,
13-
) -> anyhow::Result<wasi_poll::BytesResult> {
14-
todo!()
15-
}
16-
17-
fn bytes_writable(
18-
&mut self,
19-
fd: wasi_poll::Descriptor,
20-
) -> anyhow::Result<wasi_poll::BytesResult> {
21-
todo!()
22-
}
23-
24-
fn subscribe_read(
25-
&mut self,
26-
fd: wasi_poll::Descriptor,
27-
) -> anyhow::Result<wasi_poll::WasiFuture> {
28-
todo!()
29-
}
30-
31-
fn subscribe_write(
32-
&mut self,
33-
fd: wasi_poll::Descriptor,
34-
) -> anyhow::Result<wasi_poll::WasiFuture> {
35-
todo!()
36-
}
37-
38-
fn subscribe_wall_clock(
39-
&mut self,
40-
when: wasi_poll::Datetime,
41-
absolute: bool,
42-
) -> anyhow::Result<wasi_poll::WasiFuture> {
43-
todo!()
44-
}
45-
46-
fn subscribe_monotonic_clock(
47-
&mut self,
48-
when: wasi_poll::Instant,
49-
absolute: bool,
50-
) -> anyhow::Result<wasi_poll::WasiFuture> {
51-
todo!()
52-
}
53-
54-
fn poll_oneoff(&mut self, futures: Vec<wasi_poll::WasiFuture>) -> anyhow::Result<Vec<bool>> {
14+
fn poll_oneoff(&mut self, futures: Vec<WasiFuture>) -> Result<Vec<u8>> {
5515
todo!()
5616
}
5717
}

host/src/tcp.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::{
2+
wasi_tcp::{self, BytesResult, Socket, WasiFuture, WasiTcp},
3+
WasiCtx,
4+
};
5+
use anyhow::Result;
6+
use wit_bindgen_host_wasmtime_rust::Error;
7+
8+
impl WasiTcp for WasiCtx {
9+
fn bytes_readable(&mut self, socket: Socket) -> Result<BytesResult, Error<wasi_tcp::Error>> {
10+
drop(socket);
11+
todo!()
12+
}
13+
14+
fn bytes_writable(&mut self, socket: Socket) -> Result<BytesResult, Error<wasi_tcp::Error>> {
15+
drop(socket);
16+
todo!()
17+
}
18+
19+
fn subscribe_read(&mut self, socket: Socket) -> Result<WasiFuture> {
20+
drop(socket);
21+
todo!()
22+
}
23+
24+
fn subscribe_write(&mut self, socket: Socket) -> Result<WasiFuture> {
25+
drop(socket);
26+
todo!()
27+
}
28+
}

0 commit comments

Comments
 (0)