Path: blob/main/crates/wasi/src/p3/bindings.rs
1692 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.async_support(true);64//! config.wasm_component_model_async(true);65//! let engine = Engine::new(&config)?;66//! let mut linker: Linker<MyState> = Linker::new(&engine);67//! wasmtime_wasi::p3::add_to_linker(&mut linker)?;68//! example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;69//!70//! // .. use `Linker` to instantiate component ...71//!72//! Ok(())73//! }74//! ```7576mod generated {77wasmtime::component::bindgen!({78path: "src/p3/wit",79world: "wasi:cli/command",80imports: {81"wasi:cli/stdin": async | store | tracing | trappable,82"wasi:cli/stdout": async | store | tracing | trappable,83"wasi:cli/stderr": async | store | tracing | trappable,84"wasi:filesystem/types/[method]descriptor.read-via-stream": async | store | tracing | trappable,85"wasi:sockets/types/[method]tcp-socket.bind": async | store | tracing | trappable,86"wasi:sockets/types/[method]tcp-socket.listen": async | store | tracing | trappable,87"wasi:sockets/types/[method]tcp-socket.receive": async | store | tracing | trappable,88"wasi:sockets/types/[method]udp-socket.bind": async | store | tracing | trappable,89"wasi:sockets/types/[method]udp-socket.connect": async | store | tracing | trappable,90default: tracing | trappable,91},92exports: { default: async | store },93with: {94"wasi:cli/terminal-input/terminal-input": crate::p3::cli::TerminalInput,95"wasi:cli/terminal-output/terminal-output": crate::p3::cli::TerminalOutput,96"wasi:filesystem/types/descriptor": crate::filesystem::Descriptor,97"wasi:sockets/types/tcp-socket": crate::sockets::TcpSocket,98"wasi:sockets/types/udp-socket": crate::sockets::UdpSocket,99},100trappable_error_type: {101"wasi:filesystem/types/error-code" => crate::p3::filesystem::FilesystemError,102"wasi:sockets/types/error-code" => crate::p3::sockets::SocketError,103},104});105}106pub use self::generated::LinkOptions;107pub use self::generated::exports;108pub use self::generated::wasi::*;109110/// Bindings to execute and run a `wasi:cli/command`.111///112/// This structure is automatically generated by `bindgen!`.113///114/// This can be used for a more "typed" view of executing a command component115/// through the [`Command::wasi_cli_run`] method plus116/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).117///118/// # Examples119///120/// ```no_run121/// use wasmtime::{Engine, Result, Store, Config};122/// use wasmtime::component::{Component, Linker, ResourceTable};123/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};124/// use wasmtime_wasi::p3::bindings::Command;125///126/// // This example is an example shim of executing a component based on the127/// // command line arguments provided to this program.128/// #[tokio::main]129/// async fn main() -> Result<()> {130/// let args = std::env::args().skip(1).collect::<Vec<_>>();131///132/// // Configure and create `Engine`133/// let mut config = Config::new();134/// config.async_support(true);135/// config.wasm_component_model_async(true);136/// let engine = Engine::new(&config)?;137///138/// // Configure a `Linker` with WASI, compile a component based on139/// // command line arguments, and then pre-instantiate it.140/// let mut linker = Linker::<MyState>::new(&engine);141/// wasmtime_wasi::p3::add_to_linker(&mut linker)?;142/// let component = Component::from_file(&engine, &args[0])?;143///144///145/// // Configure a `WasiCtx` based on this program's environment. Then146/// // build a `Store` to instantiate into.147/// let mut builder = WasiCtx::builder();148/// builder.inherit_stdio().inherit_env().args(&args);149/// let mut store = Store::new(150/// &engine,151/// MyState {152/// ctx: builder.build(),153/// table: ResourceTable::default(),154/// },155/// );156///157/// // Instantiate the component and we're off to the races.158/// let instance = linker.instantiate_async(&mut store, &component).await?;159/// let command = Command::new(&mut store, &instance)?;160/// let program_result = instance.run_concurrent(&mut store, async move |store| {161/// command.wasi_cli_run().call_run(store).await162/// }).await??;163/// match program_result {164/// Ok(()) => Ok(()),165/// Err(()) => std::process::exit(1),166/// }167/// }168///169/// struct MyState {170/// ctx: WasiCtx,171/// table: ResourceTable,172/// }173///174/// impl WasiView for MyState {175/// fn ctx(&mut self) -> WasiCtxView<'_> {176/// WasiCtxView{177/// ctx: &mut self.ctx,178/// table: &mut self.table,179/// }180/// }181/// }182/// ```183///184/// ---185pub use self::generated::Command;186187/// Pre-instantiated analog of [`Command`]188///189/// This can be used to front-load work such as export lookup before190/// instantiation.191///192/// # Examples193///194/// ```no_run195/// use wasmtime::{Engine, Result, Store, Config};196/// use wasmtime::component::{Linker, Component, ResourceTable};197/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};198/// use wasmtime_wasi::p3::bindings::CommandPre;199///200/// // This example is an example shim of executing a component based on the201/// // command line arguments provided to this program.202/// #[tokio::main]203/// async fn main() -> Result<()> {204/// let args = std::env::args().skip(1).collect::<Vec<_>>();205///206/// // Configure and create `Engine`207/// let mut config = Config::new();208/// config.async_support(true);209/// config.wasm_component_model_async(true);210/// let engine = Engine::new(&config)?;211///212/// // Configure a `Linker` with WASI, compile a component based on213/// // command line arguments, and then pre-instantiate it.214/// let mut linker = Linker::<MyState>::new(&engine);215/// wasmtime_wasi::p3::add_to_linker(&mut linker)?;216/// let component = Component::from_file(&engine, &args[0])?;217/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;218///219///220/// // Configure a `WasiCtx` based on this program's environment. Then221/// // build a `Store` to instantiate into.222/// let mut builder = WasiCtx::builder();223/// builder.inherit_stdio().inherit_env().args(&args);224/// let mut store = Store::new(225/// &engine,226/// MyState {227/// ctx: builder.build(),228/// table: ResourceTable::default(),229/// },230/// );231///232/// // Instantiate the component and we're off to the races.233/// let command = pre.instantiate_async(&mut store).await?;234/// // TODO: Construct an accessor from `store` to call `run`235/// // https://github.com/bytecodealliance/wasmtime/issues/11249236/// //let program_result = command.wasi_cli_run().call_run(&mut store).await?;237/// let program_result = todo!();238/// match program_result {239/// Ok(()) => Ok(()),240/// Err(()) => std::process::exit(1),241/// }242/// }243///244/// struct MyState {245/// ctx: WasiCtx,246/// table: ResourceTable,247/// }248///249/// impl WasiView for MyState {250/// fn ctx(&mut self) -> WasiCtxView<'_> {251/// WasiCtxView{252/// ctx: &mut self.ctx,253/// table: &mut self.table,254/// }255/// }256/// }257/// ```258///259/// ---260// TODO: Make this public, once `CommandPre` can be used for261// calling exports262// https://github.com/bytecodealliance/wasmtime/issues/11249263#[doc(hidden)]264pub use self::generated::CommandPre;265266pub use self::generated::CommandIndices;267268269