Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add `-Shttp,p3` support to `wasmtime run` (#11741)

Alex Crichtonc90925f608bc
diff --git a/src/commands/run.rs b/src/commands/run.rs
index e3fd86d27b..2e658b725f 100644
--- a/src/commands/run.rs
+++ b/src/commands/run.rs
@@ -976,6 +976,10 @@ impl RunCommand {
}
CliLinker::Component(linker) => {
wasmtime_wasi_http::add_only_http_to_linker_sync(linker)?;
+ #[cfg(feature = "component-model-async")]
+ if self.run.common.wasi.p3.unwrap_or(crate::common::P3_DEFAULT) {
+ wasmtime_wasi_http::p3::add_to_linker(linker)?;
+ }
}
}
@@ -1138,6 +1142,8 @@ struct Host {
wasi_http_outgoing_body_buffer_chunks: Option<usize>,
#[cfg(feature = "wasi-http")]
wasi_http_outgoing_body_chunk_size: Option<usize>,
+ #[cfg(all(feature = "wasi-http", feature = "component-model-async"))]
+ p3_http: crate::common::DefaultP3Ctx,
limits: StoreLimits,
#[cfg(feature = "profiling")]
guest_profiler: Option<Arc<wasmtime::GuestProfiler>>,
@@ -1152,14 +1158,18 @@ struct Host {
impl Host {
fn wasip1_ctx(&mut self) -> &mut wasmtime_wasi::p1::WasiP1Ctx {
- let ctx = self.wasip1_ctx.as_mut().expect("wasi is not configured");
- Arc::get_mut(ctx)
- .expect("wasmtime_wasi is not compatible with threads")
- .get_mut()
- .unwrap()
+ unwrap_singlethread_context(&mut self.wasip1_ctx)
}
}
+fn unwrap_singlethread_context<T>(ctx: &mut Option<Arc<Mutex<T>>>) -> &mut T {
+ let ctx = ctx.as_mut().expect("context not configured");
+ Arc::get_mut(ctx)
+ .expect("context is not compatible with threads")
+ .get_mut()
+ .unwrap()
+}
+
impl WasiView for Host {
fn ctx(&mut self) -> WasiCtxView<'_> {
WasiView::ctx(self.wasip1_ctx())
@@ -1188,6 +1198,16 @@ impl wasmtime_wasi_http::types::WasiHttpView for Host {
}
}
+#[cfg(all(feature = "wasi-http", feature = "component-model-async"))]
+impl wasmtime_wasi_http::p3::WasiHttpView for Host {
+ fn http(&mut self) -> wasmtime_wasi_http::p3::WasiHttpCtxView<'_> {
+ wasmtime_wasi_http::p3::WasiHttpCtxView {
+ table: WasiView::ctx(unwrap_singlethread_context(&mut self.wasip1_ctx)).table,
+ ctx: &mut self.p3_http,
+ }
+ }
+}
+
#[cfg(not(unix))]
fn ctx_set_listenfd(num_fd: usize, _builder: &mut WasiCtxBuilder) -> Result<usize> {
Ok(num_fd)
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index e308f7de71..907a83eb60 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -46,7 +46,7 @@ struct Host {
http_outgoing_body_chunk_size: Option<usize>,
#[cfg(feature = "component-model-async")]
- p3_http: DefaultP3Ctx,
+ p3_http: crate::common::DefaultP3Ctx,
limits: StoreLimits,
@@ -91,11 +91,6 @@ impl WasiHttpView for Host {
}
}
-#[cfg(feature = "component-model-async")]
-struct DefaultP3Ctx;
-#[cfg(feature = "component-model-async")]
-impl wasmtime_wasi_http::p3::WasiHttpCtx for DefaultP3Ctx {}
-
#[cfg(feature = "component-model-async")]
impl wasmtime_wasi_http::p3::WasiHttpView for Host {
fn http(&mut self) -> wasmtime_wasi_http::p3::WasiHttpCtxView<'_> {
@@ -212,7 +207,7 @@ impl ServeCommand {
#[cfg(feature = "profiling")]
guest_profiler: None,
#[cfg(feature = "component-model-async")]
- p3_http: DefaultP3Ctx,
+ p3_http: crate::common::DefaultP3Ctx,
};
if self.run.common.wasi.nn == Some(true) {
diff --git a/src/common.rs b/src/common.rs
index cb399728f7..6975caf4c2 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -429,3 +429,9 @@ impl Profile {
}
}
}
+
+#[derive(Default, Clone)]
+#[cfg(all(feature = "wasi-http", feature = "component-model-async"))]
+pub struct DefaultP3Ctx;
+#[cfg(all(feature = "wasi-http", feature = "component-model-async"))]
+impl wasmtime_wasi_http::p3::WasiHttpCtx for DefaultP3Ctx {}