* Implement defining host functions at the Config level. This commit introduces defining host functions at the `Config` rather than with `Func` tied to a `Store`. The intention here is to enable a host to define all of the functions once with a `Config` and then use a `Linker` (or directly with `Store::get_host_func`) to use the functions when instantiating a module. This should help improve the performance of use cases where a `Store` is short-lived and redefining the functions at every module instantiation is a noticeable performance hit. This commit adds `add_to_config` to the code generation for Wasmtime's `Wasi` type. The new method adds the WASI functions to the given config as host functions. This commit adds context functions to `Store`: `get` to get a context of a particular type and `set` to set the context on the store. For safety, `set` cannot replace an existing context value of the same type. `Wasi::set_context` was added to set the WASI context for a `Store` when using `Wasi::add_to_config`. * Add `Config::define_host_func_async`. * Make config "async" rather than store. This commit moves the concept of "async-ness" to `Config` rather than `Store`. Note: this is a breaking API change for anyone that's already adopted the new async support in Wasmtime. Now `Config::new_async` is used to create an "async" config and any `Store` associated with that config is inherently "async". This is needed for async shared host functions to have some sanity check during their execution (async host functions, like "async" `Func`, need to be called with the "async" variants). * Update async function tests to smoke async shared host functions. This commit updates the async function tests to also smoke the shared host functions, plus `Func::wrap0_async`. This also changes the "wrap async" method names on `Config` to `wrap$N_host_func_async` to slightly better match what is on `Func`. * Move the instance allocator into `Engine`. This commit moves the instantiated instance allocator from `Config` into `Engine`. This makes certain settings in `Config` no longer order-dependent, which is how `Config` should ideally be. This also removes the confusing concept of the "default" instance allocator, instead opting to construct the on-demand instance allocator when needed. This does alter the semantics of the instance allocator as now each `Engine` gets its own instance allocator rather than sharing a single one between all engines created from a configuration. * Make `Engine::new` return `Result`. This is a breaking API change for anyone using `Engine::new`. As creating the pooling instance allocator may fail (likely cause is not enough memory for the provided limits), instead of panicking when creating an `Engine`, `Engine::new` now returns a `Result`. * Remove `Config::new_async`. This commit removes `Config::new_async` in favor of treating "async support" as any other setting on `Config`. The setting is `Config::async_support`. * Remove order dependency when defining async host functions in `Config`. This commit removes the order dependency where async support must be enabled on the `Config` prior to defining async host functions. The check is now delayed to when an `Engine` is created from the config. * Update WASI example to use shared `Wasi::add_to_config`. This commit updates the WASI example to use `Wasi::add_to_config`. As only a single store and instance are used in the example, it has no semantic difference from the previous example, but the intention is to steer users towards defining WASI on the config and only using `Wasi::add_to_linker` when more explicit scoping of the WASI context is required.
104 lines
4.4 KiB
Rust
104 lines
4.4 KiB
Rust
//! `wasmtime-wasi` now supports using multiple snapshots to interface to the
|
|
//! same `WasiCtx`!
|
|
//!
|
|
//! `wasmtime_wasi::Wasi::new(&Store, WasiCtx)` is a struct which owns your
|
|
//! `WasiCtx` and provides linkage to every available snapshot.
|
|
//!
|
|
//! Individual snapshots are available through
|
|
//! `wasmtime_wasi::snapshots::preview_{0, 1}::Wasi::new(&Store, Rc<RefCell<WasiCtx>>)`.
|
|
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
pub use wasi_common::{Error, WasiCtx, WasiCtxBuilder, WasiDir, WasiFile};
|
|
use wasmtime::{Config, Linker, Store};
|
|
|
|
/// An instantiated instance of all available wasi exports. Presently includes
|
|
/// both the "preview1" snapshot and the "unstable" (preview0) snapshot.
|
|
pub struct Wasi {
|
|
preview_1: snapshots::preview_1::Wasi,
|
|
preview_0: snapshots::preview_0::Wasi,
|
|
}
|
|
|
|
impl Wasi {
|
|
pub fn new(store: &Store, context: WasiCtx) -> Self {
|
|
let context = Rc::new(RefCell::new(context));
|
|
let preview_1 = snapshots::preview_1::Wasi::new(store, context.clone());
|
|
let preview_0 = snapshots::preview_0::Wasi::new(store, context);
|
|
Self {
|
|
preview_1,
|
|
preview_0,
|
|
}
|
|
}
|
|
pub fn add_to_linker(&self, linker: &mut Linker) -> Result<(), anyhow::Error> {
|
|
self.preview_1.add_to_linker(linker)?;
|
|
self.preview_0.add_to_linker(linker)?;
|
|
Ok(())
|
|
}
|
|
pub fn add_to_config(config: &mut Config) {
|
|
snapshots::preview_1::Wasi::add_to_config(config);
|
|
snapshots::preview_0::Wasi::add_to_config(config);
|
|
}
|
|
pub fn set_context(store: &Store, context: WasiCtx) -> Result<(), WasiCtx> {
|
|
// It doesn't matter which underlying `Wasi` type this gets called on as the
|
|
// implementations are identical
|
|
snapshots::preview_1::Wasi::set_context(store, context)
|
|
}
|
|
}
|
|
|
|
pub mod snapshots {
|
|
pub mod preview_1 {
|
|
use wasi_common::WasiCtx;
|
|
// Defines a `struct Wasi` with member fields and appropriate APIs for dealing
|
|
// with all the various WASI exports.
|
|
wasmtime_wiggle::wasmtime_integration!({
|
|
// The wiggle code to integrate with lives here:
|
|
target: wasi_common::snapshots::preview_1,
|
|
// This must be the same witx document as used above. This should be ensured by
|
|
// the `WASI_ROOT` env variable, which is set in wasi-common's `build.rs`.
|
|
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
|
|
// This must be the same ctx type as used for the target:
|
|
ctx: WasiCtx,
|
|
// This macro will emit a struct to represent the instance,
|
|
// with this name and docs:
|
|
modules: { wasi_snapshot_preview1 =>
|
|
{ name: Wasi,
|
|
docs: "An instantiated instance of the wasi exports.
|
|
|
|
This represents a wasi module which can be used to instantiate other wasm
|
|
modules. This structure exports all that various fields of the wasi instance
|
|
as fields which can be used to implement your own instantiation logic, if
|
|
necessary. Additionally [`Wasi::get_export`] can be used to do name-based
|
|
resolution.",
|
|
},
|
|
},
|
|
});
|
|
}
|
|
pub mod preview_0 {
|
|
use wasi_common::WasiCtx;
|
|
// Defines a `struct Wasi` with member fields and appropriate APIs for dealing
|
|
// with all the various WASI exports.
|
|
wasmtime_wiggle::wasmtime_integration!({
|
|
// The wiggle code to integrate with lives here:
|
|
target: wasi_common::snapshots::preview_0,
|
|
// This must be the same witx document as used above. This should be ensured by
|
|
// the `WASI_ROOT` env variable, which is set in wasi-common's `build.rs`.
|
|
witx: ["$WASI_ROOT/phases/old/snapshot_0/witx/wasi_unstable.witx"],
|
|
// This must be the same ctx type as used for the target:
|
|
ctx: WasiCtx,
|
|
// This macro will emit a struct to represent the instance,
|
|
// with this name and docs:
|
|
modules: { wasi_unstable =>
|
|
{ name: Wasi,
|
|
docs: "An instantiated instance of the wasi exports.
|
|
|
|
This represents a wasi module which can be used to instantiate other wasm
|
|
modules. This structure exports all that various fields of the wasi instance
|
|
as fields which can be used to implement your own instantiation logic, if
|
|
necessary. Additionally [`Wasi::get_export`] can be used to do name-based
|
|
resolution.",
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|