From a8a23f84de56ac594da51b21694b18e67754745a Mon Sep 17 00:00:00 2001
From: Pat Hickey
Date: Fri, 10 Jan 2025 14:01:00 -0800
Subject: [PATCH 1/5] wip: WasiCtx has a WasiExecutor
---
crates/wasi/src/ctx.rs | 68 +++++++++++++++++++++++++++++++------
crates/wasi/src/lib.rs | 20 +++++++----
crates/wasi/src/preview0.rs | 9 ++---
crates/wasi/src/preview1.rs | 40 +++++++++++-----------
4 files changed, 97 insertions(+), 40 deletions(-)
diff --git a/crates/wasi/src/ctx.rs b/crates/wasi/src/ctx.rs
index 693876ea896a..6fdad0b288d3 100644
--- a/crates/wasi/src/ctx.rs
+++ b/crates/wasi/src/ctx.rs
@@ -463,7 +463,7 @@ impl WasiCtxBuilder {
/// Panics if this method is called twice. Each [`WasiCtxBuilder`] can be
/// used to create only a single [`WasiCtx`]. Repeated usage of this method
/// is not allowed and should use a second builder instead.
- pub fn build(&mut self) -> WasiCtx {
+ pub fn build(&mut self) -> WasiCtx {
assert!(!self.built);
let Self {
@@ -500,6 +500,7 @@ impl WasiCtxBuilder {
monotonic_clock,
allowed_network_uses,
allow_blocking_current_thread,
+ _executor: std::marker::PhantomData,
}
}
@@ -519,7 +520,7 @@ impl WasiCtxBuilder {
/// usage of this method is not allowed and should use a second builder
/// instead.
#[cfg(feature = "preview1")]
- pub fn build_p1(&mut self) -> crate::preview1::WasiP1Ctx {
+ pub fn build_p1(&mut self) -> crate::preview1::WasiP1Ctx {
let wasi = self.build();
crate::preview1::WasiP1Ctx::new(wasi)
}
@@ -545,7 +546,7 @@ impl WasiCtxBuilder {
/// # Example
///
/// ```
-/// use wasmtime_wasi::{WasiCtx, ResourceTable, WasiView, WasiCtxBuilder};
+/// use wasmtime_wasi::{WasiCtx, ResourceTable, WasiView, Tokio, WasiCtxBuilder};
///
/// struct MyState {
/// ctx: WasiCtx,
@@ -553,7 +554,8 @@ impl WasiCtxBuilder {
/// }
///
/// impl WasiView for MyState {
-/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
+/// type Executor = Tokio;
+/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
///
@@ -572,6 +574,7 @@ impl WasiCtxBuilder {
/// }
/// ```
pub trait WasiView: Send {
+ type Executor: WasiExecutor;
/// Yields mutable access to the internal resource management that this
/// context contains.
///
@@ -582,23 +585,25 @@ pub trait WasiView: Send {
/// Yields mutable access to the configuration used for this context.
///
/// The returned type is created through [`WasiCtxBuilder`].
- fn ctx(&mut self) -> &mut WasiCtx;
+ fn ctx(&mut self) -> &mut WasiCtx;
}
impl WasiView for &mut T {
+ type Executor = T::Executor;
fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}
- fn ctx(&mut self) -> &mut WasiCtx {
+ fn ctx(&mut self) -> &mut WasiCtx {
T::ctx(self)
}
}
impl WasiView for Box {
+ type Executor = T::Executor;
fn table(&mut self) -> &mut ResourceTable {
T::table(self)
}
- fn ctx(&mut self) -> &mut WasiCtx {
+ fn ctx(&mut self) -> &mut WasiCtx {
T::ctx(self)
}
}
@@ -619,14 +624,56 @@ impl WasiView for Box {
pub struct WasiImpl(pub T);
impl WasiView for WasiImpl {
+ type Executor = T::Executor;
fn table(&mut self) -> &mut ResourceTable {
T::table(&mut self.0)
}
- fn ctx(&mut self) -> &mut WasiCtx {
+ fn ctx(&mut self) -> &mut WasiCtx {
T::ctx(&mut self.0)
}
}
+pub trait WasiExecutor: Send {
+ fn run_blocking(body: F) -> impl std::future::Future