Skip to content

Commit

Permalink
Promote wasmtime-wasi::preview2 to root of crate (#7933)
Browse files Browse the repository at this point in the history
* better top matter

* eliminate wasi-common deprecations from wasmtime-wasi

* wasmtime-wasi: preview2 feature always enabled, so just eliminate it

* rename preview1-on-preview2 feature to just preview1

* wasi-http fix

* dep fixes. change some log::debug! to tracing::debug!

* mv preview2 up to root

and `sed -i 's/crate::preview2/crate/g' **/*.rs`

* fix tests too

* fixes throughout wasmtime-wasi-http

* cli: s/wasmtime_wasi::preview2/wasmtime_wasi

* rustfmt

* fix wasi-async example

* fix docs build (needs wasi-common built to compile examples)

* c-api: use wasi-common directly

i guess we didnt build the c-api in CI with deprecation warnings denied

prtest:full

* fix example required-features

* fix examples CMakeLists build
  • Loading branch information
Pat Hickey authored Feb 21, 2024
1 parent 2515b7a commit a56bd5e
Show file tree
Hide file tree
Showing 61 changed files with 689 additions and 840 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ jobs:
echo "${{ runner.tool_cache }}/mdbook/bin" >> $GITHUB_PATH
cargo install --root ${{ runner.tool_cache }}/mdbook --version ${{ env.CARGO_MDBOOK_VERSION }} mdbook
- run: (cd docs && mdbook build)
- run: cargo build -p wasmtime-wasi --features wasmtime/wat,wasmtime/cranelift
- run: cargo build -p wasi-common --features wasmtime/wat,wasmtime/cranelift
- run: (cd docs/rust_wasi_markdown_parser && cargo build)
- run: (cd docs && mdbook test -L ../target/debug/deps)

Expand Down
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ wasmtime-wast = { workspace = true, optional = true }
wasi-common = { workspace = true, default-features = true, features = [
"exit",
] }
wasmtime-wasi = { workspace = true, default-features = true, features = [
"exit",
] }
wasmtime-wasi = { workspace = true, default-features = true }
wasmtime-wasi-nn = { workspace = true, optional = true }
wasmtime-wasi-threads = { workspace = true, optional = true }
wasmtime-wasi-http = { workspace = true, optional = true }
Expand Down Expand Up @@ -394,7 +392,7 @@ harness = false

[[example]]
name = "tokio"
required-features = ["wasmtime-wasi/tokio"]
required-features = ["wasi-common/tokio"]

[[bench]]
name = "instantiation"
Expand Down
3 changes: 1 addition & 2 deletions crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ tracing = { workspace = true }
wat = { workspace = true, optional = true }

# Optional dependencies for the `wasi` feature
wasmtime-wasi = { workspace = true, default-features = true, optional = true }
cap-std = { workspace = true, optional = true }
wasi-common = { workspace = true, optional = true, features = ["sync"] }

Expand All @@ -43,7 +42,7 @@ async = ['wasmtime/async', 'futures']
profiling = ["wasmtime/profiling"]
cache = ["wasmtime/cache"]
parallel-compilation = ['wasmtime/parallel-compilation']
wasi = ['wasmtime-wasi', 'cap-std', 'wasi-common']
wasi = ['cap-std', 'wasi-common']
logging = ['dep:env_logger']
disable-logging = ["log/max_level_off", "tracing/max_level_off"]
coredump = ["wasmtime/coredump"]
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub extern "C" fn wasmtime_linker_define_wasi(
linker: &mut wasmtime_linker_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(
wasmtime_wasi::add_to_linker(&mut linker.linker, |cx| {
wasi_common::sync::add_to_linker(&mut linker.linker, |cx| {
cx.wasi.as_mut().expect(
"failed to define WASI on linker; did you set a WASI configuration in the store?",
)
Expand Down
4 changes: 1 addition & 3 deletions crates/wasi-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ http = { workspace = true }
http-body = { workspace = true }
http-body-util = { workspace = true }
tracing = { workspace = true }
wasmtime-wasi = { workspace = true, default-features = false, features = [
"preview2",
] }
wasmtime-wasi = { workspace = true }
wasmtime = { workspace = true, features = ['component-model'] }

# The `ring` crate, used to implement TLS, does not build on riscv64 or s390x
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::mem;
use std::task::{Context, Poll};
use std::{pin::Pin, sync::Arc, time::Duration};
use tokio::sync::{mpsc, oneshot};
use wasmtime_wasi::preview2::{
use wasmtime_wasi::{
self, poll_noop, AbortOnDropJoinHandle, HostInputStream, HostOutputStream, StreamError,
Subscribe,
};
Expand All @@ -36,7 +36,7 @@ impl BodyWithTimeout {
inner,
between_bytes_timeout,
reset_sleep: true,
timeout: Box::pin(preview2::with_ambient_tokio_runtime(|| {
timeout: Box::pin(wasmtime_wasi::with_ambient_tokio_runtime(|| {
tokio::time::sleep(Duration::new(0, 0))
})),
}
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub mod bindings {
tracing: true,
async: false,
with: {
"wasi:io/error": wasmtime_wasi::preview2::bindings::io::error,
"wasi:io/streams": wasmtime_wasi::preview2::bindings::io::streams,
"wasi:io/poll": wasmtime_wasi::preview2::bindings::io::poll,
"wasi:io/error": wasmtime_wasi::bindings::io::error,
"wasi:io/streams": wasmtime_wasi::bindings::io::streams,
"wasi:io/poll": wasmtime_wasi::bindings::io::poll,

"wasi:http/types/outgoing-body": super::body::HostOutgoingBody,
"wasi:http/types/future-incoming-response": super::types::HostFutureIncomingResponse,
Expand Down
69 changes: 34 additions & 35 deletions crates/wasi-http/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
use crate::{bindings, WasiHttpView};
use wasmtime_wasi::preview2;

wasmtime::component::bindgen!({
world: "wasi:http/proxy",
tracing: true,
async: true,
with: {
"wasi:cli/stderr": preview2::bindings::cli::stderr,
"wasi:cli/stdin": preview2::bindings::cli::stdin,
"wasi:cli/stdout": preview2::bindings::cli::stdout,
"wasi:clocks/monotonic-clock": preview2::bindings::clocks::monotonic_clock,
"wasi:clocks/timezone": preview2::bindings::clocks::timezone,
"wasi:clocks/wall-clock": preview2::bindings::clocks::wall_clock,
"wasi:cli/stderr": wasmtime_wasi::bindings::cli::stderr,
"wasi:cli/stdin": wasmtime_wasi::bindings::cli::stdin,
"wasi:cli/stdout": wasmtime_wasi::bindings::cli::stdout,
"wasi:clocks/monotonic-clock": wasmtime_wasi::bindings::clocks::monotonic_clock,
"wasi:clocks/timezone": wasmtime_wasi::bindings::clocks::timezone,
"wasi:clocks/wall-clock": wasmtime_wasi::bindings::clocks::wall_clock,
"wasi:http/incoming-handler": bindings::http::incoming_handler,
"wasi:http/outgoing-handler": bindings::http::outgoing_handler,
"wasi:http/types": bindings::http::types,
"wasi:io/streams": preview2::bindings::io::streams,
"wasi:io/poll": preview2::bindings::io::poll,
"wasi:random/random": preview2::bindings::random::random,
"wasi:io/streams": wasmtime_wasi::bindings::io::streams,
"wasi:io/poll": wasmtime_wasi::bindings::io::poll,
"wasi:random/random": wasmtime_wasi::bindings::random::random,
},
});

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
{
preview2::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
preview2::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
preview2::bindings::io::poll::add_to_linker(l, |t| t)?;
preview2::bindings::io::error::add_to_linker(l, |t| t)?;
preview2::bindings::io::streams::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stdin::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stdout::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stderr::add_to_linker(l, |t| t)?;
preview2::bindings::random::random::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::io::poll::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::io::error::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::io::streams::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdin::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdout::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stderr::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::random::random::add_to_linker(l, |t| t)?;

add_only_http_to_linker(l)
}

#[doc(hidden)]
pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
{
bindings::http::outgoing_handler::add_to_linker(l, |t| t)?;
bindings::http::types::add_to_linker(l, |t| t)?;
Expand All @@ -51,46 +50,46 @@ where

pub mod sync {
use crate::{bindings, WasiHttpView};
use wasmtime_wasi::preview2;
use wasmtime_wasi;

wasmtime::component::bindgen!({
world: "wasi:http/proxy",
tracing: true,
async: false,
with: {
"wasi:cli/stderr": preview2::bindings::cli::stderr,
"wasi:cli/stdin": preview2::bindings::cli::stdin,
"wasi:cli/stdout": preview2::bindings::cli::stdout,
"wasi:clocks/monotonic-clock": preview2::bindings::clocks::monotonic_clock,
"wasi:clocks/timezone": preview2::bindings::clocks::timezone,
"wasi:clocks/wall-clock": preview2::bindings::clocks::wall_clock,
"wasi:cli/stderr": wasmtime_wasi::bindings::cli::stderr,
"wasi:cli/stdin": wasmtime_wasi::bindings::cli::stdin,
"wasi:cli/stdout": wasmtime_wasi::bindings::cli::stdout,
"wasi:clocks/monotonic-clock": wasmtime_wasi::bindings::clocks::monotonic_clock,
"wasi:clocks/timezone": wasmtime_wasi::bindings::clocks::timezone,
"wasi:clocks/wall-clock": wasmtime_wasi::bindings::clocks::wall_clock,
"wasi:http/incoming-handler": bindings::http::incoming_handler,
"wasi:http/outgoing-handler": bindings::http::outgoing_handler,
"wasi:http/types": bindings::http::types,
"wasi:io/streams": preview2::bindings::io::streams,
"wasi:poll/poll": preview2::bindings::poll::poll,
"wasi:random/random": preview2::bindings::random::random,
"wasi:io/streams": wasmtime_wasi::bindings::io::streams,
"wasi:poll/poll": wasmtime_wasi::bindings::poll::poll,
"wasi:random/random": wasmtime_wasi::bindings::random::random,
},
});

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
{
// TODO: this shouldn't be required, but the adapter unconditionally pulls in all of these
// dependencies.
preview2::command::sync::add_to_linker(l)?;
wasmtime_wasi::command::sync::add_to_linker(l)?;

add_only_http_to_linker(l)?;

Ok(())
}

#[doc(hidden)]
// TODO: This is temporary solution until the preview2 command functions can be removed
// TODO: This is temporary solution until the wasmtime_wasi command functions can be removed
pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
{
bindings::http::outgoing_handler::add_to_linker(l, |t| t)?;
bindings::http::types::add_to_linker(l, |t| t)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::time::Duration;
use tokio::net::TcpStream;
use tokio::time::timeout;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::preview2::{self, AbortOnDropJoinHandle, Subscribe};
use wasmtime_wasi::{self, AbortOnDropJoinHandle, Subscribe};

/// Capture the state necessary for use in the wasi-http API implementation.
pub struct WasiHttpCtx;
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn default_send_request(
between_bytes_timeout,
}: OutgoingRequest,
) -> wasmtime::Result<Resource<HostFutureIncomingResponse>> {
let handle = preview2::spawn(async move {
let handle = wasmtime_wasi::spawn(async move {
let resp = handler(
authority,
use_tls,
Expand Down Expand Up @@ -212,7 +212,7 @@ async fn handler(
.map_err(|_| types::ErrorCode::ConnectionTimeout)?
.map_err(hyper_request_error)?;

let worker = preview2::spawn(async move {
let worker = wasmtime_wasi::spawn(async move {
match conn.await {
Ok(()) => {}
// TODO: shouldn't throw away this error and ideally should
Expand All @@ -234,7 +234,7 @@ async fn handler(
.map_err(|_| types::ErrorCode::ConnectionTimeout)?
.map_err(hyper_request_error)?;

let worker = preview2::spawn(async move {
let worker = wasmtime_wasi::spawn(async move {
match conn.await {
Ok(()) => {}
// TODO: same as above, shouldn't throw this error away.
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use anyhow::Context;
use std::any::Any;
use std::str::FromStr;
use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::preview2::{
use wasmtime_wasi::{
bindings::io::streams::{InputStream, OutputStream},
Pollable,
};
Expand Down Expand Up @@ -644,7 +644,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFutureTrailers for T {
&mut self,
index: Resource<HostFutureTrailers>,
) -> wasmtime::Result<Resource<Pollable>> {
wasmtime_wasi::preview2::subscribe(self.table(), index)
wasmtime_wasi::subscribe(self.table(), index)
}

fn get(
Expand Down Expand Up @@ -852,7 +852,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFutureIncomingResponse f
&mut self,
id: Resource<HostFutureIncomingResponse>,
) -> wasmtime::Result<Resource<Pollable>> {
wasmtime_wasi::preview2::subscribe(self.table(), id)
wasmtime_wasi::subscribe(self.table(), id)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/tests/all/async_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::preview2::command::Command;
use wasmtime_wasi::command::Command;

foreach_http!(assert_test_exists);

Expand All @@ -13,7 +13,7 @@ async fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi::preview2::command::add_to_linker(&mut linker)?;
wasmtime_wasi::command::add_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?;
let result = command.wasi_cli_run().call_run(&mut store).await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use wasmtime::{
component::{Component, Linker, Resource, ResourceTable},
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{self, pipe::MemoryOutputPipe, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{
bindings::http::types::ErrorCode,
body::HyperIncomingBody,
Expand Down Expand Up @@ -160,7 +160,7 @@ async fn run_wasi_http(
let (sender, receiver) = tokio::sync::oneshot::channel();
let out = store.data_mut().new_response_outparam(sender)?;

let handle = preview2::spawn(async move {
let handle = wasmtime_wasi::spawn(async move {
proxy
.wasi_http_incoming_handler()
.call_handle(&mut store, req, out)
Expand Down Expand Up @@ -267,7 +267,7 @@ async fn do_wasi_http_hash_all(override_send_request: bool) -> Result<()> {
let response = handle(request.into_parts().0).map(|resp| {
Ok(IncomingResponseInternal {
resp,
worker: Arc::new(preview2::spawn(future::ready(()))),
worker: Arc::new(wasmtime_wasi::spawn(future::ready(()))),
between_bytes_timeout,
})
});
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/tests/all/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::preview2::command::sync::Command;
use wasmtime_wasi::command::sync::Command;

foreach_http!(assert_test_exists);

Expand Down
Loading

0 comments on commit a56bd5e

Please sign in to comment.