Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 61216c6

Browse files
committed
More merging with main
prtest:full
1 parent f65cd7c commit 61216c6

16 files changed

Lines changed: 226 additions & 389 deletions

File tree

crates/wasi-http/src/p3/body.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use pin_project_lite::pin_project;
1313
use tokio::sync::mpsc;
1414
use tokio::sync::oneshot;
1515
use wasmtime::AsContextMut;
16-
use wasmtime::component::{Accessor, FutureReader, FutureWriter, HasData, Resource, StreamReader};
16+
use wasmtime::component::{
17+
Accessor, AsAccessor, FutureReader, FutureWriter, HasData, Resource, StreamReader,
18+
};
1719
use wasmtime_wasi::p3::WithChildren;
1820

1921
pub(crate) fn empty_body() -> impl http_body::Body<Data = Bytes, Error = Option<ErrorCode>> {
@@ -113,7 +115,7 @@ impl Body {
113115
}
114116
}
115117

116-
pub(crate) fn drop_with_store(&mut self, mut store: impl AsContextMut) -> wasmtime::Result<()> {
118+
pub(crate) fn drop_with_store(&mut self, mut store: impl AsContextMut) {
117119
if let Body::Guest {
118120
contents,
119121
trailers,
@@ -123,39 +125,44 @@ impl Body {
123125
{
124126
let mut store = store.as_context_mut();
125127
if let MaybeTombstone::Some(contents) = contents {
126-
contents.close(&mut store)?;
128+
contents.close(&mut store);
127129
}
128130
if let MaybeTombstone::Some(trailers) = trailers {
129-
trailers.close(&mut store)?;
131+
trailers.close(&mut store);
130132
}
131-
tx.close(store)?;
133+
tx.close(store);
132134
}
133-
anyhow::Ok(())
134135
}
135136

136-
pub(crate) fn with_store<'a, T, D>(self, store: &'a Accessor<T, D>) -> BodyWithStore<'a, T, D>
137+
pub(crate) fn with_store<A>(self, store: A) -> BodyWithStore<A>
137138
where
138-
D: HasData + ?Sized,
139+
A: AsAccessor,
139140
{
140-
BodyWithStore { store, body: self }
141+
BodyWithStore {
142+
store,
143+
body: Some(self),
144+
}
141145
}
142146
}
143147

144-
pub(crate) struct BodyWithStore<'a, T, D>
145-
where
146-
T: 'static,
147-
D: HasData + ?Sized,
148-
{
149-
store: &'a Accessor<T, D>,
150-
body: Body,
148+
pub(crate) struct BodyWithStore<A: AsAccessor> {
149+
store: A,
150+
body: Option<Body>,
151151
}
152152

153-
impl<T, D> Drop for BodyWithStore<'_, T, D>
154-
where
155-
D: HasData + ?Sized,
156-
{
153+
impl<A: AsAccessor> BodyWithStore<A> {
154+
pub(crate) fn into_inner(mut self) -> Body {
155+
self.body.take().unwrap()
156+
}
157+
}
158+
159+
impl<A: AsAccessor> Drop for BodyWithStore<A> {
157160
fn drop(&mut self) {
158-
self.store.with(|store| self.body.drop_with_store(store))
161+
if let Some(body) = &mut self.body {
162+
self.store
163+
.as_accessor()
164+
.with(|store| body.drop_with_store(store))
165+
}
159166
}
160167
}
161168

crates/wasi-http/src/p3/host/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ where
828828
.delete(req)
829829
.context("failed to delete request from table")?;
830830
mem::replace(request.body.lock().unwrap().deref_mut(), Body::Consumed)
831-
.drop_with_store(store)
831+
.drop_with_store(store);
832+
Ok(())
832833
})
833834
}
834835
}
@@ -1151,7 +1152,8 @@ where
11511152
.delete(req)
11521153
.context("failed to delete request from table")?;
11531154
mem::replace(request.body.lock().unwrap().deref_mut(), Body::Consumed)
1154-
.drop_with_store(store)
1155+
.drop_with_store(store);
1156+
Ok(())
11551157
})
11561158
}
11571159
}

