Skip to content

Commit

Permalink
feat(wasmtime-cli): restore support for wasi http module
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomourar committed Aug 26, 2023
1 parent d1b8af2 commit 9401c72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ jitdump = ["wasmtime/jitdump"]
vtune = ["wasmtime/vtune"]
wasi-nn = ["dep:wasmtime-wasi-nn"]
wasi-threads = ["dep:wasmtime-wasi-threads"]
wasi-http = ["dep:wasmtime-wasi-http"]
wasi-http = ["dep:wasmtime-wasi-http", "wasmtime-wasi-http/sync"]
pooling-allocator = [
"wasmtime/pooling-allocator",
"wasmtime-cli-flags/pooling-allocator",
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn add_to_linker<T: WasiHttpView>(linker: &mut wasmtime::Linker<T>) -> anyho
}

pub mod sync {
use crate::r#struct::WasiHttpView;
use crate::types::WasiHttpView;

pub fn add_to_linker<T: WasiHttpView>(linker: &mut wasmtime::Linker<T>) -> anyhow::Result<()> {
crate::component_impl::sync::add_component_to_linker::<T>(linker, |t| t)
Expand Down
43 changes: 30 additions & 13 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use wasmtime_wasi_nn::WasiNnCtx;
#[cfg(feature = "wasi-threads")]
use wasmtime_wasi_threads::WasiThreadsCtx;

// #[cfg(feature = "wasi-http")]
// use wasmtime_wasi_http::WasiHttpCtx;
#[cfg(feature = "wasi-http")]
use wasmtime_wasi_http::WasiHttpCtx;

fn parse_env_var(s: &str) -> Result<(String, Option<String>)> {
let mut parts = s.splitn(2, '=');
Expand Down Expand Up @@ -645,12 +645,8 @@ impl RunCommand {

let component = module.unwrap_component();

let (command, _instance) = preview2::command::sync::Command::instantiate(
&mut *store,
&component,
&linker,
)?;

let (command, _instance) =
preview2::command::sync::Command::instantiate(&mut *store, component, linker)?;
let result = command
.wasi_cli_run()
.call_run(&mut *store)
Expand Down Expand Up @@ -895,7 +891,7 @@ impl RunCommand {
match linker {
CliLinker::Core(linker) => {
if self.preview2 {
wasmtime_wasi::preview2::preview1::add_to_linker_sync(linker)?;
preview2::preview1::add_to_linker_sync(linker)?;
self.set_preview2_ctx(store)?;
} else {
wasmtime_wasi::add_to_linker(linker, |host| {
Expand All @@ -906,7 +902,7 @@ impl RunCommand {
}
#[cfg(feature = "component-model")]
CliLinker::Component(linker) => {
wasmtime_wasi::preview2::command::sync::add_to_linker(linker)?;
preview2::command::sync::add_to_linker(linker)?;
self.set_preview2_ctx(store)?;
}
}
Expand Down Expand Up @@ -981,7 +977,16 @@ impl RunCommand {
}
#[cfg(feature = "wasi-http")]
{
bail!("wasi-http support will be swapped over to component CLI support soon");
match linker {
CliLinker::Core(linker) => {
wasmtime_wasi_http::sync::add_to_linker(linker)?;
}
#[cfg(feature = "component-model")]
CliLinker::Component(linker) => {
wasmtime_wasi_http::proxy::sync::add_to_linker(linker)?;
}
}
store.data_mut().wasi_http = Some(Arc::new(WasiHttpCtx::new()));
}
}

Expand Down Expand Up @@ -1075,8 +1080,8 @@ struct Host {
wasi_nn: Option<Arc<WasiNnCtx>>,
#[cfg(feature = "wasi-threads")]
wasi_threads: Option<Arc<WasiThreadsCtx<Host>>>,
// #[cfg(feature = "wasi-http")]
// wasi_http: Option<WasiHttp>,
#[cfg(feature = "wasi-http")]
wasi_http: Option<Arc<WasiHttpCtx>>,
limits: StoreLimits,
guest_profiler: Option<Arc<GuestProfiler>>,
}
Expand Down Expand Up @@ -1110,6 +1115,18 @@ impl preview2::preview1::WasiPreview1View for Host {
}
}

#[cfg(feature = "wasi-http")]
impl wasmtime_wasi_http::types::WasiHttpView for Host {
fn http_ctx(&self) -> &WasiHttpCtx {
self.wasi_http.as_ref().unwrap()
}

fn http_ctx_mut(&mut self) -> &mut WasiHttpCtx {
let ctx = self.wasi_http.as_mut().unwrap();
Arc::get_mut(ctx).expect("wasi-http is not compatible with threads")
}
}

#[cfg(not(unix))]
fn ctx_set_listenfd(num_fd: usize, _builder: &mut WasiCtxBuilder) -> Result<usize> {
Ok(num_fd)
Expand Down

0 comments on commit 9401c72

Please sign in to comment.