Skip to content

Commit 3da2e24

Browse files
committed
Start to port Wasmtime to the new wasi-io API with resources.
This imports the new APIs posted in WebAssembly/wasi-io#46.
1 parent 40c1f29 commit 3da2e24

File tree

18 files changed

+292
-311
lines changed

18 files changed

+292
-311
lines changed

crates/wasi/src/preview2/command.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ wasmtime::component::bindgen!({
1313
"wasi:filesystem/preopens": crate::preview2::bindings::filesystem::preopens,
1414
"wasi:sockets/tcp": crate::preview2::bindings::sockets::tcp,
1515
"wasi:clocks/monotonic_clock": crate::preview2::bindings::clocks::monotonic_clock,
16-
"wasi:poll/poll": crate::preview2::bindings::poll::poll,
16+
"wasi:io/poll": crate::preview2::bindings::io::poll,
1717
"wasi:io/streams": crate::preview2::bindings::io::streams,
1818
"wasi:clocks/timezone": crate::preview2::bindings::clocks::timezone,
1919
"wasi:clocks/wall_clock": crate::preview2::bindings::clocks::wall_clock,
@@ -37,7 +37,7 @@ pub fn add_to_linker<T: WasiView>(l: &mut wasmtime::component::Linker<T>) -> any
3737
crate::preview2::bindings::clocks::timezone::add_to_linker(l, |t| t)?;
3838
crate::preview2::bindings::filesystem::types::add_to_linker(l, |t| t)?;
3939
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
40-
crate::preview2::bindings::poll::poll::add_to_linker(l, |t| t)?;
40+
crate::preview2::bindings::io::poll::add_to_linker(l, |t| t)?;
4141
crate::preview2::bindings::io::streams::add_to_linker(l, |t| t)?;
4242
crate::preview2::bindings::random::random::add_to_linker(l, |t| t)?;
4343
crate::preview2::bindings::cli::exit::add_to_linker(l, |t| t)?;
@@ -73,7 +73,7 @@ pub mod sync {
7373
"wasi:filesystem/preopens": crate::preview2::bindings::filesystem::preopens,
7474
"wasi:sockets/tcp": crate::preview2::bindings::sockets::tcp,
7575
"wasi:clocks/monotonic_clock": crate::preview2::bindings::clocks::monotonic_clock,
76-
"wasi:poll/poll": crate::preview2::bindings::sync_io::poll::poll,
76+
"wasi:io/poll": crate::preview2::bindings::sync_io::io::poll,
7777
"wasi:io/streams": crate::preview2::bindings::sync_io::io::streams,
7878
"wasi:clocks/timezone": crate::preview2::bindings::clocks::timezone,
7979
"wasi:clocks/wall_clock": crate::preview2::bindings::clocks::wall_clock,
@@ -99,7 +99,7 @@ pub mod sync {
9999
crate::preview2::bindings::clocks::timezone::add_to_linker(l, |t| t)?;
100100
crate::preview2::bindings::sync_io::filesystem::types::add_to_linker(l, |t| t)?;
101101
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
102-
crate::preview2::bindings::sync_io::poll::poll::add_to_linker(l, |t| t)?;
102+
crate::preview2::bindings::sync_io::io::poll::add_to_linker(l, |t| t)?;
103103
crate::preview2::bindings::sync_io::io::streams::add_to_linker(l, |t| t)?;
104104
crate::preview2::bindings::random::random::add_to_linker(l, |t| t)?;
105105
crate::preview2::bindings::cli::exit::add_to_linker(l, |t| t)?;

crates/wasi/src/preview2/host/clocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::preview2::bindings::{
44
clocks::monotonic_clock::{self, Instant},
55
clocks::timezone::{self, Timezone, TimezoneDisplay},
66
clocks::wall_clock::{self, Datetime},
7-
poll::poll::Pollable,
7+
io::poll::Pollable,
88
};
99
use crate::preview2::{HostPollable, TablePollableExt, WasiView};
1010
use cap_std::time::SystemTime;

crates/wasi/src/preview2/host/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::preview2::{
2+
bindings::io::poll::Pollable,
23
bindings::io::streams::{self, InputStream, OutputStream},
3-
bindings::poll::poll::Pollable,
44
filesystem::FileInputStream,
55
poll::PollableFuture,
66
stream::{
@@ -400,8 +400,8 @@ impl<T: WasiView> streams::Host for T {
400400
pub mod sync {
401401
use crate::preview2::{
402402
bindings::io::streams::{self as async_streams, Host as AsyncHost},
403+
bindings::sync_io::io::poll::Pollable,
403404
bindings::sync_io::io::streams::{self, InputStream, OutputStream},
404-
bindings::sync_io::poll::poll::Pollable,
405405
in_tokio, WasiView,
406406
};
407407

crates/wasi/src/preview2/host/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::preview2::bindings::{
2+
io::poll::Pollable,
23
io::streams::{InputStream, OutputStream},
3-
poll::poll::Pollable,
44
sockets::network::{self, ErrorCode, IpAddressFamily, IpSocketAddress, Network},
55
sockets::tcp::{self, ShutdownType},
66
};

crates/wasi/src/preview2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub mod bindings {
7171
}
7272
});
7373
}
74-
pub use self::_internal::wasi::{filesystem, io, poll};
74+
pub use self::_internal::wasi::{filesystem, io};
7575
}
7676

7777
wasmtime::component::bindgen!({

crates/wasi/src/preview2/poll.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::preview2::{
2-
bindings::poll::poll::{self, Pollable},
2+
bindings::io::poll::{self, Pollable},
33
Table, TableError, WasiView,
44
};
55
use anyhow::Result;
@@ -13,12 +13,12 @@ pub type PollableFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + '
1313
pub type MakeFuture = for<'a> fn(&'a mut dyn Any) -> PollableFuture<'a>;
1414
pub type ClosureFuture = Box<dyn Fn() -> PollableFuture<'static> + Send + Sync + 'static>;
1515

16-
/// A host representation of the `wasi:poll/poll.pollable` resource.
16+
/// A host representation of the `wasi:io/poll.pollable` resource.
1717
///
1818
/// A pollable is not the same thing as a Rust Future: the same pollable may be used to
1919
/// repeatedly check for readiness of a given condition, e.g. if a stream is readable
2020
/// or writable. So, rather than containing a Future, which can only become Ready once, a
21-
/// HostPollable contains a way to create a Future in each call to poll_oneoff.
21+
/// HostPollable contains a way to create a Future in each call to `poll_list`.
2222
pub enum HostPollable {
2323
/// Create a Future by calling a fn on another resource in the table. This
2424
/// indirection means the created Future can use a mut borrow of another
@@ -58,7 +58,7 @@ impl<T: WasiView> poll::Host for T {
5858
Ok(())
5959
}
6060

61-
async fn poll_oneoff(&mut self, pollables: Vec<Pollable>) -> Result<Vec<bool>> {
61+
async fn poll_list(&mut self, pollables: Vec<Pollable>) -> Result<Vec<bool>> {
6262
type ReadylistIndex = usize;
6363

6464
let table = self.table_mut();
@@ -107,7 +107,7 @@ impl<T: WasiView> poll::Host for T {
107107
}
108108
Poll::Ready(Err(e)) => {
109109
return Poll::Ready(Err(
110-
e.context(format!("poll_oneoff {readylist_indicies:?}"))
110+
e.context(format!("poll_list {readylist_indicies:?}"))
111111
));
112112
}
113113
Poll::Pending => {}
@@ -130,8 +130,8 @@ impl<T: WasiView> poll::Host for T {
130130

131131
pub mod sync {
132132
use crate::preview2::{
133-
bindings::poll::poll::Host as AsyncHost,
134-
bindings::sync_io::poll::poll::{self, Pollable},
133+
bindings::io::poll::Host as AsyncHost,
134+
bindings::sync_io::io::poll::{self, Pollable},
135135
in_tokio, WasiView,
136136
};
137137
use anyhow::Result;
@@ -141,8 +141,8 @@ pub mod sync {
141141
AsyncHost::drop_pollable(self, pollable)
142142
}
143143

144-
fn poll_oneoff(&mut self, pollables: Vec<Pollable>) -> Result<Vec<bool>> {
145-
in_tokio(async { AsyncHost::poll_oneoff(self, pollables).await })
144+
fn poll_list(&mut self, pollables: Vec<Pollable>) -> Result<Vec<bool>> {
145+
in_tokio(async { AsyncHost::poll_list(self, pollables).await })
146146
}
147147
}
148148
}

crates/wasi/src/preview2/preview1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::preview2::bindings::cli::{
44
};
55
use crate::preview2::bindings::clocks::{monotonic_clock, wall_clock};
66
use crate::preview2::bindings::filesystem::{preopens, types as filesystem};
7+
use crate::preview2::bindings::io::poll;
78
use crate::preview2::bindings::io::streams;
8-
use crate::preview2::bindings::poll;
99
use crate::preview2::filesystem::TableFsExt;
1010
use crate::preview2::host::filesystem::TableReaddirExt;
1111
use crate::preview2::{bindings, IsATTY, TableError, WasiView};
@@ -69,7 +69,7 @@ impl BlockingMode {
6969
}
7070
async fn write(
7171
&self,
72-
host: &mut (impl streams::Host + poll::poll::Host),
72+
host: &mut (impl streams::Host + poll::Host),
7373
output_stream: streams::OutputStream,
7474
mut bytes: &[u8],
7575
) -> Result<usize, types::Error> {
@@ -849,7 +849,7 @@ impl<
849849
+ bindings::cli::exit::Host
850850
+ bindings::filesystem::preopens::Host
851851
+ bindings::filesystem::types::Host
852-
+ bindings::poll::poll::Host
852+
+ bindings::io::poll::Host
853853
+ bindings::random::random::Host
854854
+ bindings::io::streams::Host
855855
+ bindings::clocks::monotonic_clock::Host

crates/wasi/wit/command-extended.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ world command-extended {
1616
import wasi:random/random
1717
import wasi:random/insecure
1818
import wasi:random/insecure-seed
19-
import wasi:poll/poll
19+
import wasi:io/poll
2020
import wasi:io/streams
2121
import wasi:cli/environment
2222
import wasi:cli/exit

crates/wasi/wit/deps/clocks/monotonic-clock.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package wasi:clocks
1111
///
1212
/// It is intended for measuring elapsed time.
1313
interface monotonic-clock {
14-
use wasi:poll/poll.{pollable}
14+
use wasi:io/poll.{pollable}
1515

1616
/// A timestamp in nanoseconds.
1717
type instant = u64

crates/wasi/wit/deps/http/types.wit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// imported and exported interfaces.
44
interface types {
55
use wasi:io/streams.{input-stream, output-stream}
6-
use wasi:poll/poll.{pollable}
6+
use wasi:io/poll.{pollable}
77

88
// This type corresponds to HTTP standard Methods.
99
variant method {
@@ -95,7 +95,7 @@ interface types {
9595
// Additional optional parameters that can be set when making a request.
9696
record request-options {
9797
// The following timeouts are specific to the HTTP protocol and work
98-
// independently of the overall timeouts passed to `io.poll.poll-oneoff`.
98+
// independently of the overall timeouts passed to `io.poll.poll-list`.
9999

100100
// The timeout for the initial connect.
101101
connect-timeout-ms: option<u32>,
@@ -147,7 +147,7 @@ interface types {
147147
// `future-incoming-response`, the client can call the non-blocking `get`
148148
// method to get the result if it is available. If the result is not available,
149149
// the client can call `listen` to get a `pollable` that can be passed to
150-
// `io.poll.poll-oneoff`.
150+
// `wasi:io/poll.poll-list`.
151151
type future-incoming-response = u32
152152
drop-future-incoming-response: func(f: future-incoming-response)
153153
future-incoming-response-get: func(f: future-incoming-response) -> option<result<incoming-response, error>>

crates/wasi/wit/deps/io/poll.wit

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package wasi:io
2+
3+
/// A poll API intended to let users wait for I/O events on multiple handles
4+
/// at once.
5+
interface poll {
6+
/// A "pollable" handle.
7+
resource pollable
8+
9+
/// Poll for completion on a set of pollables.
10+
///
11+
/// This function takes a list of pollables, which identify I/O sources of
12+
/// interest, and waits until one or more of the events is ready for I/O.
13+
///
14+
/// The result `list<u32>` contains one or more indices of handles in the
15+
/// argument list that is ready for I/O.
16+
///
17+
/// If the list contains more elements than can be indexed with a `u32`
18+
/// value, this function traps.
19+
///
20+
/// A timeout can be implemented by adding a pollable from the
21+
/// wasi-clocks API to the list.
22+
///
23+
/// This function does not return a `result`; polling in itself does not
24+
/// do any I/O so it doesn't fail. If any of the I/O sources identified by
25+
/// the pollables has an error, it is indicated by marking the source as
26+
/// being reaedy for I/O.
27+
poll-list: func(in: list<pollable>) -> list<u32>
28+
29+
/// Poll for completion on a single pollable.
30+
///
31+
/// This function is similar to `poll-list`, but operates on only a single
32+
/// pollable. When it returns, the handle is ready for I/O.
33+
poll-one: func(in: pollable)
34+
}

0 commit comments

Comments
 (0)