crates/wasi-http/tests/all/p3/incoming.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use http_body::Body;
44
use http_body_util::{BodyExt as _, Collected, Empty};
55
use wasmtime::Store;
66
use wasmtime::component::{Component, Linker};
7-
use wasmtime_wasi::p3::cli::WasiCliCtx;
87
use wasmtime_wasi_http::p3::bindings::Proxy;
98
use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;
109
use wasmtime_wasi_http::p3::{Response, WasiHttpCtx};
@@ -26,9 +25,6 @@ pub async fn run_wasi_http<E: Into<ErrorCode> + 'static>(
2625
let mut store = Store::new(
2726
&engine,
2827
Ctx {
29-
cli: WasiCliCtx {
30-
..WasiCliCtx::default()
31-
},
3228
http: WasiHttpCtx { client },
3329
..Ctx::default()
3430
},

crates/wasi-http/tests/all/p3/mod.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ use core::future::Future;
33
use bytes::Bytes;
44
use wasmtime::Store;
55
use wasmtime::component::{Component, Linker, ResourceTable};
6-
use wasmtime_wasi::clocks::{WasiClocksCtx, WasiClocksView};
76
use wasmtime_wasi::p2::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
87
use wasmtime_wasi::p3::ResourceView;
9-
use wasmtime_wasi::p3::cli::{WasiCliCtx, WasiCliView};
108
use wasmtime_wasi::p3::filesystem::{WasiFilesystemCtx, WasiFilesystemView};
11-
use wasmtime_wasi::p3::sockets::{WasiSocketsCtx, WasiSocketsView};
12-
use wasmtime_wasi::random::{WasiRandomCtx, WasiRandomView};
139
use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;
1410
use wasmtime_wasi_http::p3::{
1511
Client, DEFAULT_FORBIDDEN_HEADERS, RequestOptions, WasiHttpCtx, WasiHttpView,
@@ -21,9 +17,7 @@ mod outgoing;
2117
mod proxy;
2218

2319
struct Ctx<C: Client = TestClient> {
24-
cli: WasiCliCtx,
2520
filesystem: WasiFilesystemCtx,
26-
sockets: WasiSocketsCtx,
2721
table: ResourceTable,
2822
wasip2: WasiCtx,
2923
wasip3: wasmtime_wasi::p3::WasiCtx,
@@ -36,9 +30,7 @@ where
3630
{
3731
fn default() -> Self {
3832
Self {
39-
cli: WasiCliCtx::default(),
4033
filesystem: WasiFilesystemCtx::default(),
41-
sockets: WasiSocketsCtx::default(),
4234
table: ResourceTable::default(),
4335
wasip2: WasiCtxBuilder::new().inherit_stdio().build(),
4436
wasip3: wasmtime_wasi::p3::WasiCtxBuilder::new()
@@ -62,8 +54,11 @@ impl<C: Client> IoView for Ctx<C> {
6254
}
6355

6456
impl<C: Client> wasmtime_wasi::p3::WasiView for Ctx<C> {
65-
fn ctx(&mut self) -> &mut wasmtime_wasi::p3::WasiCtx {
66-
&mut self.wasip3
57+
fn ctx(&mut self) -> wasmtime_wasi::p3::WasiCtxView<'_> {
58+
wasmtime_wasi::p3::WasiCtxView {
59+
ctx: &mut self.wasip3,
60+
table: &mut self.table,
61+
}
6762
}
6863
}
6964

@@ -73,36 +68,12 @@ impl<C: Client> ResourceView for Ctx<C> {
7368
}
7469
}
7570

76-
impl<C: Client> WasiCliView for Ctx<C> {
77-
fn cli(&mut self) -> &WasiCliCtx {
78-
&self.cli
79-
}
80-
}
81-
82-
impl<C: Client> WasiClocksView for Ctx<C> {
83-
fn clocks(&mut self) -> &WasiClocksCtx {
84-
&self.wasip3.clocks
85-
}
86-
}
87-
8871
impl<C: Client> WasiFilesystemView for Ctx<C> {
8972
fn filesystem(&self) -> &WasiFilesystemCtx {
9073
&self.filesystem
9174
}
9275
}
9376

94-
impl<C: Client> WasiRandomView for Ctx<C> {
95-
fn random(&mut self) -> &mut WasiRandomCtx {
96-
&mut self.wasip3.random
97-
}
98-
}
99-
100-
impl<C: Client> WasiSocketsView for Ctx<C> {
101-
fn sockets(&self) -> &WasiSocketsCtx {
102-
&self.sockets
103-
}
104-
}
105-
10677
impl<C: Client> WasiHttpView for Ctx<C> {
10778
type Client = C;
10879

crates/wasi-http/tests/all/p3/outgoing.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ async fn run(path: &str, server: &Server) -> anyhow::Result<()> {
2020
let mut store = Store::new(
2121
&engine,
2222
Ctx {
23-
cli: WasiCliCtx {
24-
environment: vec![("HTTP_SERVER".into(), server.addr())],
25-
..WasiCliCtx::default()
26-
},
23+
wasip3: wasmtime_wasi::p3::WasiCtxBuilder::new()
24+
.env("HTTP_SERVER", server.addr())
25+
.build(),
2726
..Ctx::<TestClient>::default()
2827
},
2928
);

crates/wasi/src/clocks.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ pub trait WasiClocksView: Send {
2020
fn clocks(&mut self) -> &mut WasiClocksCtx;
2121
}
2222

23-
impl<T: WasiClocksView> WasiClocksView for &mut T {
24-
fn clocks(&mut self) -> &mut WasiClocksCtx {
25-
T::clocks(self)
26-
}
27-
}
28-
29-
impl<T: WasiClocksView> WasiClocksView for Box<T> {
30-
fn clocks(&mut self) -> &mut WasiClocksCtx {
31-
T::clocks(self)
32-
}
33-
}
34-
3523
impl WasiClocksView for WasiClocksCtx {
3624
fn clocks(&mut self) -> &mut WasiClocksCtx {
3725
self

crates/wasi/src/p3/bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! use this modules's bindings rather than generate fresh bindings. That can be
1212
//! done using the `with` option to [`bindgen!`]:
1313
//!
14-
//! ```rust
14+
//! ```rust,ignore(TODO fix when wasi finishes merging upstream)
1515
//! use wasmtime_wasi::p3::{WasiCtx, WasiCtxView, WasiView};
1616
//! use wasmtime::{Result, Engine, Config};
1717
//! use wasmtime::component::{Linker, HasSelf, ResourceTable};

crates/wasi/src/p3/cli/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ pub struct WasiCliCtxView<'a> {
1313
pub table: &'a mut ResourceTable,
1414
}
1515

16-
impl<T: WasiCliView> WasiCliView for &mut T {
17-
fn cli(&mut self) -> WasiCliCtxView<'_> {
18-
T::cli(self)
19-
}
20-
}
21-
22-
impl<T: WasiCliView> WasiCliView for Box<T> {
23-
fn cli(&mut self) -> WasiCliCtxView<'_> {
24-
T::cli(self)
25-
}
26-
}
27-
2816
pub trait WasiCliView: Send {
2917
fn cli(&mut self) -> WasiCliCtxView<'_>;
3018
}

crates/wasi/src/p3/mod.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::p3::bindings::LinkOptions;
2-
use crate::p3::cli::WasiCliCtxView;
3-
use crate::sockets::WasiSocketsCtxView;
42
use anyhow::{Result, anyhow, bail};
53
use core::future::Future;
64
use core::ops::{Deref, DerefMut};
@@ -113,22 +111,10 @@ pub fn add_to_linker_with_options<T>(
113111
where
114112
T: WasiView + filesystem::WasiFilesystemView + 'static,
115113
{
116-
cli::add_to_linker_impl(linker, &options.into(), |x| {
117-
let WasiCtxView { ctx, table } = x.ctx();
118-
WasiCliCtxView {
119-
ctx: &mut ctx.cli,
120-
table,
121-
}
122-
})?;
123-
clocks::add_to_linker_impl(linker, |x| &mut x.ctx().ctx.clocks)?;
124-
random::add_to_linker_impl(linker, |x| &mut x.ctx().ctx.random)?;
125-
sockets::add_to_linker_impl(linker, |x| {
126-
let WasiCtxView { ctx, table } = x.ctx();
127-
WasiSocketsCtxView {
128-
ctx: &mut ctx.sockets,
129-
table,
130-
}
131-
})?;
114+
cli::add_to_linker_with_options(linker, &options.into())?;
115+
clocks::add_to_linker(linker)?;
116+
random::add_to_linker(linker)?;
117+
sockets::add_to_linker(linker)?;
132118
filesystem::add_to_linker(linker)?;
133119
Ok(())
134120
}

crates/wasi/src/p3/sockets/host/types/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl HostTcpSocketWithStore for WasiSockets {
392392
) -> wasmtime::Result<(StreamReader<u8>, FutureReader<Result<(), ErrorCode>>)> {
393393
store.with(|mut view| {
394394
let instance = view.instance();
395-
let (data_tx, data_rx) = instance
395+
let (mut data_tx, data_rx) = instance
396396
.stream(&mut view)
397397
.context("failed to create stream")?;
398398
let TcpSocket { tcp_state, .. } = get_socket_mut(view.get().table, &socket)?;
@@ -411,7 +411,7 @@ impl HostTcpSocketWithStore for WasiSockets {
411411
}
412412
prev => {
413413
*tcp_state = prev;
414-
let (result_tx, result_rx) = instance
414+
let (mut result_tx, result_rx) = instance
415415
.future(&mut view, || Err(ErrorCode::InvalidState))
416416
.context("failed to create future")?;
417417
result_tx.close(&mut view);

0 commit comments

Comments
 (0)