From beb7dd9b2e388614fa7923e448fd25c8281e8fee Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Fri, 30 Oct 2026 11:51:28 +0100 Subject: [PATCH] Add support for pure WASIp2 builds This changes the `wasm32-wasip2` build target to really target WASIp2, without any P3 APIs or Component Model async features. In most regards, the behavior is the same, but there are some key differences in custom WIT bindings. Specifically, where WASIp3 `streams`s and `future`s are mapped to JavaScript `ReadableStream`s and `Promise`s, WASIp2 bindings expose `wasi:io` interfaces directly. For builtin features, the key difference is that the inbound HTTP handler can't process multiple requests concurrently, since the incoming handler only returns once the request has been fully handled. --- .github/workflows/ci.yml | 23 +- Cargo.lock | 20 +- Cargo.toml | 8 +- README.md | 37 +- componentize/Cargo.toml | 2 +- componentize/src/command.rs | 9 +- componentize/src/finalize.rs | 8 +- componentize/src/lib.rs | 195 ++++-- componentize/src/supported.rs | 22 + componentize/src/versions.rs | 3 +- componentize/src/wit_to_ts.rs | 107 ++- componentize/tests/async_export.rs | 41 ++ componentize/tests/async_import.rs | 9 + componentize/tests/cli_run.rs | 59 +- componentize/tests/exported_resource.rs | 37 +- componentize/tests/fetch_streams.rs | 24 + componentize/tests/fixtures/cli_run_p2.wit | 15 + .../tests/fixtures/exported_resource.wit | 14 +- componentize/tests/fixtures/serve_p2.wit | 13 + componentize/tests/fixtures/serve_raw_p2.js | 34 + .../tests/fixtures/serve_raw_response_p2.js | 49 ++ componentize/tests/fixtures/values.js | 2 +- componentize/tests/fixtures/values.wit | 16 +- componentize/tests/lowering.rs | 33 + componentize/tests/serve.rs | 240 ++++--- componentize/tests/shared/mod.rs | 173 ++++- componentize/tests/streams.rs | 171 +++++ componentize/tests/surface.rs | 2 +- componentize/tests/values.rs | 57 +- componentize/tests/wit_dir.rs | 23 +- componentize/tests/wit_to_ts.rs | 117 +++- .../wit_to_ts/builtin_cli.p2.d.ts.expected | 603 ++++++++++++++++ .../wit_to_ts/builtin_serve.p2.d.ts.expected | 646 +++++++++++++++++ .../wit_to_ts/exported_resource.d.ts.expected | 3 - .../tests/wit_to_ts/serve_p2.d.ts.expected | 138 ++++ .../tests/wit_to_ts/serve_p2.handler.ts | 25 + componentize/tests/wit_to_ts/serve_p2.wit | 44 ++ crates/libstarling/Cargo.toml | 3 - crates/libstarling/src/lib.rs | 8 +- crates/libstarling/src/serve_wasm.rs | 59 +- crates/platform/Cargo.toml | 8 +- crates/platform/src/clock.rs | 12 +- crates/platform/src/http.rs | 32 +- crates/platform/src/http_p2.rs | 398 +++++++++++ crates/platform/src/http_p3.rs | 366 ++++++++++ crates/platform/src/http_wasm.rs | 658 +++++++----------- crates/platform/src/lib.rs | 20 +- crates/platform/src/task.rs | 27 + crates/platform/src/task_p2.rs | 242 +++++++ crates/platform/tests/task.rs | 73 ++ crates/serve-test-support/src/wasm_serve.rs | 36 +- .../tests/serve_wasm_e2e.rs | 55 +- crates/wit-bridge/Cargo.toml | 10 +- crates/wit-bridge/src/interpreter.rs | 336 ++++++--- crates/wit-bridge/src/lib.rs | 10 +- crates/wit-bridge/src/promises.rs | 2 +- crates/wit-bridge/src/readable.rs | 2 +- crates/wit-bridge/src/resources.rs | 2 +- crates/wit-bridge/src/streams.rs | 2 +- crates/wit-bridge/src/tracing.rs | 2 +- crown/rust-toolchain.toml | 10 +- crown/src/bare_value_in_gc_struct.rs | 2 +- crown/src/common.rs | 4 +- crown/src/main.rs | 5 +- crown/src/trace_in_no_trace.rs | 7 +- crown/src/unrooted_must_root.rs | 2 +- crown/tests/compile_test.rs | 2 +- justfile | 5 +- scripts/build-runtime.sh | 14 + scripts/check-crown.sh | 6 +- scripts/check-init-wit.sh | 18 +- starling/Cargo.toml | 28 +- starling/src/lib.rs | 423 ++++++----- starling/wit/p2/init.wit | 47 ++ 74 files changed, 4926 insertions(+), 1032 deletions(-) create mode 100644 componentize/tests/fixtures/cli_run_p2.wit create mode 100644 componentize/tests/fixtures/serve_p2.wit create mode 100644 componentize/tests/fixtures/serve_raw_p2.js create mode 100644 componentize/tests/fixtures/serve_raw_response_p2.js create mode 100644 componentize/tests/wit_to_ts/builtin_cli.p2.d.ts.expected create mode 100644 componentize/tests/wit_to_ts/builtin_serve.p2.d.ts.expected create mode 100644 componentize/tests/wit_to_ts/serve_p2.d.ts.expected create mode 100644 componentize/tests/wit_to_ts/serve_p2.handler.ts create mode 100644 componentize/tests/wit_to_ts/serve_p2.wit create mode 100644 crates/platform/src/http_p2.rs create mode 100644 crates/platform/src/http_p3.rs create mode 100644 crates/platform/src/task.rs create mode 100644 crates/platform/src/task_p2.rs create mode 100644 crates/platform/tests/task.rs create mode 100644 starling/wit/p2/init.wit diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c974a0c..6feef303 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,14 +115,20 @@ jobs: run: just ${{ matrix.recipe }} --examples # --------------------------------------------------------------------------- - # Componentization suites against the debug runtime (`just test-componentize`). - # That recipe builds the runtime for wasm32-wasip3, the target the componentizer - # links against, so this job installs beta and a wasi-sdk with a sysroot for it. + # Componentization suites against the debug runtime (`just test-componentize`), + # built for each wasm target (`RUNTIME_TARGET`). A wasm32-wasip3 build needs + # beta and a wasi-sdk with a sysroot for it, which the setup action installs. # --------------------------------------------------------------------------- componentize: - name: Componentize + name: Componentize (${{ matrix.target }}) + strategy: + fail-fast: false + matrix: + target: [wasm32-wasip2, wasm32-wasip3] runs-on: ubuntu-latest env: + RUNTIME_TARGET: ${{ matrix.target }} + WASM_TARGET: ${{ matrix.target }} WASMTIME_BACKTRACE_DETAILS: '1' RUST_BACKTRACE: 'full' steps: @@ -130,7 +136,7 @@ jobs: - uses: ./.github/actions/setup with: - target: wasm32-wasip3 + target: ${{ matrix.target }} - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: @@ -149,7 +155,7 @@ jobs: # Snapshots with `wizer-initialize`, including CLI tools whose `run` export a snapshot calls. - name: Test Wizer snapshots - run: WASM_TARGET=p3 just test-wizer + run: just test-wizer # --------------------------------------------------------------------------- # WPT suite: {native, wasm32-wasip2, wasm32-wasip3} @@ -205,8 +211,9 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - # `just check-gc` also checks the wasm32-wasip2 build, which needs the target's std and the - # WASI SDK, and runs crown's own tests. + # `just check-gc` also checks the wasm32-wasip2 and wasm32-wasip3 builds, which need the WASI + # SDK, and runs crown's own tests. Crown's toolchain, which rustup installs from + # `crown/rust-toolchain.toml`, brings both targets' std. - uses: ./.github/actions/setup with: target: wasm32-wasip2 diff --git a/Cargo.lock b/Cargo.lock index 9e6aa0bf..e01c821c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,12 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -1518,7 +1524,6 @@ dependencies = [ "tempfile", "tokio", "url", - "wasip3", "web-fetch", "web-globals", "web-streams", @@ -1959,6 +1964,7 @@ checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" name = "platform" version = "0.3.0" dependencies = [ + "async-task", "bytes", "futures-channel", "futures-lite", @@ -1966,6 +1972,7 @@ dependencies = [ "libc", "reqwest", "url", + "wasip2", "wasip3", "wit-bindgen", ] @@ -2546,7 +2553,9 @@ dependencies = [ "js", "libc", "libstarling", + "platform", "tempfile", + "wasip2", "wasip3", "web-fetch", "wit-bindgen", @@ -2935,6 +2944,15 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "2.0.1+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89aafd4b69fb41a64cfd5d7f214cde92ab422ec4d9bcd55fcc816d3f5f49bbf3" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "wasip3" version = "0.9.0+wasi-0.3.0" diff --git a/Cargo.toml b/Cargo.toml index c3a5c2bf..8dfddadb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ exclude = ["crown"] resolver = "2" [workspace.dependencies] +async-task = "4.7.1" bitflags = "2" bytes = "1" clap = { version = "4.5.60", features = ["derive"] } @@ -48,10 +49,13 @@ smallvec = "1.16" starling-macro = { path = "crates/starling-macro" } tokio = { version = "1", features = ["rt", "time", "net", "io-util"] } url = "2.5.8" +wasip2 = "2.0.1" wasip3 = { version = "0.9.0", features = ["http-compat"] } wasmparser = { version = "0.262", default-features = false, features = ["validate", "component-model"] } -# Must equal the `wit-bindgen` version `wasip3` depends on. See `starling/Cargo.toml`. -wit-bindgen = { version = "=0.62.0", features = ["macros"] } +# Must equal the `wit-bindgen` version `wasip2` and `wasip3` depend on. See `starling/Cargo.toml`. +# The async features are enabled only by the wasm32-wasip3 dependency tables, so a wasm32-wasip2 +# build uses none of the component model's async ABI. +wit-bindgen = { version = "=0.62.0", default-features = false, features = ["macros", "realloc", "std", "bitflags", "macro-string"] } wit-bridge = { path = "crates/wit-bridge" } wit-name-mapping = { path = "crates/wit-name-mapping" } wpt-support = { path = "crates/wpt-support" } diff --git a/README.md b/README.md index 104e3c17..ee4ab792 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ JavaScript application and a WIT world. - [Components that serve and run](#components-that-serve-and-run) - [Initialization](#initialization) - [Refused worlds](#refused-worlds) + - [WASIp2 runtimes](#wasip2-runtimes) - [Disabling WASI features](#disabling-wasi-features) - [The interfaces the runtime imports](#the-interfaces-the-runtime-imports) - [WIT values in JavaScript](#wit-values-in-javascript) @@ -415,6 +416,24 @@ Componentizing these worlds fails with an error naming the function or export: - a world that imports an interface at several compatible versions, from its own WIT or the runtime's, where a version lacks a type or function that a lower one declares. +### WASIp2 support + +StarlingMonkey has full WASIp2 support, enabling it to run on hosts that don't support WASIp3 +interfaces or the Component Model's async functions, `future`s, and `stream`s. + +Besides using WASIp2 APIs and `wasi:io` for streaming, a key implication is that such a build +doesn't support concurrent reuse, since incoming event handlers only return from the stack once +they have fully finished processing the event, including any post-return work, such as +body-streaming and promises passed to `waitUntil`. + +The componentizer's CLI options, such as `--export-http`, automatically use the interface version +the runtime targets. That is, a runtime built for `wasm32-wasip2` will export +`wasi:http/incoming-handler@0.2.0` with `--export-http` set. + +Trying to target a world with `async` functions, or using `stream` or `future`, will fail with a +`wasm32-wasip2` runtime. That limitation could be lifted, though it's advisable to use a P3 runtime +when targeting host environments that support WASIp3. + ### Disabling WASI features `--disable` takes a comma-separated list of `stdio`, `random`, `clocks`, `http` and `filesystem`, @@ -467,9 +486,11 @@ highest otherwise. Its types then have their plain names. A version that lacks a of a lower compatible version is [refused](#refused-worlds). A module specifier names an interface by its canonical version, as in `wasi:cli/environment@0.3`, -and `starling-componentize imports` and the -[TypeScript declarations](#typescript-declarations-for-a-world) name it that way. A specifier with -any compatible full version, such as `wasi:cli/environment@0.3.0`, resolves to the same module. +and `starling-componentize imports` and the [TypeScript +declarations](#typescript-declarations-for-a-world) name it that way. A specifier with any +compatible full version, such as `wasi:cli/environment@0.3.0`, resolves to the same module. A +runtime built for WASIp2 imports its WASI interfaces at `0.2` versions, so the same import is +spelled `wasi:cli/environment@0.2`. ### WIT values in JavaScript @@ -1189,7 +1210,7 @@ just check-all # more extensive tests, including GC checks `--workspace` by default. It accepts all additional arguments to `cargo test`, so to test specific packages, pass `-p [package name]`. -**For WebAssembly (WASIp2):** +**For WebAssembly:** ```bash just build-wasm # debug build for wasm32-wasip2 @@ -1197,6 +1218,10 @@ just test-wasm # all Rust tests, on wasm32-wasip2 just check-wasm # fmt-check + clippy + wasm tests ``` +A wasm32-wasip2 build imports and exports only WASI 0.2 interfaces and uses none of the Component +Model's async features. It runs on WASIp2 hosts, with the restrictions [WASIp2 +runtimes](#wasip2-runtimes) lists. A wasm32-wasip3 build imports and exports WASI 0.3 interfaces. + `WASM_TARGET` selects the wasm target of the recipes above, the wasm WPT recipes and `just test-wizer`, as `p2`, `p3` or a full triple. It defaults to `p2`: @@ -1218,8 +1243,8 @@ component `target/wasm32-wasip2/debug/starling.wasm` from the crate's `cdylib` t `target/wasm32-wasip3/release/starling.wasm`, with SpiderMonkey, libc and libc++ statically linked and dead-code eliminated. `just build-runtime-debug` builds it with `debugmozjs`, at `target/wasm32-wasip3/debug/starling.wasm`. `RUNTIME_TARGET` selects their target as `WASM_TARGET` -does, and defaults to `p3`. `RUNTIME_TARGET=p2` builds for wasm32-wasip2 from the stable toolchain -and an older SDK. +does, and defaults to `p3`. `RUNTIME_TARGET=p2` builds the runtime for WASIp2 hosts, from the +stable toolchain. The component runs scripts under `wasmtime run`, serves under `wasmtime serve`, and is the runtime `starling-componentize` links against. `just test-runtime` builds the debug component and runs two diff --git a/componentize/Cargo.toml b/componentize/Cargo.toml index 2c755e19..edcb78c1 100644 --- a/componentize/Cargo.toml +++ b/componentize/Cargo.toml @@ -59,7 +59,7 @@ mimalloc = "0.1.52" tokio = { version = "1", features = ["macros", "rt"] } proptest = "1.11" # `default-features = false` drops `default-send-request`, so no request reaches a real network. -wasmtime-wasi-http = { version = "50", features = ["p3"], default-features = false } +wasmtime-wasi-http = { version = "50", features = ["p2", "p3"], default-features = false } http = "1" http-body = "1" http-body-util = "0.1" diff --git a/componentize/src/command.rs b/componentize/src/command.rs index 509b5f6e..a6365e23 100644 --- a/componentize/src/command.rs +++ b/componentize/src/command.rs @@ -47,7 +47,8 @@ const CLI_WORLD_NAME: &str = "command"; /// The name of the world selected by `--export-http`, which exports /// `wasi:http/handler`. The interface is implemented by the application, or by /// the runtime, which dispatches each request to the `fetch` listeners that the -/// application added while it was initialized. +/// application added while it was initialized. A runtime built for WASIp2 +/// exports `wasi:http/incoming-handler` instead. const SERVE_WORLD_NAME: &str = "serve"; /// The WIT sources of the worlds selected by `--export-cli` and @@ -81,7 +82,11 @@ impl BuiltinWorlds { serve: builtin_world_wit( "starling:serve", SERVE_WORLD_NAME, - &interface(crate::HTTP_HANDLER_INTERFACE_PREFIX)?, + &interface( + runtime + .map_or(crate::WasiVersion::P3, crate::Runtime::wasi_version) + .http_handler_prefix(), + )?, ), }) } diff --git a/componentize/src/finalize.rs b/componentize/src/finalize.rs index a6491609..8a0577f3 100644 --- a/componentize/src/finalize.rs +++ b/componentize/src/finalize.rs @@ -467,12 +467,14 @@ enum StubBody { } /// The body of the stub of `func` in the interface named `interface`: zeroes -/// for a clock's `now`, the seed for `get-insecure-seed` if `insecure_seed` is -/// set, and a trap for everything else. +/// for a clock's `now`, the seed for `get-insecure-seed` (`insecure-seed` in +/// WASI 0.2) if `insecure_seed` is set, and a trap for everything else. fn stub_body(interface: &str, func: &Function, insecure_seed: Option) -> StubBody { match (func.name.as_str(), insecure_seed) { ("now", _) if interface.starts_with("wasi:clocks/") => StubBody::Zero, - ("get-insecure-seed", Some(seed)) if interface.starts_with(INSECURE_SEED_INTERFACE) => { + ("get-insecure-seed" | "insecure-seed", Some(seed)) + if interface.starts_with(INSECURE_SEED_INTERFACE) => + { StubBody::Seed(seed) } _ => StubBody::Trap, diff --git a/componentize/src/lib.rs b/componentize/src/lib.rs index 752e9449..13618d6f 100644 --- a/componentize/src/lib.rs +++ b/componentize/src/lib.rs @@ -30,7 +30,8 @@ use { wit_parser::{Resolve, WorldId, WorldKey}, }; -/// Host bindings of the runtime's `init` interface, `starling/wit/init.wit`. +/// Host bindings of the `init` interface of a runtime built for WASIp3, +/// `starling/wit/init.wit`. mod bindings { wasmtime::component::bindgen!({ path: "../starling/wit/init.wit", @@ -39,6 +40,16 @@ mod bindings { }); } +/// Host bindings of the `init` interface of a runtime built for WASIp2, +/// `starling/wit/p2/init.wit`. +mod bindings_p2 { + wasmtime::component::bindgen!({ + path: "../starling/wit/p2/init.wit", + world: "runtime", + exports: { default: async }, + }); +} + /// `starling/wit/init.wit`, which declares the interface between the runtime /// and the componentizer. const INIT_WIT: &str = include_str!("../../starling/wit/init.wit"); @@ -127,6 +138,26 @@ pub enum Wit<'a, P = PathBuf> { /// sections. pub struct Runtime { module: Vec, + wasi_version: WasiVersion, +} + +/// The WASI version a [`Runtime`] was built for. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum WasiVersion { + /// WASIp2: a runtime that imports and exports `0.2` interfaces and has no async functions. + P2, + /// WASIp3: a runtime that imports and exports `0.3` interfaces. + P3, +} + +impl WasiVersion { + /// The interface of the runtime's builtin HTTP handler export, without a version. + pub(crate) fn http_handler_prefix(self) -> &'static str { + match self { + WasiVersion::P2 => HTTP_INCOMING_HANDLER_INTERFACE_PREFIX, + WasiVersion::P3 => HTTP_HANDLER_INTERFACE_PREFIX, + } + } } impl Runtime { @@ -140,7 +171,7 @@ impl Runtime { /// a component has no such copies. pub fn new(bytes: Vec) -> anyhow::Result { if !wasmparser::Parser::is_component(&bytes) { - return Ok(Runtime { module: bytes }); + return Runtime::from_module(bytes); } let mut main: Option<&[u8]> = None; for payload in wasmparser::Parser::new(0).parse_all(&bytes) { @@ -164,7 +195,28 @@ impl Runtime { preserve_component_type::PREFIX ); } - Ok(Runtime { module }) + Runtime::from_module(module) + } + + /// The runtime whose core module is `module`. + /// + /// Its WASI version is that of its builtin `wasi:cli/run` export: WASIp2 for a `0.2` version + /// of the interface, WASIp3 otherwise. + fn from_module(module: Vec) -> anyhow::Result { + let cli_run = find_builtin_export(&module, CLI_RUN_INTERFACE_PREFIX)?; + let wasi_version = match cli_run { + Some((_, _, name)) if same_interface(&name, "wasi:cli/run@0.2.0") => WasiVersion::P2, + _ => WasiVersion::P3, + }; + Ok(Runtime { + module, + wasi_version, + }) + } + + /// The WASI version the runtime was built for. + pub fn wasi_version(&self) -> WasiVersion { + self.wasi_version } /// The runtime's core module. @@ -183,15 +235,20 @@ impl Runtime { pub(crate) const CLI_RUN_INTERFACE_PREFIX: &str = "wasi:cli/run"; /// Like [`CLI_RUN_INTERFACE_PREFIX`], for the runtime's own `wasi:http/handler` -/// export. The application can also implement the interface itself (see -/// [`RAW_HTTP_HANDLER_EXPORT`]). +/// export, which a runtime built for WASIp3 has. The application can also +/// implement the interface itself (see [`RAW_HTTP_HANDLER_EXPORT`]). pub(crate) const HTTP_HANDLER_INTERFACE_PREFIX: &str = "wasi:http/handler"; -/// The name of a second export of the runtime's `wasi:http/handler` interface, -/// which is added to a world that exports `wasi:http/handler`, so that the -/// application can implement the interface itself. The main module exports its -/// `handle` under the names for `wasi:http/handler`: `wasiHttp.handler.handle`, -/// `handler.handle` or `handle`. +/// Like [`HTTP_HANDLER_INTERFACE_PREFIX`], for the runtime's own +/// `wasi:http/incoming-handler` export, which a runtime built for WASIp2 has. +pub(crate) const HTTP_INCOMING_HANDLER_INTERFACE_PREFIX: &str = "wasi:http/incoming-handler"; + +/// The name of a second export of the runtime's HTTP handler interface (see +/// [`WasiVersion::http_handler_prefix`]), which is added to a world that exports +/// that interface, so that the application can implement the interface itself. +/// The main module exports its `handle` under the names for the interface, such +/// as `wasiHttp.handler.handle`, `handler.handle` or `handle` for +/// `wasi:http/handler`. pub(crate) const RAW_HTTP_HANDLER_EXPORT: &str = "wasi-http-handler"; /// The worlds selected by [`load_world`]: the worlds in `worlds`, or the default @@ -434,6 +491,8 @@ pub fn runtime_imports_world( /// is replaced as in [`componentize`], by an export named /// `RAW_HTTP_HANDLER_EXPORT` with the runtime's declaration of the interface. /// The declarations then describe the `handle` implemented by the application. +/// For a runtime built for WASIp2, the same applies to a world's +/// `wasi:http/incoming-handler` export. pub fn load_world_for_types<'a>( wit: Wit<'_, impl AsRef>, world: impl Into>, @@ -442,19 +501,29 @@ pub fn load_world_for_types<'a>( runtime: Option<&Runtime>, ) -> anyhow::Result<(Resolve, WorldId)> { let (mut resolve, world) = load_world(wit, world, features, all_features)?; + if runtime.is_some_and(|runtime| runtime.wasi_version() == WasiVersion::P2) { + supported::check_wasip2_types(&resolve, world)?; + } supported::check_supported_types(&resolve, world)?; let Some(runtime) = runtime else { return canonicalize_compatible_versions(resolve, world); }; - let Some(removed) = remove_world_export(&mut resolve, world, HTTP_HANDLER_INTERFACE_PREFIX) + let prefix = runtime.wasi_version().http_handler_prefix(); + let Some(removed) = remove_world_export(&mut resolve, world, prefix) .into_iter() .next() else { return canonicalize_compatible_versions(resolve, world); }; - match builtin_export(runtime.module(), HTTP_HANDLER_INTERFACE_PREFIX)? { + match builtin_export(runtime.module(), prefix)? { Some((builtin, name)) if same_interface(&name, &removed) => { - add_raw_http_handler_export(&mut resolve, world, builtin, &name)?; + add_raw_http_handler_export( + &mut resolve, + world, + builtin, + &name, + runtime.wasi_version(), + )?; canonicalize_compatible_versions(resolve, world) } builtin => anyhow::bail!( @@ -543,7 +612,11 @@ pub async fn componentize( cache, } = *options; check_runtime_interface(runtime)?; + let wasi_version = runtime.wasi_version(); let (mut resolve, world) = load_world(wit, world, features, all_features)?; + if wasi_version == WasiVersion::P2 { + supported::check_wasip2_types(&resolve, world)?; + } supported::check_supported_types(&resolve, world)?; supported::reject_world_level_borrows(&resolve, world)?; check_export_names(&resolve, world)?; @@ -556,14 +629,15 @@ pub async fn componentize( .map(finalize::KeptExport::unrenamed) .collect(); - // The world's name for its `wasi:http/handler` export, if it has one. + // The world's name for its HTTP handler export, if it has one. let mut http_handler = None; let mut cli = false; - // A world's `wasi:cli/run` and `wasi:http/handler` exports are implemented - // by the runtime's own exports. They are removed from the world before the + // A world's `wasi:cli/run` and HTTP handler exports are implemented by the + // runtime's own exports. They are removed from the world before the // bindings and the metadata section below are generated, because they would // otherwise be duplicate exports. - for prefix in [CLI_RUN_INTERFACE_PREFIX, HTTP_HANDLER_INTERFACE_PREFIX] { + let http_handler_prefix = wasi_version.http_handler_prefix(); + for prefix in [CLI_RUN_INTERFACE_PREFIX, http_handler_prefix] { if let Some(removed) = remove_world_export(&mut resolve, world, prefix).first() { cli |= prefix == CLI_RUN_INTERFACE_PREFIX; // The output component exports the runtime's builtin, so the world @@ -588,8 +662,8 @@ pub async fn componentize( } } } - if let (HTTP_HANDLER_INTERFACE_PREFIX, Some((builtin, name))) = (prefix, builtin) { - add_raw_http_handler_export(&mut resolve, world, builtin, &name)?; + if let (true, Some((builtin, name))) = (prefix == http_handler_prefix, builtin) { + add_raw_http_handler_export(&mut resolve, world, builtin, &name, wasi_version)?; http_handler = Some(removed.clone()); } } @@ -670,7 +744,10 @@ pub async fn componentize( let engine = Engine::new(&config)?; let mut store = Store::new(&engine, Ctx { wasi, table }); - let wizer = Wizer::new(); + let mut wizer = Wizer::new(); + // The runtime's `wizer-initialize` export stays in the component's type, so its core function + // is kept. The function isn't called: `init` initializes the instance. + wizer.keep_init_func(true); let (cx, component) = wizer.instrument_component(&component)?; let component = Component::new(&engine, &component)?; @@ -687,7 +764,6 @@ pub async fn componentize( let instance = linker.instantiate_async(&mut store, &component).await?; let init_output; { - let instance = bindings::Runtime::new(&mut store, &instance)?; // `init` builds the import modules from the wit-dylib `Wit` metadata, so // no module sources are passed to it. It reads the application's own // dependency modules through the preopened `base_directory`, and the @@ -695,25 +771,47 @@ pub async fn componentize( let raw_http_handler = http_handler .as_ref() .map(|_| RAW_HTTP_HANDLER_EXPORT.to_string()); - // `init` is an async export, so it runs as a concurrent task, which - // lets the application's top-level `await` complete. - let result = store - .run_concurrent(async |accessor| { + let init_location = init_location.map(str::to_string); + let result = match wasi_version { + // `init` is an async export, so it runs as a concurrent task, which + // lets the application's top-level `await` complete. + WasiVersion::P3 => { + let instance = bindings::Runtime::new(&mut store, &instance)?; + store + .run_concurrent(async |accessor| { + instance + .starling_componentize_init() + .call_init( + accessor, + js.text.to_string(), + js.name.to_string(), + main_path, + cli, + raw_http_handler, + init_location, + ) + .await + }) + .await + .and_then(|call| call) + } + // `init` blocks until the application's top-level `await` has settled. + WasiVersion::P2 => { + let instance = bindings_p2::Runtime::new(&mut store, &instance)?; instance .starling_componentize_init() .call_init( - accessor, - js.text.to_string(), - js.name.to_string(), - main_path, + &mut store, + js.text, + js.name, + main_path.as_deref(), cli, - raw_http_handler, - init_location.map(str::to_string), + raw_http_handler.as_deref(), + init_location.as_deref(), ) .await - }) - .await - .and_then(|call| call); + } + }; let printed = format!( "{}{}", String::from_utf8_lossy(&stdout.contents()), @@ -821,8 +919,9 @@ impl<'a> From<&'a str> for JsSource<'a> { } /// Add the export [`RAW_HTTP_HANDLER_EXPORT`] to `world`: an interface with the -/// same types and function as the runtime's builtin `wasi:http/handler`, which -/// `builtin` exports as `handler`. +/// same types and function as the runtime's builtin HTTP handler, which +/// `builtin` exports as `handler`: `wasi:http/handler` for a runtime built for +/// WASIp3, `wasi:http/incoming-handler` for one built for WASIp2. /// /// `builtin`'s packages are merged into `resolve` first, so that the export uses /// the `wasi:http/types` resources imported by the runtime. If `resolve` @@ -833,36 +932,44 @@ fn add_raw_http_handler_export( world: WorldId, builtin: Resolve, handler: &str, + wasi_version: WasiVersion, ) -> anyhow::Result<()> { resolve.merge(builtin).with_context(|| { format!( "merging the runtime's `{handler}` into the world. A world declaring \ - `{HTTP_HANDLER_INTERFACE_PREFIX}` must declare it as the runtime does, or leave \ - the interface empty." + `{}` must declare it as the runtime does, or leave the interface empty.", + wasi_version.http_handler_prefix() ) })?; let version = handler .split_once('@') .map(|(_, version)| format!("@{version}")) .unwrap_or_default(); + let interface = match wasi_version { + WasiVersion::P2 => format!( + "use wasi:http/types{version}.{{incoming-request, response-outparam}};\n\ + handle: func(request: incoming-request, response-out: response-outparam);\n" + ), + WasiVersion::P3 => format!( + "use wasi:http/types{version}.{{request, response, error-code}};\n\ + handle: async func(request: request) -> result;\n" + ), + }; let raw = resolve .push_str( "raw-http-handler.wit", &format!( "package starling:raw-http-handler;\n\ world raw {{\n\ - export {RAW_HTTP_HANDLER_EXPORT}: interface {{\n\ - use wasi:http/types{version}.{{request, response, error-code}};\n\ - handle: async func(request: request) -> result;\n\ - }}\n\ + export {RAW_HTTP_HANDLER_EXPORT}: interface {{\n{interface}}}\n\ }}\n" ), ) - .context("declaring the raw `wasi:http/handler` export")?; + .context("declaring the raw HTTP handler export")?; let raw = resolve.select_world(&[raw], Some("raw"))?; resolve .merge_worlds(raw, world, &mut wit_parser::CloneMaps::default()) - .context("adding the raw `wasi:http/handler` export to the world") + .context("adding the raw HTTP handler export to the world") } /// Remove from `world` every export of the interface `prefix` at any version, diff --git a/componentize/src/supported.rs b/componentize/src/supported.rs index 3a71bfa6..981758de 100644 --- a/componentize/src/supported.rs +++ b/componentize/src/supported.rs @@ -33,6 +33,28 @@ pub(crate) fn check_supported_types(resolve: &Resolve, world: WorldId) -> anyhow Ok(()) } +/// Fail if any function the world imports or exports is `async`, or takes or +/// returns a `stream` or `future`, naming the first such function. A runtime +/// built for WASIp2 can't lift or lower any of them. +pub(crate) fn check_wasip2_types(resolve: &Resolve, world: WorldId) -> anyhow::Result<()> { + let world = &resolve.worlds[world]; + for (name, func) in functions(resolve, world.imports.iter().chain(world.exports.iter())) { + if func.kind.is_async() { + bail!( + "`{name}` is an `async` function, which needs a runtime built for WASIp3. \ + This runtime was built for WASIp2." + ); + } + if types(func).any(|ty| has_stream_or_future(resolve, ty)) { + bail!( + "`{name}` takes or returns a `stream` or `future`, which need a runtime built for \ + WASIp3. This runtime was built for WASIp2." + ); + } + } + Ok(()) +} + /// The functions of the world items `items`, each with its name in error /// messages: `#`, or the function's own name for a /// world-level function. diff --git a/componentize/src/versions.rs b/componentize/src/versions.rs index 37f4b071..2a69bb04 100644 --- a/componentize/src/versions.rs +++ b/componentize/src/versions.rs @@ -1,7 +1,8 @@ // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception //! Canonicalizing the interfaces a world imports at several compatible versions, such as -//! `wasi:io/poll@0.2.0` and `wasi:io/poll@0.2.12` (see [`same_interface`]). +//! `wasi:io/poll@0.2.0` and `wasi:io/poll@0.2.12` (see [`same_interface`]). A runtime built for +//! WASIp2 imports such versions from the bindings of its different dependencies. use anyhow::Context as _; use wit_name_mapping::same_interface; diff --git a/componentize/src/wit_to_ts.rs b/componentize/src/wit_to_ts.rs index f1c6092d..a2f464a4 100644 --- a/componentize/src/wit_to_ts.rs +++ b/componentize/src/wit_to_ts.rs @@ -18,8 +18,9 @@ //! WIT names. These match the modules created by //! `wit_bridge::imports::synthesize_import_modules`. //! -//! A world exporting `wasi:http/handler` also gets file-level declarations for -//! the `fetch` event the runtime dispatches to listeners. +//! A world exporting `wasi:http/handler` or `wasi:http/incoming-handler` also +//! gets file-level declarations for the `fetch` event the runtime dispatches to +//! listeners. //! //! The TypeScript types follow the conversions in `wit_bridge::value`. //! Each rendering function names the conversion functions it matches. @@ -58,11 +59,12 @@ declare var ComponentError: { "; -/// The globals the runtime provides to a world exporting `wasi:http/handler`, -/// which the DOM library lacks. Declared by interface merging only, so the file -/// also compiles against the `webworker` library, which declares `FetchEvent` -/// itself. Adding `fetch` to `WindowEventMap` types the event of an -/// `addEventListener('fetch', …)` listener. +/// The globals the runtime provides to a world exporting `wasi:http/handler` or +/// `wasi:http/incoming-handler`, which the DOM library lacks. Declared by +/// interface merging only, so the file also compiles against the `webworker` +/// library, which declares `FetchEvent` itself. Adding `fetch` to +/// `WindowEventMap` types the event of an `addEventListener('fetch', …)` +/// listener. const FETCH_EVENT_DECL: &str = "\ // The event the runtime dispatches to `addEventListener('fetch', …)` listeners. interface FetchEvent extends Event { @@ -76,10 +78,13 @@ interface WindowEventMap { "; -/// The `wasi:http/types` interface whose `request` resource is accepted by the -/// runtime's `fetch`. Versions for which [`same_interface`] returns `true` are -/// accepted as well. -const WASI_HTTP_TYPES: &str = "wasi:http/types@0.3.0"; +/// The `wasi:http/types` interfaces whose request resources are accepted by the +/// runtime's `fetch`, each with the name of its request resource. Versions for +/// which [`same_interface`] returns `true` are accepted as well. +const WASI_HTTP_REQUESTS: [(&str, &str); 2] = [ + ("wasi:http/types@0.3.0", "request"), + ("wasi:http/types@0.2.0", "incoming-request"), +]; /// The specifier of the module that declares the JavaScript module's exports. /// @@ -444,10 +449,16 @@ impl<'a> Generator<'a> { } if let Some(request) = self.wit_request_type() { let name = self.decl_name(Decl::Type(request, Side::Import)); + let wit_name = self.type_name(request); + let handler = if wit_name == "incoming-request" { + crate::HTTP_INCOMING_HANDLER_INTERFACE_PREFIX + } else { + crate::HTTP_HANDLER_INTERFACE_PREFIX + }; let _ = write!( out, - "// `fetch` also takes a `wasi:http/types` `request`, such as the one a\n\ - // `wasi:http/handler` implementation receives, and sends it.\n\ + "// `fetch` also takes a `wasi:http/types` `{wit_name}`, such as the one a\n\ + // `{handler}` implementation receives, and sends it.\n\ declare function fetch(\n \ input: import('{types}').{name},\n \ init?: RequestInit,\n\ @@ -1259,16 +1270,16 @@ impl<'a> Generator<'a> { " It fails with an `error-code` by throwing an `ErrorCode` from `{types}`." ); } + let interface = crate::unversioned(&group.names.wit_name); let mut out = format!( - "// The `{handler}` export is served in one of two ways. Either\n\ + "// The `{interface}` export is served in one of two ways. Either\n\ // register a listener on the global, and the runtime dispatches each\n\ // request to it as a `FetchEvent`:\n\ //\n\ // addEventListener('fetch', (event) => {{\n\ // event.respondWith(new Response('hello'));\n\ // }});\n\ - //", - handler = crate::HTTP_HANDLER_INTERFACE_PREFIX, + //" ); // The paragraph, wrapped to lines of at most 76 characters after `// `. let mut line = String::new(); @@ -1352,19 +1363,21 @@ impl<'a> Generator<'a> { format!("(request: {request}): {returned} | Promise<{returned}>") } - /// The `request` resource of [`WASI_HTTP_TYPES`], or of an interface for - /// which [`same_interface`] returns `true`, if the world imports it and the - /// declarations name it. + /// The request resource of an interface in [`WASI_HTTP_REQUESTS`], or of an + /// interface for which [`same_interface`] returns `true`, if the world + /// imports it and the declarations name it. fn wit_request_type(&self) -> Option { let id = self .world() .imports .iter() .find_map(|(key, item)| match item { - WorldItem::Interface { id, .. } - if same_interface(&self.resolve.name_world_key(key), WASI_HTTP_TYPES) => - { - self.resolve.interfaces[*id].types.get("request").copied() + WorldItem::Interface { id, .. } => { + let name = self.resolve.name_world_key(key); + let (_, resource) = WASI_HTTP_REQUESTS + .iter() + .find(|(types, _)| same_interface(&name, types))?; + self.resolve.interfaces[*id].types.get(*resource).copied() } _ => None, })?; @@ -1374,9 +1387,28 @@ impl<'a> Generator<'a> { .then_some(id) } - /// The `ok` type of the WIT `handle`'s `result`, if it is a resource. + /// The `ok` type of the WIT `handle`'s `result`, if it is a resource. A + /// WASIp2 `handle` has no result, so on p2, this is the `outgoing-response` + /// of the interface that defines its `response-outparam` parameter. fn http_response_type(&self, func: Option<&Function>) -> Option { - let (ok, _) = self.as_result(&func?.result?)?; + let func = func?; + let Some(result) = &func.result else { + let Type::Id(outparam) = func.params.get(1)?.ty else { + return None; + }; + let outparam = match self.resolve.types[self.canonical(outparam)].kind { + TypeDefKind::Handle(Handle::Own(resource)) => self.canonical(resource), + _ => self.canonical(outparam), + }; + let TypeOwner::Interface(types) = self.resolve.types[outparam].owner else { + return None; + }; + let response = self.resolve.interfaces[types] + .types + .get("outgoing-response")?; + return Some(Type::Id(*response)); + }; + let (ok, _) = self.as_result(result)?; let Some(Type::Id(id)) = ok else { return None; }; @@ -2157,8 +2189,8 @@ struct ExportGroup<'a> { /// Whether this is the `wasi:cli/run` export, which has no items and makes /// the guest export a bare `run`. cli_run: bool, - /// Whether this is the `wasi:http/handler` export, whose one item is - /// `handle`. + /// Whether this is the HTTP handler export, `wasi:http/handler` or + /// `wasi:http/incoming-handler`, whose one item is `handle`. http_handler: bool, } @@ -2190,11 +2222,12 @@ fn world_export_functions(world: &World) -> impl Iterator { /// [`InterfaceNames::of`]) and items. /// /// Its items are its freestanding functions and the resources it declares a -/// constructor, method or static function of. Two interfaces are +/// constructor, method or static function of. Two kinds of interface are /// special-cased. `wasi:cli/run` has no items, since the guest exports a bare -/// `run`. `wasi:http/handler`, which the componentizer exports as the -/// plain-named interface [`RAW_HTTP_HANDLER_EXPORT`](crate::RAW_HTTP_HANDLER_EXPORT), -/// keeps the names of `wasi:http/handler` and has the one item `handle`. +/// `run`. The HTTP handler, `wasi:http/handler` or `wasi:http/incoming-handler`, +/// which the componentizer exports as the plain-named interface +/// [`RAW_HTTP_HANDLER_EXPORT`](crate::RAW_HTTP_HANDLER_EXPORT), keeps the names of +/// its interface and has the one item `handle`. fn export_groups(resolve: &Resolve, world: WorldId) -> Vec> { let mut groups = Vec::new(); for (key, item) in &resolve.worlds[world].exports { @@ -2213,12 +2246,20 @@ fn export_groups(resolve: &Resolve, world: WorldId) -> Vec> { continue; } if names_interface(&wit_name, crate::HTTP_HANDLER_INTERFACE_PREFIX) + || names_interface(&wit_name, crate::HTTP_INCOMING_HANDLER_INTERFACE_PREFIX) || wit_name == crate::RAW_HTTP_HANDLER_EXPORT { let handle = iface.functions.get("handle"); - // Comments name the raw export by its interface, `wasi:http/handler`. + // Comments name the raw export by its interface: `wasi:http/incoming-handler` for the + // WASIp2 export, whose `handle` takes a `response-outparam` and has no result, and + // `wasi:http/handler` otherwise. let wit_name = if wit_name == crate::RAW_HTTP_HANDLER_EXPORT { - crate::HTTP_HANDLER_INTERFACE_PREFIX.to_string() + match handle { + Some(handle) if handle.params.len() == 2 && handle.result.is_none() => { + crate::HTTP_INCOMING_HANDLER_INTERFACE_PREFIX.to_string() + } + _ => crate::HTTP_HANDLER_INTERFACE_PREFIX.to_string(), + } } else { wit_name }; diff --git a/componentize/tests/async_export.rs b/componentize/tests/async_export.rs index c5c5c89b..067cb903 100644 --- a/componentize/tests/async_export.rs +++ b/componentize/tests/async_export.rs @@ -69,6 +69,9 @@ fn store() -> Store { /// on the same instance afterwards still complete. #[tokio::test] async fn perpetual_trailing_work_does_not_hang() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let result = tokio::time::timeout( @@ -115,6 +118,9 @@ async fn perpetual_trailing_work_does_not_hang() -> anyhow::Result<()> { /// export, is printed to stderr. #[tokio::test] async fn deadlocked_async_export_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = shared::store_capturing_stderr(&ENGINE); let instance = pre().await.instantiate_async(&mut store).await?; let result = store @@ -145,6 +151,9 @@ async fn deadlocked_async_export_traps() -> anyhow::Result<()> { /// as unhandled. #[tokio::test] async fn rejected_async_export_returns_err() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = shared::store_capturing_stderr(&ENGINE); let instance = pre().await.instantiate_async(&mut store).await?; let result = store @@ -164,6 +173,9 @@ async fn rejected_async_export_returns_err() -> anyhow::Result<()> { /// `setTimeout` in a sync export throws a `TypeError` naming the export. #[tokio::test] async fn timers_in_a_sync_export_throw() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let message = instance @@ -182,6 +194,9 @@ async fn timers_in_a_sync_export_throw() -> anyhow::Result<()> { /// the export. #[tokio::test] async fn fetch_in_a_sync_export_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let simple = instance.test_async_export_simple_export(); @@ -198,6 +213,9 @@ async fn fetch_in_a_sync_export_rejects() -> anyhow::Result<()> { /// The microtasks a sync export queues run before the call returns. #[tokio::test] async fn sync_export_microtasks_run_before_it_returns() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let simple = instance.test_async_export_simple_export(); @@ -212,6 +230,9 @@ async fn sync_export_microtasks_run_before_it_returns() -> anyhow::Result<()> { /// they return, and `setTimeout` throws in them. #[tokio::test] async fn sync_exports_run_while_an_async_export_is_parked() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre() .await @@ -262,3 +283,23 @@ async fn sync_exports_run_while_an_async_export_is_parked() -> anyhow::Result<() ); Ok(()) } + +/// A runtime built for WASIp2 refuses the world, naming its first `async` function. +#[tokio::test] +async fn async_exports_need_a_wasip3_runtime() { + if shared::wasi_version() != componentize::WasiVersion::P2 { + return; + } + let error = shared::try_componentize( + include_str!("fixtures/async_export.wit"), + "async-export", + include_str!("fixtures/async_export.js"), + ) + .await + .expect_err("an `async` export needs a WASIp3 runtime"); + let message = format!("{error:#}"); + assert!( + message.contains("is an `async` function, which needs a runtime built for WASIp3"), + "{message}" + ); +} diff --git a/componentize/tests/async_import.rs b/componentize/tests/async_import.rs index 183a515a..6709639b 100644 --- a/componentize/tests/async_import.rs +++ b/componentize/tests/async_import.rs @@ -71,6 +71,9 @@ fn store() -> Store { /// which yields and returns 44, `+ 3` => 47. #[tokio::test] async fn async_import_and_export() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let result = store @@ -95,6 +98,9 @@ async fn async_import_and_export() -> anyhow::Result<()> { /// rejects the guest's promise with a `ComponentError`, `+ 3` => `err(48)`. #[tokio::test] async fn async_import_and_export_result() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let ok = store @@ -123,6 +129,9 @@ async fn async_import_and_export_result() -> anyhow::Result<()> { /// `TypeError` naming the export, rather than throwing. #[tokio::test] async fn async_import_in_a_sync_export_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let runner = instance.test_async_import_runner(); diff --git a/componentize/tests/cli_run.rs b/componentize/tests/cli_run.rs index e509ecc8..a8d818ea 100644 --- a/componentize/tests/cli_run.rs +++ b/componentize/tests/cli_run.rs @@ -1,7 +1,8 @@ // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception //! `wasi:cli/run` components: JS modules that export `run`, componentized against the world of -//! `fixtures/cli_run.wit` and run through `wasi:cli/run.run()`. +//! `fixtures/cli_run.wit`, or `fixtures/cli_run_p2.wit` for a runtime built for WASIp2, and run +//! through `wasi:cli/run.run()`. mod shared; @@ -14,7 +15,6 @@ use { }, wasmtime_wasi::{ p2::pipe::{MemoryInputPipe, MemoryOutputPipe}, - p3::bindings::Command, WasiCtxBuilder, WasiCtxView, WasiView, }, }; @@ -35,11 +35,25 @@ impl WasiView for Ctx { static ENGINE: std::sync::LazyLock = std::sync::LazyLock::new(shared::engine); -const WIT: &str = include_str!("fixtures/cli_run.wit"); +/// The CLI world for the runtime's WASI version. +fn wit() -> &'static str { + match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/cli_run_p2.wit"), + componentize::WasiVersion::P3 => include_str!("fixtures/cli_run.wit"), + } +} + +/// The name under which a component built for [`wit`] exports `wasi:cli/run`. +fn cli_run_export() -> &'static str { + match shared::wasi_version() { + componentize::WasiVersion::P2 => "wasi:cli/run@0.2.5", + componentize::WasiVersion::P3 => "wasi:cli/run@0.3.5", + } +} /// Componentize a CLI application against the fixed CLI world and compile it. async fn componentize_cli(js: &'static str) -> Component { - shared::compile_fixture(&ENGINE, WIT, "command", js).await + shared::compile_fixture(&ENGINE, wit(), "command", js).await } /// The componentized CLI app that exports `run`, built once. @@ -106,10 +120,7 @@ fn linker(component: &Component) -> Linker { /// the captured stdout/stderr. async fn run_cli(component: &Component) -> anyhow::Result<(Result<(), ()>, String, String)> { let mut store = store(); - let command = Command::instantiate_async(&mut store, component, &linker(component)).await?; - let result = store - .run_concurrent(async move |store| command.wasi_cli_run().call_run(store).await) - .await??; + let result = shared::call_cli_run(&mut store, component, &linker(component)).await?; let stdout = String::from_utf8_lossy(&store.data().stdout.contents()).into_owned(); let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned(); Ok((result, stdout, stderr)) @@ -130,7 +141,7 @@ async fn cli_run_forwards_to_js_run() -> anyhow::Result<()> { .map(|(name, _)| name.to_string()) .collect(); assert!( - exports.contains(&"wasi:cli/run@0.3.5".to_string()), + exports.contains(&cli_run_export().to_string()), "{exports:?}" ); let (result, stdout, _stderr) = run_cli(run_exporting_app().await).await?; @@ -179,7 +190,7 @@ async fn cli_run_forwards_to_js_run() -> anyhow::Result<()> { #[tokio::test] async fn cli_run_without_run_export_fails_componentization() { let error = - shared::try_componentize(WIT, "command", include_str!("fixtures/cli_run_no_run.js")) + shared::try_componentize(wit(), "command", include_str!("fixtures/cli_run_no_run.js")) .await .expect_err("an app without `run` must not componentize as a CLI tool"); let message = format!("{error:#}"); @@ -194,27 +205,25 @@ async fn cli_run_without_run_export_fails_componentization() { /// the builtin's version. #[tokio::test] async fn cli_run_version_mismatch_is_rejected() { - const OLD_WIT: &str = "\ -package test:cli-run-old; - -world command { - export wasi:cli/run@0.2.3; -} - -package wasi:cli@0.2.3 { - interface run { - run: func() -> result; - } -} -"; + // A synchronous `run` at a version neither runtime provides, and the start of the name the + // runtime gives its builtin. + let (version, builtin) = match shared::wasi_version() { + componentize::WasiVersion::P2 => ("0.1.3", "wasi:cli/run@0.2."), + componentize::WasiVersion::P3 => ("0.2.3", "wasi:cli/run@0.3.0"), + }; + let old_wit = format!( + "package test:cli-run-old;\n\n\ + world command {{\n export wasi:cli/run@{version};\n}}\n\n\ + package wasi:cli@{version} {{\n interface run {{\n run: func() -> result;\n }}\n}}\n" + ); - let error = shared::try_componentize(OLD_WIT, "command", include_str!("fixtures/cli_run.js")) + let error = shared::try_componentize(&old_wit, "command", include_str!("fixtures/cli_run.js")) .await .expect_err("a mismatched cli/run version should be rejected"); let message = format!("{error:#}"); assert!( - message.contains("wasi:cli/run@0.2.3") && message.contains("wasi:cli/run@0.3.0"), + message.contains(&format!("wasi:cli/run@{version}")) && message.contains(builtin), "the error should name both the world's version and the runtime's; got: {message}" ); } diff --git a/componentize/tests/exported_resource.rs b/componentize/tests/exported_resource.rs index 0f657d08..effbe0cb 100644 --- a/componentize/tests/exported_resource.rs +++ b/componentize/tests/exported_resource.rs @@ -4,6 +4,9 @@ //! `counters.counter`. The host calls the methods and statics of counters the guest constructs, //! and the `live-count` and `dispose-calls` statics observe the `[Symbol.dispose]()` a host-side //! drop runs. +//! +//! For a runtime built for WASIp3, the component is built from `exported-resource-async`, which +//! adds the `async` export `delayed.read-later`. mod shared; @@ -29,10 +32,14 @@ static ENGINE: LazyLock = LazyLock::new(shared::engine); async fn pre() -> &'static ExportedResourcePre { static PRE: OnceCell, String>> = OnceCell::const_new(); shared::build_once(&PRE, async { + let world = match shared::wasi_version() { + componentize::WasiVersion::P2 => "exported-resource", + componentize::WasiVersion::P3 => "exported-resource-async", + }; let component = shared::compile_fixture( &ENGINE, include_str!("fixtures/exported_resource.wit"), - "exported-resource", + world, include_str!("fixtures/exported_resource.js"), ) .await; @@ -125,18 +132,30 @@ async fn non_extensible_object_backs_a_resource() -> anyhow::Result<()> { /// borrow ending leaves the resource to its owner. #[tokio::test] async fn async_export_reads_a_borrow_after_a_timer() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); - let instance = pre().await.instantiate_async(&mut store).await?; + let component_instance = pre() + .await + .instance_pre() + .instantiate_async(&mut store) + .await?; + let instance = ExportedResource::new(&mut store, &component_instance)?; let counters = instance.test_exported_resource_counters(); let counter = counters.call_make_counter(&mut store, 10).await?; - let value = store - .run_concurrent(async |accessor| { - instance - .test_exported_resource_counters() - .call_read_later(accessor, counter) - .await - }) + // `exported-resource-async`'s export, which the world's bindings don't name. + let delayed = component_instance + .get_export_index(&mut store, None, "test:exported-resource/delayed") + .expect("the component exports `delayed`"); + let read_later = component_instance + .get_export_index(&mut store, Some(&delayed), "read-later") + .expect("`delayed` exports `read-later`"); + let read_later = component_instance + .get_typed_func::<(wasmtime::component::ResourceAny,), (u32,)>(&mut store, &read_later)?; + let (value,) = store + .run_concurrent(async |accessor| read_later.call_concurrent(accessor, (counter,)).await) .await??; assert_eq!(value, 10); assert_eq!(1, counters.counter().call_live_count(&mut store).await?); diff --git a/componentize/tests/fetch_streams.rs b/componentize/tests/fetch_streams.rs index a847407b..a53448a5 100644 --- a/componentize/tests/fetch_streams.rs +++ b/componentize/tests/fetch_streams.rs @@ -49,6 +49,9 @@ fn store() -> Store { /// echoes back. #[tokio::test] async fn stream_as_request_body() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -68,6 +71,9 @@ async fn stream_as_request_body() -> anyhow::Result<()> { /// A response's body is returned as a `stream`. #[tokio::test] async fn response_body_as_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -90,6 +96,9 @@ async fn response_body_as_stream() -> anyhow::Result<()> { /// directly. #[tokio::test] async fn piped_response_body_as_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let settled = store @@ -119,6 +128,9 @@ async fn piped_response_body_as_stream() -> anyhow::Result<()> { /// `stream` can't hold an error. #[tokio::test] async fn aborted_response_body_as_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = shared::http_store_capturing_stderr(&ENGINE, SERVED_BODY); let instance = pre().await.instantiate_async(&mut store).await?; let text = store @@ -154,6 +166,9 @@ async fn aborted_response_body_as_stream() -> anyhow::Result<()> { /// response body, which is returned as a `stream`. #[tokio::test] async fn stream_through_fetch() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -176,6 +191,9 @@ async fn stream_through_fetch() -> anyhow::Result<()> { /// reader, rejects with a `TypeError` naming the export. #[tokio::test] async fn body_reads_in_a_sync_export_reject() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let fetching = instance.test_fetch_streams_fetching(); @@ -211,6 +229,9 @@ async fn body_reads_in_a_sync_export_reject() -> anyhow::Result<()> { /// through the WASIp3 HTTP client, all under the call's one event loop. #[tokio::test] async fn combined_builtins_export_runs() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let result = store @@ -234,6 +255,9 @@ async fn combined_builtins_export_runs() -> anyhow::Result<()> { /// serialized run would not. #[tokio::test] async fn concurrent_async_exports_interleave() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let (first, second) = store diff --git a/componentize/tests/fixtures/cli_run_p2.wit b/componentize/tests/fixtures/cli_run_p2.wit new file mode 100644 index 00000000..fedde73a --- /dev/null +++ b/componentize/tests/fixtures/cli_run_p2.wit @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +package test:cli-run; + +// The CLI world of `cli_run.wit` for a runtime built for WASIp2, whose `wasi:cli/run` is the +// synchronous `0.2` version. +world command { + export wasi:cli/run@0.2.5; +} + +package wasi:cli@0.2.5 { + interface run { + run: func() -> result; + } +} diff --git a/componentize/tests/fixtures/exported_resource.wit b/componentize/tests/fixtures/exported_resource.wit index a1a4e277..559c3f0c 100644 --- a/componentize/tests/fixtures/exported_resource.wit +++ b/componentize/tests/fixtures/exported_resource.wit @@ -6,8 +6,8 @@ package test:exported-resource; // `counters.counter` in JavaScript and returns counters to the host, which calls // their methods through its handles and finally drops them. // -// `read-later` is the one async export: it takes a borrow and reads it after a -// timer turn. +// `exported-resource-async` adds `delayed`, whose `read-later` is the one +// async export: it takes a borrow and reads it after a timer turn. interface counters { resource counter { @@ -32,6 +32,10 @@ interface counters { make-counter: func(start: u32) -> counter; // Returns a counter that `Object.preventExtensions` made non-extensible. make-sealed-counter: func(start: u32) -> counter; +} + +interface delayed { + use counters.{counter}; // Waits for a timer turn, then returns the value of the borrowed counter. read-later: async func(c: borrow) -> u32; @@ -40,3 +44,9 @@ interface counters { world exported-resource { export counters; } + +// `exported-resource` with an `async` export, for a runtime built for WASIp3. +world exported-resource-async { + include exported-resource; + export delayed; +} diff --git a/componentize/tests/fixtures/serve_p2.wit b/componentize/tests/fixtures/serve_p2.wit new file mode 100644 index 00000000..73b33faf --- /dev/null +++ b/componentize/tests/fixtures/serve_p2.wit @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +package test:serve; + +// The serve world of `serve.wit` for a runtime built for WASIp2, whose builtin HTTP handler export +// is `wasi:http/incoming-handler`. +world serve { + export wasi:http/incoming-handler@0.2.0; +} + +package wasi:http@0.2.0 { + interface incoming-handler {} +} diff --git a/componentize/tests/fixtures/serve_raw_p2.js b/componentize/tests/fixtures/serve_raw_p2.js new file mode 100644 index 00000000..0b77ea75 --- /dev/null +++ b/componentize/tests/fixtures/serve_raw_p2.js @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception +// +// `serve_raw.js` for a runtime built for WASIp2: a serve application that implements +// `wasi:http/incoming-handler` itself instead of registering a `fetch` listener. +// +// The handler awaits a real `setTimeout`, so the export's per-call event loop takes a turn, then +// responds with a `200` whose body holds the request's method and path, which come from the +// imported `incoming-request` resource's methods. A request for `/fail` fails with the +// `error-code` variant's `internal-error` case instead, thrown as an instance of `ErrorCode`. A +// request for `/throw` throws an `Error` before `handle` returns, and the `error-code` cannot +// hold it. The handler is in the package layer. + +import { ErrorCode } from "wasi:http/types@0.2"; + +export const wasiHttp = { + incomingHandler: { + handle(request) { + if (request.pathWithQuery() === "/throw") { + throw new Error("boom from handle"); + } + return respond(request); + }, + }, +}; + +async function respond(request) { + await new Promise((resolve) => setTimeout(resolve, 5)); + const method = request.method().tag; + const path = request.pathWithQuery(); + if (path === "/fail") { + throw new ErrorCode({ tag: "internalError", val: `raw ${method} ${path}` }); + } + return new Response(`raw ${method} ${path}`); +} diff --git a/componentize/tests/fixtures/serve_raw_response_p2.js b/componentize/tests/fixtures/serve_raw_response_p2.js new file mode 100644 index 00000000..6c9bdf9b --- /dev/null +++ b/componentize/tests/fixtures/serve_raw_response_p2.js @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception +// +// `serve_raw_response.js` for a runtime built for WASIp2: a serve application +// whose own `wasi:http/incoming-handler` returns `Response` objects, which set +// the handler's `response-outparam`: the one `fetch` resolves to for `/proxy`, +// the one `fetch` resolves to when it sends the request itself for paths +// starting with `/echo`, one whose body a stream produces after a timer per +// chunk for `/slow`, one whose body never produces anything for `/stall`, one +// whose body was already read for `/used`, and a constructed one otherwise. +// `handle` is a bare export. + +export async function handle(request) { + const path = request.pathWithQuery(); + if (path === "/proxy") { + return await fetch("http://upstream.example/data"); + } + if (path.startsWith("/echo")) { + return await fetch(request); + } + if (path === "/slow") { + const encoder = new TextEncoder(); + let remaining = ["one ", "two ", "three"]; + return new Response( + new ReadableStream({ + async pull(controller) { + await new Promise((resolve) => setTimeout(resolve, 2)); + const next = remaining.shift(); + if (next === undefined) { + controller.close(); + } else { + controller.enqueue(encoder.encode(next)); + } + }, + }), + ); + } + if (path === "/stall") { + return new Response(new ReadableStream({ pull() { return new Promise(() => {}); } })); + } + if (path === "/used") { + const response = new Response("read already"); + await response.text(); + return response; + } + return new Response(`built ${path}`, { + status: 201, + headers: { "x-built": "yes" }, + }); +} diff --git a/componentize/tests/fixtures/values.js b/componentize/tests/fixtures/values.js index 41886148..f3aea783 100644 --- a/componentize/tests/fixtures/values.js +++ b/componentize/tests/fixtures/values.js @@ -92,7 +92,7 @@ export const checks = { throw new Error("bigArgument accepted a number for a string field"); }, async promised() { - return "never lowered"; + return "settled"; }, enumObjects: () => JSON.stringify([ diff --git a/componentize/tests/fixtures/values.wit b/componentize/tests/fixtures/values.wit index 6882a42d..543e58b5 100644 --- a/componentize/tests/fixtures/values.wit +++ b/componentize/tests/fixtures/values.wit @@ -196,7 +196,8 @@ interface checks { shifting-import-argument: func() -> string; // Implemented as an `async` method, so it returns a promise, which a - // function WIT does not declare `async` cannot return. + // function WIT does not declare `async` cannot return on WASIp3. On WASIp2, + // where no function is `async`, the call returns the promise's value. promised: func() -> string; // The guest returns, as JSON, members of the objects the `flags` and @@ -236,7 +237,16 @@ world values { // The guest returns `key` for "present", throws an `Error` whose message is // `notFound` for "missing", and throws a `KvError` holding `denied` for - // "secret". `lookup-async` rejects the same ways. + // "secret". export lookup: func(key: string) -> result; - export lookup-async: async func(key: string) -> result; +} + +// `values` with an `async` export, for a runtime built for WASIp3. +world values-async { + include values; + // `values` already imports `kv-error` under that name. + use errors.{kv-error as lookup-error}; + + // Rejects the same ways `lookup` throws. + export lookup-async: async func(key: string) -> result; } diff --git a/componentize/tests/lowering.rs b/componentize/tests/lowering.rs index 66b744bd..961d398c 100644 --- a/componentize/tests/lowering.rs +++ b/componentize/tests/lowering.rs @@ -162,6 +162,9 @@ async fn run_in(store: &mut Store, which: &str) -> anyhow::Result { /// dispose right after it does nothing. #[tokio::test] async fn dispose_after_an_own_argument_does_nothing() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(run("disposeAfterConsume").await?, "ok 5"); Ok(()) } @@ -170,6 +173,9 @@ async fn dispose_after_an_own_argument_does_nothing() -> anyhow::Result<()> { /// second with a `TypeError`. #[tokio::test] async fn own_argument_to_two_pending_calls_rejects_the_second() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!( run("consumeTwice").await?, "TypeError: argument 1 of `consume`: the resource was disposed of, or moved to the host" @@ -181,6 +187,9 @@ async fn own_argument_to_two_pending_calls_rejects_the_second() -> anyhow::Resul /// call returns. #[tokio::test] async fn dispose_waits_for_a_pending_borrow() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, _) = store(); assert_eq!(run_in(&mut store, "disposeWhilePeeked").await?, "ok 6"); assert_eq!(store.data().thing_drops, 1); @@ -191,6 +200,9 @@ async fn dispose_waits_for_a_pending_borrow() -> anyhow::Result<()> { /// call returns. #[tokio::test] async fn own_argument_while_borrowed_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!( run("consumeWhilePeeked").await?, "TypeError: argument 1 of `consume`: the resource is lent to an import call that has \ @@ -203,6 +215,9 @@ async fn own_argument_while_borrowed_rejects() -> anyhow::Result<()> { /// with a `TypeError`. #[tokio::test] async fn stream_argument_to_two_pending_calls_rejects_the_second() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!( run("drainTwice").await?, "TypeError: argument 1 of `drain`: the stream is locked" @@ -214,6 +229,9 @@ async fn stream_argument_to_two_pending_calls_rejects_the_second() -> anyhow::Re /// not change what the host receives. #[tokio::test] async fn typed_array_argument_is_read_when_the_call_starts() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(run("changeBufferAfterCall").await?, "ok 1"); Ok(()) } @@ -222,6 +240,9 @@ async fn typed_array_argument_is_read_when_the_call_starts() -> anyhow::Result<( /// it cannot dispose of a later argument before the host receives it. #[tokio::test] async fn stream_source_runs_after_the_arguments_are_lowered() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(run("pullDisposesArgument").await?, "ok 9"); Ok(()) } @@ -230,6 +251,9 @@ async fn stream_source_runs_after_the_arguments_are_lowered() -> anyhow::Result< /// class, and its wrappers have `[Symbol.dispose]()`. #[tokio::test] async fn bare_imported_resource_has_a_class() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(run("bareResource").await?, "function true 77"); Ok(()) } @@ -239,6 +263,9 @@ async fn bare_imported_resource_has_a_class() -> anyhow::Result<()> { /// together are written together, and a later chunk finds the wrapper moved. #[tokio::test] async fn repeated_resource_in_a_stream_ends_it_early() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } for (together, reason) in [ (true, "the resource is used elsewhere in the same call"), (false, "the resource was disposed of, or moved to the host"), @@ -267,6 +294,9 @@ async fn repeated_resource_in_a_stream_ends_it_early() -> anyhow::Result<()> { /// write it. The import call throws a `TypeError` naming the export. #[tokio::test] async fn future_argument_in_a_sync_export_throws() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, _) = store(); let instance = pre().await.instantiate_async(&mut store).await?; let described = instance @@ -287,6 +317,9 @@ async fn future_argument_in_a_sync_export_throws() -> anyhow::Result<()> { /// override runs. #[tokio::test] async fn exported_method_dispatches_on_the_instance() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, _) = store(); let instance = pre().await.instantiate_async(&mut store).await?; let widget = instance.test_lowering_runner().widget(); diff --git a/componentize/tests/serve.rs b/componentize/tests/serve.rs index d9c68515..956d9397 100644 --- a/componentize/tests/serve.rs +++ b/componentize/tests/serve.rs @@ -4,6 +4,12 @@ //! `fixtures/serve.wit`, served either by the runtime's handler, which dispatches each request as //! a `fetch` event to the application's listeners, or by the application's own `handle`. The //! tests drive `wasi:http/handler.handle()` through wasmtime's p3 `wasi:http` bindings. +//! +//! Against a runtime built for WASIp2, the serve world is `fixtures/serve_p2.wit`, which exports +//! `wasi:http/incoming-handler`, the tests drive it through wasmtime's p2 bindings, and the +//! applications implementing the handler themselves are the `_p2` fixtures, which use the WASI 0.2 +//! API. The cases of `async` imports, `future`s and `Response.consumeBody` need a runtime built +//! for WASIp3. mod shared; @@ -12,7 +18,6 @@ use shared::HttpCtx as Ctx; use { tokio::sync::OnceCell, wasmtime::{component::Component, Engine, Store}, - wasmtime_wasi_http::p3::bindings::{http::types::ErrorCode, Service}, }; /// The body of the harness's response to every outgoing request. @@ -20,11 +25,40 @@ const UPSTREAM_BODY: &str = "from upstream"; static ENGINE: std::sync::LazyLock = std::sync::LazyLock::new(shared::engine); -const SERVE_WIT: &str = include_str!("fixtures/serve.wit"); +/// The serve world for the runtime's WASI version. +fn serve_wit() -> &'static str { + match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/serve_p2.wit"), + componentize::WasiVersion::P3 => include_str!("fixtures/serve.wit"), + } +} + +/// The HTTP handler interface of the runtime's WASI version, and the JS paths an +/// application may implement its `handle` under. +fn handler_interface() -> (&'static str, &'static str) { + match shared::wasi_version() { + componentize::WasiVersion::P2 => ( + "wasi:http/incoming-handler", + "`wasiHttp.incomingHandler.handle` or `incomingHandler.handle` or `handle`", + ), + componentize::WasiVersion::P3 => ( + "wasi:http/handler", + "`wasiHttp.handler.handle` or `handler.handle` or `handle`", + ), + } +} + +/// The name of the HTTP handler export of a component built from [`serve_wit`]. +fn handler_export_name() -> &'static str { + match shared::wasi_version() { + componentize::WasiVersion::P2 => "wasi:http/incoming-handler@0.2.0", + componentize::WasiVersion::P3 => "wasi:http/handler@0.3.0", + } +} /// Componentize a serve application against the serve world and compile it. async fn componentize_serve(js: &'static str) -> Component { - shared::compile_fixture(&ENGINE, SERVE_WIT, "serve", js).await + shared::compile_fixture(&ENGINE, serve_wit(), "serve", js).await } /// The componentized serve app that registers a `fetch` listener, and the @@ -32,9 +66,10 @@ async fn componentize_serve(js: &'static str) -> Component { async fn listener_build() -> &'static (Vec, Component) { static LISTENER: OnceCell, Component), String>> = OnceCell::const_new(); shared::build_once(&LISTENER, async { - let bytes = shared::try_componentize(SERVE_WIT, "serve", include_str!("fixtures/serve.js")) - .await - .expect("componentizing the serve world"); + let bytes = + shared::try_componentize(serve_wit(), "serve", include_str!("fixtures/serve.js")) + .await + .expect("componentizing the serve world"); let component = Component::new(&ENGINE, &bytes).expect("compiling component"); (bytes, component) }) @@ -46,11 +81,11 @@ async fn listener() -> &'static Component { &listener_build().await.1 } -/// The `wasi:http/handler` export of a component built from the serve world, for +/// The HTTP handler export of a component built from the serve world, for /// finalizing it again. fn handler_export() -> [componentize::finalize::KeptExport; 1] { [componentize::finalize::KeptExport::unrenamed( - "wasi:http/handler@0.3.0".to_string(), + handler_export_name().to_string(), )] } @@ -76,7 +111,10 @@ async fn raw() -> &'static Component { static RAW: OnceCell> = OnceCell::const_new(); shared::build_once( &RAW, - componentize_serve(include_str!("fixtures/serve_raw.js")), + componentize_serve(match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/serve_raw_p2.js"), + componentize::WasiVersion::P3 => include_str!("fixtures/serve_raw.js"), + }), ) .await } @@ -86,22 +124,23 @@ fn store() -> Store { shared::http_store(&ENGINE, UPSTREAM_BODY) } -/// Drive `wasi:http/handler.handle()` on a fresh instance of a component with a +/// Drive the HTTP handler export on a fresh instance of a component with a /// bodyless `method` request to `path`. Returns the response status and the -/// collected body as a string, or the `error-code` the handler returned. +/// collected body as a string, or the `error-code` the handler returned (see +/// [`shared::serve_on`]). async fn handle_request( component: &Component, method: &str, path: &str, -) -> anyhow::Result> { +) -> anyhow::Result> { let mut store = store(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - shared::handle_on(&mut store, &service, method, path, "").await + shared::serve_on(&mut store, &service, method, path, "").await } /// `--export-cli` and `--export-http` together build one component that serves @@ -134,7 +173,7 @@ async fn cli_and_serve_component_serves_and_runs() -> anyhow::Result<()> { let component = Component::new(&ENGINE, std::fs::read(&output)?)?; assert_eq!( - handle_request(&component, "GET", "/both").await??, + serve_request(&component, "GET", "/both").await?, (200, "hello from one component, serving /both".to_string()) ); @@ -153,15 +192,8 @@ async fn cli_and_serve_component_serves_and_runs() -> anyhow::Result<()> { }, }, ); - let command = wasmtime_wasi::p3::bindings::Command::instantiate_async( - &mut store, - &component, - &shared::http_linker(&ENGINE, &component), - ) - .await?; - let result = store - .run_concurrent(async move |store| command.wasi_cli_run().call_run(store).await) - .await??; + let linker = shared::http_linker(&ENGINE, &component); + let result = shared::call_cli_run(&mut store, &component, &linker).await?; assert_eq!(result, Ok(())); assert_eq!( shared::pipe_text(&stdout), @@ -183,10 +215,21 @@ async fn wasi_imports() -> &'static (Vec, Component) { resolved: vec![componentize::runtime_imports_world(&runtime, &[]) .expect("the runtime's imports world")], }; + // The fixture imports the `0.3` interfaces. A runtime built for WASIp2 imports them at + // `0.2`, where `get-insecure-seed` is named `insecure-seed`. We import them at a full + // patch version there, which resolves to the module of the version the world imports. + let js = match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/serve_wasi_imports.js") + .replace("@0.3\"", "@0.2.12\"") + .replace("getInsecureSeed", "insecureSeed"), + componentize::WasiVersion::P3 => { + include_str!("fixtures/serve_wasi_imports.js").to_string() + } + }; let bytes = componentize::componentize( - componentize::Wit::::String(SERVE_WIT), + componentize::Wit::::String(serve_wit()), selection, - include_str!("fixtures/serve_wasi_imports.js"), + js.as_str(), &runtime, &Default::default(), ) @@ -206,7 +249,7 @@ async fn wasi_imports() -> &'static (Vec, Component) { async fn serve_world_imports_what_the_runtime_imports() -> anyhow::Result<()> { let component = &wasi_imports().await.1; assert_eq!( - handle_request(component, "GET", "/").await??, + serve_request(component, "GET", "/").await?, ( 200, r#"{"arguments":true,"random":16,"clock":"bigint"}"#.to_string() @@ -252,7 +295,7 @@ async fn deterministic_insecure_seed_is_returned() -> anyhow::Result<()> { for disabled in [&[][..], &[WasiApiSet::Random][..]] { let component = finalize(disabled, Some(seed))?; assert_eq!( - handle_request(&component, "GET", "/seed").await??, + serve_request(&component, "GET", "/seed").await?, (200, "1,2".to_string()), "{disabled:?}" ); @@ -322,7 +365,7 @@ async fn runtime_imports_world_leaves_out_disabled_features() -> anyhow::Result< async fn one_instance_serves_several_requests() -> anyhow::Result<()> { let component = listener().await; let mut store = store(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), @@ -334,7 +377,7 @@ async fn one_instance_serves_several_requests() -> anyhow::Result<()> { ("GET", "/hello", "GET /hello"), ("POST", "/other", "POST /other"), ] { - let response = shared::handle_on(&mut store, &service, method, path, "") + let response = shared::serve_on(&mut store, &service, method, path, "") .await? .map_err(|code| anyhow::anyhow!("handler errored: {code:?}"))?; assert_eq!(response, (200, expected.to_string()), "{method} {path}"); @@ -347,13 +390,13 @@ async fn one_instance_serves_several_requests() -> anyhow::Result<()> { async fn listener_reads_the_request_body() -> anyhow::Result<()> { let component = listener().await; let mut store = store(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - let response = shared::handle_on(&mut store, &service, "POST", "/echo-body", "a body") + let response = shared::serve_on(&mut store, &service, "POST", "/echo-body", "a body") .await? .map_err(|code| anyhow::anyhow!("handler errored: {code:?}"))?; assert_eq!(response, (200, "a body".to_string())); @@ -366,15 +409,15 @@ async fn listener_reads_the_request_body() -> anyhow::Result<()> { async fn listener_without_respond_with_gets_500() -> anyhow::Result<()> { let component = listener().await; let mut store = store(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - let response = shared::handle_on(&mut store, &service, "GET", "/no-response", "").await?; + let response = shared::serve_on(&mut store, &service, "GET", "/no-response", "").await?; assert!(matches!(response, Ok((500, _))), "{response:?}"); - let response = shared::handle_on(&mut store, &service, "GET", "/count", "") + let response = shared::serve_on(&mut store, &service, "GET", "/count", "") .await? .map_err(|code| anyhow::anyhow!("handler errored: {code:?}"))?; assert_eq!(response, (200, "2".to_string())); @@ -392,7 +435,7 @@ async fn serve_request( .map_err(|code| anyhow::anyhow!("handler errored: {code:?}")) } -/// An application that implements `wasi:http/handler` itself serves the +/// An application that implements the HTTP handler itself serves the /// component's export: its `handle` receives the request and builds the response, /// or fails with an `error-code`, which reaches the host. #[tokio::test] @@ -406,7 +449,7 @@ async fn serve_dispatches_to_raw_handler() -> anyhow::Result<()> { let result = handle_request(raw().await, "GET", "/fail").await?; assert!( - matches!(&result, Err(ErrorCode::InternalError(Some(message))) if message == "raw get /fail"), + matches!(&result, Err(code) if code.contains("InternalError(Some(\"raw get /fail\"))")), "the application's handler should fail with the error naming the request; got: {result:?}" ); Ok(()) @@ -418,7 +461,10 @@ async fn raw_response() -> &'static Component { static RAW_RESPONSE: OnceCell> = OnceCell::const_new(); shared::build_once( &RAW_RESPONSE, - componentize_serve(include_str!("fixtures/serve_raw_response.js")), + componentize_serve(match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/serve_raw_response_p2.js"), + componentize::WasiVersion::P3 => include_str!("fixtures/serve_raw_response.js"), + }), ) .await } @@ -458,13 +504,13 @@ async fn stalled_and_read_response_bodies_fail_the_request() -> anyhow::Result<( let component = raw_response().await; let request = async |path: &str| { let (mut store, stderr) = store_capturing_stderr(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - let result = shared::handle_on(&mut store, &service, "GET", path, "").await; + let result = shared::serve_on(&mut store, &service, "GET", path, "").await; anyhow::Ok((result, shared::pipe_text(&stderr))) }; @@ -509,19 +555,25 @@ async fn listener_added_after_top_level_await_serves_the_first_request() -> anyh /// `fetch` listener fails to componentize, naming the export. #[tokio::test] async fn serve_with_raw_handler_and_listener_is_rejected() { - let error = shared::try_componentize( - SERVE_WIT, - "serve", - include_str!("fixtures/serve_raw_and_listener.js"), - ) - .await - .expect_err("an application with both handlers must not componentize"); + // The fixture's handler is in the interface layer, which names WASIp2's interface + // `incomingHandler`. + let js = match shared::wasi_version() { + componentize::WasiVersion::P2 => include_str!("fixtures/serve_raw_and_listener.js") + .replace("export const handler =", "export const incomingHandler ="), + componentize::WasiVersion::P3 => { + include_str!("fixtures/serve_raw_and_listener.js").to_string() + } + }; + let error = shared::try_componentize(serve_wit(), "serve", &js) + .await + .expect_err("an application with both handlers must not componentize"); let message = format!("{error:#}"); + let (interface, paths) = handler_interface(); assert!( - message.contains( - "both exports a `wasi:http/handler` implementation as `wasiHttp.handler.handle` or \ - `handler.handle` or `handle` and registers a `fetch` listener" - ), + message.contains(&format!( + "both exports a `{interface}` implementation as {paths} and registers a `fetch` \ + listener" + )), "the error should name both handlers; got: {message}" ); } @@ -532,18 +584,19 @@ async fn serve_with_raw_handler_and_listener_is_rejected() { #[tokio::test] async fn serve_without_handler_is_rejected() { let error = shared::try_componentize( - SERVE_WIT, + serve_wit(), "serve", include_str!("fixtures/serve_no_listener.js"), ) .await .expect_err("an application with no handler must not componentize"); let message = format!("{error:#}"); + let (interface, paths) = handler_interface(); assert!( - message.contains( - "neither exports a `wasi:http/handler` implementation as `wasiHttp.handler.handle` \ - or `handler.handle` or `handle` nor registers a `fetch` listener" - ), + message.contains(&format!( + "neither exports a `{interface}` implementation as {paths} nor registers a `fetch` \ + listener" + )), "the error should name both ways to serve requests; got: {message}" ); } @@ -638,8 +691,8 @@ async fn serve_imports_app(path: &str) -> anyhow::Result<(u16, String)> { ) }); let (mut store, stderr) = store_capturing_stderr(); - let service = Service::instantiate_async(&mut store, component, &linker).await?; - shared::handle_on(&mut store, &service, "GET", path, "") + let service = shared::HttpService::instantiate(&mut store, component, &linker).await?; + shared::serve_on(&mut store, &service, "GET", path, "") .await? .map_err(|code| anyhow::anyhow!("{code:?}: {}", shared::pipe_text(&stderr))) } @@ -649,6 +702,9 @@ async fn serve_imports_app(path: &str) -> anyhow::Result<(u16, String)> { /// `get` and `has`, which a raw handler calls on the request's headers. #[tokio::test] async fn world_imports_extend_the_runtime_wasi_http_types() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!( serve_imports_app("/").await?, (200, "content-length=0 absent=false".to_string()) @@ -670,7 +726,10 @@ async fn disabling_http_for_a_serve_component_is_refused() { .expect_err("a serve component cannot go without `http`"); let message = format!("{error:#}"); assert!( - message.contains("is used by `wasi:http/handler@0.3.0`, which the component exports"), + message.contains(&format!( + "is used by `{}`, which the component exports", + handler_export_name() + )), "{message}" ); assert_eq!(message.matches("is used by").count(), 1, "{message}"); @@ -683,20 +742,23 @@ async fn disabling_http_for_a_serve_component_is_refused() { async fn failing_raw_handler_is_named_as_the_handler() -> anyhow::Result<()> { let component = raw().await; let (mut store, stderr) = store_capturing_stderr(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - let result = shared::handle_on(&mut store, &service, "GET", "/throw", "").await; + let result = shared::serve_on(&mut store, &service, "GET", "/throw", "").await; assert!( !matches!(result, Ok(Ok(_))), "a throwing handler cannot answer" ); let stderr = shared::pipe_text(&stderr); + let (interface, _) = handler_interface(); assert!( - stderr.contains("export `wasi:http/handler#handle` threw: boom from handle"), + stderr.contains(&format!( + "export `{interface}#handle` threw: boom from handle" + )), "{stderr}" ); Ok(()) @@ -706,20 +768,26 @@ async fn failing_raw_handler_is_named_as_the_handler() -> anyhow::Result<()> { /// componentize, saying it must match the runtime's declaration. #[tokio::test] async fn mismatched_wasi_http_declaration_is_rejected() { - const WIT: &str = "\ -package test:serve-mismatch; - -world serve { - export wasi:http/handler@0.3.0; -} - -package wasi:http@0.3.0 { - interface handler { - handle: func(n: u32) -> u32; - } -} -"; - let error = shared::try_componentize(WIT, "serve", include_str!("fixtures/serve.js")) + // At the version of the runtime's builtin: a declaration at another patch version is of + // another package, which merges without a conflict. + let (interface, _) = handler_interface(); + let (package, name) = interface + .split_once('/') + .expect("an interface has a package"); + let runtime = std::fs::read(shared::runtime_path()).expect("reading the runtime build"); + let (_, exports) = shared::surface(&runtime); + let builtin = exports + .iter() + .find(|export| export.starts_with(&format!("{interface}@"))) + .expect("the runtime exports its HTTP handler"); + let (_, version) = builtin.split_once('@').expect("the export has a version"); + let wit = format!( + "package test:serve-mismatch;\n\n\ + world serve {{\n export {interface}@{version};\n}}\n\n\ + package {package}@{version} {{\n interface {name} {{\n \ + handle: func(n: u32) -> u32;\n }}\n}}\n" + ); + let error = shared::try_componentize(&wit, "serve", include_str!("fixtures/serve.js")) .await .expect_err("a mismatched `wasi:http` declaration must not componentize"); let message = format!("{error:#}"); @@ -733,6 +801,9 @@ package wasi:http@0.3.0 { /// with a `TypeError`, rather than moving the request while it is lent. #[tokio::test] async fn fetch_of_a_lent_request_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!( serve_imports_app("/lend").await?, ( @@ -748,6 +819,9 @@ async fn fetch_of_a_lent_request_rejects() -> anyhow::Result<()> { /// reads it only after the request has finished. #[tokio::test] async fn listener_future_is_written_after_the_request() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } const WIT: &str = "\ package test:serve-future; @@ -784,8 +858,8 @@ addEventListener('fetch', (event) => { ) }); let (mut store, stderr) = store_capturing_stderr(); - let service = Service::instantiate_async(&mut store, &component, &linker).await?; - let (status, _) = shared::handle_on(&mut store, &service, "GET", "/", "") + let service = shared::HttpService::instantiate(&mut store, &component, &linker).await?; + let (status, _) = shared::serve_on(&mut store, &service, "GET", "/", "") .await? .map_err(|code| anyhow::anyhow!("{code:?}"))?; assert_eq!(status, 200); @@ -810,13 +884,13 @@ addEventListener('fetch', (event) => { async fn listener_unhandled_rejection_is_reported() -> anyhow::Result<()> { let component = listener().await; let (mut store, stderr) = store_capturing_stderr(); - let service = Service::instantiate_async( + let service = shared::HttpService::instantiate( &mut store, component, &shared::http_linker(&ENGINE, component), ) .await?; - let (status, _) = shared::handle_on(&mut store, &service, "GET", "/reject", "") + let (status, _) = shared::serve_on(&mut store, &service, "GET", "/reject", "") .await? .map_err(|code| anyhow::anyhow!("{code:?}"))?; assert_eq!(status, 200); @@ -832,6 +906,9 @@ async fn listener_unhandled_rejection_is_reported() -> anyhow::Result<()> { /// `response` throws a `TypeError` the guest can catch. #[tokio::test] async fn plain_object_for_an_owned_response_throws() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (status, body) = serve_request(raw_response().await, "GET", "/plain-object").await?; assert_eq!(status, 200); assert_eq!( @@ -847,6 +924,9 @@ async fn plain_object_for_an_owned_response_throws() -> anyhow::Result<()> { /// catch. #[tokio::test] async fn unusable_response_for_an_owned_response_throws() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (status, body) = serve_request(raw_response().await, "GET", "/unusable").await?; assert_eq!(status, 200); let unusable = "TypeError: argument 1 of `Response.consumeBody`: the Response has an \ diff --git a/componentize/tests/shared/mod.rs b/componentize/tests/shared/mod.rs index 0c7ffdb1..fb356ee0 100644 --- a/componentize/tests/shared/mod.rs +++ b/componentize/tests/shared/mod.rs @@ -82,6 +82,23 @@ pub fn runtime() -> anyhow::Result { componentize::Runtime::new(bytes) } +/// The WASI version of the runtime build [`runtime`] reads. +pub fn wasi_version() -> componentize::WasiVersion { + static VERSION: std::sync::OnceLock = std::sync::OnceLock::new(); + *VERSION.get_or_init(|| runtime().expect("reading the runtime build").wasi_version()) +} + +/// Whether the case that calls this has to be skipped, because it needs a +/// runtime built for WASIp3 and the runtime build [`runtime`] reads was built +/// for WASIp2. Prints that it was skipped if so. +pub fn needs_wasip3() -> bool { + let skip = wasi_version() == componentize::WasiVersion::P2; + if skip { + eprintln!("skipped: needs a runtime built for WASIp3"); + } + skip +} + /// The WASI context and resource table a suite's store data holds. /// /// A suite that needs host state of its own (a captured log, an HTTP context) @@ -633,9 +650,9 @@ pub fn linker( linker } -/// Like [`linker`], for a component that exports `wasi:http/handler`, and with -/// the p3 `wasi:http` host, which defines the handler's request and response -/// resources. +/// Like [`linker`], for a component that exports the runtime's HTTP handler +/// interface, and with the `wasi:http` host of the runtime's WASI version, which +/// defines the handler's request and response resources. pub fn http_linker( engine: &wasmtime::Engine, component: &wasmtime::component::Component, @@ -656,11 +673,159 @@ pub fn http_linker_with( .chain(provided.iter().copied()) .collect(); linker(engine, component, &provided, |linker| { - wasmtime_wasi_http::p3::add_to_linker(linker)?; + match wasi_version() { + componentize::WasiVersion::P2 => { + wasmtime_wasi_http::p2::add_only_http_to_linker_async(linker)? + } + componentize::WasiVersion::P3 => wasmtime_wasi_http::p3::add_to_linker(linker)?, + } add(linker) }) } +/// An instance's HTTP handler export: `wasi:http/incoming-handler` for a +/// runtime built for WASIp2, `wasi:http/handler` for one built for WASIp3. +pub enum HttpService { + P2(wasmtime_wasi_http::p2::bindings::Proxy), + P3(wasmtime_wasi_http::p3::bindings::Service), +} + +impl HttpService { + /// Instantiate `component` in `store` with `linker`, which [`http_linker`] + /// or [`http_linker_with`] made. + pub async fn instantiate( + store: &mut Store, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker, + ) -> anyhow::Result { + Ok(match wasi_version() { + componentize::WasiVersion::P2 => HttpService::P2( + wasmtime_wasi_http::p2::bindings::Proxy::instantiate_async( + store, component, linker, + ) + .await?, + ), + componentize::WasiVersion::P3 => HttpService::P3( + wasmtime_wasi_http::p3::bindings::Service::instantiate_async( + store, component, linker, + ) + .await?, + ), + }) + } +} + +/// Like [`handle_on`], for an instance of either WASI version. The handler's +/// `error-code` is returned as its debug representation. +pub async fn serve_on( + store: &mut Store, + service: &HttpService, + method: &str, + path: &str, + body: &'static str, +) -> anyhow::Result> { + match service { + HttpService::P2(proxy) => handle_on_p2(store, proxy, method, path, body).await, + HttpService::P3(service) => Ok(handle_on(store, service, method, path, body) + .await? + .map_err(|code| format!("{code:?}"))), + } +} + +/// [`serve_on`] for a `wasi:http/incoming-handler` export. +/// +/// The response is read while `handle` runs, since the guest writes the body +/// before `handle` returns, and the host holds only a chunk of it at a time. A +/// `handle` that traps is returned as an error at once, since the host keeps the +/// response outparam of a trapped instance, so the response never arrives. +async fn handle_on_p2( + store: &mut Store, + proxy: &wasmtime_wasi_http::p2::bindings::Proxy, + method: &str, + path: &str, + body: &'static str, +) -> anyhow::Result> { + use bytes::Bytes; + use http_body_util::{BodyExt as _, Full}; + use wasmtime_wasi_http::WasiHttpView as _; + + let request = http::Request::builder() + .method(method) + .uri(format!("http://example.com{path}")) + .header(http::header::CONTENT_LENGTH, body.len()) + .body(Full::new(Bytes::from_static(body.as_bytes())).map_err( + |never: std::convert::Infallible| -> wasmtime_wasi_http::Error { match never {} }, + )) + .expect("building the request"); + let (sender, receiver) = tokio::sync::oneshot::channel(); + let (request, response_out) = { + let mut http = store.data_mut().http(); + ( + http.new_incoming_request( + wasmtime_wasi_http::p2::bindings::http::types::Scheme::Http, + request, + )?, + http.new_response_outparam(sender)?, + ) + }; + let handled = + proxy + .wasi_http_incoming_handler() + .call_handle(&mut *store, request, response_out); + let response = async { + match receiver.await { + Ok(Ok(response)) => { + let status = response.status().as_u16(); + let body = response.into_body().collect().await?.to_bytes(); + anyhow::Ok(Ok((status, String::from_utf8_lossy(&body).into_owned()))) + } + Ok(Err(code)) => Ok(Err(format!("{code:?}"))), + Err(_) => anyhow::bail!("the handler set no response"), + } + }; + match futures::future::select(std::pin::pin!(handled), std::pin::pin!(response)).await { + futures::future::Either::Left((handled, response)) => { + handled?; + response.await + } + futures::future::Either::Right((response, handled)) => { + handled.await?; + response + } + } +} + +/// Call `wasi:cli/run.run()` on a fresh instance of `component` in `store`, with +/// the bindings of the runtime's WASI version. +pub async fn call_cli_run( + store: &mut Store, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker, +) -> anyhow::Result> { + Ok(match wasi_version() { + componentize::WasiVersion::P2 => { + let command = wasmtime_wasi::p2::bindings::Command::instantiate_async( + &mut *store, + component, + linker, + ) + .await?; + command.wasi_cli_run().call_run(&mut *store).await? + } + componentize::WasiVersion::P3 => { + let command = wasmtime_wasi::p3::bindings::Command::instantiate_async( + &mut *store, + component, + linker, + ) + .await?; + store + .run_concurrent(async move |store| command.wasi_cli_run().call_run(store).await) + .await?? + } + }) +} + /// Drive `wasi:http/handler.handle()` on `service`, an instance in `store`, with /// a `method` request to `path` whose body is `body`. Returns the response status /// and the collected body as a string, or the `error-code` the handler returned. diff --git a/componentize/tests/streams.rs b/componentize/tests/streams.rs index 6c2cae01..ea7a390d 100644 --- a/componentize/tests/streams.rs +++ b/componentize/tests/streams.rs @@ -227,6 +227,9 @@ where /// batch, in chunks larger than one batch, arrives whole and in order. #[tokio::test] async fn large_byte_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let data: Vec = (0..210_000u32).map(|i| (i % 251) as u8).collect(); @@ -251,6 +254,9 @@ async fn large_byte_stream() -> anyhow::Result<()> { /// order. #[tokio::test] async fn many_elements() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -274,6 +280,9 @@ async fn many_elements() -> anyhow::Result<()> { /// straight back to the host. #[tokio::test] async fn pass_stream_u8() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -296,6 +305,9 @@ async fn pass_stream_u8() -> anyhow::Result<()> { /// its value. #[tokio::test] async fn echo_futures() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -320,6 +332,9 @@ async fn echo_futures() -> anyhow::Result<()> { /// per read, which forces the guest's writes into a sequence of partial writes. #[tokio::test] async fn short_reads_string() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -353,6 +368,9 @@ async fn short_reads_string() -> anyhow::Result<()> { /// A `stream` round-trips its records through the guest. #[tokio::test] async fn echo_stream_point() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } use exports::test::streams::echoes::Point; let mut store = store(); @@ -380,6 +398,9 @@ async fn echo_stream_point() -> anyhow::Result<()> { /// and each echoed handle still reaches the same `thing`. #[tokio::test] async fn echo_stream_thing() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } use wasmtime::component::ResourceAny; let mut store = store(); @@ -425,6 +446,9 @@ async fn echo_stream_thing() -> anyhow::Result<()> { /// own buffers, here smaller than the chunks the host delivers. #[tokio::test] async fn byob_read() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -447,6 +471,9 @@ async fn byob_read() -> anyhow::Result<()> { /// generator waits on between chunks. #[tokio::test] async fn async_generator_chunks_across_timers() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -468,6 +495,9 @@ async fn async_generator_chunks_across_timers() -> anyhow::Result<()> { /// what the guest left unread. #[tokio::test] async fn partly_read_stream_returns_the_rest() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -490,6 +520,9 @@ async fn partly_read_stream_returns_the_rest() -> anyhow::Result<()> { /// producer being dropped, even though the producer never ends by itself. #[tokio::test] async fn cancel_after_first_chunk() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -515,6 +548,9 @@ async fn cancel_after_first_chunk() -> anyhow::Result<()> { /// the instance then streams again. #[tokio::test] async fn host_stops_reading() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let cancelled = store @@ -555,6 +591,9 @@ async fn host_stops_reading() -> anyhow::Result<()> { /// and the runtime reports why on stderr. #[tokio::test] async fn wrong_chunks_end_the_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -581,6 +620,9 @@ async fn wrong_chunks_end_the_stream() -> anyhow::Result<()> { /// `stream` traps. #[tokio::test] async fn not_a_stream_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -606,6 +648,9 @@ async fn not_a_stream_traps() -> anyhow::Result<()> { /// `err`, since a `string` can hold it. #[tokio::test] async fn future_result() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -636,6 +681,9 @@ async fn future_result() -> anyhow::Result<()> { /// rejects with a `ComponentError` holding its `err` payload. #[tokio::test] async fn future_result_settles_with_ok_or_component_error() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -662,6 +710,9 @@ async fn future_result_settles_with_ok_or_component_error() -> anyhow::Result<() /// names the type through `use`, so the class is the one `failing` exports. #[tokio::test] async fn future_result_rejects_with_the_named_error_class() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let described = store @@ -686,6 +737,9 @@ async fn future_result_rejects_with_the_named_error_class() -> anyhow::Result<() /// read. #[tokio::test] async fn stream_of_host_resources_round_trips() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let strings = ["a", "b", "c", "d", "e"]; @@ -736,6 +790,9 @@ async fn stream_of_host_resources_round_trips() -> anyhow::Result<()> { /// extends `ComponentError`. #[tokio::test] async fn import_errors_are_error_class_instances() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -764,6 +821,9 @@ async fn import_errors_are_error_class_instances() -> anyhow::Result<()> { /// that case. #[tokio::test] async fn error_classes_lower_to_err() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -792,6 +852,9 @@ async fn error_classes_lower_to_err() -> anyhow::Result<()> { /// stream and the future imported functions return. #[tokio::test] async fn import_streams_and_futures() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let described = store @@ -810,6 +873,9 @@ async fn import_streams_and_futures() -> anyhow::Result<()> { /// before it, and the runtime reports the failing field and the reason on stderr. #[tokio::test] async fn wrong_element_ends_the_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let points = store @@ -837,6 +903,9 @@ async fn wrong_element_ends_the_stream() -> anyhow::Result<()> { /// something else on a later read does not change what is sent. #[tokio::test] async fn chunk_is_read_once() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let points = store @@ -865,6 +934,9 @@ async fn chunk_is_read_once() -> anyhow::Result<()> { /// timeout interrupts, so the calls run on a thread of their own. #[test] fn dropped_readers_leave_the_instance_intact() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (done, finished) = std::sync::mpsc::channel(); std::thread::spawn(move || { let runtime = tokio::runtime::Runtime::new().expect("creating a runtime"); @@ -916,6 +988,9 @@ async fn drop_readers_and_ping() -> anyhow::Result<()> { /// A `stream` takes `ArrayBuffer` chunks as well as views. #[tokio::test] async fn array_buffer_chunks() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -936,6 +1011,9 @@ async fn array_buffer_chunks() -> anyhow::Result<()> { /// Empty chunks a guest's `stream` source yields are skipped. #[tokio::test] async fn empty_chunks_are_skipped() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -957,6 +1035,9 @@ async fn empty_chunks_are_skipped() -> anyhow::Result<()> { /// since nothing else can read it. #[tokio::test] async fn locked_stream_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -981,6 +1062,9 @@ async fn locked_stream_traps() -> anyhow::Result<()> { /// future's value. #[tokio::test] async fn plain_value_as_future() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let result = store @@ -999,6 +1083,9 @@ async fn plain_value_as_future() -> anyhow::Result<()> { /// call with a `TypeError`, and an iterable object is read as the stream. #[tokio::test] async fn stream_argument_misuse_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let messages = store @@ -1030,6 +1117,9 @@ async fn stream_argument_misuse_rejects() -> anyhow::Result<()> { /// export. #[tokio::test] async fn stream_read_in_a_sync_export_rejects() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let echoes = instance.test_streams_echoes(); @@ -1054,6 +1144,9 @@ async fn stream_read_in_a_sync_export_rejects() -> anyhow::Result<()> { /// future has no way to deliver the rejection. #[tokio::test] async fn rejected_future_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -1081,6 +1174,9 @@ async fn rejected_future_traps() -> anyhow::Result<()> { /// traps the instance, because the `Error` can't be converted into a `u32` `err` value. #[tokio::test] async fn rejected_result_future_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -1108,6 +1204,9 @@ async fn rejected_result_future_traps() -> anyhow::Result<()> { /// leaves the instance usable: the guest's write fails quietly. #[tokio::test] async fn host_drops_future_reader() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1127,6 +1226,9 @@ async fn host_drops_future_reader() -> anyhow::Result<()> { /// object the guest created. #[tokio::test] async fn future_of_a_resource() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } use wasmtime::component::ResourceAny; let mut store = store(); @@ -1154,6 +1256,9 @@ async fn future_of_a_resource() -> anyhow::Result<()> { /// read and drops the host's producer. #[tokio::test] async fn cancel_with_a_read_in_flight() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1187,6 +1292,9 @@ async fn cancel_with_a_read_in_flight() -> anyhow::Result<()> { /// readable end. A pump writes its bytes instead. #[tokio::test] async fn pass_stream_as_another_type() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1210,6 +1318,9 @@ async fn pass_stream_as_another_type() -> anyhow::Result<()> { /// source is cancelled. #[tokio::test] async fn idle_source_is_cancelled() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let cancelled = store @@ -1239,6 +1350,9 @@ async fn idle_source_is_cancelled() -> anyhow::Result<()> { /// finishes. #[tokio::test] async fn never_settling_future_lets_the_call_finish() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let echoes = instance.test_streams_echoes(); @@ -1280,6 +1394,9 @@ async fn never_settling_future_lets_the_call_finish() -> anyhow::Result<()> { /// cleared. #[tokio::test] async fn collected_unsettled_future_is_reported() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let echoes = instance.test_streams_echoes(); @@ -1307,6 +1424,9 @@ async fn collected_unsettled_future_is_reported() -> anyhow::Result<()> { /// of right after that is still delivered. #[tokio::test] async fn future_value_is_lowered_when_the_promise_settles() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let value = @@ -1391,6 +1511,9 @@ async fn settle_held_future(sync_settle: bool, read_first: bool) -> anyhow::Resu /// the next async call, whether the host reads the future before or after that. #[tokio::test] async fn future_settled_by_a_sync_export_is_written() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(settle_held_future(true, false).await?, Some(7)); assert_eq!(settle_held_future(true, true).await?, Some(7)); Ok(()) @@ -1400,6 +1523,9 @@ async fn future_settled_by_a_sync_export_is_written() -> anyhow::Result<()> { /// whether the host reads the future before or after that. #[tokio::test] async fn future_settled_by_an_async_export_is_written() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } assert_eq!(settle_held_future(false, false).await?, Some(7)); assert_eq!(settle_held_future(false, true).await?, Some(7)); Ok(()) @@ -1409,6 +1535,9 @@ async fn future_settled_by_an_async_export_is_written() -> anyhow::Result<()> { /// delivers later, both finish: each call's event loop reads its own future. #[tokio::test] async fn concurrent_calls_with_future_arguments_finish() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let delayed = |store: &Accessor, ms: u64| -> anyhow::Result<_> { @@ -1441,6 +1570,9 @@ async fn concurrent_calls_with_future_arguments_finish() -> anyhow::Result<()> { /// returned. The stream is longer than the pump's 64-element batch. #[tokio::test] async fn payloadless_echo() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1472,6 +1604,9 @@ async fn payloadless_echo() -> anyhow::Result<()> { /// fulfilled with `undefined`. #[tokio::test] async fn payloadless_consumed() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1496,6 +1631,9 @@ async fn payloadless_consumed() -> anyhow::Result<()> { /// `future` without one. #[tokio::test] async fn payloadless_produced() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; store @@ -1534,6 +1672,9 @@ async fn payloadless_produced() -> anyhow::Result<()> { /// and a host-written future, and reads the stream and future the host returns. #[tokio::test] async fn payloadless_imports() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let mut store = store(); let instance = pre().await.instantiate_async(&mut store).await?; let described = store @@ -1552,6 +1693,9 @@ async fn payloadless_imports() -> anyhow::Result<()> { /// the chunks before it, and the runtime reports why on stderr. #[tokio::test] async fn wrong_tick_ends_the_stream() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let ticks = store @@ -1578,6 +1722,9 @@ async fn wrong_tick_ends_the_stream() -> anyhow::Result<()> { /// value other than `undefined` traps the instance. #[tokio::test] async fn valued_payloadless_future_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -1608,6 +1755,9 @@ async fn valued_payloadless_future_traps() -> anyhow::Result<()> { /// the instance. #[tokio::test] async fn rejected_payloadless_future_traps() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = store_capturing_stderr(); let instance = pre().await.instantiate_async(&mut store).await?; let outcome = store @@ -1630,3 +1780,24 @@ async fn rejected_payloadless_future_traps() -> anyhow::Result<()> { ); Ok(()) } + +/// A runtime built for WASIp2 refuses a world with a `stream`, even in a synchronous function. +#[tokio::test] +async fn streams_need_a_wasip3_runtime() { + if shared::wasi_version() != componentize::WasiVersion::P2 { + return; + } + let error = shared::try_componentize( + "package test:sync-stream;\n\ + world sync-stream {\n import produce: func() -> stream;\n}\n", + "sync-stream", + "export {};", + ) + .await + .expect_err("a `stream` needs a WASIp3 runtime"); + let message = format!("{error:#}"); + assert!( + message.contains("`produce` takes or returns a `stream` or `future`, which need a runtime"), + "{message}" + ); +} diff --git a/componentize/tests/surface.rs b/componentize/tests/surface.rs index 4ad8a9c3..f38f595d 100644 --- a/componentize/tests/surface.rs +++ b/componentize/tests/surface.rs @@ -152,7 +152,7 @@ async fn disabled_features_are_stubbed() -> anyhow::Result<()> { .await .expect_err("Math.random must trap with random disabled"); assert!( - format!("{err:?}").contains("disabled-features!wasi:random/random@0.3.0#"), + format!("{err:?}").contains("disabled-features!wasi:random/random@"), "the trap is in the disabled function's stub: {err:?}" ); Ok(()) diff --git a/componentize/tests/values.rs b/componentize/tests/values.rs index 136340db..2c24b3b0 100644 --- a/componentize/tests/values.rs +++ b/componentize/tests/values.rs @@ -3,6 +3,9 @@ //! Value shapes beyond the sync suite's echoes: the world of `fixtures/values.{wit,js}`. Every //! interface but `errors`, `strings` and `checks` is imported and exported, so each value is //! passed in both directions. +//! +//! For a runtime built for WASIp3, the component is built from `values-async`, which adds an +//! `async` export to `values`. mod shared; @@ -30,10 +33,14 @@ static ENGINE: LazyLock = LazyLock::new(shared::engine); async fn pre() -> &'static ValuesPre { static PRE: OnceCell, String>> = OnceCell::const_new(); shared::build_once(&PRE, async { + let world = match shared::wasi_version() { + componentize::WasiVersion::P2 => "values", + componentize::WasiVersion::P3 => "values-async", + }; let component = shared::compile_fixture( &ENGINE, include_str!("fixtures/values.wit"), - "values", + world, include_str!("fixtures/values.js"), ) .await; @@ -345,8 +352,7 @@ async fn result_with_bytes_error() -> anyhow::Result<()> { } /// An `enum` `err` lowers from an `Error` whose message names a case and from an -/// instance of the enum's error class, for a sync export and for a rejected -/// async one. +/// instance of the enum's error class. #[tokio::test] async fn enum_err_lowers_from_errors() -> anyhow::Result<()> { let mut store = store(); @@ -364,11 +370,32 @@ async fn enum_err_lowers_from_errors() -> anyhow::Result<()> { instance.call_lookup(&mut store, "secret").await?, Err(errors::KvError::Denied) ); + Ok(()) +} + +/// An `enum` `err` lowers from the reasons a rejected async export rejects with, +/// as [`enum_err_lowers_from_errors`] describes. The export is `values-async`'s, +/// which the world's bindings don't name, so it is called through a typed function. +#[tokio::test] +async fn enum_err_lowers_from_async_rejections() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } + let mut store = store(); + let instance = pre() + .await + .instance_pre() + .instantiate_async(&mut store) + .await?; + let lookup = instance.get_typed_func::<(String,), (Result,)>( + &mut store, + "lookup-async", + )?; let results = store .run_concurrent(async |store| { let mut results = Vec::new(); for key in ["present", "missing", "secret"] { - results.push(instance.call_lookup_async(store, key.to_string()).await?); + results.push(lookup.call_concurrent(store, (key.to_string(),)).await?.0); } anyhow::Ok(results) }) @@ -612,6 +639,9 @@ async fn mismatched_import_argument_throws() -> anyhow::Result<()> { /// the function `async`. #[tokio::test] async fn sync_export_returning_a_promise_traps_with_a_hint() -> anyhow::Result<()> { + if shared::needs_wasip3() { + return Ok(()); + } let (mut store, stderr) = shared::store_capturing_stderr(&ENGINE); let instance = pre().await.instantiate_async(&mut store).await?; let result = instance @@ -627,6 +657,25 @@ async fn sync_export_returning_a_promise_traps_with_a_hint() -> anyhow::Result<( Ok(()) } +/// On WASIp2, a sync export that returns a promise returns its value once it has +/// settled. +#[tokio::test] +async fn sync_export_returning_a_promise_returns_its_value_on_wasip2() -> anyhow::Result<()> { + if shared::wasi_version() != componentize::WasiVersion::P2 { + return Ok(()); + } + let mut store = store(); + let instance = pre().await.instantiate_async(&mut store).await?; + assert_eq!( + instance + .test_values_checks() + .call_promised(&mut store) + .await?, + "settled" + ); + Ok(()) +} + /// An import argument is read once, so a getter that returns something else on a /// later read changes neither what is checked nor what the host receives. #[tokio::test] diff --git a/componentize/tests/wit_dir.rs b/componentize/tests/wit_dir.rs index 7d6597e6..4da0c3fe 100644 --- a/componentize/tests/wit_dir.rs +++ b/componentize/tests/wit_dir.rs @@ -199,26 +199,35 @@ package wasi:filesystem@0.3.0 { fn wit_dir_world_imports_what_the_runtime_imports() { let wit = shared::workspace_root().join("componentize/tests/fixtures/wit_dir"); let wit = wit.to_string_lossy(); + // The runtime imports its WASI interfaces at a version of its WASI version, and `wasi:http`'s + // client interface under a name of that version. + let (version, http_client) = match shared::wasi_version() { + componentize::WasiVersion::P2 => ("0.2", "wasi:http/outgoing-handler"), + componentize::WasiVersion::P3 => ("0.3", "wasi:http/client"), + }; let modules = shared::cli_output(&["-w", &wit, "imports"]); let modules: Vec<&str> = modules.lines().collect(); for module in [ - "local:hello/hello@1", - "wasi:cli/environment@0.3", - "wasi:random/random@0.3", - "wasi:http/client@0.3", + "local:hello/hello@1".to_string(), + format!("wasi:cli/environment@{version}"), + format!("wasi:random/random@{version}"), + format!("{http_client}@{version}"), ] { - assert!(modules.contains(&module), "`{module}` in {modules:?}"); + assert!( + modules.contains(&module.as_str()), + "`{module}` in {modules:?}" + ); } assert!(!modules.contains(&"local:b/foo"), "{modules:?}"); let modules = shared::cli_output(&["-w", &wit, "--disable", "random", "imports"]); let modules: Vec<&str> = modules.lines().collect(); assert!( - !modules.contains(&"wasi:random/random@0.3"), + !modules.contains(&format!("wasi:random/random@{version}").as_str()), "no `wasi:random/random` in {modules:?}" ); assert!( - modules.contains(&"wasi:random/insecure-seed@0.3"), + modules.contains(&format!("wasi:random/insecure-seed@{version}").as_str()), "`wasi:random/insecure-seed` in {modules:?}" ); } diff --git a/componentize/tests/wit_to_ts.rs b/componentize/tests/wit_to_ts.rs index f007362b..14571282 100644 --- a/componentize/tests/wit_to_ts.rs +++ b/componentize/tests/wit_to_ts.rs @@ -155,6 +155,13 @@ const FIXTURES: &[Fixture] = &[ source: Source::Wit("serve", "serve"), guests: &["serve.handler.ts"], }, + // The world of an application implementing the `wasi:http/incoming-handler` of a runtime + // built for WASIp2 itself. + Fixture { + name: "serve_p2", + source: Source::Wit("serve_p2", "serve-p2"), + guests: &["serve_p2.handler.ts"], + }, Fixture { name: "builtin_cli", source: Source::Builtin("--export-cli"), @@ -167,6 +174,31 @@ const FIXTURES: &[Fixture] = &[ }, ]; +/// Guests of [`FIXTURES`] type-checked only against a runtime built for WASIp3: +/// an application's own `wasi:http/handler`, which a WASIp2 component can't have. +const WASIP3_GUESTS: &[&str] = &["builtin_serve.handler.ts"]; + +/// The guests of `fixture` to type-check against the runtime's WASI version. +fn guests(fixture: &Fixture) -> impl Iterator { + let wasip2 = shared::wasi_version() == componentize::WasiVersion::P2; + fixture + .guests + .iter() + .copied() + .filter(move |guest| !(wasip2 && WASIP3_GUESTS.contains(guest))) +} + +/// The snapshot of `fixture`'s declaration: `.d.ts.expected`, or for a +/// built-in world of a runtime built for WASIp2, `.p2.d.ts.expected`. +fn snapshot_path(fixture: &Fixture) -> PathBuf { + let stem = fixture.name; + let wasip2 = shared::wasi_version() == componentize::WasiVersion::P2; + match fixture.source { + Source::Builtin(_) if wasip2 => fixtures_dir().join(format!("{stem}.p2.d.ts.expected")), + _ => fixtures_dir().join(format!("{stem}.d.ts.expected")), + } +} + fn fixtures_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/wit_to_ts") } @@ -210,6 +242,18 @@ struct SuiteFixture { world: &'static [&'static str], /// The guests, as paths relative to `tests/fixtures`. guests: &'static [&'static str], + /// How the fixture is checked against a runtime built for WASIp2. + on_wasip2: OnWasip2, +} + +/// How a [`SuiteFixture`] is checked against a runtime built for WASIp2. +enum OnWasip2 { + /// As against a runtime built for WASIp3. + Same, + /// Not at all, since its world needs a runtime built for WASIp3. + Skip, + /// With the world these flags select, and these guests. + With(&'static [&'static str], &'static [&'static str]), } /// Every suite world with its guests. @@ -218,16 +262,19 @@ const SUITE_FIXTURES: &[SuiteFixture] = &[ name: "async_export", world: &["-w", "async_export.wit"], guests: &["async_export.js"], + on_wasip2: OnWasip2::Skip, }, SuiteFixture { name: "async_import", world: &["-w", "async_import.wit"], guests: &["async_import.js"], + on_wasip2: OnWasip2::Skip, }, SuiteFixture { name: "cli_and_serve", world: &["--export-cli", "--export-http"], guests: &["cli_and_serve.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "cli_merged", @@ -242,31 +289,48 @@ const SUITE_FIXTURES: &[SuiteFixture] = &[ "two", ], guests: &["cli/app/lib.js", "cli/app/src/main.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "cli_run", world: &["-w", "cli_run.wit"], guests: &["cli_run.js", "cli_run_no_run.js"], + on_wasip2: OnWasip2::With( + &["-w", "cli_run_p2.wit"], + &["cli_run.js", "cli_run_no_run.js"], + ), }, SuiteFixture { name: "exported_resource", - world: &["-w", "exported_resource.wit"], + world: &[ + "-w", + "exported_resource.wit", + "-n", + "exported-resource-async", + ], guests: &["exported_resource.js"], + on_wasip2: OnWasip2::With( + &["-w", "exported_resource.wit", "-n", "exported-resource"], + &["exported_resource.js"], + ), }, SuiteFixture { name: "fetch_streams", world: &["-w", "fetch_streams.wit"], guests: &["fetch_streams.js"], + on_wasip2: OnWasip2::Skip, }, SuiteFixture { name: "lowering", world: &["-w", "lowering.wit"], guests: &["lowering.js"], + on_wasip2: OnWasip2::Skip, }, SuiteFixture { name: "resources", world: &["-w", "resources.wit"], guests: &["resources.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "serve", @@ -279,21 +343,33 @@ const SUITE_FIXTURES: &[SuiteFixture] = &[ "serve_raw_response.js", "serve_wasi_imports.js", ], + on_wasip2: OnWasip2::With( + &["-w", "serve_p2.wit"], + &[ + "serve.js", + "serve_no_listener.js", + "serve_raw_p2.js", + "serve_raw_response_p2.js", + ], + ), }, SuiteFixture { name: "streams", world: &["-w", "streams.wit"], guests: &["streams.js"], + on_wasip2: OnWasip2::Skip, }, SuiteFixture { name: "surface", world: &["-w", "surface.wit"], guests: &["surface.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "sync_suite", world: &["-w", "sync_suite.wit"], guests: &["sync_suite.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "top_level", @@ -305,21 +381,25 @@ const SUITE_FIXTURES: &[SuiteFixture] = &[ "top_level/lib/late.js", "top_level/lib/name.js", ], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "values", - world: &["-w", "values.wit"], + world: &["-w", "values.wit", "-n", "values-async"], guests: &["values.js"], + on_wasip2: OnWasip2::With(&["-w", "values.wit", "-n", "values"], &["values.js"]), }, SuiteFixture { name: "wit_dir", world: &["-w", "wit_dir"], guests: &["wit_dir.js"], + on_wasip2: OnWasip2::Same, }, SuiteFixture { name: "world_resource", world: &["-w", "world_resource.wit"], guests: &["world_resource.js"], + on_wasip2: OnWasip2::Same, }, ]; @@ -330,7 +410,7 @@ fn snapshots_match() { for (fixture, actual) in FIXTURES.iter().zip(GENERATED.iter()) { let stem = fixture.name; - let snapshot = fixtures_dir().join(format!("{stem}.d.ts.expected")); + let snapshot = snapshot_path(fixture); if bless { std::fs::write(&snapshot, actual).expect("writing blessed snapshot"); @@ -378,12 +458,7 @@ fn generated_dts_is_valid_typescript() { std::fs::write(&consumer_path, consumer_for(dts)).expect("writing consumer for tsc"); let mut files = vec![dts_path, consumer_path]; - files.extend( - fixture - .guests - .iter() - .map(|guest| fixtures_dir().join(guest)), - ); + files.extend(guests(fixture).map(|guest| fixtures_dir().join(guest))); checks.push((stem.to_string(), files)); } let failures = run_tsc_all(&tsc, checks, Guests::TypeScript); @@ -411,7 +486,14 @@ fn suite_guests_type_check() { let mut checks = Vec::new(); for fixture in SUITE_FIXTURES { - let args: Vec<&str> = fixture.world.iter().copied().chain(["types"]).collect(); + let (world, guests) = match (shared::wasi_version(), &fixture.on_wasip2) { + (componentize::WasiVersion::P3, _) | (_, OnWasip2::Same) => { + (fixture.world, fixture.guests) + } + (componentize::WasiVersion::P2, OnWasip2::Skip) => continue, + (componentize::WasiVersion::P2, OnWasip2::With(world, guests)) => (*world, *guests), + }; + let args: Vec<&str> = world.iter().copied().chain(["types"]).collect(); let output = shared::run_cli(&args, Some(&suite_fixtures)); assert!( output.status.success(), @@ -425,12 +507,7 @@ fn suite_guests_type_check() { std::fs::write(&dts_path, &dts).expect("writing generated .d.ts for tsc"); let mut files = vec![dts_path]; - files.extend( - fixture - .guests - .iter() - .map(|guest| suite_fixtures.join(guest)), - ); + files.extend(guests.iter().map(|guest| suite_fixtures.join(guest))); checks.push((stem.to_string(), files)); } let failures = run_tsc_all(&tsc, checks, Guests::JavaScript); @@ -465,7 +542,13 @@ fn every_suite_guest_is_type_checked() { modules(&base, &base, &mut found); let checked: Vec<&str> = SUITE_FIXTURES .iter() - .flat_map(|fixture| fixture.guests.iter().copied()) + .flat_map(|fixture| { + let wasip2_guests: &[&str] = match fixture.on_wasip2 { + OnWasip2::With(_, guests) => guests, + OnWasip2::Same | OnWasip2::Skip => &[], + }; + fixture.guests.iter().chain(wasip2_guests).copied() + }) .collect(); let unchecked: Vec<&String> = found .iter() diff --git a/componentize/tests/wit_to_ts/builtin_cli.p2.d.ts.expected b/componentize/tests/wit_to_ts/builtin_cli.p2.d.ts.expected new file mode 100644 index 00000000..7cc0fcd3 --- /dev/null +++ b/componentize/tests/wit_to_ts/builtin_cli.p2.d.ts.expected @@ -0,0 +1,603 @@ +// Generated by `starling-componentize types`. Do not edit by hand. +// +// The guest JavaScript module a `command` componentize target implements, and +// the modules it may import. Add this file to the program (a `files` or +// `include` entry in `tsconfig.json`, or a `/// `). +// +// Nothing here is exported at file level, which makes every `declare +// module` below an ambient module declaration. An import then +// resolves: +// +// import { log } from 'test:example/logger'; +// +// The guest's own obligations are `starling:guest`. Reference it to have the +// compiler check what the module exports: +// +// import type * as Guest from 'starling:guest'; +// export const example: typeof Guest.example = { … }; +// +// Every WIT type the world uses is declared in the module +// +// starling:types/starling:cli/command +// +// Each interface's module re-exports the types the interface defines or +// `use`s under their WIT names. A name that several types share is +// qualified there. + +// `ComponentError` holds the `err` payload of a WIT `result`. A string payload +// is also its `message`. The error classes in the types module extend it. +interface ComponentError extends Error { + readonly payload: unknown; +} +declare var ComponentError: { + new (payload: unknown): ComponentError; + readonly prototype: ComponentError; +}; + +// `fetch` also takes a `wasi:http/types` `incoming-request`, such as the one a +// `wasi:http/incoming-handler` implementation receives, and sends it. +declare function fetch( + input: import('starling:types/starling:cli/command').IncomingRequest, + init?: RequestInit, +): Promise; + +declare module 'starling:types/starling:cli/command' { + export class Error { + private constructor(); + toDebugString(): string; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class Pollable { + private constructor(); + ready(): boolean; + block(): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class InputStream { + private constructor(); + read(len: bigint): Uint8Array; + subscribe(): Pollable; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type StreamError = { tag: 'lastOperationFailed'; val: Error } | { tag: 'closed' }; + + export class OutputStream { + private constructor(); + checkWrite(): bigint; + write(contents: Uint8Array | Uint8ClampedArray | number[]): void; + flush(): void; + blockingFlush(): void; + subscribe(): Pollable; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class TerminalInput { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class TerminalOutput { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Instant = bigint; + + export type Duration = bigint; + + export type Datetime = { seconds: bigint; nanoseconds: number; }; + + export class Descriptor { + private constructor(); + readViaStream(offset: Filesize): InputStream; + writeViaStream(offset: Filesize): OutputStream; + appendViaStream(): OutputStream; + getFlags(): DescriptorFlagsSet; + stat(): DescriptorStat; + statAt(pathFlags: PathFlagsSet, path: string): DescriptorStat; + openAt(pathFlags: PathFlagsSet, path: string, openFlags: OpenFlagsSet, flags: DescriptorFlagsSet): Descriptor; + readlinkAt(path: string): string; + removeDirectoryAt(path: string): void; + unlinkFileAt(path: string): void; + metadataHash(): MetadataHashValue; + metadataHashAt(pathFlags: PathFlagsSet, path: string): MetadataHashValue; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Filesize = bigint; + + export enum WasiFilesystemTypesErrorCode { + access = 0, + wouldBlock = 1, + already = 2, + badDescriptor = 3, + busy = 4, + deadlock = 5, + quota = 6, + exist = 7, + fileTooLarge = 8, + illegalByteSequence = 9, + inProgress = 10, + interrupted = 11, + invalid = 12, + io = 13, + isDirectory = 14, + loop = 15, + tooManyLinks = 16, + messageSize = 17, + nameTooLong = 18, + noDevice = 19, + noEntry = 20, + noLock = 21, + insufficientMemory = 22, + insufficientSpace = 23, + notDirectory = 24, + notEmpty = 25, + notRecoverable = 26, + unsupported = 27, + noTty = 28, + noSuchDevice = 29, + overflow = 30, + notPermitted = 31, + pipe = 32, + readOnly = 33, + invalidSeek = 34, + textFileBusy = 35, + crossDevice = 36, + } + + export enum DescriptorFlags { + read = 1, + write = 2, + fileIntegritySync = 4, + dataIntegritySync = 8, + requestedWriteSync = 16, + mutateDirectory = 32, + } + + // A set of `DescriptorFlags` members, combined with `|`. + export type DescriptorFlagsSet = number; + + export type DescriptorStat = { type: DescriptorType; linkCount: LinkCount; size: Filesize; dataAccessTimestamp?: Datetime | undefined; dataModificationTimestamp?: Datetime | undefined; statusChangeTimestamp?: Datetime | undefined; }; + + export enum DescriptorType { + unknown = 0, + blockDevice = 1, + characterDevice = 2, + directory = 3, + fifo = 4, + symbolicLink = 5, + regularFile = 6, + socket = 7, + } + + export type LinkCount = bigint; + + export enum PathFlags { + symlinkFollow = 1, + } + + // A set of `PathFlags` members, combined with `|`. + export type PathFlagsSet = number; + + export enum OpenFlags { + create = 1, + directory = 2, + exclusive = 4, + truncate = 8, + } + + // A set of `OpenFlags` members, combined with `|`. + export type OpenFlagsSet = number; + + export type MetadataHashValue = { lower: bigint; upper: bigint; }; + + export type NewTimestamp = { tag: 'noChange' } | { tag: 'now' } | { tag: 'timestamp'; val: Datetime }; + + export type DirectoryEntry = { type: DescriptorType; name: string; }; + + export enum Advice { + normal = 0, + sequential = 1, + random = 2, + willNeed = 3, + dontNeed = 4, + noReuse = 5, + } + + export class DirectoryEntryStream { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class Fields { + constructor(); + append(name: FieldKey, value: FieldValue): void; + entries(): [FieldKey, FieldValue][]; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type FieldKey = string; + + export type FieldValue = Uint8Array; + + export type HeaderError = { tag: 'invalidSyntax' } | { tag: 'forbidden' } | { tag: 'immutable' }; + + export class IncomingRequest { + private constructor(); + method(): Method; + pathWithQuery(): string | undefined; + scheme(): Scheme | undefined; + authority(): string | undefined; + headers(): Fields; + consume(): IncomingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Method = { tag: 'get' } | { tag: 'head' } | { tag: 'post' } | { tag: 'put' } | { tag: 'delete' } | { tag: 'connect' } | { tag: 'options' } | { tag: 'trace' } | { tag: 'patch' } | { tag: 'other'; val: string }; + + export type Scheme = { tag: 'http' } | { tag: 'https' } | { tag: 'other'; val: string }; + + export class IncomingBody { + private constructor(); + stream(): InputStream; + static finish(this_: IncomingBody): FutureTrailers; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class FutureTrailers { + private constructor(); + subscribe(): Pollable; + get(): ({ tag: 'ok'; val: { tag: 'ok'; val?: Fields | undefined } | { tag: 'err'; val: WasiHttpTypesErrorCode } } | { tag: 'err' }) | undefined; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type WasiHttpTypesErrorCode = { tag: 'dnsTimeout' } | { tag: 'dnsError'; val: DnsErrorPayload } | { tag: 'destinationNotFound' } | { tag: 'destinationUnavailable' } | { tag: 'destinationIpProhibited' } | { tag: 'destinationIpUnroutable' } | { tag: 'connectionRefused' } | { tag: 'connectionTerminated' } | { tag: 'connectionTimeout' } | { tag: 'connectionReadTimeout' } | { tag: 'connectionWriteTimeout' } | { tag: 'connectionLimitReached' } | { tag: 'tlsProtocolError' } | { tag: 'tlsCertificateError' } | { tag: 'tlsAlertReceived'; val: TlsAlertReceivedPayload } | { tag: 'httpRequestDenied' } | { tag: 'httpRequestLengthRequired' } | { tag: 'httpRequestBodySize'; val?: bigint | undefined } | { tag: 'httpRequestMethodInvalid' } | { tag: 'httpRequestUriInvalid' } | { tag: 'httpRequestUriTooLong' } | { tag: 'httpRequestHeaderSectionSize'; val?: number | undefined } | { tag: 'httpRequestHeaderSize'; val?: FieldSizePayload | undefined } | { tag: 'httpRequestTrailerSectionSize'; val?: number | undefined } | { tag: 'httpRequestTrailerSize'; val: FieldSizePayload } | { tag: 'httpResponseIncomplete' } | { tag: 'httpResponseHeaderSectionSize'; val?: number | undefined } | { tag: 'httpResponseHeaderSize'; val: FieldSizePayload } | { tag: 'httpResponseBodySize'; val?: bigint | undefined } | { tag: 'httpResponseTrailerSectionSize'; val?: number | undefined } | { tag: 'httpResponseTrailerSize'; val: FieldSizePayload } | { tag: 'httpResponseTransferCoding'; val?: string | undefined } | { tag: 'httpResponseContentCoding'; val?: string | undefined } | { tag: 'httpResponseTimeout' } | { tag: 'httpUpgradeFailed' } | { tag: 'httpProtocolError' } | { tag: 'loopDetected' } | { tag: 'configurationError' } | { tag: 'internalError'; val?: string | undefined }; + + export type DnsErrorPayload = { rcode?: string | undefined; infoCode?: number | undefined; }; + + export type TlsAlertReceivedPayload = { alertId?: number | undefined; alertMessage?: string | undefined; }; + + export type FieldSizePayload = { fieldName?: string | undefined; fieldSize?: number | undefined; }; + + export class OutgoingRequest { + constructor(headers: Fields); + body(): OutgoingBody; + setMethod(method: Method): void; + setPathWithQuery(pathWithQuery: string | undefined | null): void; + setScheme(scheme: Scheme | undefined | null): void; + setAuthority(authority: string | undefined | null): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingBody { + private constructor(); + write(): OutputStream; + static finish(this_: OutgoingBody, trailers: Fields | undefined | null): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class ResponseOutparam { + private constructor(); + static set(param: ResponseOutparam, response: { tag: 'ok'; val: OutgoingResponse } | { tag: 'err'; val: WasiHttpTypesErrorCode }): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingResponse { + constructor(headers: Fields); + setStatusCode(statusCode: StatusCode): void; + body(): OutgoingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type StatusCode = number; + + export class IncomingResponse { + private constructor(); + status(): StatusCode; + headers(): Fields; + consume(): IncomingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class FutureIncomingResponse { + private constructor(); + subscribe(): Pollable; + get(): ({ tag: 'ok'; val: { tag: 'ok'; val: IncomingResponse } | { tag: 'err'; val: WasiHttpTypesErrorCode } } | { tag: 'err' }) | undefined; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class RequestOptions { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class StreamErrorError extends ComponentError { + constructor(payload: StreamError); + readonly payload: StreamError; + } + + export class WasiFilesystemTypesErrorCodeError extends ComponentError { + constructor(payload: WasiFilesystemTypesErrorCode); + readonly payload: WasiFilesystemTypesErrorCode; + } + + export class HeaderErrorError extends ComponentError { + constructor(payload: HeaderError); + readonly payload: HeaderError; + } + + export class WasiHttpTypesErrorCodeError extends ComponentError { + constructor(payload: WasiHttpTypesErrorCode); + readonly payload: WasiHttpTypesErrorCode; + } + + export class OutgoingHandlerErrorCodeError extends ComponentError { + constructor(payload: WasiHttpTypesErrorCode); + readonly payload: WasiHttpTypesErrorCode; + } +} + +declare module 'starling:guest' { + // The `wasi:cli/run` command entry point. It may be synchronous or + // `async`. Throwing, or a rejected promise, maps to a failed run. + export function run(): void | Promise; +} + +declare module 'wasi:cli/environment@0.2' { + export function getEnvironment(): [string, string][]; + export function getArguments(): string[]; +} + +declare module 'wasi:cli/exit@0.2' { + export function exit(status: { tag: 'ok' } | { tag: 'err' }): void; +} + +declare module 'wasi:io/error@0.2' { + export { + Error, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:io/poll@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function poll(in_: $t.Pollable[]): Uint32Array; + + export { + Pollable, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:io/streams@0.2' { + export { + StreamErrorError as StreamError, + InputStream, + OutputStream, + } from 'starling:types/starling:cli/command'; + export type { + Error, + Pollable, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/stderr@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getStderr(): $t.OutputStream; + + export type { + OutputStream, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/stdin@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getStdin(): $t.InputStream; + + export type { + InputStream, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/stdout@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getStdout(): $t.OutputStream; + + export type { + OutputStream, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/terminal-input@0.2' { + export { + TerminalInput, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/terminal-output@0.2' { + export { + TerminalOutput, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/terminal-stderr@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getTerminalStderr(): $t.TerminalOutput | undefined; + + export type { + TerminalOutput, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/terminal-stdin@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getTerminalStdin(): $t.TerminalInput | undefined; + + export type { + TerminalInput, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:cli/terminal-stdout@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getTerminalStdout(): $t.TerminalOutput | undefined; + + export type { + TerminalOutput, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:clocks/monotonic-clock@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function now(): $t.Instant; + export function subscribeDuration(when: $t.Duration): $t.Pollable; + + export { + Instant, + Duration, + } from 'starling:types/starling:cli/command'; + export type { + Pollable, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:clocks/wall-clock@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function now(): $t.Datetime; + + export { + Datetime, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:filesystem/types@0.2' { + export { + WasiFilesystemTypesErrorCodeError as ErrorCode, + Datetime, + Filesize, + DescriptorType, + DescriptorFlagsSet, + DescriptorFlags, + PathFlagsSet, + PathFlags, + OpenFlagsSet, + OpenFlags, + LinkCount, + DescriptorStat, + NewTimestamp, + DirectoryEntry, + Advice, + MetadataHashValue, + Descriptor, + DirectoryEntryStream, + } from 'starling:types/starling:cli/command'; + export type { + InputStream, + OutputStream, + Error, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:filesystem/preopens@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function getDirectories(): [$t.Descriptor, string][]; + + export type { + Descriptor, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:http/types@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function httpErrorCode(err: $t.Error): $t.WasiHttpTypesErrorCode | undefined; + + export { + HeaderErrorError as HeaderError, + WasiHttpTypesErrorCodeError as ErrorCode, + Duration, + Method, + Scheme, + DnsErrorPayload, + TlsAlertReceivedPayload, + FieldSizePayload, + FieldKey, + FieldKey as FieldName, + FieldValue, + Fields, + IncomingRequest, + OutgoingRequest, + RequestOptions, + ResponseOutparam, + StatusCode, + IncomingResponse, + IncomingBody, + FutureTrailers, + OutgoingResponse, + OutgoingBody, + FutureIncomingResponse, + } from 'starling:types/starling:cli/command'; + export type { + InputStream, + OutputStream, + Error as IoError, + Pollable, + Fields as Headers, + Fields as Trailers, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:http/outgoing-handler@0.2' { + import type * as $t from 'starling:types/starling:cli/command'; + + export function handle(request: $t.OutgoingRequest, options: $t.RequestOptions | undefined | null): $t.FutureIncomingResponse; + + export { + OutgoingHandlerErrorCodeError as ErrorCode, + } from 'starling:types/starling:cli/command'; + export type { + OutgoingRequest, + RequestOptions, + FutureIncomingResponse, + } from 'starling:types/starling:cli/command'; +} + +declare module 'wasi:random/insecure-seed@0.2' { + export function insecureSeed(): [bigint, bigint]; +} + +declare module 'wasi:random/random@0.2' { + export function getRandomBytes(len: bigint): Uint8Array; +} + diff --git a/componentize/tests/wit_to_ts/builtin_serve.p2.d.ts.expected b/componentize/tests/wit_to_ts/builtin_serve.p2.d.ts.expected new file mode 100644 index 00000000..5a643bdb --- /dev/null +++ b/componentize/tests/wit_to_ts/builtin_serve.p2.d.ts.expected @@ -0,0 +1,646 @@ +// Generated by `starling-componentize types`. Do not edit by hand. +// +// The guest JavaScript module a `serve` componentize target implements, and +// the modules it may import. Add this file to the program (a `files` or +// `include` entry in `tsconfig.json`, or a `/// `). +// +// Nothing here is exported at file level, which makes every `declare +// module` below an ambient module declaration. An import then +// resolves: +// +// import { log } from 'test:example/logger'; +// +// The guest's own obligations are `starling:guest`. Reference it to have the +// compiler check what the module exports: +// +// import type * as Guest from 'starling:guest'; +// export const example: typeof Guest.example = { … }; +// +// Every WIT type the world uses is declared in the module +// +// starling:types/starling:serve/serve +// +// Each interface's module re-exports the types the interface defines or +// `use`s under their WIT names. A name that several types share is +// qualified there. + +// `ComponentError` holds the `err` payload of a WIT `result`. A string payload +// is also its `message`. The error classes in the types module extend it. +interface ComponentError extends Error { + readonly payload: unknown; +} +declare var ComponentError: { + new (payload: unknown): ComponentError; + readonly prototype: ComponentError; +}; + +// The event the runtime dispatches to `addEventListener('fetch', …)` listeners. +interface FetchEvent extends Event { + readonly request: Request; + respondWith(response: Response | PromiseLike): void; + waitUntil(promise: PromiseLike): void; +} +interface WindowEventMap { + fetch: FetchEvent; +} + +// `fetch` also takes a `wasi:http/types` `incoming-request`, such as the one a +// `wasi:http/incoming-handler` implementation receives, and sends it. +declare function fetch( + input: import('starling:types/starling:serve/serve').IncomingRequest, + init?: RequestInit, +): Promise; + +declare module 'starling:types/starling:serve/serve' { + export class Pollable { + private constructor(); + ready(): boolean; + block(): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Instant = bigint; + + export type Duration = bigint; + + export class Error { + private constructor(); + toDebugString(): string; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class InputStream { + private constructor(); + read(len: bigint): Uint8Array; + subscribe(): Pollable; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type StreamError = { tag: 'lastOperationFailed'; val: Error } | { tag: 'closed' }; + + export class OutputStream { + private constructor(); + checkWrite(): bigint; + write(contents: Uint8Array | Uint8ClampedArray | number[]): void; + flush(): void; + blockingFlush(): void; + subscribe(): Pollable; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class Fields { + constructor(); + append(name: FieldKey, value: FieldValue): void; + entries(): [FieldKey, FieldValue][]; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type FieldKey = string; + + export type FieldValue = Uint8Array; + + export type HeaderError = { tag: 'invalidSyntax' } | { tag: 'forbidden' } | { tag: 'immutable' }; + + export class IncomingRequest { + private constructor(); + method(): Method; + pathWithQuery(): string | undefined; + scheme(): Scheme | undefined; + authority(): string | undefined; + headers(): Fields; + consume(): IncomingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Method = { tag: 'get' } | { tag: 'head' } | { tag: 'post' } | { tag: 'put' } | { tag: 'delete' } | { tag: 'connect' } | { tag: 'options' } | { tag: 'trace' } | { tag: 'patch' } | { tag: 'other'; val: string }; + + export type Scheme = { tag: 'http' } | { tag: 'https' } | { tag: 'other'; val: string }; + + export class IncomingBody { + private constructor(); + stream(): InputStream; + static finish(this_: IncomingBody): FutureTrailers; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class FutureTrailers { + private constructor(); + subscribe(): Pollable; + get(): ({ tag: 'ok'; val: { tag: 'ok'; val?: Fields | undefined } | { tag: 'err'; val: WasiHttpTypesErrorCode } } | { tag: 'err' }) | undefined; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type WasiHttpTypesErrorCode = { tag: 'dnsTimeout' } | { tag: 'dnsError'; val: DnsErrorPayload } | { tag: 'destinationNotFound' } | { tag: 'destinationUnavailable' } | { tag: 'destinationIpProhibited' } | { tag: 'destinationIpUnroutable' } | { tag: 'connectionRefused' } | { tag: 'connectionTerminated' } | { tag: 'connectionTimeout' } | { tag: 'connectionReadTimeout' } | { tag: 'connectionWriteTimeout' } | { tag: 'connectionLimitReached' } | { tag: 'tlsProtocolError' } | { tag: 'tlsCertificateError' } | { tag: 'tlsAlertReceived'; val: TlsAlertReceivedPayload } | { tag: 'httpRequestDenied' } | { tag: 'httpRequestLengthRequired' } | { tag: 'httpRequestBodySize'; val?: bigint | undefined } | { tag: 'httpRequestMethodInvalid' } | { tag: 'httpRequestUriInvalid' } | { tag: 'httpRequestUriTooLong' } | { tag: 'httpRequestHeaderSectionSize'; val?: number | undefined } | { tag: 'httpRequestHeaderSize'; val?: FieldSizePayload | undefined } | { tag: 'httpRequestTrailerSectionSize'; val?: number | undefined } | { tag: 'httpRequestTrailerSize'; val: FieldSizePayload } | { tag: 'httpResponseIncomplete' } | { tag: 'httpResponseHeaderSectionSize'; val?: number | undefined } | { tag: 'httpResponseHeaderSize'; val: FieldSizePayload } | { tag: 'httpResponseBodySize'; val?: bigint | undefined } | { tag: 'httpResponseTrailerSectionSize'; val?: number | undefined } | { tag: 'httpResponseTrailerSize'; val: FieldSizePayload } | { tag: 'httpResponseTransferCoding'; val?: string | undefined } | { tag: 'httpResponseContentCoding'; val?: string | undefined } | { tag: 'httpResponseTimeout' } | { tag: 'httpUpgradeFailed' } | { tag: 'httpProtocolError' } | { tag: 'loopDetected' } | { tag: 'configurationError' } | { tag: 'internalError'; val?: string | undefined }; + + export type DnsErrorPayload = { rcode?: string | undefined; infoCode?: number | undefined; }; + + export type TlsAlertReceivedPayload = { alertId?: number | undefined; alertMessage?: string | undefined; }; + + export type FieldSizePayload = { fieldName?: string | undefined; fieldSize?: number | undefined; }; + + export class OutgoingRequest { + constructor(headers: Fields); + body(): OutgoingBody; + setMethod(method: Method): void; + setPathWithQuery(pathWithQuery: string | undefined | null): void; + setScheme(scheme: Scheme | undefined | null): void; + setAuthority(authority: string | undefined | null): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingBody { + private constructor(); + write(): OutputStream; + static finish(this_: OutgoingBody, trailers: Fields | undefined | null): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class ResponseOutparam { + private constructor(); + static set(param: ResponseOutparam, response: { tag: 'ok'; val: OutgoingResponse } | { tag: 'err'; val: WasiHttpTypesErrorCode }): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingResponse { + constructor(headers: Fields); + setStatusCode(statusCode: StatusCode): void; + body(): OutgoingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type StatusCode = number; + + export class IncomingResponse { + private constructor(); + status(): StatusCode; + headers(): Fields; + consume(): IncomingBody; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class FutureIncomingResponse { + private constructor(); + subscribe(): Pollable; + get(): ({ tag: 'ok'; val: { tag: 'ok'; val: IncomingResponse } | { tag: 'err'; val: WasiHttpTypesErrorCode } } | { tag: 'err' }) | undefined; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class RequestOptions { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class TerminalInput { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class TerminalOutput { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Datetime = { seconds: bigint; nanoseconds: number; }; + + export class Descriptor { + private constructor(); + readViaStream(offset: Filesize): InputStream; + writeViaStream(offset: Filesize): OutputStream; + appendViaStream(): OutputStream; + getFlags(): DescriptorFlagsSet; + stat(): DescriptorStat; + statAt(pathFlags: PathFlagsSet, path: string): DescriptorStat; + openAt(pathFlags: PathFlagsSet, path: string, openFlags: OpenFlagsSet, flags: DescriptorFlagsSet): Descriptor; + readlinkAt(path: string): string; + removeDirectoryAt(path: string): void; + unlinkFileAt(path: string): void; + metadataHash(): MetadataHashValue; + metadataHashAt(pathFlags: PathFlagsSet, path: string): MetadataHashValue; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type Filesize = bigint; + + export enum WasiFilesystemTypesErrorCode { + access = 0, + wouldBlock = 1, + already = 2, + badDescriptor = 3, + busy = 4, + deadlock = 5, + quota = 6, + exist = 7, + fileTooLarge = 8, + illegalByteSequence = 9, + inProgress = 10, + interrupted = 11, + invalid = 12, + io = 13, + isDirectory = 14, + loop = 15, + tooManyLinks = 16, + messageSize = 17, + nameTooLong = 18, + noDevice = 19, + noEntry = 20, + noLock = 21, + insufficientMemory = 22, + insufficientSpace = 23, + notDirectory = 24, + notEmpty = 25, + notRecoverable = 26, + unsupported = 27, + noTty = 28, + noSuchDevice = 29, + overflow = 30, + notPermitted = 31, + pipe = 32, + readOnly = 33, + invalidSeek = 34, + textFileBusy = 35, + crossDevice = 36, + } + + export enum DescriptorFlags { + read = 1, + write = 2, + fileIntegritySync = 4, + dataIntegritySync = 8, + requestedWriteSync = 16, + mutateDirectory = 32, + } + + // A set of `DescriptorFlags` members, combined with `|`. + export type DescriptorFlagsSet = number; + + export type DescriptorStat = { type: DescriptorType; linkCount: LinkCount; size: Filesize; dataAccessTimestamp?: Datetime | undefined; dataModificationTimestamp?: Datetime | undefined; statusChangeTimestamp?: Datetime | undefined; }; + + export enum DescriptorType { + unknown = 0, + blockDevice = 1, + characterDevice = 2, + directory = 3, + fifo = 4, + symbolicLink = 5, + regularFile = 6, + socket = 7, + } + + export type LinkCount = bigint; + + export enum PathFlags { + symlinkFollow = 1, + } + + // A set of `PathFlags` members, combined with `|`. + export type PathFlagsSet = number; + + export enum OpenFlags { + create = 1, + directory = 2, + exclusive = 4, + truncate = 8, + } + + // A set of `OpenFlags` members, combined with `|`. + export type OpenFlagsSet = number; + + export type MetadataHashValue = { lower: bigint; upper: bigint; }; + + export type NewTimestamp = { tag: 'noChange' } | { tag: 'now' } | { tag: 'timestamp'; val: Datetime }; + + export type DirectoryEntry = { type: DescriptorType; name: string; }; + + export enum Advice { + normal = 0, + sequential = 1, + random = 2, + willNeed = 3, + dontNeed = 4, + noReuse = 5, + } + + export class DirectoryEntryStream { + private constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class StreamErrorError extends ComponentError { + constructor(payload: StreamError); + readonly payload: StreamError; + } + + export class HeaderErrorError extends ComponentError { + constructor(payload: HeaderError); + readonly payload: HeaderError; + } + + export class WasiHttpTypesErrorCodeError extends ComponentError { + constructor(payload: WasiHttpTypesErrorCode); + readonly payload: WasiHttpTypesErrorCode; + } + + export class WasiFilesystemTypesErrorCodeError extends ComponentError { + constructor(payload: WasiFilesystemTypesErrorCode); + readonly payload: WasiFilesystemTypesErrorCode; + } + + export class OutgoingHandlerErrorCodeError extends ComponentError { + constructor(payload: WasiHttpTypesErrorCode); + readonly payload: WasiHttpTypesErrorCode; + } +} + +declare module 'starling:guest' { + import type * as $t from 'starling:types/starling:serve/serve'; + + // The `wasi:http/incoming-handler` export is served in one of two ways. Either + // register a listener on the global, and the runtime dispatches each + // request to it as a `FetchEvent`: + // + // addEventListener('fetch', (event) => { + // event.respondWith(new Response('hello')); + // }); + // + // Or implement `handle` below, which receives the WIT request and returns a + // `Response` or the WIT `response`, or a promise of either. `fetch(request)` + // sends the request on. It fails with an `error-code` by throwing an + // `ErrorCode` from `wasi:http/types@0.2.12`. + // + // An application that does both, or neither, fails to componentize. + + // Each item of an exported interface is implemented through exactly one + // of the shapes below: a member of its package's object, a member of its + // interface's object, or a bare export. A shape is declared only where the + // world allows it for the item. + + export const wasiHttp: { + // The exported interface `wasi:http/incoming-handler`. + incomingHandler: { + handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; + }; + }; + + // The exported interface `wasi:http/incoming-handler`. + export const incomingHandler: { + handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; + }; + + // Items of the exported interface `wasi:http/incoming-handler`. + export function handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; +} + +declare module 'wasi:io/poll@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function poll(in_: $t.Pollable[]): Uint32Array; + + export { + Pollable, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:clocks/monotonic-clock@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function now(): $t.Instant; + export function subscribeDuration(when: $t.Duration): $t.Pollable; + + export { + Instant, + Duration, + } from 'starling:types/starling:serve/serve'; + export type { + Pollable, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:io/error@0.2' { + export { + Error, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:io/streams@0.2' { + export { + StreamErrorError as StreamError, + InputStream, + OutputStream, + } from 'starling:types/starling:serve/serve'; + export type { + Error, + Pollable, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:http/types@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function httpErrorCode(err: $t.Error): $t.WasiHttpTypesErrorCode | undefined; + + export { + HeaderErrorError as HeaderError, + WasiHttpTypesErrorCodeError as ErrorCode, + Duration, + Method, + Scheme, + DnsErrorPayload, + TlsAlertReceivedPayload, + FieldSizePayload, + FieldKey, + FieldKey as FieldName, + FieldValue, + Fields, + IncomingRequest, + OutgoingRequest, + RequestOptions, + ResponseOutparam, + StatusCode, + IncomingResponse, + IncomingBody, + FutureTrailers, + OutgoingResponse, + OutgoingBody, + FutureIncomingResponse, + } from 'starling:types/starling:serve/serve'; + export type { + InputStream, + OutputStream, + Error as IoError, + Pollable, + Fields as Headers, + Fields as Trailers, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/environment@0.2' { + export function getEnvironment(): [string, string][]; + export function getArguments(): string[]; +} + +declare module 'wasi:cli/exit@0.2' { + export function exit(status: { tag: 'ok' } | { tag: 'err' }): void; +} + +declare module 'wasi:cli/stderr@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getStderr(): $t.OutputStream; + + export type { + OutputStream, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/stdin@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getStdin(): $t.InputStream; + + export type { + InputStream, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/stdout@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getStdout(): $t.OutputStream; + + export type { + OutputStream, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/terminal-input@0.2' { + export { + TerminalInput, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/terminal-output@0.2' { + export { + TerminalOutput, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/terminal-stderr@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getTerminalStderr(): $t.TerminalOutput | undefined; + + export type { + TerminalOutput, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/terminal-stdin@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getTerminalStdin(): $t.TerminalInput | undefined; + + export type { + TerminalInput, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:cli/terminal-stdout@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getTerminalStdout(): $t.TerminalOutput | undefined; + + export type { + TerminalOutput, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:clocks/wall-clock@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function now(): $t.Datetime; + + export { + Datetime, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:filesystem/types@0.2' { + export { + WasiFilesystemTypesErrorCodeError as ErrorCode, + Datetime, + Filesize, + DescriptorType, + DescriptorFlagsSet, + DescriptorFlags, + PathFlagsSet, + PathFlags, + OpenFlagsSet, + OpenFlags, + LinkCount, + DescriptorStat, + NewTimestamp, + DirectoryEntry, + Advice, + MetadataHashValue, + Descriptor, + DirectoryEntryStream, + } from 'starling:types/starling:serve/serve'; + export type { + InputStream, + OutputStream, + Error, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:filesystem/preopens@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function getDirectories(): [$t.Descriptor, string][]; + + export type { + Descriptor, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:http/outgoing-handler@0.2' { + import type * as $t from 'starling:types/starling:serve/serve'; + + export function handle(request: $t.OutgoingRequest, options: $t.RequestOptions | undefined | null): $t.FutureIncomingResponse; + + export { + OutgoingHandlerErrorCodeError as ErrorCode, + } from 'starling:types/starling:serve/serve'; + export type { + OutgoingRequest, + RequestOptions, + FutureIncomingResponse, + } from 'starling:types/starling:serve/serve'; +} + +declare module 'wasi:random/insecure-seed@0.2' { + export function insecureSeed(): [bigint, bigint]; +} + +declare module 'wasi:random/random@0.2' { + export function getRandomBytes(len: bigint): Uint8Array; +} + diff --git a/componentize/tests/wit_to_ts/exported_resource.d.ts.expected b/componentize/tests/wit_to_ts/exported_resource.d.ts.expected index 4970161f..122b3afc 100644 --- a/componentize/tests/wit_to_ts/exported_resource.d.ts.expected +++ b/componentize/tests/wit_to_ts/exported_resource.d.ts.expected @@ -59,7 +59,6 @@ declare module 'starling:guest' { counters: { makeCounter(start: number): $t.Counter; makeSealedCounter(start: number): $t.Counter; - readLater(c: $t.Counter): number | Promise; Counter: typeof $t.Counter; }; }; @@ -68,14 +67,12 @@ declare module 'starling:guest' { export const counters: { makeCounter(start: number): $t.Counter; makeSealedCounter(start: number): $t.Counter; - readLater(c: $t.Counter): number | Promise; Counter: typeof $t.Counter; }; // Items of the exported interface `test:exported-resource/counters`. export function makeCounter(start: number): $t.Counter; export function makeSealedCounter(start: number): $t.Counter; - export function readLater(c: $t.Counter): number | Promise; export { Counter, } from 'starling:types/test:exported-resource/exported-resource'; diff --git a/componentize/tests/wit_to_ts/serve_p2.d.ts.expected b/componentize/tests/wit_to_ts/serve_p2.d.ts.expected new file mode 100644 index 00000000..305de2c2 --- /dev/null +++ b/componentize/tests/wit_to_ts/serve_p2.d.ts.expected @@ -0,0 +1,138 @@ +// Generated by `starling-componentize types`. Do not edit by hand. +// +// The guest JavaScript module a `serve-p2` componentize target implements, and +// the modules it may import. Add this file to the program (a `files` or +// `include` entry in `tsconfig.json`, or a `/// `). +// +// Nothing here is exported at file level, which makes every `declare +// module` below an ambient module declaration. An import then +// resolves: +// +// import { log } from 'test:example/logger'; +// +// The guest's own obligations are `starling:guest`. Reference it to have the +// compiler check what the module exports: +// +// import type * as Guest from 'starling:guest'; +// export const example: typeof Guest.example = { … }; +// +// Every WIT type the world uses is declared in the module +// +// starling:types/test:serve-p2/serve-p2 +// +// Each interface's module re-exports the types the interface defines or +// `use`s under their WIT names. A name that several types share is +// qualified there. + +// `ComponentError` holds the `err` payload of a WIT `result`. A string payload +// is also its `message`. The error classes in the types module extend it. +interface ComponentError extends Error { + readonly payload: unknown; +} +declare var ComponentError: { + new (payload: unknown): ComponentError; + readonly prototype: ComponentError; +}; + +// The event the runtime dispatches to `addEventListener('fetch', …)` listeners. +interface FetchEvent extends Event { + readonly request: Request; + respondWith(response: Response | PromiseLike): void; + waitUntil(promise: PromiseLike): void; +} +interface WindowEventMap { + fetch: FetchEvent; +} + +// `fetch` also takes a `wasi:http/types` `incoming-request`, such as the one a +// `wasi:http/incoming-handler` implementation receives, and sends it. +declare function fetch( + input: import('starling:types/test:serve-p2/serve-p2').IncomingRequest, + init?: RequestInit, +): Promise; + +declare module 'starling:types/test:serve-p2/serve-p2' { + export class IncomingRequest { + private constructor(); + pathWithQuery(): string | undefined; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingResponse { + constructor(); + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class OutgoingBody { + private constructor(); + static finish(this_: OutgoingBody): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export type ErrorCode = { tag: 'internalError'; val?: string | undefined }; + + export class ResponseOutparam { + private constructor(); + static set(param: ResponseOutparam, response: { tag: 'ok'; val: OutgoingResponse } | { tag: 'err'; val: ErrorCode }): void; + private $brand: unknown; + [Symbol.dispose](): void; + } + + export class ErrorCodeError extends ComponentError { + constructor(payload: ErrorCode); + readonly payload: ErrorCode; + } +} + +declare module 'starling:guest' { + import type * as $t from 'starling:types/test:serve-p2/serve-p2'; + + // The `wasi:http/incoming-handler` export is served in one of two ways. Either + // register a listener on the global, and the runtime dispatches each + // request to it as a `FetchEvent`: + // + // addEventListener('fetch', (event) => { + // event.respondWith(new Response('hello')); + // }); + // + // Or implement `handle` below, which receives the WIT request and returns a + // `Response` or the WIT `response`, or a promise of either. `fetch(request)` + // sends the request on. It fails with an `error-code` by throwing an + // `ErrorCode` from `wasi:http/types@0.2.0`. + // + // An application that does both, or neither, fails to componentize. + + // Each item of an exported interface is implemented through exactly one + // of the shapes below: a member of its package's object, a member of its + // interface's object, or a bare export. A shape is declared only where the + // world allows it for the item. + + export const wasiHttp: { + // The exported interface `wasi:http/incoming-handler`. + incomingHandler: { + handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; + }; + }; + + // The exported interface `wasi:http/incoming-handler`. + export const incomingHandler: { + handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; + }; + + // Items of the exported interface `wasi:http/incoming-handler`. + export function handle(request: $t.IncomingRequest): Response | $t.OutgoingResponse | Promise; +} + +declare module 'wasi:http/types@0.2' { + export { + ErrorCodeError as ErrorCode, + IncomingRequest, + OutgoingResponse, + OutgoingBody, + ResponseOutparam, + } from 'starling:types/test:serve-p2/serve-p2'; +} + diff --git a/componentize/tests/wit_to_ts/serve_p2.handler.ts b/componentize/tests/wit_to_ts/serve_p2.handler.ts new file mode 100644 index 00000000..b69416ab --- /dev/null +++ b/componentize/tests/wit_to_ts/serve_p2.handler.ts @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception +// +// A guest implementing the `wasi:http/incoming-handler` of a runtime built for WASIp2 itself in +// the package layer, returning either a web `Response` or the WIT `outgoing-response`, forwarding +// the WIT request with `fetch`, and failing with an `ErrorCode`. +import type * as Guest from 'starling:guest'; +import { ErrorCode, OutgoingResponse } from 'wasi:http/types@0.2'; + +export const wasiHttp: typeof Guest.wasiHttp = { + incomingHandler: { + async handle(request) { + const path = request.pathWithQuery(); + if (path === '/fail') { + throw new ErrorCode({ tag: 'internalError', val: 'failed' }); + } + if (path === '/wit') { + return new OutgoingResponse(); + } + if (path === '/forward') { + return await fetch(request); + } + return new Response(`hello ${path}`); + }, + }, +}; diff --git a/componentize/tests/wit_to_ts/serve_p2.wit b/componentize/tests/wit_to_ts/serve_p2.wit new file mode 100644 index 00000000..548a0c89 --- /dev/null +++ b/componentize/tests/wit_to_ts/serve_p2.wit @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception +// +// The world the componentizer gives an application that implements the +// `wasi:http/incoming-handler` of a runtime built for WASIp2 itself, against a minimal stub of +// `wasi:http/types@0.2.0`. +// +// The guest implements `handle` under `wasiHttp.incomingHandler`, under `incomingHandler`, or as a +// bare export. `handle` receives the `incoming-request`, but not the `response-outparam`, which +// the runtime sets. It returns a `Response` or the `outgoing-response` the WIT declares, and fails +// with the `ErrorCode` class of `wasi:http/types`. +package test:serve-p2; + +world serve-p2 { + import wasi:http/types@0.2.0; + export wasi-http-handler: interface { + use wasi:http/types@0.2.0.{incoming-request, response-outparam}; + + handle: func(request: incoming-request, response-out: response-outparam); + } +} + +package wasi:http@0.2.0 { + interface types { + variant error-code { + internal-error(option), + } + + resource incoming-request { + path-with-query: func() -> option; + } + + resource outgoing-response { + constructor(); + } + + resource outgoing-body { + finish: static func(this: outgoing-body) -> result<_, error-code>; + } + + resource response-outparam { + set: static func(param: response-outparam, response: result); + } + } +} diff --git a/crates/libstarling/Cargo.toml b/crates/libstarling/Cargo.toml index ec30be87..464387f9 100644 --- a/crates/libstarling/Cargo.toml +++ b/crates/libstarling/Cargo.toml @@ -40,9 +40,6 @@ hyper.workspace = true hyper-util.workspace = true tokio.workspace = true -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasip3.workspace = true - [dev-dependencies] futures-channel.workspace = true serve-test-support.workspace = true diff --git a/crates/libstarling/src/lib.rs b/crates/libstarling/src/lib.rs index 0ab65018..e6d0744f 100644 --- a/crates/libstarling/src/lib.rs +++ b/crates/libstarling/src/lib.rs @@ -73,11 +73,9 @@ pub fn run(config: config::RuntimeConfig) -> Result<(), String> { /// Run a JavaScript script or module on wasm32 targets. /// -/// Must be `.await`ed from inside an async-lifted `wasi:cli/run.run` task -/// — the event loop awaits WASIp3 async-lowered imports (e.g. -/// `monotonic_clock::wait_for`), and those are only valid inside an -/// async-lifted task. The cdylib in the `starling` package provides such a -/// task via `wasip3::cli::command::export!`. +/// On WASIp3, it must be `.await`ed from inside an async-lifted `wasi:cli/run.run` task, since +/// the event loop awaits async-lowered imports such as `monotonic_clock::wait_for`. On WASIp2, it +/// must run inside [`platform::task::block_on`]. #[cfg(target_arch = "wasm32")] pub async fn run(config: config::RuntimeConfig) -> Result<(), String> { apply_pre_init_config(&config)?; diff --git a/crates/libstarling/src/serve_wasm.rs b/crates/libstarling/src/serve_wasm.rs index 5018f8c2..5d1cefea 100644 --- a/crates/libstarling/src/serve_wasm.rs +++ b/crates/libstarling/src/serve_wasm.rs @@ -1,13 +1,14 @@ // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception -//! Wasm serve mode: the `wasi:http/handler` export's body. The runtime is created once, running -//! the content script that registers the `fetch` handlers, and reused across requests, each -//! dispatched as a `fetch` event on its own [`EventLoop`]. +//! Wasm serve mode: the body of the HTTP handler export, `wasi:http/handler` on WASIp3 and +//! `wasi:http/incoming-handler` on WASIp2. The runtime is created once, running the content +//! script that registers the `fetch` handlers, and reused across requests, each dispatched as a +//! `fetch` event on its own [`EventLoop`]. //! -//! A WASIp3 host keeps sending requests to an instance rather than retiring one per request: -//! sequentially, and concurrently whenever every request in flight is parked on I/O or a timer. -//! Requests therefore share one global, and are separated by their event loops rather than their -//! realms. +//! A host can keep sending requests to an instance rather than retiring one per request: a +//! WASIp3 host sequentially, and concurrently whenever every request in flight is parked on I/O or +//! a timer, and a WASIp2 host sequentially. Requests therefore share one global, and are separated +//! by their event loops rather than their realms. //! //! Shares the dispatch core in `serve_common` with `serve_native`, and the request/response //! bridging in `platform::http`. @@ -20,10 +21,10 @@ use core_runtime::event_loop::{run_until_evaluated, EventLoop}; use core_runtime::invocation::{InvocationState, OwnedInvocation}; use core_runtime::runtime::Runtime; use platform::http::OutgoingBody; +use platform::http::{HostErrorCode, HostRequest, HostResponse}; use std::cell::RefCell; use std::rc::Rc; use std::sync::atomic::AtomicU64; -use wasip3::http::types::{ErrorCode, Request as WasiRequest, Response as WasiResponse}; js::instance_local! { /// The JS runtime and its raw context, created once. The single global's realm is entered for @@ -174,7 +175,7 @@ pub fn adopt_runtime( Ok(()) } -/// The implementation that serves a componentized application's `wasi:http/handler` export. +/// The implementation that serves a componentized application's HTTP handler export. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum HttpHandler { /// The runtime's builtin, which dispatches each request as a `fetch` event. @@ -183,7 +184,7 @@ pub enum HttpHandler { Raw, } -/// A componentized application's own export of `wasi:http/handler`. +/// A componentized application's own export of the HTTP handler interface. pub struct RawHttpHandler<'a> { /// The JS paths the main module may provide the interface's `handle` under, /// such as `handler.handle`. @@ -193,7 +194,7 @@ pub struct RawHttpHandler<'a> { } /// Select the [`HttpHandler`] of a componentized application whose component exports -/// `wasi:http/handler`, once its top level has finished evaluating. +/// the HTTP handler interface, once its top level has finished evaluating. /// /// Fails if the application both provides `raw` and registers a `fetch` listener, or does /// neither. @@ -214,42 +215,40 @@ pub unsafe fn select_http_handler( .map(|path| format!("`{path}`")) .collect::>() .join(" or "); + let handler = platform::http::HOST_HTTP_HANDLER; match (raw.provided, listener) { (true, false) => Ok(HttpHandler::Raw), (false, true) => Ok(HttpHandler::FetchEvent), (true, true) => Err(format!( - "the application both exports a `wasi:http/handler` implementation as {paths} and \ - registers a `fetch` listener. The component's `wasi:http/handler` export is served \ + "the application both exports a `{handler}` implementation as {paths} and \ + registers a `fetch` listener. The component's `{handler}` export is served \ by exactly one of them, so remove the other." )), (false, false) => Err(format!( - "the application neither exports a `wasi:http/handler` implementation as {paths} \ - nor registers a `fetch` listener, so the component's `wasi:http/handler` export \ + "the application neither exports a `{handler}` implementation as {paths} \ + nor registers a `fetch` listener, so the component's `{handler}` export \ could not serve any request. Export `handle(request)`, or register a listener \ with `addEventListener('fetch', …)`." )), } } -/// The `wasi:http/types` interface of the `wasi:http` version the runtime links: -/// the one its `wasip3` bindings target. -pub const WASI_HTTP_TYPES: &str = "wasi:http/types@0.3.0"; - /// A `Request` with the method, URL, headers and body of the incoming -/// [`WASI_HTTP_TYPES`] `request` `handle`, which it takes ownership of. The body -/// is not read until the `Request`'s body is. Throws a `TypeError` for a request -/// whose fields `http` cannot represent. +/// [`HOST_REQUEST_RESOURCE`](platform::http::HOST_REQUEST_RESOURCE) `handle`, +/// which it takes ownership of. The body is not read until the `Request`'s body +/// is. Throws a `TypeError` for a request whose fields `http` cannot represent. /// /// # Safety /// -/// `handle` must be a live handle of a [`WASI_HTTP_TYPES`] `request` that the +/// `handle` must be a live handle of a +/// [`HOST_REQUEST_RESOURCE`](platform::http::HOST_REQUEST_RESOURCE) that the /// caller owns, and must not be used after this call. pub unsafe fn request_from_handle<'s>( scope: &'s js::gc::scope::Scope<'_>, handle: u32, ) -> Result, js::error::ExnThrown> { // SAFETY: guaranteed by the caller. - let request = unsafe { WasiRequest::from_handle(handle) }; + let request = unsafe { HostRequest::from_handle(handle) }; let (method, url, headers, body) = platform::http::read_incoming_request(request).map_err(|code| { js::error::ThrowException::throw( @@ -632,7 +631,7 @@ fn report_missing_fetch_listener( /// left nothing behind to the instance's lifetime for one whose `fetch` never responds. // TODO: Should reconsider this, it might make more sense to abort async work that's not added to waitUntil. fn keep_startup_loop_running(raw_cx: *mut js::native::RawJSContext, invocation: OwnedInvocation) { - wasip3::wit_bindgen::spawn_local(async move { + platform::task::spawn_background(async move { // SAFETY: `runtime` entered the default global's realm for the process lifetime, and // `raw_cx` outlives this task, since the runtime is held in a process-lifetime // thread-local. @@ -650,7 +649,7 @@ fn keep_startup_loop_running(raw_cx: *mut js::native::RawJSContext, invocation: /// Handle one incoming request: create (or reuse) the runtime, drive the /// content script's startup loop, then dispatch the request. -pub async fn handle(wasi_request: WasiRequest) -> Result { +pub async fn handle(wasi_request: HostRequest) -> Result { let Ok((runtime, raw_cx, timeouts)) = runtime() else { // No runtime means no loop to drain, and no config to take a timeout from. return Ok(error_response(500, "Internal Server Error", None).0); @@ -672,9 +671,9 @@ pub async fn handle(wasi_request: WasiRequest) -> Result, raw_cx: *mut js::native::RawJSContext, - wasi_request: WasiRequest, + wasi_request: HostRequest, timeouts: ServeTimeouts, -) -> Result { +) -> Result { let clock = timeouts.start_clock(); let (method, url, headers, body) = match platform::http::read_incoming_request(wasi_request) { Ok(parts) => parts, @@ -745,7 +744,7 @@ async fn dispatch_request( }; // Continue running the event loop until the response body has been fully sent out and all // `waitUntil` promises have been settled. - wasip3::wit_bindgen::spawn_local(async move { + platform::task::spawn(async move { // SAFETY: the process-lifetime realm is entered, and `raw_cx` outlives this task. unsafe { let outcome = drive_body_send( @@ -877,7 +876,7 @@ fn error_response( message: &str, body_timeout: Option, ) -> ( - WasiResponse, + HostResponse, platform::http::BodyDone, platform::http::AbandonBody, ) { diff --git a/crates/platform/Cargo.toml b/crates/platform/Cargo.toml index 199b6135..0eb38644 100644 --- a/crates/platform/Cargo.toml +++ b/crates/platform/Cargo.toml @@ -22,8 +22,14 @@ reqwest.workspace = true [target.'cfg(target_arch = "wasm32")'.dependencies] http.workspace = true url.workspace = true + +[target.'cfg(target_env = "p2")'.dependencies] +async-task.workspace = true +wasip2.workspace = true + +[target.'cfg(target_env = "p3")'.dependencies] wasip3.workspace = true -wit-bindgen.workspace = true +wit-bindgen = { workspace = true, features = ["async"] } [lints] workspace = true diff --git a/crates/platform/src/clock.rs b/crates/platform/src/clock.rs index c2dd48c1..bea0b4b7 100644 --- a/crates/platform/src/clock.rs +++ b/crates/platform/src/clock.rs @@ -18,7 +18,12 @@ static OFFSET_NS: AtomicU64 = AtomicU64::new(0); /// [`resume_from_snapshot`] to apply. Zero for an instance that was never wizened. static SNAPSHOT_NS: AtomicU64 = AtomicU64::new(0); -#[cfg(target_arch = "wasm32")] +#[cfg(target_env = "p2")] +fn host_monotonic_ns() -> u64 { + wasip2::clocks::monotonic_clock::now() +} + +#[cfg(target_env = "p3")] fn host_monotonic_ns() -> u64 { wasip3::clocks::monotonic_clock::now() } @@ -100,9 +105,12 @@ pub fn resume_from_snapshot() -> u64 { } /// Wait for `duration` on the host's monotonic clock. -#[cfg(target_arch = "wasm32")] +#[cfg(any(target_env = "p2", target_env = "p3"))] pub async fn sleep(duration: std::time::Duration) { let nanos = duration.as_nanos().min(u64::MAX as u128) as u64; + #[cfg(target_env = "p2")] + crate::task::wait_for(wasip2::clocks::monotonic_clock::subscribe_duration(nanos)).await; + #[cfg(target_env = "p3")] wasip3::clocks::monotonic_clock::wait_for(nanos).await; } diff --git a/crates/platform/src/http.rs b/crates/platform/src/http.rs index fcbbf83c..0b24f03c 100644 --- a/crates/platform/src/http.rs +++ b/crates/platform/src/http.rs @@ -2,24 +2,36 @@ //! Async HTTP transport. //! -//! `send` returns once the response status and headers are available; the body -//! is a [`IncomingBody`] read lazily from the host (reqwest on native, the -//! WASIp3 incoming body stream on wasm). Callers either drain it with +//! `send` returns once the response status and headers are available. The body +//! is an [`IncomingBody`] read lazily from the host: reqwest natively, and the +//! `wasi:http` incoming body stream on wasm. Callers either drain it with //! [`IncomingBody::read_all`] or read it piece by piece with //! [`IncomingBody::read_into`]. Natively, `IncomingBody::next_chunk` also returns //! each chunk as it was received. //! -//! This module holds the platform-agnostic parts; the backends live in -//! [`http_native`](crate::http_native) and [`http_wasm`](crate::http_wasm), -//! whose platform-specific items are re-exported here. +//! This module holds the platform-agnostic parts. The backends are `http_native`, `http_p2` and +//! `http_p3`, and the two wasm backends share `http_wasm`. Their platform-specific items are +//! re-exported here. +//! +//! On wasm, `HostRequest`, `HostResponse` and `HostErrorCode` are the `wasi:http` types of the +//! runtime's HTTP handler export. #[cfg(not(target_arch = "wasm32"))] pub use crate::http_native::{incoming_body_channel, send, IncomingBody, IncomingBodySender}; -#[cfg(target_arch = "wasm32")] -pub use crate::http_wasm::{ - build_outgoing_response, read_incoming_request, send, AbandonBody, BodyDone, BodySendOutcome, - IncomingBody, +#[cfg(target_env = "p2")] +pub use crate::http_p2::{ + build_outgoing_response, read_incoming_request, send, HostErrorCode, HostRequest, HostResponse, + IncomingBody, HOST_HTTP_HANDLER, HOST_HTTP_TYPES, HOST_REQUEST_RESOURCE, + HOST_RESPONSE_RESOURCE, }; +#[cfg(target_env = "p3")] +pub use crate::http_p3::{ + build_outgoing_response, read_incoming_request, send, HostErrorCode, HostRequest, HostResponse, + IncomingBody, HOST_HTTP_HANDLER, HOST_HTTP_TYPES, HOST_REQUEST_RESOURCE, + HOST_RESPONSE_RESOURCE, +}; +#[cfg(target_arch = "wasm32")] +pub use crate::http_wasm::{AbandonBody, BodyDone, BodySendOutcome}; /// An outgoing HTTP request. pub struct Request { diff --git a/crates/platform/src/http_p2.rs b/crates/platform/src/http_p2.rs new file mode 100644 index 00000000..839e2cd9 --- /dev/null +++ b/crates/platform/src/http_p2.rs @@ -0,0 +1,398 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +//! The WASIp2 backend of [`http`](crate::http): the `wasi:http@0.2` client for sending, +//! [`IncomingBody`] wrapping an incoming body's input stream, and the request and response +//! conversions of the `wasi:http/incoming-handler` export. +//! +//! `wasi:http@0.2` gives every body to the guest as a `wasi:io` stream, so the guest copies a +//! host body it passes on, chunk by chunk. + +use wasip2::http::types::{ + ErrorCode, Fields, IncomingBody as WasiIncomingBody, IncomingRequest, IncomingResponse, Method, + OutgoingBody as WasiOutgoingBody, OutgoingRequest, OutgoingResponse, Scheme, +}; +use wasip2::io::streams::{InputStream, OutputStream, StreamError}; + +use crate::http::{BodyBytes, Error, OutgoingBody, Remaining, Request, Response}; +use crate::http_wasm::{ + AbandonBody, BodyDone, BodySendOutcome, HostBodyWriter, UrlParts, HOST_STOPPED_READING, +}; + +/// The `wasi:http/types` interface of the `wasi:http` version the runtime links: the one its +/// `wasip2` bindings target. +pub const HOST_HTTP_TYPES: &str = "wasi:http/types@0.2.12"; + +/// The interface of the HTTP handler export, without a version. +pub const HOST_HTTP_HANDLER: &str = "wasi:http/incoming-handler"; + +/// The resource of [`HOST_HTTP_TYPES`] that [`HostRequest`] is. +pub const HOST_REQUEST_RESOURCE: &str = "incoming-request"; + +/// The resource of [`HOST_HTTP_TYPES`] that [`HostResponse`] is. +pub const HOST_RESPONSE_RESOURCE: &str = "outgoing-response"; + +/// The request the `wasi:http/incoming-handler` export receives. +pub type HostRequest = IncomingRequest; +/// The response the `wasi:http/incoming-handler` export sets its response outparam to. +pub type HostResponse = OutgoingResponse; +/// The error the `wasi:http/incoming-handler` export sets its response outparam to. +pub type HostErrorCode = ErrorCode; + +/// The size of the reads from an incoming body's stream. +const READ_SIZE: u64 = 64 * 1024; + +crate::http_wasm::wasi_http_conversions!( + method: Method, + scheme: Scheme, + fields: Fields, + request: OutgoingRequest, + response: OutgoingResponse, +); + +/// Send `request` with the `wasi:http` client, returning once the response status and headers +/// are available. +pub async fn send(request: Request) -> Result { + let url = UrlParts::parse(&request.url)?; + let wasi_request = OutgoingRequest::new(request_fields(&request)?); + set_request_target(&wasi_request, &request.method, &url)?; + let wasi_body = wasi_request + .body() + .expect("a new request's body is taken only here"); + + let future = wasip2::http::outgoing_handler::handle(wasi_request, None) + .map_err(|e| Error(format!("fetch failed: {e:?}")))?; + // No timeout: the serve timeouts bound responses, not an outgoing request's body. + drop(spawn_body_writer(wasi_body, request.body, None, None)); + + crate::task::wait_for(future.subscribe()).await; + let response = match future.get() { + Some(Ok(Ok(response))) => response, + Some(Ok(Err(e))) => return Err(Error(format!("fetch failed: {e:?}"))), + Some(Err(())) | None => { + return Err(Error( + "fetch failed: the host did not provide the response".to_string(), + )) + } + }; + let status = response.status(); + let headers = response + .headers() + .entries() + .into_iter() + .map(|(name, value)| (name, crate::http::isomorphic_decode(&value))) + .collect(); + let body = IncomingBody::from_parent(BodyParent::Response(response)) + .map_err(|message| Error(format!("fetch failed: {message}")))?; + Ok(Response { + status, + headers, + body, + }) +} + +/// Read an incoming `wasi:http` request into its parts (method, full URL, headers, body). +/// +/// Returns `Err` for a field the host accepted that `http` cannot represent. +pub fn read_incoming_request( + request: IncomingRequest, +) -> Result<(String, String, http::HeaderMap, IncomingBody), ErrorCode> { + let method = string_of_method(request.method()); + let url = incoming_request_url( + request.scheme(), + request.authority(), + request.path_with_query(), + ); + let mut headers = http::HeaderMap::new(); + for (name, value) in request.headers().entries() { + let unrepresentable = + || ErrorCode::InternalError(Some(format!("the request header `{name}` is invalid"))); + let header_name = + http::HeaderName::from_bytes(name.as_bytes()).map_err(|_| unrepresentable())?; + let header_value = http::HeaderValue::from_bytes(&value).map_err(|_| unrepresentable())?; + headers.append(header_name, header_value); + } + let body = IncomingBody::from_parent(BodyParent::Request(request)) + .map_err(|message| ErrorCode::InternalError(Some(message)))?; + Ok((method, url, headers, body)) +} + +/// Build a `wasi:http` response from parts. +/// +/// The body is written from a task started with [`spawn`](crate::task::spawn), so the export call +/// finishes sending it before it returns. The response must be set as the export's response +/// before that task first runs, since the host doesn't read the body before then. +/// +/// The returned [`BodyDone`] resolves once every chunk was handed to the host and the body was +/// finished. `body_timeout` bounds the whole send, including copying a host body and waiting for +/// the host to take the last chunk. +/// +/// `declared_length` is used as the `Content-Length` header, and enforced for streamed and host +/// bodies. +pub fn build_outgoing_response( + status: u16, + headers: http::HeaderMap, + body: OutgoingBody, + body_timeout: Option, + declared_length: Option, +) -> (OutgoingResponse, BodyDone, AbandonBody) { + let response = OutgoingResponse::new(response_fields(&headers)); + set_status(&response, status); + let wasi_body = response + .body() + .expect("a new response's body is taken only here"); + let (body_done, abandon) = spawn_body_writer(wasi_body, body, body_timeout, declared_length); + (response, body_done, abandon) +} + +/// Write `contents` to `body` from a task started with [`spawn`](crate::task::spawn), then +/// finish `body`. A body whose send doesn't end in [`BodySendOutcome::Sent`] is dropped without +/// being finished, which makes the host abort it. +/// +/// The returned [`BodyDone`] resolves when the send is over, with its [`BodySendOutcome`]. +fn spawn_body_writer( + body: WasiOutgoingBody, + contents: OutgoingBody, + timeout: Option, + declared_length: Option, +) -> (BodyDone, AbandonBody) { + let (done_tx, done_rx) = futures_channel::oneshot::channel::(); + let (abandon_tx, abandon_rx) = futures_channel::oneshot::channel::<()>(); + crate::task::spawn(async move { + let outcome = match BodyWriter::new(body) { + None => BodySendOutcome::Failed("the host refused the body's stream".to_string()), + Some(mut writer) => { + let send = async move { + let outcome = crate::http_wasm::write_owned_body( + &mut writer, + contents, + declared_length, + abandon_rx, + ) + .await; + match outcome { + BodySendOutcome::Sent => match writer.finish().await { + Ok(()) => BodySendOutcome::Sent, + Err(message) => BodySendOutcome::Failed(message), + }, + outcome => outcome, + } + }; + crate::http_wasm::bounded(timeout, send).await + } + }; + let _ = done_tx.send(outcome); + }); + (BodyDone(done_rx), AbandonBody(abandon_tx)) +} + +/// An outgoing body and its output stream. Dropping it without [`BodyWriter::finish`] makes the +/// host abort the body. +struct BodyWriter { + // Fields are dropped in declaration order, so the stream is dropped before the body it + // belongs to, which the host requires. + stream: OutputStream, + body: WasiOutgoingBody, + /// The trailers [`BodyWriter::finish`] sends. + trailers: Option, +} + +impl BodyWriter { + fn new(body: WasiOutgoingBody) -> Option { + let stream = body.write().ok()?; + Some(BodyWriter { + stream, + body, + trailers: None, + }) + } + + /// Wait until everything written was flushed, then finish the body with its trailers. + async fn finish(self) -> Result<(), String> { + let flushed = self.flush().await; + let BodyWriter { + stream, + body, + trailers, + } = self; + drop(stream); + if !flushed { + drop(body); + return Err(HOST_STOPPED_READING.to_string()); + } + WasiOutgoingBody::finish(body, trailers) + .map_err(|e| format!("the host refused to finish the body: {e:?}")) + } + + /// Wait until everything written was flushed. Returns `false` if the write failed. + /// + /// The host closes the stream once a body has the length its `Content-Length` declares, so a + /// closed stream counts as flushed. [`WasiOutgoingBody::finish`] fails for a body that is + /// shorter. + async fn flush(&self) -> bool { + match self.stream.flush() { + Ok(()) => {} + Err(StreamError::Closed) => return true, + Err(StreamError::LastOperationFailed(_)) => return false, + } + // The flush is complete once the stream accepts writes again. + loop { + match self.stream.check_write() { + Ok(0) => crate::task::wait_for(self.stream.subscribe()).await, + Ok(_) | Err(StreamError::Closed) => return true, + Err(StreamError::LastOperationFailed(_)) => return false, + } + } + } +} + +impl HostBodyWriter for BodyWriter { + async fn send_chunk(&mut self, chunk: Vec) -> bool { + let mut written = 0; + while written < chunk.len() { + match self.stream.check_write() { + Ok(0) => crate::task::wait_for(self.stream.subscribe()).await, + Ok(permitted) => { + let end = chunk + .len() + .min(written + usize::try_from(permitted).unwrap_or(usize::MAX)); + if self.stream.write(&chunk[written..end]).is_err() { + return false; + } + written = end; + } + Err(_) => return false, + } + } + true + } + + async fn send_host_body( + &mut self, + mut body: IncomingBody, + remaining: &mut Remaining, + ) -> BodySendOutcome { + loop { + match body.next_part(READ_SIZE).await { + Ok(BodyPart::Chunk(chunk)) => { + // Content past the declared length is dropped, and the body ends. + let Some(chunk) = remaining.take(chunk) else { + return BodySendOutcome::Sent; + }; + if !self.send_chunk(chunk).await { + return BodySendOutcome::Failed(HOST_STOPPED_READING.to_string()); + } + } + Ok(BodyPart::End(trailers)) => { + self.trailers = trailers; + return crate::http_wasm::outcome_at_end(remaining); + } + Err(e) => return BodySendOutcome::Failed(e.0), + } + } + } +} + +/// The resource an incoming body was consumed from, which the host requires to outlive it. +enum BodyParent { + Request(IncomingRequest), + Response(IncomingResponse), +} + +/// What [`IncomingBody::next_part`] read. +enum BodyPart { + Chunk(Vec), + /// The body ended cleanly, with these trailers. + End(Option), +} + +/// A body read lazily from the host's incoming `wasi:http` body stream: an HTTP client response, +/// or an incoming request body on the serve path. +pub struct IncomingBody { + // Fields are dropped in declaration order, so each resource is dropped before the one it was + // obtained from, which the host requires. `stream` and `body` are `None` once the body ended. + stream: Option, + body: Option, + _parent: BodyParent, +} + +impl IncomingBody { + /// Consume `parent`'s body. `Err` explains why the host refused it. + fn from_parent(parent: BodyParent) -> Result { + let body = match &parent { + BodyParent::Request(request) => request.consume(), + BodyParent::Response(response) => response.consume(), + } + .map_err(|()| "the host refused the body".to_string())?; + let stream = body + .stream() + .map_err(|()| "the host refused the body's stream".to_string())?; + Ok(IncomingBody { + stream: Some(stream), + body: Some(body), + _parent: parent, + }) + } + + /// Read the entire body to a byte sequence. + pub async fn read_all(mut self) -> Result { + let mut bytes = Vec::new(); + while let BodyPart::Chunk(chunk) = self.next_part(READ_SIZE).await? { + bytes.extend_from_slice(&chunk); + } + Ok(BodyBytes::from(bytes)) + } + + /// Read the next bytes, at most as many as `buf` has capacity for. Returns the bytes read, or + /// `Ok(None)` at the end of the body. The bytes are never empty, and are in an allocation + /// made by the host rather than in `buf`. + pub async fn read_into(&mut self, buf: Vec) -> Result>, Error> { + let max = u64::try_from(buf.capacity()).unwrap_or(u64::MAX); + match self.next_part(max).await? { + BodyPart::Chunk(chunk) => Ok(Some(chunk)), + BodyPart::End(_) => Ok(None), + } + } + + /// Read the next chunk of at most `max` bytes, or the trailers at end of body. Returns an + /// error if the peer aborted the body. Once the body has ended, every call returns + /// `BodyPart::End(None)`. + async fn next_part(&mut self, max: u64) -> Result { + loop { + let Some(stream) = &self.stream else { + return Ok(BodyPart::End(None)); + }; + match stream.read(max) { + Ok(chunk) if !chunk.is_empty() => return Ok(BodyPart::Chunk(chunk)), + Ok(_) => crate::task::wait_for(stream.subscribe()).await, + Err(StreamError::Closed) => return self.end_of_body().await.map(BodyPart::End), + Err(StreamError::LastOperationFailed(e)) => { + let message = match wasip2::http::types::http_error_code(&e) { + Some(code) => format!("the body was aborted: {code:?}"), + None => format!("the body was aborted: {}", e.to_debug_string()), + }; + self.stream = None; + self.body = None; + return Err(Error(message)); + } + } + } + } + + /// Finish a body whose stream reported its end, and return its trailers, or an error if the + /// peer aborted the body. + async fn end_of_body(&mut self) -> Result, Error> { + self.stream = None; + let Some(body) = self.body.take() else { + return Ok(None); + }; + let trailers = WasiIncomingBody::finish(body); + crate::task::wait_for(trailers.subscribe()).await; + match trailers.get() { + Some(Ok(Ok(trailers))) => Ok(trailers), + Some(Ok(Err(code))) => Err(Error(format!("the body was aborted: {code:?}"))), + Some(Err(())) | None => Err(Error( + "the host did not provide the body's trailers".to_string(), + )), + } + } +} diff --git a/crates/platform/src/http_p3.rs b/crates/platform/src/http_p3.rs new file mode 100644 index 00000000..96b9b6a6 --- /dev/null +++ b/crates/platform/src/http_p3.rs @@ -0,0 +1,366 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +//! The WASIp3 backend of [`http`](crate::http): the `wasi:http@0.3` client for sending, +//! [`IncomingBody`] wrapping a `wasi:http` body stream, and the request and response conversions +//! of the `wasi:http/handler` export. + +use wasip3::http::types::{ + ErrorCode, Fields, Method, Request as WasiRequest, Response as WasiResponse, Scheme, +}; +use wasip3::{wit_future, wit_stream}; + +use crate::http::{BodyBytes, Error, OutgoingBody, Request, Response}; +use crate::http_wasm::{AbandonBody, BodyDone, BodySendOutcome, HostBodyWriter, UrlParts}; + +/// The `wasi:http/types` interface of the `wasi:http` version the runtime links: the one its +/// `wasip3` bindings target. +pub const HOST_HTTP_TYPES: &str = "wasi:http/types@0.3.0"; + +/// The interface of the HTTP handler export, without a version. +pub const HOST_HTTP_HANDLER: &str = "wasi:http/handler"; + +/// The resource of [`HOST_HTTP_TYPES`] that [`HostRequest`] is. +pub const HOST_REQUEST_RESOURCE: &str = "request"; + +/// The resource of [`HOST_HTTP_TYPES`] that [`HostResponse`] is. +pub const HOST_RESPONSE_RESOURCE: &str = "response"; + +/// The request the `wasi:http/handler` export receives. +pub type HostRequest = WasiRequest; +/// The response the `wasi:http/handler` export returns. +pub type HostResponse = WasiResponse; +/// The error the `wasi:http/handler` export returns. +pub type HostErrorCode = ErrorCode; + +type BodyStreamReader = wasip3::wit_bindgen::rt::async_support::StreamReader; +type BodyStreamWriter = wasip3::wit_bindgen::rt::async_support::StreamWriter; +type TrailersResult = Result, wasip3::http::types::ErrorCode>; +type TrailersWriter = wasip3::wit_bindgen::rt::async_support::FutureWriter; +type TrailersReader = wasip3::wit_bindgen::rt::async_support::FutureReader; + +/// The pieces of an [`OutgoingBody`] handed to the host: the contents stream, +/// the trailers future, and, when we own the bytes, the writer half for +/// [`spawn_body_writer`] to feed. +struct BodyParts { + contents: Option, + /// The trailers future given to the host. For a piped host body this is + /// the pipe source's trailers reader, so an abort on the incoming body + /// propagates to the peer instead of looking like a clean end. Otherwise + /// it is a fresh future that [`spawn_body_writer`] resolves. + trailers: TrailersReader, + writer: Option<(BodyStreamWriter, OutgoingBody)>, +} + +/// Split `body` into the pieces the host needs. A host body is handed +/// straight through, trailers and all. A body-less request/response +/// gets no stream at all. +/// +/// The host reads a host body's stream itself, so `body_timeout` doesn't apply to it. Bounding it +/// would require copying every chunk of a proxied body through the guest. +fn body_contents(body: OutgoingBody) -> (BodyParts, TrailersWriter) { + let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None)); + let (contents, piped_trailers, writer) = match body { + OutgoingBody::Host(response_body) => { + (Some(response_body.stream), response_body.trailers, None) + } + OutgoingBody::Consumed => (None, None, None), + OutgoingBody::Bytes(bytes) if bytes.is_empty() => (None, None, None), + other => { + let (body_tx, body_rx) = wit_stream::new(); + (Some(body_rx), None, Some((body_tx, other))) + } + }; + ( + BodyParts { + contents, + trailers: piped_trailers.unwrap_or(trailers_rx), + writer, + }, + trailers_tx, + ) +} + +/// Feed the owned body (in-memory, or streamed through the channel) to its +/// wit stream from a spawned task, then write the trailers: `Ok(None)` on a +/// clean end, an `ErrorCode` otherwise. The trailers future is the only abort +/// signal a wit stream's consumer can observe. +/// +/// `timeout` bounds the write, so a handler that never ends its stream doesn't hold the wit +/// stream open for the life of the instance. Running out aborts the body with error trailers. +/// +/// The returned [`BodyDone`] resolves when the send is over, with its +/// [`BodySendOutcome`]. +fn spawn_body_writer( + writer_body: Option<(BodyStreamWriter, OutgoingBody)>, + trailers_tx: TrailersWriter, + timeout: Option, + declared_length: Option, +) -> (BodyDone, AbandonBody) { + let (done_tx, done_rx) = futures_channel::oneshot::channel::(); + let (abandon_tx, abandon_rx) = futures_channel::oneshot::channel::<()>(); + crate::task::spawn(async move { + let mut outcome = BodySendOutcome::Sent; + if let Some((body_tx, body)) = writer_body { + let mut writer = StreamBodyWriter(body_tx); + outcome = crate::http_wasm::bounded( + timeout, + crate::http_wasm::write_owned_body(&mut writer, body, declared_length, abandon_rx), + ) + .await; + // Ends the stream either way, and before the trailers below go out: a consumer must + // not see the abort reason while the body still looks open. + drop(writer); + } + let trailers = match abort_message(&outcome) { + None => Ok(None), + Some(message) => Err(ErrorCode::InternalError(Some(message))), + }; + // The outcome is sent before the trailers, so a host slow to take the trailers doesn't + // delay the serve path. + let _ = done_tx.send(outcome); + let _ = trailers_tx.write(trailers).await; + }); + (BodyDone(done_rx), AbandonBody(abandon_tx)) +} + +/// A body's wit stream, which the host reads host bodies from directly. +struct StreamBodyWriter(BodyStreamWriter); + +impl HostBodyWriter for StreamBodyWriter { + async fn send_chunk(&mut self, chunk: Vec) -> bool { + // `write_all` returns the values left unwritten because the host dropped the read end. + self.0.write_all(chunk).await.is_empty() + } + + async fn send_host_body( + &mut self, + _body: IncomingBody, + _remaining: &mut crate::http::Remaining, + ) -> BodySendOutcome { + unreachable!("`body_contents` hands host bodies to the host") + } +} + +crate::http_wasm::wasi_http_conversions!( + method: Method, + scheme: Scheme, + fields: Fields, + request: WasiRequest, + response: WasiResponse, +); + +/// The message the peer receives in the error trailers of a body whose send ended with +/// `outcome`, or `None` for a body sent whole. +fn abort_message(outcome: &BodySendOutcome) -> Option { + match outcome { + BodySendOutcome::Sent => None, + BodySendOutcome::Truncated => { + Some("the response body ended before the `Content-Length` it declared".to_string()) + } + BodySendOutcome::TimedOut => Some( + "the response body was not fully sent within the response-body or end-to-end \ + timeout" + .to_string(), + ), + BodySendOutcome::Failed(message) => Some(message.clone()), + } +} + +/// Send `request` with the `wasi:http` client, returning once the response status and headers +/// are available. +pub async fn send(request: Request) -> Result { + let url = UrlParts::parse(&request.url)?; + let fields = request_fields(&request)?; + let (body, trailers_tx) = body_contents(request.body); + let (wasi_request, transmitted) = WasiRequest::new(fields, body.contents, body.trailers, None); + set_request_target(&wasi_request, &request.method, &url)?; + + // Write the body we own (in-memory or channel-streamed) and the trailers once the + // request exists, so the host is ready to read from the stream. A host-piped body has no + // writer here: the host reads its stream directly. + // No timeout: the serve timeouts bound responses, not an outgoing request's body. + drop(spawn_body_writer(body.writer, trailers_tx, None, None)); + + // wasmtime aborts the request's connection when this future is dropped, cutting off the + // response body at whatever the host has already buffered. Awaiting it keeps the connection + // open until the exchange is over, including for a response body handed on to the host. + crate::task::spawn(async move { + let _ = transmitted.await; + }); + + let response = wasip3::http::client::send(wasi_request) + .await + .map_err(|e| Error(format!("fetch failed: {e:?}")))?; + + let status = response.get_status_code(); + let headers = response + .get_headers() + .copy_all() + .into_iter() + .map(|(name, value)| (name, crate::http::isomorphic_decode(&value))) + .collect(); + + let (result_tx, result_rx) = wit_future::new(|| Ok(())); + let (body_stream, trailers) = WasiResponse::consume_body(response, result_rx); + // The result future isn't used, so we resolve it at once. + crate::task::spawn(async move { + let _ = result_tx.write(Ok(())).await; + }); + + Ok(Response { + status, + headers, + body: IncomingBody { + stream: body_stream, + trailers: Some(trailers), + }, + }) +} + +/// Read an incoming `wasi:http` request into its parts (method, full URL, headers, body). +/// +/// Returns `Err` for a field that the host accepted but `http` cannot represent. +pub fn read_incoming_request( + request: WasiRequest, +) -> Result<(String, String, http::HeaderMap, IncomingBody), ErrorCode> { + let method = string_of_method(request.get_method()); + let url = incoming_request_url( + request.get_scheme(), + request.get_authority(), + request.get_path_with_query(), + ); + let headers = http::HeaderMap::try_from(request.get_headers())?; + + // Take the body's stream without reading it. + let (result_tx, result_rx) = wit_future::new(|| Ok(())); + let (body_stream, trailers) = WasiRequest::consume_body(request, result_rx); + crate::task::spawn(async move { + let _ = result_tx.write(Ok(())).await; + }); + Ok(( + method, + url, + headers, + IncomingBody { + stream: body_stream, + trailers: Some(trailers), + }, + )) +} + +/// Build a `wasi:http` response from parts. +/// +/// The body is written from a spawned task: an in-memory body inline, +/// a host body handed straight through, a stream body forwarded chunk by chunk. +/// +/// The returned [`BodyDone`] resolves once every chunk was handed to the host, +/// which is at once for a host body. `body_timeout` bounds the send (see +/// [`spawn_body_writer`]). +/// +/// `declared_length` is used as the `Content-Length` header, and enforced for guest-produced +/// streams. +pub fn build_outgoing_response( + status: u16, + headers: http::HeaderMap, + body: OutgoingBody, + body_timeout: Option, + declared_length: Option, +) -> (WasiResponse, BodyDone, AbandonBody) { + let (body, trailers_tx) = body_contents(body); + let (response, _result) = + WasiResponse::new(response_fields(&headers), body.contents, body.trailers); + set_status(&response, status); + + let (body_done, abandon) = + spawn_body_writer(body.writer, trailers_tx, body_timeout, declared_length); + (response, body_done, abandon) +} + +/// Bytes read lazily from a host stream: the body of an HTTP client response, an +/// incoming request body on the serve path, or any other `stream` (see +/// [`IncomingBody::from_stream`]). +pub struct IncomingBody { + stream: BodyStreamReader, + /// The body's trailers future, taken when the body ends. + /// + /// `wasi:http` communicates errors that abort the stream through the trailers + /// future, so we store it here to consult once the body stream closed. + trailers: Option, +} + +impl IncomingBody { + /// The bytes of `stream`, which has no trailers that could report an abort. + pub fn from_stream(stream: BodyStreamReader) -> Self { + IncomingBody { + stream, + trailers: None, + } + } + + /// The stream the bytes are read from. A stream can't hold an error, so if the body has + /// trailers, a task awaits them and prints an abort they report to stderr. + pub fn into_stream(self) -> BodyStreamReader { + if let Some(trailers) = self.trailers { + crate::task::spawn_background(async move { + if let Err(code) = trailers.await { + eprintln!( + "a stream's source ended the stream early: the body was aborted: {code:?}" + ); + } + }); + } + self.stream + } + + /// Read the entire body to a byte sequence. + pub async fn read_all(self) -> Result { + // `collect` consumes the stream, so destructure first and consult the trailers directly + // rather than through `end_of_body` (which needs `self`). + let IncomingBody { stream, trailers } = self; + let bytes = bytes::Bytes::from(stream.collect().await); + if let Some(trailers) = trailers { + trailers + .await + .map_err(|code| Error(format!("the body was aborted: {code:?}")))?; + } + Ok(bytes) + } + + /// Read the next bytes into `buf`, at most as many as it has capacity for. Returns the bytes + /// read, or `Ok(None)` at the end of the body. The bytes are never empty. + pub async fn read_into(&mut self, mut buf: Vec) -> Result>, Error> { + use wasip3::wit_bindgen::rt::async_support::StreamResult; + loop { + let (status, read) = self.stream.read(buf).await; + buf = read; + match status { + // The stream is still open after a zero-length read, so it is read again. + // Awaiting the trailers here would hang, since they only resolve once the stream + // is closed. + StreamResult::Complete(0) => {} + StreamResult::Complete(_) => return Ok(Some(buf)), + // The body has ended. The trailers report whether it ended cleanly. + StreamResult::Dropped if !buf.is_empty() => return Ok(Some(buf)), + StreamResult::Dropped => return self.end_of_body().await.map(|()| None), + // The read was cancelled at teardown. The stream was never reported closed, so + // the trailers may never resolve and aren't awaited. + StreamResult::Cancelled => return Ok(None), + } + } + } + + /// Await the trailers future of a body whose stream has ended, and return an error if the + /// peer aborted the body. + /// + /// Only valid once the stream has been reported closed. The future does not resolve before + /// then. + async fn end_of_body(&mut self) -> Result<(), Error> { + let Some(trailers) = self.trailers.take() else { + return Ok(()); + }; + match trailers.await { + Ok(_) => Ok(()), + Err(code) => Err(Error(format!("the body was aborted: {code:?}"))), + } + } +} diff --git a/crates/platform/src/http_wasm.rs b/crates/platform/src/http_wasm.rs index 91a1b6ea..a309b233 100644 --- a/crates/platform/src/http_wasm.rs +++ b/crates/platform/src/http_wasm.rs @@ -1,62 +1,27 @@ // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception -//! The wasm backend of [`http`](crate::http): the WASIp3 `wasi:http` client for -//! sending, and [`IncomingBody`] wrapping a `wasi:http` body stream. +//! The parts of the wasm backends of [`http`](crate::http) that don't depend on the `wasi:http` +//! version: how an owned outgoing body is written to the host, and how its send ends. +//! +//! [`http_p2`](crate::http_p2) and [`http_p3`](crate::http_p3) implement [`HostBodyWriter`] for +//! their body streams. use futures_lite::StreamExt; -use wasip3::http::types::{ - ErrorCode, Fields, Method, Request as WasiRequest, Response as WasiResponse, Scheme, -}; -use wasip3::{wit_bindgen, wit_future, wit_stream}; -use crate::http::{BodyBytes, Error, OutgoingBody, Request, Response}; +use crate::http::{IncomingBody, OutgoingBody, Remaining, Request}; -type BodyStreamReader = wasip3::wit_bindgen::rt::async_support::StreamReader; -type BodyStreamWriter = wasip3::wit_bindgen::rt::async_support::StreamWriter; -type TrailersResult = Result, wasip3::http::types::ErrorCode>; -type TrailersWriter = wasip3::wit_bindgen::rt::async_support::FutureWriter; -type TrailersReader = wasip3::wit_bindgen::rt::async_support::FutureReader; +/// The write end of an outgoing body's host stream. +pub(crate) trait HostBodyWriter { + /// Write all of `chunk`. Returns `false` if the host stopped reading. + async fn send_chunk(&mut self, chunk: Vec) -> bool; -/// The pieces of an [`OutgoingBody`] handed to the host: the contents stream, -/// the trailers future, and, when we own the bytes, the writer half for -/// [`spawn_body_writer`] to feed. -struct BodyParts { - contents: Option, - /// The trailers future given to the host. For a piped host body this is - /// the pipe source's trailers reader, so an abort on the incoming body - /// propagates to the peer instead of looking like a clean end; otherwise - /// it is a fresh future [`spawn_body_writer`] resolves. - trailers: TrailersReader, - writer: Option<(BodyStreamWriter, OutgoingBody)>, -} - -/// Split `body` into the pieces the host needs. A host body is handed -/// straight through, trailers and all. A body-less request/response -/// gets no stream at all. -/// -/// The host reads a host body's stream itself, so `body_timeout` doesn't apply to it. Bounding it -/// would require copying every chunk of a proxied body through the guest. -fn body_contents(body: OutgoingBody) -> (BodyParts, TrailersWriter) { - let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None)); - let (contents, piped_trailers, writer) = match body { - OutgoingBody::Host(response_body) => { - (Some(response_body.stream), response_body.trailers, None) - } - OutgoingBody::Consumed => (None, None, None), - OutgoingBody::Bytes(bytes) if bytes.is_empty() => (None, None, None), - other => { - let (body_tx, body_rx) = wit_stream::new(); - (Some(body_rx), None, Some((body_tx, other))) - } - }; - ( - BodyParts { - contents, - trailers: piped_trailers.unwrap_or(trailers_rx), - writer, - }, - trailers_tx, - ) + /// Copy `body` to the host stream, trimmed by `remaining`. Only called by backends that copy + /// host bodies through the guest. + async fn send_host_body( + &mut self, + body: IncomingBody, + remaining: &mut Remaining, + ) -> BodySendOutcome; } /// How a body's send ended, reported through [`BodyDone`]. @@ -65,7 +30,7 @@ pub enum BodySendOutcome { Sent, /// The body ended before the `Content-Length` the response declared for it. Truncated, - /// The `timeout` given to [`spawn_body_writer`] expired before the send finished. + /// The `timeout` given to the body writer expired before the send finished. TimedOut, /// The send failed: the host dropped the stream's read end (the connection /// is gone), or a streamed chunk was itself an error. @@ -74,7 +39,7 @@ pub enum BodySendOutcome { /// Resolves once a body's writer task has ended, with how it ended, or with `None` if the task /// was cancelled first, as at instance teardown. -pub struct BodyDone(futures_channel::oneshot::Receiver); +pub struct BodyDone(pub(crate) futures_channel::oneshot::Receiver); impl std::future::Future for BodyDone { type Output = Option; @@ -86,92 +51,72 @@ impl std::future::Future for BodyDone { } } -/// Feed the owned body (in-memory, or streamed through the channel) to its -/// wit stream from a spawned task, then write the trailers: `Ok(None)` on a -/// clean end, an `ErrorCode` otherwise. The trailers future is the only abort -/// signal a wit stream's consumer can observe. -/// -/// `timeout` bounds the write, so a handler that never ends its stream doesn't hold the wit -/// stream open for the life of the instance. Running out aborts the body with error trailers. -/// -/// The returned [`BodyDone`] resolves when the send is over, with its -/// [`BodySendOutcome`]. -fn spawn_body_writer( - writer_body: Option<(BodyStreamWriter, OutgoingBody)>, - trailers_tx: TrailersWriter, - timeout: Option, - declared_length: Option, -) -> (BodyDone, AbandonBody) { - let (done_tx, done_rx) = futures_channel::oneshot::channel::(); - let (abandon_tx, abandon_rx) = futures_channel::oneshot::channel::<()>(); - wit_bindgen::spawn_local(async move { - let mut outcome = BodySendOutcome::Sent; - if let Some((mut body_tx, body)) = writer_body { - let mut remaining = crate::http::Remaining::new(declared_length); - let write = async { - match body { - OutgoingBody::Bytes(bytes) => { - // Zero-copy when the bytes are uniquely owned and Vec-backed. - if !body_tx.write_all(Vec::from(bytes)).await.is_empty() { - return BodySendOutcome::Failed(HOST_STOPPED_READING.to_string()); - } - BodySendOutcome::Sent - } - OutgoingBody::Stream(mut receiver) => { - write_stream_body(&mut body_tx, &mut receiver, abandon_rx, &mut remaining) - .await - } - OutgoingBody::Host(_) | OutgoingBody::Consumed => BodySendOutcome::Sent, - } - }; - outcome = match timeout { - None => write.await, - Some(limit) => { - let expired = async { - crate::clock::sleep(limit).await; - BodySendOutcome::TimedOut - }; - futures_lite::future::or(write, expired).await - } - }; - // Ends the stream either way, and before the trailers below go out: a consumer must - // not see the abort reason while the body still looks open. `write` only borrows the - // writer, so a lost race hands it back here. - drop(body_tx); - } - let trailers = match &outcome { - BodySendOutcome::Sent => Ok(None), - BodySendOutcome::Truncated => Err(ErrorCode::InternalError(Some( - "the response body ended before the `Content-Length` it declared".to_string(), - ))), - BodySendOutcome::TimedOut => Err(ErrorCode::InternalError(Some( - "the response body was not fully sent within the response-body or \ - end-to-end timeout" - .to_string(), - ))), - BodySendOutcome::Failed(message) => Err(wasip3::http::types::ErrorCode::InternalError( - Some(message.clone()), - )), - }; - // The outcome is sent before the trailers, so a host slow to take the trailers doesn't - // delay the serve path. - let _ = done_tx.send(outcome); - let _ = trailers_tx.write(trailers).await; - }); - (BodyDone(done_rx), AbandonBody(abandon_tx)) +/// Signals a streamed body's writer that the event loop feeding it has run out of work, so no +/// further chunks will be produced. The writer then sends what the channel already holds and ends +/// the body, as [`finish_abandoned_body`] describes. Dropping it doesn't signal the writer. +pub struct AbandonBody(pub(crate) futures_channel::oneshot::Sender<()>); + +impl AbandonBody { + pub fn abandon(self) { + let _ = self.0.send(()); + } } -/// The failure of a `write_all` that returned unwritten values because the host dropped the read -/// end. -const HOST_STOPPED_READING: &str = "the host stopped reading the response body"; +/// The failure of a write that the host stopped reading. +pub(crate) const HOST_STOPPED_READING: &str = "the host stopped reading the response body"; /// How an abandoned body's send is reported. const ABANDONED_BODY: &str = "the event loop finished while the response body was still open, so the body can never \ complete; ending it without the terminating chunk"; +/// Write `body` to `writer`, and return how the send ended. +/// +/// `declared_length` is enforced for streamed bodies, and for host bodies, which only backends +/// that copy them through the guest pass. `abandoned` is the receiving end of the body's +/// [`AbandonBody`]. +pub(crate) async fn write_owned_body( + writer: &mut impl HostBodyWriter, + body: OutgoingBody, + declared_length: Option, + abandoned: futures_channel::oneshot::Receiver<()>, +) -> BodySendOutcome { + let mut remaining = Remaining::new(declared_length); + match body { + OutgoingBody::Bytes(bytes) => { + // Zero-copy when the bytes are uniquely owned and Vec-backed. + if !writer.send_chunk(Vec::from(bytes)).await { + return BodySendOutcome::Failed(HOST_STOPPED_READING.to_string()); + } + BodySendOutcome::Sent + } + OutgoingBody::Stream(mut receiver) => { + write_stream_body(writer, &mut receiver, abandoned, &mut remaining).await + } + OutgoingBody::Host(body) => writer.send_host_body(body, &mut remaining).await, + OutgoingBody::Consumed => BodySendOutcome::Sent, + } +} + +/// Run `send`, or end it with [`BodySendOutcome::TimedOut`] once `timeout` has passed. +pub(crate) async fn bounded( + timeout: Option, + send: impl std::future::Future, +) -> BodySendOutcome { + match timeout { + None => send.await, + Some(limit) => { + let expired = async { + crate::clock::sleep(limit).await; + BodySendOutcome::TimedOut + }; + futures_lite::future::or(send, expired).await + } + } +} + /// The outcome of finishing sending a body. -fn outcome_at_end(remaining: &crate::http::Remaining) -> BodySendOutcome { +pub(crate) fn outcome_at_end(remaining: &Remaining) -> BodySendOutcome { if remaining.is_unfilled() { BodySendOutcome::Truncated } else { @@ -182,10 +127,10 @@ fn outcome_at_end(remaining: &crate::http::Remaining) -> BodySendOutcome { /// Feed a streamed body's chunks to the host as they arrive, until the stream ends, one of them /// fails, the declared length was reached, or [`AbandonBody`] is signaled. async fn write_stream_body( - body_tx: &mut BodyStreamWriter, + writer: &mut impl HostBodyWriter, receiver: &mut crate::http::OutgoingBodyReceiver, mut abandoned: futures_channel::oneshot::Receiver<()>, - remaining: &mut crate::http::Remaining, + remaining: &mut Remaining, ) -> BodySendOutcome { loop { let chunk = futures_lite::future::or(async { Some(receiver.next().await) }, async { @@ -203,7 +148,7 @@ async fn write_stream_body( let Some(chunk) = remaining.take(chunk) else { return BodySendOutcome::Sent; }; - if !body_tx.write_all(chunk).await.is_empty() { + if !writer.send_chunk(chunk).await { return BodySendOutcome::Failed(HOST_STOPPED_READING.to_string()); } if let Some(e) = error { @@ -212,7 +157,7 @@ async fn write_stream_body( } Some(Some(Err(e))) => return BodySendOutcome::Failed(e.to_string()), Some(None) => return outcome_at_end(remaining), - None => return finish_abandoned_body(body_tx, receiver, remaining).await, + None => return finish_abandoned_body(writer, receiver, remaining).await, } } } @@ -224,9 +169,9 @@ async fn write_stream_body( /// the stream as an abort, so the client sees the truncation rather than a complete-looking /// response (RFC 9112 §7.1). async fn finish_abandoned_body( - body_tx: &mut BodyStreamWriter, + writer: &mut impl HostBodyWriter, receiver: &mut crate::http::OutgoingBodyReceiver, - remaining: &mut crate::http::Remaining, + remaining: &mut Remaining, ) -> BodySendOutcome { // `poll_once` takes only what is ready. A pending read means the channel is still open with // nothing in it, which is the abandoned-body case. @@ -237,7 +182,7 @@ async fn finish_abandoned_body( let Some(chunk) = remaining.take(chunk) else { return BodySendOutcome::Sent; }; - if !body_tx.write_all(chunk).await.is_empty() { + if !writer.send_chunk(chunk).await { return BodySendOutcome::Failed(HOST_STOPPED_READING.to_string()); } } @@ -247,324 +192,193 @@ async fn finish_abandoned_body( BodySendOutcome::Failed(ABANDONED_BODY.to_string()) } -/// Signals a streamed body's writer that the event loop feeding it has run out of work, so no -/// further chunks will be produced. The writer then sends what the channel already holds and ends -/// the body, as [`finish_abandoned_body`] describes. Dropping it doesn't signal the writer. -pub struct AbandonBody(futures_channel::oneshot::Sender<()>); - -impl AbandonBody { - pub fn abandon(self) { - let _ = self.0.send(()); - } -} - -/// Map a method name onto a `wasi:http` method, case-sensitively, so `patch` stays `patch`. -/// Anything unrecognized is passed through as `Other`. -fn method_of_string(method: &str) -> Method { - match method { - "GET" => Method::Get, - "HEAD" => Method::Head, - "POST" => Method::Post, - "PUT" => Method::Put, - "DELETE" => Method::Delete, - "CONNECT" => Method::Connect, - "OPTIONS" => Method::Options, - "TRACE" => Method::Trace, - "PATCH" => Method::Patch, - _ => Method::Other(method.to_string()), - } +/// The scheme, authority and path with query of an absolute request URL. +pub(crate) struct UrlParts { + pub scheme: String, + pub authority: Option, + pub path_with_query: String, } -fn string_of_method(method: Method) -> String { - match method { - Method::Get => "GET".to_string(), - Method::Head => "HEAD".to_string(), - Method::Post => "POST".to_string(), - Method::Put => "PUT".to_string(), - Method::Delete => "DELETE".to_string(), - Method::Connect => "CONNECT".to_string(), - Method::Options => "OPTIONS".to_string(), - Method::Trace => "TRACE".to_string(), - Method::Patch => "PATCH".to_string(), - Method::Other(other) => other, +impl UrlParts { + pub(crate) fn parse(url: &str) -> Result { + let parsed = + url::Url::parse(url).map_err(|e| crate::http::Error(format!("invalid URL: {e}")))?; + let authority = match (parsed.host_str(), parsed.port()) { + (Some(host), Some(port)) => Some(format!("{host}:{port}")), + (Some(host), None) => Some(host.to_string()), + (None, _) => None, + }; + let path_with_query = match parsed.query() { + Some(query) => format!("{}?{}", parsed.path(), query), + None => parsed.path().to_string(), + }; + Ok(UrlParts { + scheme: parsed.scheme().to_string(), + authority, + path_with_query, + }) } } -/// Send `request` with the `wasi:http` client, returning once the response status and headers -/// are available. -pub async fn send(request: Request) -> Result { - let parsed = url::Url::parse(&request.url).map_err(|e| Error(format!("invalid URL: {e}")))?; - let scheme = match parsed.scheme() { - "http" => Scheme::Http, - "https" => Scheme::Https, - other => Scheme::Other(other.to_string()), - }; - let authority = match (parsed.host_str(), parsed.port()) { - (Some(host), Some(port)) => Some(format!("{host}:{port}")), - (Some(host), None) => Some(host.to_string()), - (None, _) => None, - }; - let path_with_query = match parsed.query() { - Some(query) => format!("{}?{}", parsed.path(), query), - None => parsed.path().to_string(), - }; - - let mut header_entries: Vec<(String, Vec)> = request +/// The header entries to send for `request`, with values [`isomorphic_encode`]d. +/// +/// An in-memory body has a known length: it is advertised with a `Content-Length` (as a +/// non-streaming client would) so the peer frames it correctly. A streamed body of unknown length +/// is sent without one. +/// +/// [`isomorphic_encode`]: crate::http::isomorphic_encode +pub(crate) fn request_header_entries(request: &Request) -> Vec<(String, Vec)> { + let mut entries: Vec<(String, Vec)> = request .headers .iter() .map(|(name, value)| (name.clone(), crate::http::isomorphic_encode(value))) .collect(); - // An in-memory body has a known length: advertise it with Content-Length (as a - // non-streaming client would) so the peer frames it correctly. A streamed body of unknown - // length is sent without one. if let OutgoingBody::Bytes(bytes) = &request.body { if !bytes.is_empty() - && !header_entries + && !entries .iter() .any(|(name, _)| name.eq_ignore_ascii_case("content-length")) { - header_entries.push(( + entries.push(( "Content-Length".to_string(), bytes.len().to_string().into_bytes(), )); } } - // Appended one at a time rather than with the all-or-nothing `Fields::from_list`, so a header - // the host refuses fails the request with an error naming it. - let fields = Fields::new(); - for (name, value) in &header_entries { - fields.append(name, value).map_err(|e| { - Error(format!( - "the host refused the request header `{name}`: {e:?}" - )) - })?; - } - - let (body, trailers_tx) = body_contents(request.body); - let (wasi_request, transmitted) = WasiRequest::new(fields, body.contents, body.trailers, None); - - wasi_request - .set_method(&method_of_string(&request.method)) - .map_err(|()| Error("invalid request method".to_string()))?; - wasi_request - .set_scheme(Some(&scheme)) - .map_err(|()| Error("invalid request scheme".to_string()))?; - wasi_request - .set_authority(authority.as_deref()) - .map_err(|()| Error("invalid request authority".to_string()))?; - wasi_request - .set_path_with_query(Some(&path_with_query)) - .map_err(|()| Error("invalid request path".to_string()))?; - - // Write the body we own (in-memory or channel-streamed) and the trailers once the - // request exists, so the host is ready to read from the stream. A host-piped body has no - // writer here: the host reads its stream directly. - // No timeout: the serve timeouts bound responses, not an outgoing request's body. - drop(spawn_body_writer(body.writer, trailers_tx, None, None)); - - // wasmtime aborts the request's connection when this future is dropped, cutting off the - // response body at whatever the host has already buffered. Awaiting it keeps the connection - // open until the exchange is over, including for a response body handed on to the host. - wit_bindgen::spawn_local(async move { - let _ = transmitted.await; - }); - - let response = wasip3::http::client::send(wasi_request) - .await - .map_err(|e| Error(format!("fetch failed: {e:?}")))?; - - let status = response.get_status_code(); - let headers = response - .get_headers() - .copy_all() - .into_iter() - .map(|(name, value)| (name, crate::http::isomorphic_decode(&value))) - .collect(); - - let (result_tx, result_rx) = wit_future::new(|| Ok(())); - let (body_stream, trailers) = WasiResponse::consume_body(response, result_rx); - // The result future is unused; resolve it so it is not left dangling. - wit_bindgen::spawn_local(async move { - let _ = result_tx.write(Ok(())).await; - }); - - Ok(Response { - status, - headers, - body: IncomingBody { - stream: body_stream, - trailers: Some(trailers), - }, - }) + entries } -/// Read an incoming `wasi:http` request into its parts (method, full URL, headers, body). -/// -/// Returns `Err` for a field that the host accepted but `http` cannot represent. -pub fn read_incoming_request( - request: WasiRequest, -) -> Result<(String, String, http::HeaderMap, IncomingBody), ErrorCode> { - let method = string_of_method(request.get_method()); - let scheme = match request.get_scheme() { - Some(Scheme::Https) => "https".to_string(), - Some(Scheme::Other(other)) => other, - _ => "http".to_string(), - }; - let authority = request - .get_authority() - .unwrap_or_else(|| "localhost".to_string()); - let path = request - .get_path_with_query() - .unwrap_or_else(|| "/".to_string()); - let url = format!("{scheme}://{authority}{path}"); - let headers = http::HeaderMap::try_from(request.get_headers())?; - - // Take the body's stream without reading it. - let (result_tx, result_rx) = wit_future::new(|| Ok(())); - let (body_stream, trailers) = WasiRequest::consume_body(request, result_rx); - wit_bindgen::spawn_local(async move { - let _ = result_tx.write(Ok(())).await; - }); - Ok(( - method, - url, - headers, - IncomingBody { - stream: body_stream, - trailers: Some(trailers), - }, - )) -} - -/// Build a `wasi:http` response from parts. -/// -/// The body is written from a spawned task: an in-memory body inline, -/// a host body handed straight through, a stream body forwarded chunk by chunk. -/// -/// The returned [`BodyDone`] resolves once every chunk was handed to the host, -/// which is at once for a host body. `body_timeout` bounds the send (see -/// [`spawn_body_writer`]). -/// -/// `declared_length` is used as the `Content-Length` header, and enforced for guest-produced -/// streams. -pub fn build_outgoing_response( - status: u16, - headers: http::HeaderMap, - body: OutgoingBody, - body_timeout: Option, - declared_length: Option, -) -> (WasiResponse, BodyDone, AbandonBody) { - let fields = Fields::new(); - for (name, value) in &headers { - // Appended one at a time rather than converted with the all-or-nothing - // `Fields::from_list`, so a header the host refuses, such as `keep-alive`, is dropped - // without emptying the response's headers. - if let Err(e) = fields.append(name.as_str(), value.as_bytes()) { - eprintln!( - "serve: dropping the response header `{name}`, which the host refused: {e:?}" - ); +/// Define the conversions between this crate's HTTP types and the `wasi:http` types of one +/// version, whose shapes are the same in every version: `method` and `scheme` variants, `fields`, +/// and the outgoing `request` and `response` resources. +macro_rules! wasi_http_conversions { + ( + method: $method:ty, + scheme: $scheme:ty, + fields: $fields:ty, + request: $request:ty, + response: $response:ty $(,)? + ) => { + /// Map a method name onto a `wasi:http` method, case-sensitively, so `patch` stays + /// `patch`. Anything unrecognized is passed through as `Other`. + fn method_of_string(method: &str) -> $method { + type M = $method; + match method { + "GET" => M::Get, + "HEAD" => M::Head, + "POST" => M::Post, + "PUT" => M::Put, + "DELETE" => M::Delete, + "CONNECT" => M::Connect, + "OPTIONS" => M::Options, + "TRACE" => M::Trace, + "PATCH" => M::Patch, + _ => M::Other(method.to_string()), + } } - } - let (body, trailers_tx) = body_contents(body); - let (response, _result) = WasiResponse::new(fields, body.contents, body.trailers); - // The host can refuse a status that HTTP allows. A refused status becomes 500 rather than - // `Response`'s default status. - if response.set_status_code(status).is_err() { - eprintln!("the host refused status {status}; answering with a 500 instead"); - let _ = response.set_status_code(500); - } - - let (body_done, abandon) = - spawn_body_writer(body.writer, trailers_tx, body_timeout, declared_length); - (response, body_done, abandon) -} + fn string_of_method(method: $method) -> String { + type M = $method; + match method { + M::Get => "GET".to_string(), + M::Head => "HEAD".to_string(), + M::Post => "POST".to_string(), + M::Put => "PUT".to_string(), + M::Delete => "DELETE".to_string(), + M::Connect => "CONNECT".to_string(), + M::Options => "OPTIONS".to_string(), + M::Trace => "TRACE".to_string(), + M::Patch => "PATCH".to_string(), + M::Other(other) => other, + } + } -/// Bytes read lazily from a host stream: the body of an HTTP client response, an -/// incoming request body on the serve path, or any other `stream` (see -/// [`IncomingBody::from_stream`]). -pub struct IncomingBody { - stream: BodyStreamReader, - /// The body's trailers future, taken when the body ends. - /// - /// `wasi:http` communicates errors that abort the stream through the trailers - /// future, so we store it here to consult once the body stream closed. - trailers: Option, -} + /// The URL of an incoming request with `scheme`, `authority` and `path_with_query`. A + /// missing one is `http`, `localhost` or `/`. + fn incoming_request_url( + scheme: Option<$scheme>, + authority: Option, + path_with_query: Option, + ) -> String { + type S = $scheme; + let scheme = match scheme { + Some(S::Https) => "https".to_string(), + Some(S::Other(other)) => other, + _ => "http".to_string(), + }; + let authority = authority.as_deref().unwrap_or("localhost"); + let path = path_with_query.as_deref().unwrap_or("/"); + format!("{scheme}://{authority}{path}") + } -impl IncomingBody { - /// The bytes of `stream`, which has no trailers that could report an abort. - pub fn from_stream(stream: BodyStreamReader) -> Self { - IncomingBody { - stream, - trailers: None, + /// `wasi:http` fields with the headers of `request` (see + /// [`request_header_entries`](crate::http_wasm::request_header_entries)). Fails naming + /// the first header the host refuses. + fn request_fields(request: &crate::http::Request) -> Result<$fields, crate::http::Error> { + // Appended one at a time rather than with the all-or-nothing `Fields::from_list`, so + // that the error names the header the host refuses. + let fields = <$fields>::new(); + for (name, value) in &crate::http_wasm::request_header_entries(request) { + fields.append(name, value).map_err(|e| { + crate::http::Error(format!( + "the host refused the request header `{name}`: {e:?}" + )) + })?; + } + Ok(fields) } - } - /// The stream the bytes are read from. A stream can't hold an error, so if the body has - /// trailers, a task awaits them and prints an abort they report to stderr. - pub fn into_stream(self) -> BodyStreamReader { - if let Some(trailers) = self.trailers { - wit_bindgen::spawn_local(async move { - if let Err(code) = trailers.await { + /// `wasi:http` fields with `headers`. A header the host refuses, such as `keep-alive`, + /// is left out and logged. + fn response_fields(headers: &http::HeaderMap) -> $fields { + let fields = <$fields>::new(); + for (name, value) in headers { + if let Err(e) = fields.append(name.as_str(), value.as_bytes()) { eprintln!( - "a stream's source ended the stream early: the body was aborted: {code:?}" + "serve: dropping the response header `{name}`, which the host refused: \ + {e:?}" ); } - }); + } + fields } - self.stream - } - /// Read the entire body to a byte sequence. - pub async fn read_all(self) -> Result { - // `collect` consumes the stream, so destructure first and consult the trailers directly - // rather than through `end_of_body` (which needs `self`). - let IncomingBody { stream, trailers } = self; - let bytes = bytes::Bytes::from(stream.collect().await); - if let Some(trailers) = trailers { - trailers - .await - .map_err(|code| Error(format!("the body was aborted: {code:?}")))?; + /// Set the method, scheme, authority and path of `request` to `method` and those of + /// `url`. + fn set_request_target( + request: &$request, + method: &str, + url: &crate::http_wasm::UrlParts, + ) -> Result<(), crate::http::Error> { + type S = $scheme; + let scheme = match url.scheme.as_str() { + "http" => S::Http, + "https" => S::Https, + _ => S::Other(url.scheme.clone()), + }; + let invalid = |part: &str| crate::http::Error(format!("invalid request {part}")); + request + .set_method(&method_of_string(method)) + .map_err(|()| invalid("method"))?; + request + .set_scheme(Some(&scheme)) + .map_err(|()| invalid("scheme"))?; + request + .set_authority(url.authority.as_deref()) + .map_err(|()| invalid("authority"))?; + request + .set_path_with_query(Some(&url.path_with_query)) + .map_err(|()| invalid("path")) } - Ok(bytes) - } - /// Read the next bytes into `buf`, at most as many as it has capacity for. Returns the bytes - /// read, or `Ok(None)` at the end of the body. The bytes are never empty. - pub async fn read_into(&mut self, mut buf: Vec) -> Result>, Error> { - use wasip3::wit_bindgen::rt::async_support::StreamResult; - loop { - let (status, read) = self.stream.read(buf).await; - buf = read; - match status { - // The stream is still open after a zero-length read, so it is read again. - // Awaiting the trailers here would hang, since they only resolve once the stream - // is closed. - StreamResult::Complete(0) => {} - StreamResult::Complete(_) => return Ok(Some(buf)), - // The body has ended. The trailers report whether it ended cleanly. - StreamResult::Dropped if !buf.is_empty() => return Ok(Some(buf)), - StreamResult::Dropped => return self.end_of_body().await.map(|()| None), - // The read was cancelled at teardown. The stream was never reported closed, so - // the trailers may never resolve and aren't awaited. - StreamResult::Cancelled => return Ok(None), + /// Set the status of `response` to `status`. The host can refuse a status that HTTP + /// allows, and a refused status becomes 500 rather than `Response`'s default status. + fn set_status(response: &$response, status: u16) { + if response.set_status_code(status).is_err() { + eprintln!("the host refused status {status}; answering with a 500 instead"); + let _ = response.set_status_code(500); } } - } - - /// Await the trailers future of a body whose stream has ended, and return an error if the - /// peer aborted the body. - /// - /// Only valid once the stream has been reported closed. The future does not resolve before - /// then. - async fn end_of_body(&mut self) -> Result<(), Error> { - let Some(trailers) = self.trailers.take() else { - return Ok(()); - }; - match trailers.await { - Ok(_) => Ok(()), - Err(code) => Err(Error(format!("the body was aborted: {code:?}"))), - } - } + }; } +pub(crate) use wasi_http_conversions; diff --git a/crates/platform/src/lib.rs b/crates/platform/src/lib.rs index 2515da0f..7764764e 100644 --- a/crates/platform/src/lib.rs +++ b/crates/platform/src/lib.rs @@ -1,16 +1,26 @@ // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception //! Platform abstraction layer for host capabilities that need different -//! implementations on native and WASI targets. +//! implementations natively, on WASIp2 and on WASIp3. //! -//! Builtins call the backend-agnostic APIs here; each capability holds its -//! platform-agnostic parts in one module and its backends in `_native` -//! / `_wasm` modules, selected with `#[cfg(target_arch)]`. The capabilities -//! are HTTP ([`http`]), used by `fetch`, and the monotonic clock ([`clock`]). +//! Builtins call the backend-agnostic APIs here. Each capability holds its +//! platform-agnostic parts in one module and its backends in `_native`, +//! `_p2` and `_p3` modules, selected with `#[cfg]` on +//! `target_arch` and `target_env`. The capabilities are HTTP ([`http`]), used by `fetch`, +//! the monotonic clock ([`clock`]), and on wasm, running futures on the host's executor +//! ([`task`]). pub mod clock; pub mod http; #[cfg(not(target_arch = "wasm32"))] mod http_native; +#[cfg(target_env = "p2")] +mod http_p2; +#[cfg(target_env = "p3")] +mod http_p3; #[cfg(target_arch = "wasm32")] mod http_wasm; +#[cfg(target_arch = "wasm32")] +pub mod task; +#[cfg(target_env = "p2")] +mod task_p2; diff --git a/crates/platform/src/task.rs b/crates/platform/src/task.rs new file mode 100644 index 00000000..2f6fcf8f --- /dev/null +++ b/crates/platform/src/task.rs @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +//! Running futures on the wasm host's executor. +//! +//! [`spawn`] runs a future that belongs to the current export call, such as writing a response +//! body. [`spawn_background`] runs one that may need later export calls to complete, such as the +//! work a content script's top level left behind. +//! +//! On WASIp3, both run on wit-bindgen's executor as part of the current export call's task, which +//! keeps running them after the call has returned its result. On WASIp2, an export call runs its +//! work with `block_on`, which waits for every [`spawn`]ed future and leaves +//! [`spawn_background`]ed ones queued for later calls. + +#[cfg(target_env = "p2")] +pub use crate::task_p2::{block_on, spawn, spawn_background, wait_for, WaitFor}; + +/// Run `future` to completion on a task of its own, as part of the current export call's task. +#[cfg(target_env = "p3")] +pub fn spawn(future: impl std::future::Future + 'static) { + wasip3::wit_bindgen::spawn_local(future); +} + +/// Run `future` to completion on a task of its own. On WASIp3, this is [`spawn`]. +#[cfg(target_env = "p3")] +pub fn spawn_background(future: impl std::future::Future + 'static) { + spawn(future); +} diff --git a/crates/platform/src/task_p2.rs b/crates/platform/src/task_p2.rs new file mode 100644 index 00000000..54541351 --- /dev/null +++ b/crates/platform/src/task_p2.rs @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +//! The WASIp2 backend of [`task`](crate::task): a single-threaded executor whose reactor waits on +//! `wasi:io/poll` pollables. +//! +//! The executor's state lives for the whole instance, not for one [`block_on`] call. Futures that +//! wait on a pollable own it, so a future created during one export call, such as a `fetch` body +//! a script keeps in a global, can be polled again during a later one. +//! +//! [`block_on`] returns once its future and every task started with [`spawn`] have completed. +//! Tasks started with [`spawn_background`] don't hold it up. They make progress during every +//! [`block_on`] call, and their remaining work stays queued in between. + +use std::cell::{Cell, RefCell}; +use std::collections::{HashMap, VecDeque}; +use std::future::Future; +use std::pin::Pin; +use std::rc::Rc; +use std::task::{Context, Poll, Waker}; + +use async_task::Runnable; +use wasip2::io::poll::Pollable; + +/// The executor's state. A wasm32-wasip2 instance has one thread, and its thread-locals live as +/// long as the instance. +struct Executor { + /// Tasks woken and waiting to run. + ready: RefCell>, + /// The pollables that pending [`WaitFor`] futures wait on, keyed by the future's id. + waiters: RefCell>, + /// The id the next [`WaitFor`] gets. + next_waiter: Cell, + /// The number of tasks started with [`spawn`] that haven't completed yet. + foreground: Cell, + /// Whether a [`block_on`] call is running. + running: Cell, +} + +#[derive(Clone)] +struct Waiter { + pollable: Rc, + waker: Waker, + /// Set once `wasi:io/poll.poll` reported `pollable` ready. + fired: Rc>, +} + +thread_local! { + static EXECUTOR: Executor = Executor { + ready: RefCell::new(VecDeque::new()), + waiters: RefCell::new(HashMap::new()), + next_waiter: Cell::new(0), + foreground: Cell::new(0), + running: Cell::new(false), + }; +} + +fn schedule(runnable: Runnable) { + EXECUTOR.with(|executor| executor.ready.borrow_mut().push_back(runnable)); +} + +/// Decrements [`Executor::foreground`] when a task started with [`spawn`] completes or is +/// dropped unfinished. +struct ForegroundGuard; + +impl Drop for ForegroundGuard { + fn drop(&mut self) { + EXECUTOR.with(|executor| executor.foreground.set(executor.foreground.get() - 1)); + } +} + +/// Run `future` to completion on a task of its own. The [`block_on`] call it was started from, or +/// the next one if it was started outside of one, returns only once it has completed. +pub fn spawn(future: impl Future + 'static) { + EXECUTOR.with(|executor| executor.foreground.set(executor.foreground.get() + 1)); + let guard = ForegroundGuard; + spawn_background(async move { + future.await; + drop(guard); + }); +} + +/// Run `future` to completion on a task of its own, which makes progress during every +/// [`block_on`] call until it has completed. No [`block_on`] call waits for it. +pub fn spawn_background(future: impl Future + 'static) { + // SAFETY: `future` and its output are `'static`, and the instance has one thread, so the + // non-`Send` future and the `schedule` function are only ever used on it. + let (runnable, task) = unsafe { async_task::spawn_unchecked(future, schedule) }; + task.detach(); + runnable.schedule(); +} + +/// Run `future` and every task started with [`spawn`] to completion, waiting on the host for +/// readiness whenever none of them can make progress. +/// +/// Panics when called from inside another `block_on` call, and when every unfinished task waits +/// on an event that no pollable can signal, since nothing could ever wake one of them again. +pub fn block_on(future: F) -> F::Output { + EXECUTOR.with(|executor| { + assert!( + !executor.running.replace(true), + "`block_on` was called from inside another `block_on` call" + ); + }); + let output = Rc::new(RefCell::new(None)); + let root = { + let output = Rc::clone(&output); + async move { + *output.borrow_mut() = Some(future.await); + } + }; + EXECUTOR.with(|executor| executor.foreground.set(executor.foreground.get() + 1)); + let guard = ForegroundGuard; + let root = async move { + root.await; + drop(guard); + }; + // SAFETY: the loop below runs until the root task has completed, so the task never outlives + // `future`, which may borrow from the caller's frame. The instance has one thread. + let (runnable, task) = unsafe { async_task::spawn_unchecked(root, schedule) }; + task.detach(); + runnable.schedule(); + + loop { + let round = EXECUTOR.with(|executor| executor.ready.borrow().len()); + for _ in 0..round { + let Some(runnable) = EXECUTOR.with(|executor| executor.ready.borrow_mut().pop_front()) + else { + break; + }; + runnable.run(); + } + let (ready, foreground, waiting) = EXECUTOR.with(|executor| { + ( + !executor.ready.borrow().is_empty(), + executor.foreground.get(), + !executor.waiters.borrow().is_empty(), + ) + }); + if foreground == 0 { + break; + } + match (ready, waiting) { + // Tasks kept waking each other: check for host events without blocking, so tasks + // waiting on the host aren't starved. + (true, true) => check_pollables(false), + (true, false) => {} + (false, true) => check_pollables(true), + (false, false) => panic!( + "every unfinished task waits on an event only another task can signal, and no \ + task can run: the program is deadlocked" + ), + } + } + EXECUTOR.with(|executor| executor.running.set(false)); + let output = output.borrow_mut().take(); + output.expect("the root task completed") +} + +/// Call `wasi:io/poll.poll` on every pollable a [`WaitFor`] waits on, and wake those reported +/// ready. If `block` isn't set, a pollable that is already ready is added, so the call returns +/// immediately. +fn check_pollables(block: bool) { + let waiters: Vec<(u64, Waiter)> = EXECUTOR.with(|executor| { + executor + .waiters + .borrow() + .iter() + .map(|(id, waiter)| (*id, waiter.clone())) + .collect() + }); + let immediate = (!block).then(|| wasip2::clocks::monotonic_clock::subscribe_duration(0)); + let mut targets: Vec<&Pollable> = waiters + .iter() + .map(|(_, waiter)| &*waiter.pollable) + .collect(); + targets.extend(immediate.as_ref()); + for index in wasip2::io::poll::poll(&targets) { + let Some((id, waiter)) = waiters.get(index as usize) else { + continue; + }; + waiter.fired.set(true); + EXECUTOR.with(|executor| executor.waiters.borrow_mut().remove(id)); + waiter.waker.wake_by_ref(); + } +} + +/// Wait until `pollable` is ready. +pub fn wait_for(pollable: Pollable) -> WaitFor { + let id = EXECUTOR.with(|executor| { + let id = executor.next_waiter.get(); + executor.next_waiter.set(id + 1); + id + }); + WaitFor { + pollable: Rc::new(pollable), + id, + fired: Rc::new(Cell::new(false)), + registered: false, + } +} + +/// The future [`wait_for`] returns. Dropping it drops the pollable. +pub struct WaitFor { + pollable: Rc, + id: u64, + fired: Rc>, + registered: bool, +} + +impl Future for WaitFor { + type Output = (); + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { + if self.fired.get() { + return Poll::Ready(()); + } + // A pollable that is ready before the first wait needs no round trip through the reactor. + if !self.registered && self.pollable.ready() { + self.fired.set(true); + return Poll::Ready(()); + } + self.registered = true; + let waiter = Waiter { + pollable: Rc::clone(&self.pollable), + waker: cx.waker().clone(), + fired: Rc::clone(&self.fired), + }; + EXECUTOR.with(|executor| executor.waiters.borrow_mut().insert(self.id, waiter)); + Poll::Pending + } +} + +impl Drop for WaitFor { + fn drop(&mut self) { + if self.registered { + // Removing the entry drops the reactor's copy of the pollable, so the pollable is gone + // once this returns. A pollable obtained from a stream must be gone before the stream + // is dropped. + EXECUTOR.with(|executor| executor.waiters.borrow_mut().remove(&self.id)); + } + } +} diff --git a/crates/platform/tests/task.rs b/crates/platform/tests/task.rs new file mode 100644 index 00000000..ee36c8f1 --- /dev/null +++ b/crates/platform/tests/task.rs @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception + +//! The WASIp2 executor: what [`block_on`] waits for, and futures that outlive the call they were +//! created in, as a `fetch` body kept in a global does across export calls. + +#![cfg(target_env = "p2")] + +use platform::task::{block_on, spawn, spawn_background, wait_for}; +use std::cell::Cell; +use std::rc::Rc; +use std::time::Duration; + +#[test] +fn block_on_returns_the_futures_output() { + assert_eq!(block_on(async { 7 }), 7); +} + +#[test] +fn block_on_waits_for_spawned_tasks() { + let done = Rc::new(Cell::new(false)); + let task_done = Rc::clone(&done); + block_on(async move { + spawn(async move { + platform::clock::sleep(Duration::from_millis(5)).await; + task_done.set(true); + }); + }); + assert!( + done.get(), + "`block_on` returned before the spawned task finished" + ); +} + +#[test] +fn background_tasks_progress_during_later_calls() { + let ticks = Rc::new(Cell::new(0)); + let task_ticks = Rc::clone(&ticks); + block_on(async move { + spawn_background(async move { + for _ in 0..3 { + platform::clock::sleep(Duration::from_millis(1)).await; + task_ticks.set(task_ticks.get() + 1); + } + }); + }); + assert_eq!(ticks.get(), 0, "`block_on` waited for the background task"); + while ticks.get() < 3 { + block_on(platform::clock::sleep(Duration::from_millis(2))); + } +} + +#[test] +fn a_wait_started_in_one_call_finishes_in_a_later_one() { + let mut wait = Box::pin(wait_for( + wasip2::clocks::monotonic_clock::subscribe_duration(5_000_000), + )); + // Polled, so it registers with the reactor, and abandoned when the shorter timer fires. + let timer_won = block_on(async { + futures_lite::future::or( + async { + (&mut wait).await; + false + }, + async { + platform::clock::sleep(Duration::from_millis(1)).await; + true + }, + ) + .await + }); + assert!(timer_won); + block_on(wait); +} diff --git a/crates/serve-test-support/src/wasm_serve.rs b/crates/serve-test-support/src/wasm_serve.rs index 4ada5c5a..031726e3 100644 --- a/crates/serve-test-support/src/wasm_serve.rs +++ b/crates/serve-test-support/src/wasm_serve.rs @@ -99,9 +99,15 @@ impl Serve { /// Keep the host on one instance for the whole test. `--idle-instance-timeout` defaults to a /// second, so an instance that idles longer than that between two requests is dropped and the /// second request starts a fresh script — indistinguishable, from the test's side, from state - /// that failed to carry over. + /// that failed to carry over. `--max-instance-reuse-count` defaults to 1 for a WASIp2 + /// component, and is raised to the WASIp3 default of 128. pub fn reusing_one_instance(self) -> Self { - self.wasmtime_flags(["--idle-instance-timeout", "30s"]) + self.wasmtime_flags([ + "--idle-instance-timeout", + "30s", + "--max-instance-reuse-count", + "128", + ]) } /// Serve `source` as the content script, in the classic-script shape most handlers here take. @@ -485,6 +491,32 @@ pub fn component() -> Option { None } +/// Whether the case that calls this has to be skipped, because it tests what only a WASIp3 host +/// does, such as sending an instance concurrent requests or letting it run between requests, and +/// the component under test was built for WASIp2. Prints that it was skipped if so. +pub fn needs_wasip3() -> bool { + let wasip2 = built_for_wasip2(); + if wasip2 { + eprintln!("skipped: needs a component built for WASIp3"); + } + wasip2 +} + +/// Whether the component under test was built for WASIp2, which exports +/// `wasi:http/incoming-handler`. `false` if there is no component. +pub fn built_for_wasip2() -> bool { + static WASIP2: OnceLock = OnceLock::new(); + *WASIP2.get_or_init(|| { + let Some(component) = component() else { + return false; + }; + let bytes = std::fs::read(&component).expect("reading the component"); + bytes + .windows(b"wasi:http/incoming-handler".len()) + .any(|window| window == b"wasi:http/incoming-handler") + }) +} + /// The output of `wasmtime --version`, or `None` when `wasmtime` is not on PATH. Run once per /// process. fn wasmtime_version() -> Option<&'static str> { diff --git a/crates/serve-test-support/tests/serve_wasm_e2e.rs b/crates/serve-test-support/tests/serve_wasm_e2e.rs index 9ee77d7f..90742b84 100644 --- a/crates/serve-test-support/tests/serve_wasm_e2e.rs +++ b/crates/serve-test-support/tests/serve_wasm_e2e.rs @@ -99,6 +99,9 @@ fn a_host_that_retires_instances_gives_each_request_a_fresh_global() { /// (`a_repeating_timer_keeps_running_between_requests`). #[test] fn a_repeating_timer_keeps_running_between_requests() { + if common::wasm_serve::needs_wasip3() { + return; + } const TICKER: &str = r#"globalThis.ticks = 0; setInterval(() => { globalThis.ticks += 1; }, 20); addEventListener('fetch', (event) => { @@ -282,6 +285,7 @@ fn a_script_with_no_fetch_listener_reports_itself_and_answers_500s() { #[test] fn a_late_top_level_rejection_fails_startup() { let Some(server) = Serve::new() + .reusing_one_instance() .module( "entry.mjs", "addEventListener('fetch', (event) => event.respondWith(new Response('served'))); @@ -511,6 +515,9 @@ fn overlapping_work_handler(upstream: u16) -> String { /// is what keeps the second request's in-flight `fetch` out of it. #[test] fn post_response_work_does_not_disturb_the_next_request() { + if common::wasm_serve::needs_wasip3() { + return; + } let upstream = common::start_slow_upstream(Duration::from_millis(1200), "upstream-body"); let Some(server) = Serve::new() .reusing_one_instance() @@ -542,6 +549,9 @@ fn post_response_work_does_not_disturb_the_next_request() { /// on the realm in which the second is running JS. Each has to see only its own outcome. #[test] fn a_truncated_response_body_does_not_disturb_a_concurrent_request() { + if common::wasm_serve::needs_wasip3() { + return; + } let upstream = common::start_slow_upstream(Duration::from_millis(1000), "upstream-body"); let Some(server) = Serve::new() .reusing_one_instance() @@ -684,6 +694,9 @@ fn spawn_request(port: u16, path: &'static str) -> std::thread::JoinHandle {{ @@ -806,6 +819,9 @@ const PER_ROUND: usize = 1000; /// same interleaving under the ordinary collector. #[test] fn deeply_interleaved_requests_keep_the_realm_stack_intact() { + if common::wasm_serve::needs_wasip3() { + return; + } let gate = common::start_held_upstream("go"); let handler = format!( r#"addEventListener('fetch', (event) => {{ @@ -918,6 +934,9 @@ addEventListener('fetch', (event) => {{ /// all four land in it and one startup serves them all. #[test] fn concurrent_first_requests_all_wait_out_one_startup() { + if common::wasm_serve::needs_wasip3() { + return; + } let gate = common::start_held_upstream("go"); let Some(server) = Serve::new() .reusing_one_instance() @@ -1462,16 +1481,12 @@ fn serve_proxies_via_fetch() { assert!(b.join().unwrap().contains("UPSTREAM-BODY")); } -/// A proxied response body is out of `--response-body-timeout`'s reach, and deliberately: the -/// handler responded with a `fetch` response, whose body is a host stream the host pumps itself, and -/// holding onto it to bound it would mean copying every chunk of every proxied body through the -/// guest (see `body_contents`). How long a transfer it is itself performing may take is the host's -/// to limit — the native path bounds every body only because it performs every write. -/// -/// The one place this suite asserts a divergence *as* the behavior, so it is written to fail if -/// that changes silently in either direction. +/// On WASIp3, a proxied response body is out of `--response-body-timeout`'s reach: the handler +/// responded with a `fetch` response, whose body is a host stream the host copies itself (see +/// `body_contents`). On WASIp2, the guest copies every chunk of a proxied body, and the timeout +/// ends the body, as it does natively. #[test] -fn a_proxied_body_outlives_the_response_body_timeout() { +fn the_response_body_timeout_ends_a_proxied_body_only_on_wasip2() { let upstream = common::start_endless_upstream(Duration::from_millis(50)); let handler = format!( "addEventListener('fetch', (event) => \ @@ -1491,13 +1506,20 @@ fn a_proxied_body_outlives_the_response_body_timeout() { stream .write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n") .unwrap(); - // Well over the bound, and still streaming. + // Well over the bound. let ended = read_until_eof(&mut stream, Duration::from_millis(500)); - assert!( - ended.is_none(), - "the guest must not cut a body the host is pumping: {:?}", - ended.map(|bytes| String::from_utf8_lossy(&bytes).into_owned()) - ); + if common::wasm_serve::built_for_wasip2() { + assert!( + ended.is_some(), + "the timeout must end a body the guest copies" + ); + } else { + assert!( + ended.is_none(), + "the guest must not cut a body the host copies: {:?}", + ended.map(|bytes| String::from_utf8_lossy(&bytes).into_owned()) + ); + } } /// `respondWith(response)` consumes the response's body, so `bodyUsed` reads true afterwards. @@ -1729,6 +1751,9 @@ fn nothing_but_wait_until_extends_the_event_lifetime() { /// event loop makes it arrive while that instance is blocked on the fetch. #[test] fn a_handler_can_fetch_its_own_server() { + if common::wasm_serve::needs_wasip3() { + return; + } // The handler addresses its own server through the gate rather than by `request.url`'s // origin: what the guest sees there is built from the `Host` header, which has no port // here. diff --git a/crates/wit-bridge/Cargo.toml b/crates/wit-bridge/Cargo.toml index ed781e8e..aff486d9 100644 --- a/crates/wit-bridge/Cargo.toml +++ b/crates/wit-bridge/Cargo.toml @@ -20,9 +20,13 @@ web-streams.workspace = true # Only the wasm-only modules name `wit-dylib-ffi`. The rev is a `wasm-tools` # 1.262.0 commit, matching the `wit-dylib` 0.262 the componentizer uses, so both -# sides agree on the wit-dylib ABI. `async-runtime` runs async-declared exports -# on wit-bindgen's executor, the one the wasip3 builtins run on. -[target.'cfg(target_arch = "wasm32")'.dependencies] +# sides agree on the wit-dylib ABI. +[target.'cfg(target_env = "p2")'.dependencies] +wit-dylib-ffi = { git = "https://github.com/bytecodealliance/wasm-tools/", rev = "e22db44da4eaf17a0e46ec55b911c7539589a3cc" } + +# `async-runtime` runs async-declared exports on wit-bindgen's executor, the one +# the wasip3 builtins run on. +[target.'cfg(target_env = "p3")'.dependencies] wit-dylib-ffi = { git = "https://github.com/bytecodealliance/wasm-tools/", rev = "e22db44da4eaf17a0e46ec55b911c7539589a3cc", features = [ "async-runtime", ] } diff --git a/crates/wit-bridge/src/interpreter.rs b/crates/wit-bridge/src/interpreter.rs index 5d2e88bc..50e111af 100644 --- a/crates/wit-bridge/src/interpreter.rs +++ b/crates/wit-bridge/src/interpreter.rs @@ -321,11 +321,17 @@ pub fn world_desc_opt() -> Option { /// `run`. /// /// `raw_http_handler` is the name of the exported interface that has the -/// runtime's own declaration of `wasi:http/handler`. Its functions are looked -/// up under the names for `wasi:http/handler`, and the main module may leave +/// runtime's own declaration of its HTTP handler interface, `wasi:http/handler` +/// on WASIp3 and `wasi:http/incoming-handler` on WASIp2. Its functions are +/// looked up under the names for that interface, and the main module may leave /// them out. Returns whether and where the main module provides them, or `None` /// if `raw_http_handler` is `None`. /// +/// On WASIp2, the export's `handle` takes the `incoming-request` and a +/// `response-outparam`. The JavaScript `handle` gets only the request, and its +/// result or failure sets the outparam (see [`call_export_on_loop`]). Fails if the +/// world does not import `[static]response-outparam.set`, which sets it. +/// /// A world without exports has no metadata, so its table is empty. pub fn install_exports( scope: &Scope<'_>, @@ -360,7 +366,7 @@ pub fn install_exports( .map(|func| { let names = func.interface().map(|interface| { if Some(interface) == raw_http_handler { - InterfaceNames::of(HTTP_HANDLER_INTERFACE) + InterfaceNames::of(platform::http::HOST_HTTP_HANDLER) } else { InterfaceNames::of(interface) } @@ -370,6 +376,18 @@ pub fn install_exports( .collect(); let fixed: &[&str] = if cli { &["run"] } else { &[] }; let shapes = wit_name_mapping::possible_export_names(&items, fixed)?; + #[cfg(target_env = "p2")] + let outparam_result = match raw_http_handler { + Some(_) => { + let (set, shape) = response_outparam_set().ok_or( + "the world imports no `[static]response-outparam.set` of `wasi:http/types`, \ + which a raw HTTP handler's response is set with", + )?; + RESPONSE_OUTPARAM_SET.with(|cell| cell.set(Some(set))); + Some(shape) + } + None => None, + }; let mut raw_paths = Vec::new(); let specs: Vec = wit .iter_export_funcs() @@ -380,11 +398,19 @@ pub fn install_exports( let item = wit_name_mapping::item_js_name(func.name()); raw_paths.extend(shapes.iter().map(|shape| shape.path(&item))); } + // On WASIp2, the raw handler's result is that of the outparam it sets. + #[cfg(target_env = "p2")] + let result = match &outparam_result { + Some(shape) if raw => shape.clone(), + _ => result_shape(func.result()), + }; + #[cfg(target_env = "p3")] + let result = result_shape(func.result()); ExportSpec { interface: names.map(|names| names.wit_name), name: func.name().to_string(), shapes, - result: result_shape(func.result()), + result, optional: raw, } }) @@ -400,7 +426,7 @@ pub fn install_exports( } /// Whether and where the main module provides the functions of the runtime's -/// own `wasi:http/handler` export (see [`install_exports`]). +/// own HTTP handler export (see [`install_exports`]). pub struct RawHttpHandlerExport { /// Whether the main module provides it. pub provided: bool, @@ -409,9 +435,34 @@ pub struct RawHttpHandlerExport { pub paths: Vec, } -/// The interface whose export names are used for the runtime's own -/// `wasi:http/handler` export (see [`install_exports`]). -const HTTP_HANDLER_INTERFACE: &str = "wasi:http/handler"; +/// The `[static]response-outparam.set` import of the world's +/// `wasi:http/types@0.2`, which completes a call of the raw HTTP handler export: +/// its index, and the shape of the `result` it takes. +#[cfg(target_env = "p2")] +fn response_outparam_set() -> Option<(u32, ResultShape)> { + let world = world_desc_opt()?; + let set = world + .interfaces + .iter() + .filter(|interface| { + interface.wit_name.as_deref().is_some_and(|name| { + wit_name_mapping::same_interface(name, platform::http::HOST_HTTP_TYPES) + }) + }) + .flat_map(|interface| &interface.resources) + .filter(|resource| resource.name == "response-outparam") + .flat_map(|resource| &resource.statics) + .find(|(name, _)| name == "set") + .map(|(_, set)| set)?; + let Some(value::TypeShape::Result(ok, err)) = set.params.get(1) else { + return None; + }; + let shape = ResultShape::Result { + ok: ok.is_some(), + err: err.as_deref().cloned().map(Rc::new), + }; + Some((set.index, shape)) +} /// The [`ResultShape`] of a WIT result type, resolving aliases. fn result_shape(ty: Option) -> ResultShape { @@ -490,6 +541,7 @@ impl Interpreter for StarlingInterpreter { /// The export runs without an event loop, so timers and async imports throw /// a `TypeError` naming the export. Microtasks queued by the export run /// before its result is lowered. + #[cfg(target_env = "p3")] fn export_call(_wit: Wit, func: ExportFunction, cx: &mut CallCx<'_>) { init::before_export(); let index = func.index(); @@ -525,6 +577,21 @@ impl Interpreter for StarlingInterpreter { }); } + /// Dispatch one exported call on a per-call event loop, and block until the + /// promise the JS export returns has settled and the loop's async work is + /// done (see [`call_export_on_loop`]). + #[cfg(target_env = "p2")] + fn export_call(_wit: Wit, func: ExportFunction, cx: &mut CallCx<'_>) { + init::before_export(); + platform::task::block_on(async { + let invocation = + core_runtime::invocation::OwnedInvocation::new(init::runtime(), Default::default()); + call_export_on_loop(func, cx, &invocation).await; + crate::run::drain_async_work(init::raw_cx(), &invocation).await; + drop(invocation); + }); + } + /// Call an async export: create an event loop for the call, call the export /// with the lifted arguments on `cx`'s stack, run the loop until the /// returned promise settles, and lower the result with `task-return`. @@ -534,6 +601,7 @@ impl Interpreter for StarlingInterpreter { /// the timers, `fetch` calls and async imports of concurrent calls are kept /// apart. The returned promise is kept on the call's stack, because no /// [`Scope`] can be held across an `await`. + #[cfg(target_env = "p3")] #[allow(clippy::manual_async_fn)] fn export_call_async( _wit: Wit, @@ -543,77 +611,9 @@ impl Interpreter for StarlingInterpreter { async move { init::before_export(); let mut cx = cx; - let index = func.index(); - let result = crate::exports::with_export_table(|table| table.result(index)); - let invocation = core_runtime::invocation::OwnedInvocation::new(init::runtime(), Default::default()); - - // Phase 1: call the export, and keep its result on the call stack, - // which is traced during the awaits below. - // - // The result isn't necessarily a promise: a plain function returns a - // value, and an exception converted into an `err` value is returned - // as `Outcome::Threw`. The event loop isn't run for either. - let name = func.name(); - let pending = init::with_scope(|scope| { - core_runtime::event_loop::with_event_loop(invocation.state().event_loop(), |_| { - let outcome = dispatch_export(scope, index, name, cx.stack(), &result); - core_runtime::event_loop::run_microtasks(scope); - match outcome { - // For a `future` result, the returned promise is - // lowered as the future, without waiting for it. - crate::exports::Outcome::Returned(v) - if result == crate::exports::ResultShape::Future => - { - cx.stack().push_value(v.get()); - StackTop::Future - } - crate::exports::Outcome::Returned(v) => { - let pending = !crate::exports::promise_is_settled(scope, v); - cx.stack().push_value(v.get()); - // The event loop only runs if the value is a - // pending promise. - StackTop::Returned { pending } - } - crate::exports::Outcome::Threw(v) => { - // An exception thrown synchronously and converted - // into an `err` value. - cx.stack().push_value(v.get()); - StackTop::Threw - } - } - }) - }); - - // Phase 2: run this call's event loop until the promise on top of - // the stack settles. - if matches!(pending, StackTop::Returned { pending: true }) { - let stack = cx.stack_ref(); - crate::run::drive_until_settled(init::raw_cx(), &invocation, |scope| { - scope.root_value(stack.last()) - }) - .await; - } - - // Phase 3: convert the value on top of the stack into an `Outcome`, - // and push the result in the form expected by the generated lowering - // code. - init::with_scope(|scope| { - let top = scope.root_value(cx.stack().pop_value()); - let outcome = match pending { - StackTop::Threw => crate::exports::Outcome::Threw(top), - StackTop::Future => crate::exports::Outcome::Returned(top), - StackTop::Returned { .. } => settled_outcome(scope, top, &result, func), - }; - push_export_result(cx.stack(), scope, &result, outcome); - keep_lowering_source(cx.stack(), &result, index); - // Dropping a borrow calls the `[resource-drop]` import, which is - // legal here but would trap after `task-return`. - if resources::release_borrows(cx.stack(), scope, &drop_exported_handle).is_err() { - trap(scope, "releasing borrows at async export end failed") - } - }); + call_export_on_loop(func, &mut cx, &invocation).await; // Lowering a stream or future result starts the pump or write that // fills it on this call's event loop, so the loop is active during @@ -754,6 +754,7 @@ impl Call for CallCx<'_> { let flags = ty.names().len() as u32; self.pop(|s, sc| value::pop_flags(s, sc, flags), "pop_flags") } + #[cfg(target_env = "p3")] fn pop_future(&mut self, ty: Future) -> u32 { // Lowering a `future` passes its readable end to the host. let index = ty.index() as u32; @@ -762,6 +763,7 @@ impl Call for CallCx<'_> { "pop_future", ) } + #[cfg(target_env = "p3")] fn pop_stream(&mut self, ty: Stream) -> u32 { // Lowering a `stream` passes its readable end to the host. let index = ty.index() as u32; @@ -770,6 +772,16 @@ impl Call for CallCx<'_> { "pop_stream", ) } + // `starling-componentize` refuses worlds with `future` and `stream` types for + // a WASIp2 runtime, so its bindings never lift or lower one. + #[cfg(target_env = "p2")] + fn pop_future(&mut self, _ty: Future) -> u32 { + unreachable!("a WASIp2 runtime has no `future` values") + } + #[cfg(target_env = "p2")] + fn pop_stream(&mut self, _ty: Stream) -> u32 { + unreachable!("a WASIp2 runtime has no `stream` values") + } fn pop_option(&mut self, ty: WitOption) -> u32 { let nested = inner_is_option(ty); let is_some = self.pop(|s, sc| value::pop_option(s, sc, nested), "pop_option"); @@ -919,6 +931,7 @@ impl Call for CallCx<'_> { self.push(|s, sc| resources::push_own(s, sc, desc, handle), "push_own") }); } + #[cfg(target_env = "p3")] fn push_future(&mut self, ty: Future, handle: u32) { let index = ty.index() as u32; self.push( @@ -929,6 +942,7 @@ impl Call for CallCx<'_> { "push_future", ); } + #[cfg(target_env = "p3")] fn push_stream(&mut self, ty: Stream, handle: u32) { let index = ty.index() as u32; self.push( @@ -939,6 +953,14 @@ impl Call for CallCx<'_> { "push_stream", ); } + #[cfg(target_env = "p2")] + fn push_future(&mut self, _ty: Future, _handle: u32) { + unreachable!("a WASIp2 runtime has no `future` values") + } + #[cfg(target_env = "p2")] + fn push_stream(&mut self, _ty: Stream, _handle: u32) { + unreachable!("a WASIp2 runtime has no `stream` values") + } fn push_variant(&mut self, ty: Variant, discr: u32) { let cases = variant_cases(ty); self.push( @@ -1062,12 +1084,14 @@ impl ImportInvoker for WasmImportInvoker { Ok(()) } + #[cfg(target_env = "p3")] fn supports_async(&self) -> bool { true } /// Call an async import on the wit-bindgen executor. The future captures /// only the import's index, so it doesn't borrow `self`. + #[cfg(target_env = "p3")] fn invoke_async( &self, stack: CallStack, @@ -1603,7 +1627,7 @@ fn wit_function_name(interface: Option<&str>, name: &str) -> String { RAW_HTTP_HANDLER.with(|cell| cell.borrow().as_deref() == Some(interface)) }); let interface = if raw { - Some(HTTP_HANDLER_INTERFACE) + Some(platform::http::HOST_HTTP_HANDLER) } else { interface }; @@ -1612,6 +1636,7 @@ fn wit_function_name(interface: Option<&str>, name: &str) -> String { /// Return `ty` as it is written in WIT, such as `future>`. Named /// types are written as their name, without their interface. +#[cfg(target_env = "p3")] pub(crate) fn wit_type_name(ty: Type) -> String { let named = |name: Option<&str>, spelled: &dyn Fn() -> String| match name { Some(name) => name.to_string(), @@ -1678,12 +1703,161 @@ pub(crate) fn wit_type_name(ty: Type) -> String { js::instance_local! { /// The name of the exported interface that has the runtime's own - /// declaration of `wasi:http/handler` (see [`install_exports`]). + /// declaration of its HTTP handler interface (see [`install_exports`]). static RAW_HTTP_HANDLER: RefCell> = const { RefCell::new(None) }; } -/// The kind of value left on top of the call stack by phase 1 of an async -/// export call. +#[cfg(target_env = "p2")] +js::instance_local! { + /// The index of the `[static]response-outparam.set` import, if the world has a raw HTTP + /// handler export (see [`install_exports`]). + static RESPONSE_OUTPARAM_SET: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// Phases 1 to 3 of an exported call that runs on `invocation`'s event loop: +/// call the JS export with the lifted arguments on `cx`'s stack, drive the +/// loop until the promise it returns settles, and leave the result on the +/// stack in the shape the generated lowering expects. +/// +/// On WASIp2, a call of the raw HTTP handler's `handle` passes the JS function +/// only the request. The settled result instead sets the `response-outparam` +/// argument through `[static]response-outparam.set`, with the event loop active, +/// so a `Response`'s body is a pending send of the loop. +/// +/// No [`Scope`] can be held across an `await`, so the returned promise stays +/// alive on the call's stack instead. +async fn call_export_on_loop( + func: ExportFunction, + cx: &mut CallCx<'_>, + invocation: &core_runtime::invocation::OwnedInvocation, +) { + let index = func.index(); + let result = crate::exports::with_export_table(|table| table.result(index)); + #[cfg(target_env = "p2")] + let outparam_set = raw_http_handler_call(func) + .then(|| RESPONSE_OUTPARAM_SET.with(std::cell::Cell::get)) + .flatten(); + #[cfg(target_env = "p3")] + let outparam_set: Option = None; + + // Phase 1: call the export, and keep its result on the call stack, + // which is traced during the awaits below. + // + // The result isn't necessarily a promise: a plain function returns a + // value, and an exception converted into an `err` value is returned + // as `Outcome::Threw`. The event loop isn't run for either. + let name = func.name(); + let pending = init::with_scope(|scope| { + core_runtime::event_loop::with_event_loop(invocation.state().event_loop(), |_| { + // The outparam stays on the stack, under the returned value. + let outparam = outparam_set.map(|_| scope.root_value(cx.stack().pop_value())); + let outcome = dispatch_export(scope, index, name, cx.stack(), &result); + if let Some(outparam) = outparam { + cx.stack().push_value(outparam.get()); + } + core_runtime::event_loop::run_microtasks(scope); + match outcome { + // For a `future` result, the returned promise is + // lowered as the future, without waiting for it. + crate::exports::Outcome::Returned(v) + if result == crate::exports::ResultShape::Future => + { + cx.stack().push_value(v.get()); + StackTop::Future + } + crate::exports::Outcome::Returned(v) => { + let pending = !crate::exports::promise_is_settled(scope, v); + cx.stack().push_value(v.get()); + // The event loop only runs if the value is a + // pending promise. + StackTop::Returned { pending } + } + crate::exports::Outcome::Threw(v) => { + // An exception thrown synchronously and converted + // into an `err` value. + cx.stack().push_value(v.get()); + StackTop::Threw + } + } + }) + }); + + // Phase 2: run this call's event loop until the promise on top of + // the stack settles. + if matches!(pending, StackTop::Returned { pending: true }) { + let stack = cx.stack_ref(); + crate::run::drive_until_settled(init::raw_cx(), invocation, |scope| { + scope.root_value(stack.last()) + }) + .await; + } + + // Phase 3: convert the value on top of the stack into an `Outcome`, + // and push the result in the form expected by the generated lowering + // code. + init::with_scope(|scope| { + let top = scope.root_value(cx.stack().pop_value()); + let outcome = match pending { + StackTop::Threw => crate::exports::Outcome::Threw(top), + StackTop::Future => crate::exports::Outcome::Returned(top), + StackTop::Returned { .. } => settled_outcome(scope, top, &result, func), + }; + if let Some(set) = outparam_set { + let outparam = scope.root_value(cx.stack().pop_value()); + core_runtime::event_loop::with_event_loop(invocation.state().event_loop(), |_| { + if set_response_outparam(scope, set, outparam, outcome).is_err() { + trap_with( + &format!( + "setting the response of `{}#handle` failed", + platform::http::HOST_HTTP_HANDLER + ), + ExnThrown::capture(scope), + ) + } + core_runtime::event_loop::run_microtasks(scope); + }); + } else { + push_export_result(cx.stack(), scope, &result, outcome); + keep_lowering_source(cx.stack(), &result, index); + } + // Dropping a borrow calls the `[resource-drop]` import, which is + // legal here but would trap after `task-return`. + if resources::release_borrows(cx.stack(), scope, &drop_exported_handle).is_err() { + trap(scope, "releasing borrows at async export end failed") + } + }); +} + +/// Whether `func` is the `handle` of the raw HTTP handler export. +#[cfg(target_env = "p2")] +fn raw_http_handler_call(func: ExportFunction) -> bool { + func.name() == "handle" + && RAW_HTTP_HANDLER.with(|cell| cell.borrow().as_deref() == func.interface()) +} + +/// Call the `[static]response-outparam.set` import with index `set` on +/// `outparam`, with `{tag: "ok", val}` for a value the handler returned and +/// `{tag: "err", val}` for an `err` payload a failure produced. +fn set_response_outparam( + scope: &Scope<'_>, + set: u32, + outparam: HandleValue<'_>, + outcome: crate::exports::Outcome<'_>, +) -> Result<(), ExnThrown> { + let (tag, val) = match outcome { + crate::exports::Outcome::Returned(val) => ("ok", val), + crate::exports::Outcome::Threw(val) => ("err", val), + }; + let result = js::Object::new_plain(scope)?; + let tag = js::JSString::from_str(scope, tag)?; + result.set_property(scope, c"tag", tag)?; + result.set_property(scope, c"val", val)?; + let result = scope.root_value(result.as_value()); + imports::call_import_from_js(scope, set, &[outparam, result]).map(drop) +} + +/// The kind of value left on top of the call stack by phase 1 of +/// [`call_export_on_loop`]. enum StackTop { /// The value returned by the export. `pending` is set if it is a pending /// promise. diff --git a/crates/wit-bridge/src/lib.rs b/crates/wit-bridge/src/lib.rs index c88d9d3f..712403e6 100644 --- a/crates/wit-bridge/src/lib.rs +++ b/crates/wit-bridge/src/lib.rs @@ -18,15 +18,15 @@ pub mod imports; pub mod init; #[cfg(target_arch = "wasm32")] pub mod interpreter; -#[cfg(target_arch = "wasm32")] +#[cfg(target_env = "p3")] pub mod promises; -#[cfg(target_arch = "wasm32")] +#[cfg(target_env = "p3")] pub mod readable; pub mod resources; #[cfg(target_arch = "wasm32")] pub mod run; pub mod stack; -#[cfg(target_arch = "wasm32")] +#[cfg(target_env = "p3")] pub mod streams; pub mod tracing; pub mod validate; @@ -50,7 +50,7 @@ pub use tracing::install_tracer; pub use value::TypeShape; /// Install [`ComponentError`] on `global`, and register the internal classes -/// of the crate: the wrapper class of imported resources and, on wasm, the +/// of the crate: the wrapper class of imported resources and, on WASIp3, the /// classes used to implement `stream` and `future` values. pub fn add_to_global(scope: &js::gc::scope::Scope<'_>, global: js::Object<'_>) { ComponentError::add_to_global(scope, global); @@ -58,7 +58,7 @@ pub fn add_to_global(scope: &js::gc::scope::Scope<'_>, global: js::Object<'_>) { component_error::install_class_factory(scope) .expect("creating the error class factory can only fail due to OOM"); resources::ImportedResource::add_to_global(scope, global); - #[cfg(target_arch = "wasm32")] + #[cfg(target_env = "p3")] { readable::IncomingStream::add_to_global(scope, global); promises::FutureWrite::add_to_global(scope, global); diff --git a/crates/wit-bridge/src/promises.rs b/crates/wit-bridge/src/promises.rs index 7d03b300..f4e1ec17 100644 --- a/crates/wit-bridge/src/promises.rs +++ b/crates/wit-bridge/src/promises.rs @@ -34,7 +34,7 @@ //! still writes its value. If the promise is garbage-collected without //! settling, the future is never written, and a warning is reported. -#![cfg(target_arch = "wasm32")] +#![cfg(target_env = "p3")] use std::future::{Future, IntoFuture}; use std::mem::ManuallyDrop; diff --git a/crates/wit-bridge/src/readable.rs b/crates/wit-bridge/src/readable.rs index 0e3b3be6..078b16fa 100644 --- a/crates/wit-bridge/src/readable.rs +++ b/crates/wit-bridge/src/readable.rs @@ -30,7 +30,7 @@ //! reader, which happens when the source produces its next chunk, and when the //! loop has no other work that could make it produce more. -#![cfg(target_arch = "wasm32")] +#![cfg(target_env = "p3")] use core_runtime::{jsclass, jsmethods}; use js::conversion::FromJSVal; diff --git a/crates/wit-bridge/src/resources.rs b/crates/wit-bridge/src/resources.rs index 7b97d50a..943024e9 100644 --- a/crates/wit-bridge/src/resources.rs +++ b/crates/wit-bridge/src/resources.rs @@ -387,7 +387,7 @@ pub fn dispose_function<'s>(scope: &'s Scope<'_>) -> Result, ExnThr /// Move the handle held by `from` to `to`, a wrapper of the same type that /// holds no handle. Does nothing if `from` holds no handle. -#[cfg(target_arch = "wasm32")] +#[cfg(target_env = "p3")] pub(crate) fn move_handle(from: ImportedResource<'_>, to: ImportedResource<'_>) { let handle = from.data_mut().handle.take(); if handle.is_some() { diff --git a/crates/wit-bridge/src/streams.rs b/crates/wit-bridge/src/streams.rs index 20258cc7..735ea4a3 100644 --- a/crates/wit-bridge/src/streams.rs +++ b/crates/wit-bridge/src/streams.rs @@ -22,7 +22,7 @@ //! `stream` or `future` without a payload type are lifted as `undefined`, and //! lowered to nothing. -#![cfg(target_arch = "wasm32")] +#![cfg(target_env = "p3")] use std::alloc::Layout; use std::cell::RefCell; diff --git a/crates/wit-bridge/src/tracing.rs b/crates/wit-bridge/src/tracing.rs index f090d707..aa04ce2a 100644 --- a/crates/wit-bridge/src/tracing.rs +++ b/crates/wit-bridge/src/tracing.rs @@ -21,6 +21,6 @@ pub fn clear_traced_roots() { crate::resources::clear_traced_roots(); crate::exports::clear_traced_roots(); crate::component_error::clear_traced_roots(); - #[cfg(target_arch = "wasm32")] + #[cfg(target_env = "p3")] crate::streams::clear_traced_roots(); } diff --git a/crown/rust-toolchain.toml b/crown/rust-toolchain.toml index d9a04d30..6f7ac190 100644 --- a/crown/rust-toolchain.toml +++ b/crown/rust-toolchain.toml @@ -1,6 +1,8 @@ [toolchain] -# Keep this in sync with the project workspace's channel. -channel = "1.98" +# The 1.100 beta that `STARLING_BETA` in `.github/tool-versions.env` names. 1.100 is the first +# toolchain whose wasm32-wasip3 std crown can check the wasm32-wasip3 build with. Keep the two in +# sync. +channel = "beta-2026-10-18" components = [ "clippy", @@ -11,4 +13,8 @@ components = [ # For formatting "rustfmt", ] +targets = [ + "wasm32-wasip2", + "wasm32-wasip3", +] profile = "minimal" diff --git a/crown/src/bare_value_in_gc_struct.rs b/crown/src/bare_value_in_gc_struct.rs index 2d67c2ab..ab763b67 100644 --- a/crown/src/bare_value_in_gc_struct.rs +++ b/crown/src/bare_value_in_gc_struct.rs @@ -21,8 +21,8 @@ use rustc_hir::{self as hir}; use rustc_lint::{LateContext, LateLintPass, Lint, LintPass, LintStore}; +use rustc_lint_defs::declare_tool_lint; use rustc_middle::ty; -use rustc_session::declare_tool_lint; use rustc_span::symbol::Symbol; use crate::common::TyCtxtExt as _; diff --git a/crown/src/common.rs b/crown/src/common.rs index 262f04a4..2da46c8f 100644 --- a/crown/src/common.rs +++ b/crown/src/common.rs @@ -17,11 +17,11 @@ use rustc_type_ir::Upcast as _; /// Emit a lint whose diagnostic `decorate` builds, including its span. pub trait LateContextExt { - fn lint(&self, lint: &'static Lint, decorate: impl FnOnce(&mut Diag<'_, ()>)); + fn lint(&self, lint: &'static Lint, decorate: impl FnOnce(&mut Diag<'_>)); } impl<'tcx> LateContextExt for LateContext<'tcx> { - fn lint(&self, lint: &'static Lint, decorate: impl FnOnce(&mut Diag<'_, ()>)) { + fn lint(&self, lint: &'static Lint, decorate: impl FnOnce(&mut Diag<'_>)) { self.opt_span_lint(lint, None::, DiagDecorator(decorate)); } } diff --git a/crown/src/main.rs b/crown/src/main.rs index 9c65a50a..fb5e83f9 100644 --- a/crown/src/main.rs +++ b/crown/src/main.rs @@ -8,6 +8,7 @@ // This rustc crates are private so they must be manually imported. extern crate rustc_ast; +extern crate rustc_attr_ir; extern crate rustc_driver; extern crate rustc_error_messages; extern crate rustc_errors; @@ -15,10 +16,12 @@ extern crate rustc_hir; extern crate rustc_infer; extern crate rustc_interface; extern crate rustc_lint; +extern crate rustc_lint_defs; extern crate rustc_log; extern crate rustc_middle; extern crate rustc_session; extern crate rustc_span; +extern crate rustc_structures; extern crate rustc_trait_selection; extern crate rustc_type_ir; @@ -47,7 +50,7 @@ impl Callbacks for MyCallbacks { if sess .opts .crate_types - .contains(&rustc_session::config::CrateType::ProcMacro) + .contains(&rustc_structures::CrateType::ProcMacro) { return; } diff --git a/crown/src/trace_in_no_trace.rs b/crown/src/trace_in_no_trace.rs index 9b6d92c0..24c965bb 100644 --- a/crown/src/trace_in_no_trace.rs +++ b/crown/src/trace_in_no_trace.rs @@ -4,12 +4,13 @@ use rustc_ast::token::TokenKind; use rustc_ast::tokenstream::TokenTree; +use rustc_attr_ir::AttrArgs; use rustc_error_messages::MultiSpan; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::{self as hir}; use rustc_lint::{LateContext, LateLintPass, Lint, LintPass, LintStore}; +use rustc_lint_defs::declare_tool_lint; use rustc_middle::ty; -use rustc_session::declare_tool_lint; use rustc_span::symbol::Symbol; use crate::common::{find_first_crate, implements_trait, trait_in_crate, LateContextExt}; @@ -77,8 +78,8 @@ fn get_must_not_have_traceable(sym: &Symbols, attrs: &[hir::Attribute]) -> Optio ]) }) .map(|x| match &x.get_normal_item().args { - hir::AttrArgs::Empty => 0, - hir::AttrArgs::Delimited(a) => match a + AttrArgs::Empty => 0, + AttrArgs::Delimited(a) => match a .tokens .iter() .next() diff --git a/crown/src/unrooted_must_root.rs b/crown/src/unrooted_must_root.rs index 69c546ec..684f2766 100644 --- a/crown/src/unrooted_must_root.rs +++ b/crown/src/unrooted_must_root.rs @@ -4,8 +4,8 @@ use rustc_hir::{self as hir, intravisit as visit, AmbigArg, ExprKind}; use rustc_lint::{LateContext, LateLintPass, Lint, LintPass, LintStore}; +use rustc_lint_defs::declare_tool_lint; use rustc_middle::ty; -use rustc_session::declare_tool_lint; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::symbol::{sym, Symbol}; diff --git a/crown/tests/compile_test.rs b/crown/tests/compile_test.rs index 87f39280..6c2e43e9 100644 --- a/crown/tests/compile_test.rs +++ b/crown/tests/compile_test.rs @@ -26,7 +26,7 @@ fn run_mode(mode: &'static str, bless: bool) { let cfg_mode = mode.parse().expect("Invalid mode"); config.mode = cfg_mode; config.src_base = PathBuf::from("tests").join(mode); - config.rustc_path = PROFILE_PATH.join("crown"); + config.rustc_path = PathBuf::from(env!("CARGO_BIN_EXE_crown")); config.target_rustcflags = Some(format!( "-L {} -L {}", PROFILE_PATH.display(), diff --git a/justfile b/justfile index e6f85a25..e39b0a45 100644 --- a/justfile +++ b/justfile @@ -119,13 +119,14 @@ gc-zeal *ARGS: ./scripts/test-gc-zeal.sh {{ARGS}} # It checks every workspace library and example natively, then the runtime and its dependencies -# for wasm32-wasip2, which includes the code only wasm builds compile. Then it runs crown's own -# tests. +# for wasm32-wasip2 and for wasm32-wasip3, which between them include the code only wasm builds +# compile. Then it runs crown's own tests. # # Run crown static GC analysis. check-gc: ./scripts/check-crown.sh --workspace --lib --examples ./scripts/check-crown.sh -p starlingmonkey --target wasm32-wasip2 + ./scripts/check-crown.sh -p starlingmonkey --target wasm32-wasip3 cd crown && RUSTC_BOOTSTRAP=1 cargo test # Run basic checks: formatting, clippy, tests, and the componentizer's unit tests. diff --git a/scripts/build-runtime.sh b/scripts/build-runtime.sh index 78b30420..50c75f37 100755 --- a/scripts/build-runtime.sh +++ b/scripts/build-runtime.sh @@ -81,4 +81,18 @@ case "$(wasm_version "$OUT")" in ;; esac +# A wasm32-wasip2 runtime runs on hosts without WASIp3 and the Component Model's async features, so +# it may neither import nor export a `0.3` interface, nor use those features. +if [ "$TARGET" = wasm32-wasip2 ]; then + wit=$(wasm-tools component wit "$OUT") + if grep -E '^\s*(import|export) [^ ]+@0\.3' <<<"$wit"; then + echo "$OUT imports or exports WASIp3 interfaces" >&2 + exit 1 + fi + if ! wasm-tools validate --features=-cm-async "$OUT"; then + echo "$OUT uses the Component Model's async features" >&2 + exit 1 + fi +fi + echo "built $OUT ($(du -h "$OUT" | cut -f1))" diff --git a/scripts/check-crown.sh b/scripts/check-crown.sh index 8fd0a48d..1968a167 100755 --- a/scripts/check-crown.sh +++ b/scripts/check-crown.sh @@ -11,6 +11,9 @@ # ./scripts/check-crown.sh -p core-runtime # Check a specific package # ./scripts/check-crown.sh --workspace # Check the entire workspace # ./scripts/check-crown.sh -p starlingmonkey --target wasm32-wasip2 # Check the wasm-only code +# +# Crown and the checks use the toolchain `crown/rust-toolchain.toml` pins, which has a std for +# wasm32-wasip2 and wasm32-wasip3. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -21,6 +24,7 @@ ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" CROWN_DIR="$ROOT_DIR/crown" CROWN_BIN="${CARGO_TARGET_DIR:-$CROWN_DIR/target}/release/crown" CROWN_SYSROOT="$(cd "$CROWN_DIR" && rustc --print sysroot)" +CROWN_TOOLCHAIN="$(cd "$CROWN_DIR" && rustup show active-toolchain | cut -d' ' -f1)" CROWN_LIB="$CROWN_SYSROOT/lib" # Step 1: Build crown with the rpath baked in so it always finds librustc_driver @@ -38,7 +42,7 @@ echo "Crown binary: $CROWN_BIN" # Step 2: Run cargo check with crown as the compiler wrapper echo "" echo "=== Running crown check ===" -RUSTC_BOOTSTRAP="1" RUSTC_WRAPPER="$CROWN_BIN" cargo check \ +RUSTC_BOOTSTRAP="1" RUSTC_WRAPPER="$CROWN_BIN" cargo "+$CROWN_TOOLCHAIN" check \ --manifest-path "$ROOT_DIR/Cargo.toml" \ --features "js/crown" \ "$@" diff --git a/scripts/check-init-wit.sh b/scripts/check-init-wit.sh index 38fc2014..12f463a7 100755 --- a/scripts/check-init-wit.sh +++ b/scripts/check-init-wit.sh @@ -1,10 +1,11 @@ #!/usr/bin/env bash # SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception # -# Fail if `starling/wit/init.wit` changed since a release without a version of its package that -# is not compatible with the release's. `starling-componentize` accepts a runtime whose version of -# the `init` interface has the same canonical version as its own, so a change to the interface -# needs a different canonical version. Doc comments are ignored. +# Fail if `starling/wit/p2/init.wit` differs from `starling/wit/init.wit` in anything but its doc +# comments and a synchronous `init`, or if `starling/wit/init.wit` changed since a release without +# a version of its package that is not compatible with the release's. `starling-componentize` +# accepts a runtime whose version of the `init` interface has the same canonical version as its +# own, so a change to the interface needs a different canonical version. Doc comments are ignored. # # Usage: scripts/check-init-wit.sh [] # @@ -13,6 +14,15 @@ set -euo pipefail file=starling/wit/init.wit +p2_file=starling/wit/p2/init.wit + +if ! diff -u --label "$file, with a synchronous init" --label "$p2_file" \ + <(wasm-tools component wit --no-docs "$file" | sed 's/: async func(/: func(/') \ + <(wasm-tools component wit --no-docs "$p2_file") >&2; then + echo "$p2_file must declare what $file does, with a synchronous \`init\`." >&2 + exit 1 +fi + base=${1:-} if [ -z "$base" ]; then if ! base=$(git describe --tags --abbrev=0 --match 'v[0-9]*' HEAD 2>/dev/null); then diff --git a/starling/Cargo.toml b/starling/Cargo.toml index 10d78531..81930ea4 100644 --- a/starling/Cargo.toml +++ b/starling/Cargo.toml @@ -39,23 +39,29 @@ libc-alloc = ['dep:libc'] core-runtime.workspace = true js.workspace = true libstarling.workspace = true -wasip3.workspace = true -# The workspace pins `wit-bindgen` to the version `wasip3` uses, so the component contains one -# wit-bindgen, and so one `run_ctors_once` guard. `__wrap___wasm_call_ctors` in `lib.rs` makes the -# C++ constructors run once. -# -# `inter-task-wakeup` lets one wit task wake another through plain Rust wakers, which the serve -# path needs: the response-body writer task parks on a Rust channel the request's event-loop -# drain task feeds (and the drain's body-done signal crosses back the other way). Without it, the -# executor panics when such a task is the only one left to wait on, as in a long-lived streamed -# response such as server-sent events. -wit-bindgen = { workspace = true, features = ["macros", "inter-task-wakeup"] } libc = { version = "0.2", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] wit-bridge.workspace = true +platform.workspace = true web-fetch.workspace = true +# The workspace pins `wit-bindgen` to the version `wasip2` and `wasip3` use, so the component +# contains one wit-bindgen, and so one `run_ctors_once` guard. `__wrap___wasm_call_ctors` in +# `lib.rs` makes the C++ constructors run once. +[target.'cfg(target_env = "p2")'.dependencies] +wasip2.workspace = true +wit-bindgen.workspace = true + +# `inter-task-wakeup` lets one wit task wake another through plain Rust wakers, which the serve +# path needs: the response-body writer task parks on a Rust channel the request's event-loop +# drain task feeds (and the drain's body-done signal crosses back the other way). Without it, the +# executor panics when such a task is the only one left to wait on, as in a long-lived streamed +# response such as server-sent events. +[target.'cfg(target_env = "p3")'.dependencies] +wasip3.workspace = true +wit-bindgen = { workspace = true, features = ["inter-task-wakeup"] } + [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tempfile = "3" diff --git a/starling/src/lib.rs b/starling/src/lib.rs index cb38d318..05d891be 100644 --- a/starling/src/lib.rs +++ b/starling/src/lib.rs @@ -117,53 +117,75 @@ mod static_init { static INIT_TP: extern "C" fn() = init_tp; } -/// The wasm component exports both `wasi:cli/run` and `wasi:http/handler`, so one build serves -/// HTTP and runs as a command. There is no dispatch between them: the host picks which export to -/// call. `wasmtime run` calls the former, `wasmtime serve` the latter. +/// The wasm component exports both `wasi:cli/run` and an HTTP handler (`wasi:http/handler` on +/// WASIp3, `wasi:http/incoming-handler` on WASIp2), so one build serves HTTP and runs as a +/// command. There is no dispatch between them: the host picks which export to call. +/// `wasmtime run` calls the former, `wasmtime serve` the latter. /// /// The two differ only in where their configuration comes from. The CLI export is handed argv; /// the HTTP export is not (`wasmtime serve` passes none), so it reads `STARLINGMONKEY_CONFIG`. +/// +/// On WASIp2, every export is synchronous and runs its work with [`platform::task::block_on`]. mod wasm_entry { use core_runtime::invocation::{InvocationState, OwnedInvocation}; use libstarling::config::RuntimeConfig; use wit_bridge::run::{report_run_outcome, run_main_export, MainModule}; + /// The body of `wasi:cli/run.run`. + /// + /// Call the main module's `run` export in an instance whose top level ran before a + /// snapshot: a componentized command, or a script wizened with `wizer-initialize`. + /// In any other instance, run the script the arguments name. + async fn run() -> Result<(), ()> { + let snapshot = if wit_bridge::init::is_initialized() { + Some((wit_bridge::init::runtime(), MainModule::Bootstrap)) + } else { + libstarling::serve_wasm::installed_runtime() + .map(|(runtime, raw_cx)| (runtime, MainModule::Entry(raw_cx))) + }; + let Some((runtime, main)) = snapshot else { + let config = match RuntimeConfig::from_args(std::env::args()) { + Ok(config) => config, + Err(e) => { + let _ = e.print(); + return Err(()); + } + }; + return libstarling::run(config).await.map_err(|e| { + eprintln!("{e}"); + }); + }; + // The snapshot's top level left no work behind, since both kinds of snapshot refuse + // any, so `run` starts on a fresh loop, after the resume fixups have repaired the + // clocks. + libstarling::serve_wasm::fix_up_after_resume(); + let invocation = OwnedInvocation::new(runtime, InvocationState::new()); + report_run_outcome(run_main_export(&invocation, main).await) + } + + /// The body of `wizer-initialize`. + async fn wizer_initialize() { + // A failure here would otherwise be baked into the snapshot as a runtime that + // silently starts from scratch on every request. + if let Err(e) = libstarling::serve_wasm::pre_initialize().await { + panic!("pre-initialization failed: {e}"); + } + // Last, so that the clock reading it records is past every timestamp the + // snapshot holds. + libstarling::serve_wasm::prepare_for_snapshot(); + } + struct StarlingCli; + struct StarlingHttp; + #[cfg(target_env = "p3")] impl wasip3::exports::cli::run::Guest for StarlingCli { - /// Call the main module's `run` export in an instance whose top level ran before a - /// snapshot: a componentized command, or a script wizened with `wizer-initialize`. - /// In any other instance, run the script the arguments name. async fn run() -> Result<(), ()> { - let snapshot = if wit_bridge::init::is_initialized() { - Some((wit_bridge::init::runtime(), MainModule::Bootstrap)) - } else { - libstarling::serve_wasm::installed_runtime() - .map(|(runtime, raw_cx)| (runtime, MainModule::Entry(raw_cx))) - }; - let Some((runtime, main)) = snapshot else { - let config = match RuntimeConfig::from_args(std::env::args()) { - Ok(config) => config, - Err(e) => { - let _ = e.print(); - return Err(()); - } - }; - return libstarling::run(config).await.map_err(|e| { - eprintln!("{e}"); - }); - }; - // The snapshot's top level left no work behind, since both kinds of snapshot refuse - // any, so `run` starts on a fresh loop, after the resume fixups have repaired the - // clocks. - libstarling::serve_wasm::fix_up_after_resume(); - let invocation = OwnedInvocation::new(runtime, InvocationState::new()); - report_run_outcome(run_main_export(&invocation, main).await) + run().await } } - struct StarlingHttp; - + #[cfg(target_env = "p3")] impl wasip3::exports::http::handler::Guest for StarlingHttp { async fn handle( request: wasip3::http::types::Request, @@ -172,21 +194,52 @@ mod wasm_entry { } } + #[cfg(target_env = "p3")] wasip3::cli::command::export!(StarlingCli); + #[cfg(target_env = "p3")] wasip3::http::service::export!(StarlingHttp); + #[cfg(target_env = "p2")] + impl wasip2::exports::cli::run::Guest for StarlingCli { + fn run() -> Result<(), ()> { + platform::task::block_on(run()) + } + } + + #[cfg(target_env = "p2")] + impl wasip2::exports::http::incoming_handler::Guest for StarlingHttp { + fn handle( + request: wasip2::http::types::IncomingRequest, + response_out: wasip2::http::types::ResponseOutparam, + ) { + platform::task::block_on(async move { + let response = libstarling::serve_wasm::handle(request).await; + // Set before the tasks writing the response body first run, since the host + // reads the body only once the response is set. + wasip2::http::types::ResponseOutparam::set(response_out, response); + }); + } + } + + #[cfg(target_env = "p2")] + wasip2::cli::command::export!(StarlingCli); + #[cfg(target_env = "p2")] + wasip2::http::proxy::export!(StarlingHttp); + /// Pre-initialization entry point for `wasmtime wizer`, which runs it and snapshots the /// initialized instance. /// /// Wizer calls a component-level function export, and a component exports only what its world /// declares. A bare `#[export_name]` does not survive componentization, so this declares the /// smallest possible world containing just that function. Its fragment merges with the two - /// `export!`s above into one component exporting all three. + /// `export!`s above into one component exporting all three. The function is async on WASIp3, + /// and synchronous on WASIp2, which has no async functions. /// /// The export has to survive the snapshot (`--keep-init-func=true`): dropping it, which is /// wizer's default, leaves the component referring to a core export that is no longer there, /// and the result fails to load. See `scripts/test-wizer.sh`. mod wizer { + #[cfg(target_env = "p3")] wit_bindgen::generate!({ inline: r#" package local:wizer; @@ -197,18 +250,30 @@ mod wasm_entry { world: "wizer", }); + #[cfg(target_env = "p2")] + wit_bindgen::generate!({ + inline: r#" + package local:wizer; + world wizer { + export wizer-initialize: func(); + } + "#, + world: "wizer", + }); + struct Init; + #[cfg(target_env = "p3")] impl Guest for Init { async fn wizer_initialize() { - // A failure here would otherwise be baked into the snapshot as a runtime that - // silently starts from scratch on every request. - if let Err(e) = libstarling::serve_wasm::pre_initialize().await { - panic!("pre-initialization failed: {e}"); - } - // Last, so that the clock reading it records is past every timestamp the - // snapshot holds. - libstarling::serve_wasm::prepare_for_snapshot(); + super::wizer_initialize().await + } + } + + #[cfg(target_env = "p2")] + impl Guest for Init { + fn wizer_initialize() { + platform::task::block_on(super::wizer_initialize()) } } @@ -217,7 +282,7 @@ mod wasm_entry { } /// The `starling:componentize/init` export, which `starling/wit/init.wit` -/// declares. +/// declares, and on WASIp2 `starling/wit/p2/init.wit`. /// /// `starling-componentize` links this runtime together with the user's wit-dylib /// bindings and calls `init` once, under Wizer, to bootstrap the SpiderMonkey @@ -229,8 +294,9 @@ mod wasm_entry { /// The generated `run_ctors_once` shim runs the static constructors before the /// body, so SpiderMonkey is safe to use here. mod init_export { - /// A `Request` for `value` if it is a wrapper of a `wasi:http/types` - /// `request`, whose handle the `Request` takes. See + /// A `Request` for `value` if it is a wrapper of a + /// [`HOST_REQUEST_RESOURCE`](platform::http::HOST_REQUEST_RESOURCE), whose + /// handle the `Request` takes. See /// [`web_fetch::request::register_request_input_adapter`]. fn request_input<'s>( scope: &'s js::gc::scope::Scope<'_>, @@ -239,8 +305,8 @@ mod init_export { let handle = wit_bridge::interpreter::take_imported_handle( scope, value, - libstarling::serve_wasm::WASI_HTTP_TYPES, - "request", + platform::http::HOST_HTTP_TYPES, + platform::http::HOST_REQUEST_RESOURCE, )?; handle .map(|handle| { @@ -252,12 +318,20 @@ mod init_export { } mod bindings { + #[cfg(target_env = "p3")] wit_bindgen::generate!({ world: "runtime", path: "wit/init.wit", generate_all, }); + #[cfg(target_env = "p2")] + wit_bindgen::generate!({ + world: "runtime", + path: "wit/p2/init.wit", + generate_all, + }); + use super::Init; export!(Init); @@ -268,9 +342,8 @@ mod init_export { struct Init; + #[cfg(target_env = "p3")] impl bindings::exports::starling::componentize::init::Guest for Init { - /// Bootstrap the runtime and evaluate the application's modules, as - /// `init` in `wit/init.wit` describes. async fn init( main: String, main_name: String, @@ -279,120 +352,156 @@ mod init_export { raw_http_handler: Option, init_location: Option, ) -> Result { - // Registered before `initialize_runtime` calls `Runtime::init`, which - // fires every global initializer on the default global. - libstarling::register_builtins(); - libstarling::runtime::register_global_initializer(wit_bridge::add_to_global); - // A `Response` lowers to an owned `wasi:http/types` `response`, so an - // application's own `wasi:http/handler` can return one. - wit_bridge::resources::register_own_adapter( - libstarling::serve_wasm::WASI_HTTP_TYPES, - "response", - libstarling::serve_wasm::response_handle, - libstarling::serve_wasm::response_accepts, - ); - // `fetch` takes a `wasi:http/types` `request`, such as the one an - // application's own `wasi:http/handler` receives. - web_fetch::request::register_request_input_adapter(request_input); - - let config = core_runtime::config::RuntimeConfig { - pre_initialize: true, + init( + main, + main_name, + main_path, + cli, + raw_http_handler, init_location, - ..Default::default() - }; - libstarling::apply_pre_init_config(&config)?; - // The host's imports are not linked while the snapshot is taken. - wit_bridge::imports::set_imports_unavailable(true); - wit_bridge::init::set_before_export(libstarling::serve_wasm::fix_up_after_resume); - let initialized = wit_bridge::initialize_runtime( - &config, - &main, - &main_name, - main_path.as_deref(), - wit_bridge::interpreter::synthesize, - )?; - let invocation = - OwnedInvocation::new(wit_bridge::init::runtime(), initialized.invocation); - let raw_cx = wit_bridge::init::raw_cx(); - - // A top-level `await` settles only once the loop runs the timers and - // I/O it waits on, and the application's exports are in their - // temporal dead zone until it does. We drive the loop only while the - // top level is unfinished, so work it merely queued, such as a timer, - // is refused below rather than run under Wizer. - if !wit_bridge::init::with_scope(|scope| initialized.evaluation.is_finished(scope)) { + ) + .await + } + } + + #[cfg(target_env = "p2")] + impl bindings::exports::starling::componentize::init::Guest for Init { + fn init( + main: String, + main_name: String, + main_path: Option, + cli: bool, + raw_http_handler: Option, + init_location: Option, + ) -> Result { + platform::task::block_on(init( + main, + main_name, + main_path, + cli, + raw_http_handler, + init_location, + )) + } + } + + /// Bootstrap the runtime and evaluate the application's modules, as + /// `init` in `wit/init.wit` describes. + async fn init( + main: String, + main_name: String, + main_path: Option, + cli: bool, + raw_http_handler: Option, + init_location: Option, + ) -> Result { + // Registered before `initialize_runtime` calls `Runtime::init`, which + // fires every global initializer on the default global. + libstarling::register_builtins(); + libstarling::runtime::register_global_initializer(wit_bridge::add_to_global); + // A `Response` lowers to an owned response resource of the host's + // `wasi:http/types`, so an application's own HTTP handler can return one. + wit_bridge::resources::register_own_adapter( + platform::http::HOST_HTTP_TYPES, + platform::http::HOST_RESPONSE_RESOURCE, + libstarling::serve_wasm::response_handle, + libstarling::serve_wasm::response_accepts, + ); + // `fetch` takes a request resource of the host's `wasi:http/types`, such as + // the one an application's own HTTP handler receives. + web_fetch::request::register_request_input_adapter(request_input); + + let config = core_runtime::config::RuntimeConfig { + pre_initialize: true, + init_location, + ..Default::default() + }; + libstarling::apply_pre_init_config(&config)?; + // The host's imports are not linked while the snapshot is taken. + wit_bridge::imports::set_imports_unavailable(true); + wit_bridge::init::set_before_export(libstarling::serve_wasm::fix_up_after_resume); + let initialized = wit_bridge::initialize_runtime( + &config, + &main, + &main_name, + main_path.as_deref(), + wit_bridge::interpreter::synthesize, + )?; + let invocation = OwnedInvocation::new(wit_bridge::init::runtime(), initialized.invocation); + let raw_cx = wit_bridge::init::raw_cx(); + + // A top-level `await` settles only once the loop runs the timers and + // I/O it waits on, and the application's exports are in their + // temporal dead zone until it does. We drive the loop only while the + // top level is unfinished, so work it merely queued, such as a timer, + // is refused below rather than run under Wizer. + if !wit_bridge::init::with_scope(|scope| initialized.evaluation.is_finished(scope)) { + // SAFETY: `initialize_runtime` left the default global's realm + // entered for the process, and keeps its runtime alive for the + // process. + unsafe { + libstarling::serve_wasm::drive_startup( + raw_cx, + invocation.state().event_loop(), + &initialized.evaluation, + ) + .await; + } + } + wit_bridge::init::with_scope(|scope| { + initialized + .evaluation + .rejection(scope, "main module threw during evaluation")?; + if initialized.evaluation.is_finished(scope) { + Ok(()) + } else { + Err( + "the application's top-level `await` never settled: the event loop \ + ran out of work while it was still pending" + .to_string(), + ) + } + })?; + + let raw_export = wit_bridge::init::with_main_scope(|scope, ns| { + wit_bridge::interpreter::install_exports(scope, ns, cli, raw_http_handler.as_deref()) + })?; + + wit_bridge::init::with_scope(|scope| { + libstarling::serve_wasm::finish_for_snapshot(scope, invocation.state().event_loop()) + })?; + drop(invocation); + + let http_handler = raw_export + .map(|raw| { // SAFETY: `initialize_runtime` left the default global's realm - // entered for the process, and keeps its runtime alive for the - // process. + // of `raw_cx` entered for the process. unsafe { - libstarling::serve_wasm::drive_startup( + libstarling::serve_wasm::select_http_handler( raw_cx, - invocation.state().event_loop(), - &initialized.evaluation, - ) - .await; - } - } - wit_bridge::init::with_scope(|scope| { - initialized - .evaluation - .rejection(scope, "main module threw during evaluation")?; - if initialized.evaluation.is_finished(scope) { - Ok(()) - } else { - Err( - "the application's top-level `await` never settled: the event loop \ - ran out of work while it was still pending" - .to_string(), + RawHttpHandler { + paths: &raw.paths, + provided: raw.provided, + }, ) } - })?; - - let raw_export = wit_bridge::init::with_main_scope(|scope, ns| { - wit_bridge::interpreter::install_exports( - scope, - ns, - cli, - raw_http_handler.as_deref(), - ) - })?; - - wit_bridge::init::with_scope(|scope| { - libstarling::serve_wasm::finish_for_snapshot(scope, invocation.state().event_loop()) - })?; - drop(invocation); - - let http_handler = raw_export - .map(|raw| { - // SAFETY: `initialize_runtime` left the default global's realm - // of `raw_cx` entered for the process. - unsafe { - libstarling::serve_wasm::select_http_handler( - raw_cx, - RawHttpHandler { - paths: &raw.paths, - provided: raw.provided, - }, - ) - } - }) - .transpose()?; - - // Hand the runtime to the serve path, so `wasi:http/handler` - // dispatches against this global instead of standing up a second - // runtime. - libstarling::serve_wasm::adopt_runtime(wit_bridge::init::runtime(), raw_cx, &config)?; - - // The instance is wizened as soon as this returns, so everything - // above is state a resumed instance inherits. Last, since this - // records the clock reading the resumed instance's own clocks are - // advanced past. - wit_bridge::imports::set_imports_unavailable(false); - libstarling::serve_wasm::prepare_for_snapshot(); - Ok(matches!( - http_handler, - Some(libstarling::serve_wasm::HttpHandler::Raw) - )) - } + }) + .transpose()?; + + // Hand the runtime to the serve path, so `wasi:http/handler` + // dispatches against this global instead of standing up a second + // runtime. + libstarling::serve_wasm::adopt_runtime(wit_bridge::init::runtime(), raw_cx, &config)?; + + // The instance is wizened as soon as this returns, so everything + // above is state a resumed instance inherits. Last, since this + // records the clock reading the resumed instance's own clocks are + // advanced past. + wit_bridge::imports::set_imports_unavailable(false); + libstarling::serve_wasm::prepare_for_snapshot(); + Ok(matches!( + http_handler, + Some(libstarling::serve_wasm::HttpHandler::Raw) + )) } } diff --git a/starling/wit/p2/init.wit b/starling/wit/p2/init.wit new file mode 100644 index 00000000..d095a6a7 --- /dev/null +++ b/starling/wit/p2/init.wit @@ -0,0 +1,47 @@ +/// The interface between the runtime and `starling-componentize`, which calls +/// `init` under Wizer. Its version changes whenever the interface does, and the +/// componentizer accepts a runtime whose version of it differs only in the +/// patch number. +/// +/// This is the interface a wasm32-wasip2 runtime exports: `../init.wit` with a +/// synchronous `init`, since a WASIp2 host has no async functions. The two +/// files change together. +package starling:componentize@0.1.0; + +interface init { + /// `main` is the source of the application's main module, and `main-name` is + /// the name it is compiled under, which stack traces and error messages show + /// for it. + /// + /// `main-path` is the path of the file `main` was read from, relative to the + /// base directory, or `none` for a source from no file there. A module + /// importing that file gets the main module. + /// + /// `cli` is set for a component that exports `wasi:cli/run`, whose main module + /// must export a `run` function. + /// + /// `raw-http-handler` is `none` for a component that does not export + /// `wasi:http/incoming-handler`. Otherwise it names the export the application + /// implements instead of registering a `fetch` listener, and the result is + /// whether that export serves requests rather than the runtime's builtin, which + /// dispatches each request as a `fetch` event. + /// + /// `init-location` is the URL `globalThis.location` reflects while the main + /// module's top level evaluates, or `none` to leave it unset, which makes + /// reading `location` throw. + /// + /// `init` returns once the main module's top level, including a top-level + /// `await`, has finished evaluating. + init: func( + main: string, + main-name: string, + main-path: option, + cli: bool, + raw-http-handler: option, + init-location: option, + ) -> result; +} + +world runtime { + export init; +}