Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move wasmtime-wasi's Table to wasmtime::component::ResourceTable #7655

Merged
merged 10 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/wasi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! can be provided with the [`Error::context`] method. This context is only observable with the
//! `Display` and `Debug` impls of the error.

pub use crate::snapshots::preview_1::error::{Errno, Error, ErrorExt};
pub use crate::snapshots::preview_1::error::{Error, ErrorExt};
use std::fmt;

/// An error returned from the `proc_exit` host syscall.
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::time::timeout;
use wasmtime::component::Resource;
use wasmtime_wasi::preview2::{self, AbortOnDropJoinHandle, Subscribe, Table};
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::preview2::{self, AbortOnDropJoinHandle, Subscribe};

/// Capture the state necessary for use in the wasi-http API implementation.
pub struct WasiHttpCtx;
Expand All @@ -31,7 +31,7 @@ pub struct OutgoingRequest {

pub trait WasiHttpView: Send {
fn ctx(&mut self) -> &mut WasiHttpCtx;
fn table(&mut self) -> &mut Table;
fn table(&mut self) -> &mut ResourceTable;

fn new_incoming_request(
&mut self,
Expand Down
10 changes: 5 additions & 5 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::{
use anyhow::Context;
use std::any::Any;
use std::str::FromStr;
use wasmtime::component::Resource;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::preview2::{
bindings::io::streams::{InputStream, OutputStream},
Pollable, Table,
Pollable,
};

impl<T: WasiHttpView> crate::bindings::http::types::Host for T {
Expand Down Expand Up @@ -49,7 +49,7 @@ fn get_content_length(fields: &FieldMap) -> Result<Option<u64>, ()> {

/// Take ownership of the underlying [`FieldMap`] associated with this fields resource. If the
/// fields resource references another fields, the returned [`FieldMap`] will be cloned.
fn move_fields(table: &mut Table, id: Resource<HostFields>) -> wasmtime::Result<FieldMap> {
fn move_fields(table: &mut ResourceTable, id: Resource<HostFields>) -> wasmtime::Result<FieldMap> {
match table.delete(id)? {
HostFields::Ref { parent, get_fields } => {
let entry = table.get_any_mut(parent)?;
Expand All @@ -61,7 +61,7 @@ fn move_fields(table: &mut Table, id: Resource<HostFields>) -> wasmtime::Result<
}

fn get_fields<'a>(
table: &'a mut Table,
table: &'a mut ResourceTable,
id: &Resource<HostFields>,
) -> wasmtime::Result<&'a FieldMap> {
let fields = table.get(&id)?;
Expand All @@ -80,7 +80,7 @@ fn get_fields<'a>(
}

fn get_fields_mut<'a>(
table: &'a mut Table,
table: &'a mut ResourceTable,
id: &Resource<HostFields>,
) -> wasmtime::Result<Result<&'a mut FieldMap, types::HeaderError>> {
match table.get_mut(&id)? {
Expand Down
18 changes: 8 additions & 10 deletions crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ use sha2::{Digest, Sha256};
use std::{collections::HashMap, iter, net::Ipv4Addr, str, sync::Arc};
use tokio::task;
use wasmtime::{
component::{Component, Linker, Resource},
component::{Component, Linker, Resource, ResourceTable},
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{
self, pipe::MemoryOutputPipe, Table, WasiCtx, WasiCtxBuilder, WasiView,
};
use wasmtime_wasi::preview2::{self, pipe::MemoryOutputPipe, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{
bindings::http::types::ErrorCode,
body::HyperIncomingBody,
Expand All @@ -31,7 +29,7 @@ type RequestSender = Arc<
>;

struct Ctx {
table: Table,
table: ResourceTable,
wasi: WasiCtx,
http: WasiHttpCtx,
stdout: MemoryOutputPipe,
Expand All @@ -40,10 +38,10 @@ struct Ctx {
}

impl WasiView for Ctx {
fn table(&self) -> &Table {
fn table(&self) -> &ResourceTable {
&self.table
}
fn table_mut(&mut self) -> &mut Table {
fn table_mut(&mut self) -> &mut ResourceTable {
&mut self.table
}
fn ctx(&self) -> &WasiCtx {
Expand All @@ -59,7 +57,7 @@ impl WasiHttpView for Ctx {
&mut self.http
}

fn table(&mut self) -> &mut Table {
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}

Expand Down Expand Up @@ -89,7 +87,7 @@ fn store(engine: &Engine, server: &Server) -> Store<Ctx> {
builder.stderr(stderr.clone());
builder.env("HTTP_SERVER", server.addr().to_string());
let ctx = Ctx {
table: Table::new(),
table: ResourceTable::new(),
wasi: builder.build(),
http: WasiHttpCtx {},
stderr,
Expand Down Expand Up @@ -132,7 +130,7 @@ async fn run_wasi_http(
) -> anyhow::Result<Result<hyper::Response<Collected<Bytes>>, ErrorCode>> {
let stdout = MemoryOutputPipe::new(4096);
let stderr = MemoryOutputPipe::new(4096);
let table = Table::new();
let table = ResourceTable::new();

let mut config = Config::new();
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
Expand Down
7 changes: 4 additions & 3 deletions crates/wasi/src/preview2/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use crate::preview2::{
filesystem::Dir,
pipe, random, stdio,
stdio::{StdinStream, StdoutStream},
DirPerms, FilePerms, Table,
DirPerms, FilePerms,
};
use cap_rand::{Rng, RngCore, SeedableRng};
use cap_std::ipnet::{self, IpNet};
use cap_std::net::Pool;
use cap_std::{ambient_authority, AmbientAuthority};
use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr};
use wasmtime::component::ResourceTable;

pub struct WasiCtxBuilder {
stdin: Box<dyn StdinStream>,
Expand Down Expand Up @@ -302,8 +303,8 @@ impl WasiCtxBuilder {
}

pub trait WasiView: Send {
fn table(&self) -> &Table;
fn table_mut(&mut self) -> &mut Table;
fn table(&self) -> &ResourceTable;
fn table_mut(&mut self) -> &mut ResourceTable;
fn ctx(&self) -> &WasiCtx;
fn ctx_mut(&mut self) -> &mut WasiCtx;
}
Expand Down
7 changes: 3 additions & 4 deletions crates/wasi/src/preview2/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::preview2::bindings::filesystem::types;
use crate::preview2::{
spawn_blocking, AbortOnDropJoinHandle, HostOutputStream, StreamError, Subscribe, TableError,
TrappableError,
spawn_blocking, AbortOnDropJoinHandle, HostOutputStream, StreamError, Subscribe, TrappableError,
};
use anyhow::anyhow;
use bytes::{Bytes, BytesMut};
Expand All @@ -13,8 +12,8 @@ pub type FsResult<T> = Result<T, FsError>;

pub type FsError = TrappableError<types::ErrorCode>;

impl From<TableError> for FsError {
fn from(error: TableError) -> Self {
impl From<wasmtime::component::ResourceTableError> for FsError {
fn from(error: wasmtime::component::ResourceTableError) -> Self {
Self::trap(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/host/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<T: WasiView> wall_clock::Host for T {
}

fn subscribe_to_duration(
table: &mut crate::preview2::Table,
table: &mut wasmtime::component::ResourceTable,
duration: tokio::time::Duration,
) -> anyhow::Result<Resource<Pollable>> {
let sleep = if duration.is_zero() {
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/preview2/host/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::preview2::bindings::filesystem::types::{
use crate::preview2::bindings::io::streams::{InputStream, OutputStream};
use crate::preview2::filesystem::{Descriptor, Dir, File, ReaddirIterator};
use crate::preview2::filesystem::{FileInputStream, FileOutputStream};
use crate::preview2::{DirPerms, FilePerms, FsError, FsResult, Table, WasiView};
use crate::preview2::{DirPerms, FilePerms, FsError, FsResult, WasiView};
use anyhow::Context;
use wasmtime::component::Resource;
use wasmtime::component::{Resource, ResourceTable};

mod sync;

Expand Down Expand Up @@ -840,7 +840,7 @@ impl<T: WasiView> HostDirectoryEntryStream for T {
}

async fn get_descriptor_metadata(
table: &Table,
table: &ResourceTable,
fd: Resource<types::Descriptor>,
) -> FsResult<cap_std::fs::Metadata> {
match table.get(&fd)? {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ mod test {
use super::*;
#[test]
fn table_readdir_works() {
let mut table = Table::new();
let mut table = ResourceTable::new();
let ix = table
.push(ReaddirIterator::new(std::iter::empty()))
.unwrap();
Expand Down
3 changes: 1 addition & 2 deletions crates/wasi/src/preview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub mod preview1;
mod random;
mod stdio;
mod stream;
mod table;
mod tcp;
mod udp;
mod write_stream;
Expand All @@ -54,9 +53,9 @@ pub use self::stdio::{
pub use self::stream::{
HostInputStream, HostOutputStream, InputStream, OutputStream, StreamError, StreamResult,
};
pub use self::table::{Table, TableError};
pub use cap_fs_ext::SystemTimeSpec;
pub use cap_rand::RngCore;
pub use wasmtime::component::{ResourceTable, ResourceTableError};

pub mod bindings {
// Generate traits for synchronous bindings.
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi/src/preview2/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::preview2::bindings::sockets::network::{Ipv4Address, Ipv6Address};
use crate::preview2::bindings::wasi::sockets::network::ErrorCode;
use crate::preview2::{TableError, TrappableError};
use crate::preview2::TrappableError;
use cap_std::net::Pool;

pub struct Network {
Expand All @@ -12,8 +12,8 @@ pub type SocketResult<T> = Result<T, SocketError>;

pub type SocketError = TrappableError<ErrorCode>;

impl From<TableError> for SocketError {
fn from(error: TableError) -> Self {
impl From<wasmtime::component::ResourceTableError> for SocketError {
fn from(error: wasmtime::component::ResourceTableError) -> Self {
Self::trap(error)
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/preview2/poll.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::preview2::{bindings::io::poll, Table, WasiView};
use crate::preview2::{bindings::io::poll, WasiView};
use anyhow::Result;
use std::any::Any;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use wasmtime::component::Resource;
use wasmtime::component::{Resource, ResourceTable};

pub type PollableFuture<'a> = Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
pub type MakeFuture = for<'a> fn(&'a mut dyn Any) -> PollableFuture<'a>;
Expand All @@ -20,7 +20,7 @@ pub type ClosureFuture = Box<dyn Fn() -> PollableFuture<'static> + Send + Sync +
pub struct Pollable {
index: u32,
make_future: MakeFuture,
remove_index_on_delete: Option<fn(&mut Table, u32) -> Result<()>>,
remove_index_on_delete: Option<fn(&mut ResourceTable, u32) -> Result<()>>,
}

#[async_trait::async_trait]
Expand All @@ -35,7 +35,7 @@ pub trait Subscribe: Send + Sync + 'static {
/// resource is deleted. Otherwise the returned resource is considered a "child"
/// of the given `resource` which means that the given resource cannot be
/// deleted while the `pollable` is still alive.
pub fn subscribe<T>(table: &mut Table, resource: Resource<T>) -> Result<Resource<Pollable>>
pub fn subscribe<T>(table: &mut ResourceTable, resource: Resource<T>) -> Result<Resource<Pollable>>
where
T: Subscribe,
{
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/preview2/preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::preview2::bindings::{
filesystem::{preopens, types as filesystem},
io::{poll, streams},
};
use crate::preview2::{FsError, IsATTY, StreamError, StreamResult, TableError, WasiView};
use crate::preview2::{FsError, IsATTY, StreamError, StreamResult, WasiView};
use anyhow::{bail, Context};
use std::borrow::Borrow;
use std::collections::{BTreeMap, HashSet};
Expand Down Expand Up @@ -325,7 +325,7 @@ pub trait WasiPreview1View: WasiView {
// `&mut self` receivers and so this struct lets us extend the lifetime of the `&mut self` borrow
// of the [`WasiPreview1View`] to provide means to return mutably and immutably borrowed [`Descriptors`]
// without having to rely on something like `Arc<Mutex<Descriptors>>`, while also being able to
// call methods like [`TableFsExt::is_file`] and hiding complexity from preview1 method implementations.
// call methods like [`Descriptor::is_file`] and hiding complexity from preview1 method implementations.
struct Transaction<'a, T: WasiPreview1View + ?Sized> {
view: &'a mut T,
descriptors: Descriptors,
Expand Down Expand Up @@ -772,8 +772,8 @@ impl From<filesystem::ErrorCode> for types::Error {
}
}

impl From<TableError> for types::Error {
fn from(err: TableError) -> Self {
impl From<wasmtime::component::ResourceTableError> for types::Error {
fn from(err: wasmtime::component::ResourceTableError) -> Self {
types::Error::trap(err.into())
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/wasi/src/preview2/stream.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::preview2::filesystem::FileInputStream;
use crate::preview2::poll::Subscribe;
use crate::preview2::TableError;
use anyhow::Result;
use bytes::Bytes;

Expand Down Expand Up @@ -73,8 +72,8 @@ impl std::error::Error for StreamError {
}
}

impl From<TableError> for StreamError {
fn from(error: TableError) -> Self {
impl From<wasmtime::component::ResourceTableError> for StreamError {
fn from(error: wasmtime::component::ResourceTableError) -> Self {
Self::Trap(error.into())
}
}
Expand Down
17 changes: 8 additions & 9 deletions crates/wasi/tests/all/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ use cap_std::fs::Dir;
use std::io::Write;
use std::sync::Mutex;
use std::time::Duration;
use wasmtime::component::{Component, Linker};
use wasmtime::component::{Component, Linker, ResourceTable};
use wasmtime::{Config, Engine, Store};
use wasmtime_wasi::preview2::bindings::wasi::clocks::wall_clock;
use wasmtime_wasi::preview2::bindings::wasi::filesystem::types as filesystem;
use wasmtime_wasi::preview2::command::{add_to_linker, Command};
use wasmtime_wasi::preview2::{
self, DirPerms, FilePerms, HostMonotonicClock, HostWallClock, Table, WasiCtx, WasiCtxBuilder,
WasiView,
self, DirPerms, FilePerms, HostMonotonicClock, HostWallClock, WasiCtx, WasiCtxBuilder, WasiView,
};

struct CommandCtx {
table: Table,
table: ResourceTable,
wasi: WasiCtx,
}

impl WasiView for CommandCtx {
fn table(&self) -> &Table {
fn table(&self) -> &ResourceTable {
&self.table
}
fn table_mut(&mut self) -> &mut Table {
fn table_mut(&mut self) -> &mut ResourceTable {
&mut self.table
}
fn ctx(&self) -> &WasiCtx {
Expand Down Expand Up @@ -82,7 +81,7 @@ async fn api_time() -> Result<()> {
}
}

let table = Table::new();
let table = ResourceTable::new();
let wasi = WasiCtxBuilder::new()
.monotonic_clock(FakeMonotonicClock { now: Mutex::new(0) })
.wall_clock(FakeWallClock)
Expand All @@ -104,7 +103,7 @@ async fn api_read_only() -> Result<()> {
std::fs::File::create(dir.path().join("bar.txt"))?.write_all(b"And stood awhile in thought")?;
std::fs::create_dir(dir.path().join("sub"))?;

let table = Table::new();
let table = ResourceTable::new();
let open_dir = Dir::open_ambient_dir(dir.path(), ambient_authority())?;
let wasi = WasiCtxBuilder::new()
.preopened_dir(open_dir, DirPerms::READ, FilePerms::READ, "/")
Expand Down Expand Up @@ -155,7 +154,7 @@ wasmtime::component::bindgen!({

#[test_log::test(tokio::test)]
async fn api_reactor() -> Result<()> {
let table = Table::new();
let table = ResourceTable::new();
let wasi = WasiCtxBuilder::new().env("GOOD_DOG", "gussie").build();

let mut config = Config::new();
Expand Down
Loading
Loading