Path: blob/main/crates/wasi/src/p3/bindings.rs
3050 views
//! Auto-generated bindings for WASI interfaces.1//!2//! This module contains the output of the [`bindgen!`] macro when run over3//! the `wasi:cli/imports` world.4//!5//! [`bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html6//!7//! # Examples8//!9//! If you have a WIT world which refers to WASI interfaces you probably want to10//! use this modules's bindings rather than generate fresh bindings. That can be11//! done using the `with` option to [`bindgen!`]:12//!13//! ```rust14//! use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};15//! use wasmtime::{Result, Engine, Config};16//! use wasmtime::component::{Linker, HasSelf, ResourceTable};17//!18//! wasmtime::component::bindgen!({19//! inline: "20//! package example:wasi;21//!22//! // An example of extending the `wasi:cli/command` world with a23//! // custom host interface.24//! world my-world {25//! include wasi:cli/[email protected];26//!27//! import custom-host;28//! }29//!30//! interface custom-host {31//! my-custom-function: func();32//! }33//! ",34//! path: "src/p3/wit",35//! with: {36//! "wasi": wasmtime_wasi::p3::bindings,37//! },38//! require_store_data_send: true,39//! });40//!41//! struct MyState {42//! ctx: WasiCtx,43//! table: ResourceTable,44//! }45//!46//! impl example::wasi::custom_host::Host for MyState {47//! fn my_custom_function(&mut self) {48//! // ..49//! }50//! }51//!52//! impl WasiView for MyState {53//! fn ctx(&mut self) -> WasiCtxView<'_> {54//! WasiCtxView{55//! ctx: &mut self.ctx,56//! table: &mut self.table,57//! }58//! }59//! }60//!61//! fn main() -> Result<()> {62//! let mut config = Config::default();63//! config.wasm_component_model_async(true);64//! let engine = Engine::new(&config)?;65//! let mut linker: Linker<MyState> = Linker::new(&engine);66//! wasmtime_wasi::p3::add_to_linker(&mut linker)?;67//! example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;68//!69//! // .. use `Linker` to instantiate component ...70//!71//! Ok(())72//! }73//! ```7475mod generated {76wasmtime::component::bindgen!({77path: "src/p3/wit",78world: "wasi:cli/command",79imports: {80"wasi:cli/stdin": store | tracing | trappable,81"wasi:cli/stdout": store | tracing | trappable,82"wasi:cli/stderr": store | tracing | trappable,83"wasi:filesystem/types.[method]descriptor.read-via-stream": store | tracing | trappable,84"wasi:sockets/types.[method]tcp-socket.bind": async | tracing | trappable,85"wasi:sockets/types.[method]tcp-socket.listen": store | tracing | trappable,86"wasi:sockets/types.[method]tcp-socket.receive": store | tracing | trappable,87"wasi:sockets/types.[method]udp-socket.bind": async | tracing | trappable,88"wasi:sockets/types.[method]udp-socket.connect": async | tracing | trappable,89default: tracing | trappable,90},91exports: { default: async | store | task_exit },92with: {93"wasi:cli/terminal-input.terminal-input": crate::p3::cli::TerminalInput,94"wasi:cli/terminal-output.terminal-output": crate::p3::cli::TerminalOutput,95"wasi:filesystem/types.descriptor": crate::filesystem::Descriptor,96"wasi:sockets/types.tcp-socket": crate::sockets::TcpSocket,97"wasi:sockets/types.udp-socket": crate::sockets::UdpSocket,98},99trappable_error_type: {100"wasi:filesystem/types.error-code" => crate::p3::filesystem::FilesystemError,101"wasi:sockets/types.error-code" => crate::p3::sockets::SocketError,102},103});104}105pub use self::generated::LinkOptions;106pub use self::generated::exports;107pub use self::generated::wasi::*;108109/// Bindings to execute and run a `wasi:cli/command`.110///111/// This structure is automatically generated by `bindgen!`.112///113/// This can be used for a more "typed" view of executing a command component114/// through the [`Command::wasi_cli_run`] method plus115/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).116///117/// # Examples118///119/// ```no_run120/// use wasmtime::{Engine, Result, Store, Config};121/// use wasmtime::component::{Component, Linker, ResourceTable};122/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};123/// use wasmtime_wasi::p3::bindings::Command;124///125/// // This example is an example shim of executing a component based on the126/// // command line arguments provided to this program.127/// #[tokio::main]128/// async fn main() -> Result<()> {129/// let args = std::env::args().skip(1).collect::<Vec<_>>();130///131/// // Configure and create `Engine`132/// let mut config = Config::new();133/// config.wasm_component_model_async(true);134/// let engine = Engine::new(&config)?;135///136/// // Configure a `Linker` with WASI, compile a component based on137/// // command line arguments, and then pre-instantiate it.138/// let mut linker = Linker::<MyState>::new(&engine);139/// wasmtime_wasi::p3::add_to_linker(&mut linker)?;140/// let component = Component::from_file(&engine, &args[0])?;141///142///143/// // Configure a `WasiCtx` based on this program's environment. Then144/// // build a `Store` to instantiate into.145/// let mut builder = WasiCtx::builder();146/// builder.inherit_stdio().inherit_env().args(&args);147/// let mut store = Store::new(148/// &engine,149/// MyState {150/// ctx: builder.build(),151/// table: ResourceTable::default(),152/// },153/// );154///155/// // Instantiate the component and we're off to the races.156/// let command = Command::instantiate_async(&mut store, &component, &linker).await?;157/// let program_result = store.run_concurrent(async move |store| {158/// command.wasi_cli_run().call_run(store).await159/// }).await??.0;160/// match program_result {161/// Ok(()) => Ok(()),162/// Err(()) => std::process::exit(1),163/// }164/// }165///166/// struct MyState {167/// ctx: WasiCtx,168/// table: ResourceTable,169/// }170///171/// impl WasiView for MyState {172/// fn ctx(&mut self) -> WasiCtxView<'_> {173/// WasiCtxView{174/// ctx: &mut self.ctx,175/// table: &mut self.table,176/// }177/// }178/// }179/// ```180///181/// ---182pub use self::generated::Command;183184/// Pre-instantiated analog of [`Command`]185///186/// This can be used to front-load work such as export lookup before187/// instantiation.188///189/// # Examples190///191/// ```no_run192/// use wasmtime::{Engine, Result, Store, Config};193/// use wasmtime::component::{Linker, Component, ResourceTable};194/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};195/// use wasmtime_wasi::p3::bindings::CommandPre;196///197/// // This example is an example shim of executing a component based on the198/// // command line arguments provided to this program.199/// #[tokio::main]200/// async fn main() -> Result<()> {201/// let args = std::env::args().skip(1).collect::<Vec<_>>();202///203/// // Configure and create `Engine`204/// let mut config = Config::new();205/// config.wasm_component_model_async(true);206/// let engine = Engine::new(&config)?;207///208/// // Configure a `Linker` with WASI, compile a component based on209/// // command line arguments, and then pre-instantiate it.210/// let mut linker = Linker::<MyState>::new(&engine);211/// wasmtime_wasi::p3::add_to_linker(&mut linker)?;212/// let component = Component::from_file(&engine, &args[0])?;213/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;214///215///216/// // Configure a `WasiCtx` based on this program's environment. Then217/// // build a `Store` to instantiate into.218/// let mut builder = WasiCtx::builder();219/// builder.inherit_stdio().inherit_env().args(&args);220/// let mut store = Store::new(221/// &engine,222/// MyState {223/// ctx: builder.build(),224/// table: ResourceTable::default(),225/// },226/// );227///228/// // Instantiate the component and we're off to the races.229/// let command = pre.instantiate_async(&mut store).await?;230/// // TODO: Construct an accessor from `store` to call `run`231/// // https://github.com/bytecodealliance/wasmtime/issues/11249232/// //let program_result = command.wasi_cli_run().call_run(&mut store).await?;233/// let program_result = todo!();234/// match program_result {235/// Ok(()) => Ok(()),236/// Err(()) => std::process::exit(1),237/// }238/// }239///240/// struct MyState {241/// ctx: WasiCtx,242/// table: ResourceTable,243/// }244///245/// impl WasiView for MyState {246/// fn ctx(&mut self) -> WasiCtxView<'_> {247/// WasiCtxView{248/// ctx: &mut self.ctx,249/// table: &mut self.table,250/// }251/// }252/// }253/// ```254///255/// ---256// TODO: Make this public, once `CommandPre` can be used for257// calling exports258// https://github.com/bytecodealliance/wasmtime/issues/11249259#[doc(hidden)]260pub use self::generated::CommandPre;261262pub use self::generated::CommandIndices;263264265