Skip to content

Commit 29b9cad

Browse files
committed
refactor: introduce p2 module
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent 5d6cd8c commit 29b9cad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+641
-608
lines changed

ci/vendor-wit.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ make_vendor() {
3636

3737
cache_dir=$(mktemp -d)
3838

39-
make_vendor "wasi" "
39+
make_vendor "wasi/src/p2" "
4040
4141
4242
@@ -45,7 +45,7 @@ make_vendor "wasi" "
4545
4646
"
4747

48-
make_vendor "wasi-http" "
48+
make_vendor "wasi-http/src/p2" "
4949
5050
5151
@@ -55,9 +55,9 @@ make_vendor "wasi-http" "
5555
5656
"
5757

58-
make_vendor "wasi-config" "config@f4d699b"
58+
make_vendor "wasi-config/src/p2" "config@f4d699b"
5959

60-
make_vendor "wasi-keyvalue" "keyvalue@219ea36"
60+
make_vendor "wasi-keyvalue/src/p2" "keyvalue@219ea36"
6161

6262
rm -rf $cache_dir
6363

crates/test-programs/src/bin/api_reactor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::{Mutex, MutexGuard};
22

33
wit_bindgen::generate!({
44
world: "test-reactor",
5-
path: "../wasi/wit",
5+
path: "../wasi/src/p2/wit",
66
generate_all,
77
});
88

crates/test-programs/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ wit_bindgen::generate!({
1515
}
1616
",
1717
path: [
18-
"../wasi-http/wit",
19-
"../wasi-config/wit",
20-
"../wasi-keyvalue/wit",
18+
"../wasi-http/src/p2/wit",
19+
"../wasi-config/src/p2/wit",
20+
"../wasi-keyvalue/src/p2/wit",
2121
],
2222
world: "wasmtime:test/test",
2323
features: ["cli-exit-with-code"],
@@ -26,7 +26,7 @@ wit_bindgen::generate!({
2626

2727
pub mod proxy {
2828
wit_bindgen::generate!({
29-
path: "../wasi-http/wit",
29+
path: "../wasi-http/src/p2/wit",
3030
world: "wasi:http/proxy",
3131
default_bindings_module: "test_programs::proxy",
3232
pub_export_macro: true,

crates/wasi-config/src/lib.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,11 @@
6767
6868
#![deny(missing_docs)]
6969

70-
use anyhow::Result;
7170
use std::collections::HashMap;
7271

73-
mod gen_ {
74-
wasmtime::component::bindgen!({
75-
path: "wit",
76-
world: "wasi:config/imports",
77-
trappable_imports: true,
78-
});
79-
}
80-
use self::gen_::wasi::config::store as generated;
72+
pub mod p2;
73+
#[doc(inline)]
74+
pub use p2::*;
8175

8276
/// Capture the state necessary for use in the `wasi-config` API implementation.
8377
#[derive(Default)]
@@ -123,27 +117,3 @@ impl<'a> WasiConfig<'a> {
123117
Self { vars }
124118
}
125119
}
126-
127-
impl generated::Host for WasiConfig<'_> {
128-
fn get(&mut self, key: String) -> Result<Result<Option<String>, generated::Error>> {
129-
Ok(Ok(self.vars.0.get(&key).map(|s| s.to_owned())))
130-
}
131-
132-
fn get_all(&mut self) -> Result<Result<Vec<(String, String)>, generated::Error>> {
133-
Ok(Ok(self
134-
.vars
135-
.0
136-
.iter()
137-
.map(|(k, v)| (k.to_string(), v.to_string()))
138-
.collect()))
139-
}
140-
}
141-
142-
/// Add all the `wasi-config` world's interfaces to a [`wasmtime::component::Linker`].
143-
pub fn add_to_linker<T>(
144-
l: &mut wasmtime::component::Linker<T>,
145-
f: impl Fn(&mut T) -> WasiConfig<'_> + Send + Sync + Copy + 'static,
146-
) -> Result<()> {
147-
generated::add_to_linker_get_host(l, f)?;
148-
Ok(())
149-
}

crates/wasi-config/src/p2/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Implementation of wasip2 version of `wasi:config` package
2+
3+
mod gen_ {
4+
wasmtime::component::bindgen!({
5+
path: "src/p2/wit",
6+
world: "wasi:config/imports",
7+
trappable_imports: true,
8+
});
9+
}
10+
use self::gen_::wasi::config::store as generated;
11+
12+
use crate::WasiConfig;
13+
14+
impl generated::Host for WasiConfig<'_> {
15+
fn get(&mut self, key: String) -> anyhow::Result<Result<Option<String>, generated::Error>> {
16+
Ok(Ok(self.vars.0.get(&key).map(|s| s.to_owned())))
17+
}
18+
19+
fn get_all(&mut self) -> anyhow::Result<Result<Vec<(String, String)>, generated::Error>> {
20+
Ok(Ok(self
21+
.vars
22+
.0
23+
.iter()
24+
.map(|(k, v)| (k.to_string(), v.to_string()))
25+
.collect()))
26+
}
27+
}
28+
29+
/// Add all the `wasi-config` world's interfaces to a [`wasmtime::component::Linker`].
30+
pub fn add_to_linker<T>(
31+
l: &mut wasmtime::component::Linker<T>,
32+
f: impl Fn(&mut T) -> WasiConfig<'_> + Send + Sync + Copy + 'static,
33+
) -> anyhow::Result<()> {
34+
generated::add_to_linker_get_host(l, f)?;
35+
Ok(())
36+
}

crates/wasi-http/src/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementation of the `wasi:http/types` interface's various body types.
22
3-
use crate::{bindings::http::types, types::FieldMap};
3+
use crate::{p2::bindings::http::types, types::FieldMap};
44
use anyhow::anyhow;
55
use bytes::Bytes;
66
use http_body::{Body, Frame};

crates/wasi-http/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::bindings::http::types::ErrorCode;
1+
use crate::p2::bindings::http::types::ErrorCode;
22
use std::error::Error;
33
use std::fmt;
44
use wasmtime_wasi::ResourceTableError;

crates/wasi-http/src/lib.rs

Lines changed: 6 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
//! # Wasmtime's WASI HTTP Implementation
22
//!
3-
//! This crate is Wasmtime's host implementation of the `wasi:http` package as
4-
//! part of WASIp2. This crate's implementation is primarily built on top of
3+
//! This crate is Wasmtime's host implementation of the `wasi:http` package.
4+
//! This crate's implementation is primarily built on top of
55
//! [`hyper`] and [`tokio`].
66
//!
7-
//! # WASI HTTP Interfaces
8-
//!
9-
//! This crate contains implementations of the following interfaces:
10-
//!
11-
//! * [`wasi:http/incoming-handler`]
12-
//! * [`wasi:http/outgoing-handler`]
13-
//! * [`wasi:http/types`]
14-
//!
15-
//! The crate also contains an implementation of the [`wasi:http/proxy`] world.
16-
//!
17-
//! [`wasi:http/proxy`]: crate::bindings::Proxy
18-
//! [`wasi:http/outgoing-handler`]: crate::bindings::http::outgoing_handler::Host
19-
//! [`wasi:http/types`]: crate::bindings::http::types::Host
20-
//! [`wasi:http/incoming-handler`]: crate::bindings::exports::wasi::http::incoming_handler::Guest
21-
//!
22-
//! This crate is very similar to [`wasmtime-wasi`] in the it uses the
23-
//! `bindgen!` macro in Wasmtime to generate bindings to interfaces. Bindings
24-
//! are located in the [`bindings`] module.
25-
//!
267
//! # The `WasiHttpView` trait
278
//!
289
//! All `bindgen!`-generated `Host` traits are implemented in terms of a
@@ -55,7 +36,7 @@
5536
//! no others. This is useful when working with
5637
//! [`wasmtime_wasi::add_to_linker_async`] for example.
5738
//! * Add individual interfaces such as with the
58-
//! [`bindings::http::outgoing_handler::add_to_linker_get_host`] function.
39+
//! [`p2::bindings::http::outgoing_handler::add_to_linker_get_host`] function.
5940
//! 3. Use [`ProxyPre`](bindings::ProxyPre) to pre-instantiate a component
6041
//! before serving requests.
6142
//! 4. When serving requests use
@@ -217,15 +198,12 @@
217198
#![expect(clippy::allow_attributes_without_reason, reason = "crate not migrated")]
218199

219200
mod error;
220-
mod http_impl;
221-
mod types_impl;
222201

223202
pub mod body;
224203
pub mod io;
204+
pub mod p2;
225205
pub mod types;
226206

227-
pub mod bindings;
228-
229207
pub use crate::error::{
230208
http_request_error, hyper_request_error, hyper_response_error, HttpError, HttpResult,
231209
};
@@ -234,70 +212,8 @@ pub use crate::types::{
234212
WasiHttpCtx, WasiHttpImpl, WasiHttpView, DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,
235213
DEFAULT_OUTGOING_BODY_CHUNK_SIZE,
236214
};
237-
use wasmtime_wasi::IoImpl;
238-
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
239-
///
240-
/// This function will add the `async` variant of all interfaces into the
241-
/// `Linker` provided. By `async` this means that this function is only
242-
/// compatible with [`Config::async_support(true)`][async]. For embeddings with
243-
/// async support disabled see [`add_to_linker_sync`] instead.
244-
///
245-
/// [async]: wasmtime::Config::async_support
246-
///
247-
/// # Example
248-
///
249-
/// ```
250-
/// use wasmtime::{Engine, Result, Config};
251-
/// use wasmtime::component::{ResourceTable, Linker};
252-
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
253-
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
254-
///
255-
/// fn main() -> Result<()> {
256-
/// let mut config = Config::new();
257-
/// config.async_support(true);
258-
/// let engine = Engine::new(&config)?;
259-
///
260-
/// let mut linker = Linker::<MyState>::new(&engine);
261-
/// wasmtime_wasi_http::add_to_linker_async(&mut linker)?;
262-
/// // ... add any further functionality to `linker` if desired ...
263-
///
264-
/// Ok(())
265-
/// }
266-
///
267-
/// struct MyState {
268-
/// ctx: WasiCtx,
269-
/// http_ctx: WasiHttpCtx,
270-
/// table: ResourceTable,
271-
/// }
272-
///
273-
/// impl IoView for MyState {
274-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
275-
/// }
276-
/// impl WasiHttpView for MyState {
277-
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
278-
/// }
279-
/// impl WasiView for MyState {
280-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
281-
/// }
282-
/// ```
283-
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
284-
where
285-
T: WasiHttpView + wasmtime_wasi::WasiView,
286-
{
287-
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
288-
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));
289-
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
290-
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
291-
wasmtime_wasi::bindings::io::poll::add_to_linker_get_host(l, io_closure)?;
292-
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, io_closure)?;
293-
wasmtime_wasi::bindings::io::streams::add_to_linker_get_host(l, io_closure)?;
294-
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
295-
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
296-
wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
297-
wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;
298-
299-
add_only_http_to_linker_async(l)
300-
}
215+
#[doc(inline)]
216+
pub use p2::*;
301217

302218
// NB: workaround some rustc inference - a future refactoring may make this
303219
// obsolete.
@@ -319,100 +235,3 @@ where
319235
{
320236
val
321237
}
322-
323-
/// A slimmed down version of [`add_to_linker_async`] which only adds
324-
/// `wasi:http` interfaces to the linker.
325-
///
326-
/// This is useful when using [`wasmtime_wasi::add_to_linker_async`] for
327-
/// example to avoid re-adding the same interfaces twice.
328-
pub fn add_only_http_to_linker_async<T>(
329-
l: &mut wasmtime::component::Linker<T>,
330-
) -> anyhow::Result<()>
331-
where
332-
T: WasiHttpView,
333-
{
334-
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));
335-
crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
336-
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;
337-
338-
Ok(())
339-
}
340-
341-
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
342-
///
343-
/// This function will add the `sync` variant of all interfaces into the
344-
/// `Linker` provided. For embeddings with async support see
345-
/// [`add_to_linker_async`] instead.
346-
///
347-
/// # Example
348-
///
349-
/// ```
350-
/// use wasmtime::{Engine, Result, Config};
351-
/// use wasmtime::component::{ResourceTable, Linker};
352-
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
353-
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
354-
///
355-
/// fn main() -> Result<()> {
356-
/// let config = Config::default();
357-
/// let engine = Engine::new(&config)?;
358-
///
359-
/// let mut linker = Linker::<MyState>::new(&engine);
360-
/// wasmtime_wasi_http::add_to_linker_sync(&mut linker)?;
361-
/// // ... add any further functionality to `linker` if desired ...
362-
///
363-
/// Ok(())
364-
/// }
365-
///
366-
/// struct MyState {
367-
/// ctx: WasiCtx,
368-
/// http_ctx: WasiHttpCtx,
369-
/// table: ResourceTable,
370-
/// }
371-
/// impl IoView for MyState {
372-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373-
/// }
374-
/// impl WasiHttpView for MyState {
375-
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
376-
/// }
377-
/// impl WasiView for MyState {
378-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
379-
/// }
380-
/// ```
381-
pub fn add_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
382-
where
383-
T: WasiHttpView + wasmtime_wasi::WasiView,
384-
{
385-
let io_closure = type_annotate_io::<T, _>(|t| wasmtime_wasi::IoImpl(t));
386-
let closure = type_annotate_wasi::<T, _>(|t| wasmtime_wasi::WasiImpl(wasmtime_wasi::IoImpl(t)));
387-
388-
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
389-
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
390-
wasmtime_wasi::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?;
391-
wasmtime_wasi::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?;
392-
wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, io_closure)?;
393-
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
394-
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
395-
wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
396-
wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;
397-
398-
add_only_http_to_linker_sync(l)?;
399-
400-
Ok(())
401-
}
402-
403-
/// A slimmed down version of [`add_to_linker_sync`] which only adds
404-
/// `wasi:http` interfaces to the linker.
405-
///
406-
/// This is useful when using [`wasmtime_wasi::add_to_linker_sync`] for
407-
/// example to avoid re-adding the same interfaces twice.
408-
pub fn add_only_http_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
409-
where
410-
T: WasiHttpView,
411-
{
412-
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));
413-
414-
crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
415-
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;
416-
417-
Ok(())
418-
}

0 commit comments

Comments
 (0)