Skip to content

Commit

Permalink
Add documentation and refactor wasmtime-wasi (#8228)
Browse files Browse the repository at this point in the history
* Add documentation and examples for `wasmtime-wasi`

This commit adds lots of missing documentation and examples to top-level
types in `wasmtime-wasi`, mostly related to WASIp2. I've additionally
made a number of small refactorings here to try to make the APIs a bit
more straightforward and symmetric and simplify where I can.

* Remove `bindings::wasi` (reexports are still present under `bindings`)
* Rename `bindings::sync_io` to `bindings::sync`
* Generate fewer bindings in `bindings::sync` that can be pulled in from
  the `bindings` module.
* Change `WasiCtxBuilder::preopened_dir` to take a path instead of a
  `Dir` argument to avoid the need for another import.
* Synchronize `wasmtime_wasi_http::{add_to_linker, sync::add_to_linker}`
  in terms of interfaces added.
* Remove `wasmtime_wasi::command` and move the generated types to the
  `bindings` module.
* Move top-level add-to-linker functions to
  `wasmtime_wasi::add_to_linker_sync` and
  `wasmtime_wasi::add_to_linker_async`.

Closes #8187
Closes #8188

* Add documentation for `wasmtime_wasi::preview1` and refactor

This commit adds documentation for the `wasmtime_wasi::preview1` module
and additionally refactors it as well. Previously this was based on a
similar design as WASIp2 with a "view trait" and various bits and
pieces, but the design constraints of WASIp1 lends itself to a simpler
solution of "just" passing around `WasiP1Ctx` instead. This goes back to
what `wasi-common` did of sorts where the `add_to_linker_*` functions
only need a projection from `&mut T` to `&mut WasiP1Ctx`, a concrete
type, which simplifies the module and usage.

* Small refactorings to `preopened_dir`

* Add `WasiCtx::builder`.

* Fix typo

* Review comments
  • Loading branch information
alexcrichton authored Mar 28, 2024
1 parent a461382 commit 20660cb
Show file tree
Hide file tree
Showing 25 changed files with 1,361 additions and 460 deletions.
18 changes: 12 additions & 6 deletions crates/wasi-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ wasmtime::component::bindgen!({

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView,
{
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
Expand Down Expand Up @@ -47,18 +47,24 @@ pub mod sync {
async: false,
with: {
"wasi:http": bindings::http, // http is in this crate
"wasi:io": wasmtime_wasi::bindings::sync_io, // io is sync
"wasi:io": wasmtime_wasi::bindings::sync, // io is sync
"wasi": wasmtime_wasi::bindings, // everything else
},
});

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView,
{
// TODO: this shouldn't be required, but the adapter unconditionally pulls in all of these
// dependencies.
wasmtime_wasi::command::sync::add_to_linker(l)?;
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::sync::io::poll::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::sync::io::streams::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::io::error::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdin::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdout::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stderr::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::random::random::add_to_linker(l, |t| t)?;

add_only_http_to_linker(l)?;

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

foreach_http!(assert_test_exists);

Expand All @@ -13,7 +13,7 @@ async fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi::command::add_to_linker(&mut linker)?;
wasmtime_wasi::add_to_linker_async(&mut linker)?;
wasmtime_wasi_http::proxy::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?;
let result = command.wasi_cli_run().call_run(&mut store).await?;
Expand Down
5 changes: 3 additions & 2 deletions crates/wasi-http/tests/all/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::command::sync::Command;
use wasmtime_wasi::bindings::sync::Command;

foreach_http!(assert_test_exists);

Expand All @@ -12,7 +12,8 @@ fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi_http::proxy::sync::add_to_linker(&mut linker)?;
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
wasmtime_wasi_http::proxy::sync::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate(&mut store, &component, &linker)?;
let result = command.wasi_cli_run().call_run(&mut store)?;
result.map_err(|()| anyhow::anyhow!("run returned an error"))
Expand Down
Loading

0 comments on commit 20660cb

Please sign in to comment.