Path: blob/main/crates/wasi/src/p2/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/command` world. That means this module has all the generated4//! types for WASI for all of its base interfaces used by the CLI world. This5//! module itself by default contains bindings for `async`-related traits. The6//! [`sync`] module contains bindings for a non-`async` version of types.7//!8//! [`bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html9//!10//! # Examples11//!12//! If you have a WIT world which refers to WASI interfaces you probably want to13//! use this modules's bindings rather than generate fresh bindings. That can be14//! done using the `with` option to [`bindgen!`]:15//!16//! ```rust17//! use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};18//! use wasmtime::{Result, Engine, Config};19//! use wasmtime::component::{Linker, ResourceTable, HasSelf};20//!21//! wasmtime::component::bindgen!({22//! inline: "23//! package example:wasi;24//!25//! // An example of extending the `wasi:cli/command` world with a26//! // custom host interface.27//! world my-world {28//! include wasi:cli/[email protected];29//!30//! import custom-host;31//! }32//!33//! interface custom-host {34//! my-custom-function: func();35//! }36//! ",37//! path: "src/p2/wit",38//! with: {39//! "wasi": wasmtime_wasi::p2::bindings,40//! },41//! imports: { default: async },42//! });43//!44//! struct MyState {45//! table: ResourceTable,46//! ctx: WasiCtx,47//! }48//!49//! impl example::wasi::custom_host::Host for MyState {50//! async fn my_custom_function(&mut self) {51//! // ..52//! }53//! }54//!55//! impl WasiView for MyState {56//! fn ctx(&mut self) -> WasiCtxView<'_> {57//! WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }58//! }59//! }60//!61//! fn main() -> Result<()> {62//! let mut config = Config::default();63//! config.async_support(true);64//! let engine = Engine::new(&config)?;65//! let mut linker: Linker<MyState> = Linker::new(&engine);66//! wasmtime_wasi::p2::add_to_linker_async(&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//! ```7475/// Synchronous-generated bindings for WASI interfaces.76///77/// This is the same as the top-level [`bindings`](crate::p2::bindings) submodule of78/// this module except that it's for synchronous calls.79///80/// # Examples81///82/// If you have a WIT world which refers to WASI interfaces you probably want to83/// use this modules's bindings rather than generate fresh bindings. That can be84/// done using the `with` option to `bindgen!`:85///86/// ```rust87/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};88/// use wasmtime::{Result, Engine};89/// use wasmtime::component::{Linker, ResourceTable, HasSelf};90///91/// wasmtime::component::bindgen!({92/// inline: "93/// package example:wasi;94///95/// // An example of extending the `wasi:cli/command` world with a96/// // custom host interface.97/// world my-world {98/// include wasi:cli/[email protected];99///100/// import custom-host;101/// }102///103/// interface custom-host {104/// my-custom-function: func();105/// }106/// ",107/// path: "src/p2/wit",108/// with: {109/// "wasi": wasmtime_wasi::p2::bindings::sync,110/// },111/// // This is required for bindings using `wasmtime-wasi` and it otherwise112/// // isn't the default for non-async bindings.113/// require_store_data_send: true,114/// });115///116/// struct MyState {117/// table: ResourceTable,118/// ctx: WasiCtx,119/// }120///121/// impl example::wasi::custom_host::Host for MyState {122/// fn my_custom_function(&mut self) {123/// // ..124/// }125/// }126///127/// impl WasiView for MyState {128/// fn ctx(&mut self) -> WasiCtxView<'_> {129/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }130/// }131/// }132///133/// fn main() -> Result<()> {134/// let engine = Engine::default();135/// let mut linker: Linker<MyState> = Linker::new(&engine);136/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;137/// example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;138///139/// // .. use `Linker` to instantiate component ...140///141/// Ok(())142/// }143/// ```144pub mod sync {145mod generated {146use crate::p2::{FsError, SocketError};147use wasmtime_wasi_io::streams::StreamError;148149wasmtime::component::bindgen!({150path: "src/p2/wit",151world: "wasi:cli/command",152trappable_error_type: {153"wasi:io/streams/stream-error" => StreamError,154"wasi:filesystem/types/error-code" => FsError,155"wasi:sockets/network/error-code" => SocketError,156},157imports: { default: tracing | trappable },158with: {159// These interfaces contain only synchronous methods, so they160// can be aliased directly161"wasi:clocks": crate::p2::bindings::clocks,162"wasi:random": crate::p2::bindings::random,163"wasi:cli": crate::p2::bindings::cli,164"wasi:filesystem/preopens": crate::p2::bindings::filesystem::preopens,165"wasi:sockets/network": crate::p2::bindings::sockets::network,166167// Configure the resource types of the bound interfaces here168// to be the same as the async versions of the resources, that169// way everything has the same type.170"wasi:filesystem/types/descriptor": crate::filesystem::Descriptor,171"wasi:filesystem/types/directory-entry-stream": super::super::filesystem::types::DirectoryEntryStream,172"wasi:sockets/tcp/tcp-socket": super::super::sockets::tcp::TcpSocket,173"wasi:sockets/udp/incoming-datagram-stream": super::super::sockets::udp::IncomingDatagramStream,174"wasi:sockets/udp/outgoing-datagram-stream": super::super::sockets::udp::OutgoingDatagramStream,175"wasi:sockets/udp/udp-socket": crate::sockets::UdpSocket,176177// Error host trait from wasmtime-wasi-io is synchronous, so we can alias it178"wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,179// Configure the resource types from wasmtime-wasi-io, though180// this bindgen will make a new synchronous Host traits181"wasi:io/poll/pollable": wasmtime_wasi_io::poll::DynPollable,182"wasi:io/streams/input-stream": wasmtime_wasi_io::streams::DynInputStream,183"wasi:io/streams/output-stream": wasmtime_wasi_io::streams::DynOutputStream,184185},186require_store_data_send: true,187});188}189pub use self::generated::exports;190pub use self::generated::wasi::*;191192/// Synchronous bindings to execute and run a `wasi:cli/command`.193///194/// This structure is automatically generated by `bindgen!` and is intended195/// to be used with [`Config::async_support(false)`][async]. For the196/// asynchronous version see [`bindings::Command`](super::Command).197///198/// This can be used for a more "typed" view of executing a command199/// component through the [`Command::wasi_cli_run`] method plus200/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).201///202/// [async]: wasmtime::Config::async_support203/// [`wasmtime_wasi::p2::add_to_linker_sync`]: crate::p2::add_to_linker_sync204///205/// # Examples206///207/// ```no_run208/// use wasmtime::{Engine, Result, Store, Config};209/// use wasmtime::component::{ResourceTable, Linker, Component};210/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};211/// use wasmtime_wasi::p2::bindings::sync::Command;212///213/// // This example is an example shim of executing a component based on the214/// // command line arguments provided to this program.215/// fn main() -> Result<()> {216/// let args = std::env::args().skip(1).collect::<Vec<_>>();217///218/// // Configure and create `Engine`219/// let engine = Engine::default();220///221/// // Configure a `Linker` with WASI, compile a component based on222/// // command line arguments.223/// let mut linker = Linker::<MyState>::new(&engine);224/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;225/// let component = Component::from_file(&engine, &args[0])?;226///227///228/// // Configure a `WasiCtx` based on this program's environment. Then229/// // build a `Store` to instantiate into.230/// let mut builder = WasiCtx::builder();231/// builder.inherit_stdio().inherit_env().args(&args[2..]);232/// let mut store = Store::new(233/// &engine,234/// MyState {235/// ctx: builder.build(),236/// table: ResourceTable::new(),237/// },238/// );239///240/// // Instantiate the component and we're off to the races.241/// let command = Command::instantiate(&mut store, &component, &linker)?;242/// let program_result = command.wasi_cli_run().call_run(&mut store)?;243/// match program_result {244/// Ok(()) => Ok(()),245/// Err(()) => std::process::exit(1),246/// }247/// }248///249/// struct MyState {250/// ctx: WasiCtx,251/// table: ResourceTable,252/// }253///254/// impl WasiView for MyState {255/// fn ctx(&mut self) -> WasiCtxView<'_> {256/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }257/// }258/// }259/// ```260///261/// ---262pub use self::generated::Command;263264/// Pre-instantiated analogue of [`Command`].265///266/// This works the same as [`Command`] but enables front-loading work such267/// as export lookup to before instantiation.268///269/// # Examples270///271/// ```no_run272/// use wasmtime::{Engine, Result, Store, Config};273/// use wasmtime::component::{ResourceTable, Linker, Component};274/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};275/// use wasmtime_wasi::p2::bindings::sync::CommandPre;276///277/// // This example is an example shim of executing a component based on the278/// // command line arguments provided to this program.279/// fn main() -> Result<()> {280/// let args = std::env::args().skip(1).collect::<Vec<_>>();281///282/// // Configure and create `Engine`283/// let engine = Engine::default();284///285/// // Configure a `Linker` with WASI, compile a component based on286/// // command line arguments, and then pre-instantiate it.287/// let mut linker = Linker::<MyState>::new(&engine);288/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;289/// let component = Component::from_file(&engine, &args[0])?;290/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;291///292///293/// // Configure a `WasiCtx` based on this program's environment. Then294/// // build a `Store` to instantiate into.295/// let mut builder = WasiCtx::builder();296/// builder.inherit_stdio().inherit_env().args(&args);297/// let mut store = Store::new(298/// &engine,299/// MyState {300/// ctx: builder.build(),301/// table: ResourceTable::new(),302/// },303/// );304///305/// // Instantiate the component and we're off to the races.306/// let command = pre.instantiate(&mut store)?;307/// let program_result = command.wasi_cli_run().call_run(&mut store)?;308/// match program_result {309/// Ok(()) => Ok(()),310/// Err(()) => std::process::exit(1),311/// }312/// }313///314/// struct MyState {315/// ctx: WasiCtx,316/// table: ResourceTable,317/// }318///319/// impl WasiView for MyState {320/// fn ctx(&mut self) -> WasiCtxView<'_> {321/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }322/// }323/// }324/// ```325///326/// ---327pub use self::generated::CommandPre;328329pub use self::generated::CommandIndices;330331pub use self::generated::LinkOptions;332}333334mod async_io {335wasmtime::component::bindgen!({336path: "src/p2/wit",337world: "wasi:cli/command",338imports: {339// Only these functions are `async` and everything else is sync340// meaning that it basically doesn't need to block. These functions341// are the only ones that need to block.342//343// Note that at this time `only_imports` works on function names344// which in theory can be shared across interfaces, so this may345// need fancier syntax in the future.346"wasi:filesystem/types/[method]descriptor.advise": async | tracing | trappable,347"wasi:filesystem/types/[method]descriptor.create-directory-at": async | tracing | trappable,348"wasi:filesystem/types/[method]descriptor.get-flags": async | tracing | trappable,349"wasi:filesystem/types/[method]descriptor.get-type": async | tracing | trappable,350"wasi:filesystem/types/[method]descriptor.is-same-object": async | tracing | trappable,351"wasi:filesystem/types/[method]descriptor.link-at": async | tracing | trappable,352"wasi:filesystem/types/[method]descriptor.metadata-hash": async | tracing | trappable,353"wasi:filesystem/types/[method]descriptor.metadata-hash-at": async | tracing | trappable,354"wasi:filesystem/types/[method]descriptor.open-at": async | tracing | trappable,355"wasi:filesystem/types/[method]descriptor.read": async | tracing | trappable,356"wasi:filesystem/types/[method]descriptor.read-directory": async | tracing | trappable,357"wasi:filesystem/types/[method]descriptor.readlink-at": async | tracing | trappable,358"wasi:filesystem/types/[method]descriptor.remove-directory-at": async | tracing | trappable,359"wasi:filesystem/types/[method]descriptor.rename-at": async | tracing | trappable,360"wasi:filesystem/types/[method]descriptor.set-size": async | tracing | trappable,361"wasi:filesystem/types/[method]descriptor.set-times": async | tracing | trappable,362"wasi:filesystem/types/[method]descriptor.set-times-at": async | tracing | trappable,363"wasi:filesystem/types/[method]descriptor.stat": async | tracing | trappable,364"wasi:filesystem/types/[method]descriptor.stat-at": async | tracing | trappable,365"wasi:filesystem/types/[method]descriptor.symlink-at": async | tracing | trappable,366"wasi:filesystem/types/[method]descriptor.sync": async | tracing | trappable,367"wasi:filesystem/types/[method]descriptor.sync-data": async | tracing | trappable,368"wasi:filesystem/types/[method]descriptor.unlink-file-at": async | tracing | trappable,369"wasi:filesystem/types/[method]descriptor.write": async | tracing | trappable,370"wasi:filesystem/types/[method]directory-entry-stream.read-directory-entry": async | tracing | trappable,371"wasi:sockets/tcp/[method]tcp-socket.start-bind": async | tracing | trappable,372"wasi:sockets/tcp/[method]tcp-socket.start-connect": async | tracing | trappable,373"wasi:sockets/udp/[method]udp-socket.start-bind": async | tracing | trappable,374"wasi:sockets/udp/[method]udp-socket.stream": async | tracing | trappable,375"wasi:sockets/udp/[method]outgoing-datagram-stream.send": async | tracing | trappable,376default: tracing | trappable,377},378exports: { default: async },379trappable_error_type: {380"wasi:io/streams/stream-error" => wasmtime_wasi_io::streams::StreamError,381"wasi:filesystem/types/error-code" => crate::p2::FsError,382"wasi:sockets/network/error-code" => crate::p2::SocketError,383},384with: {385// All interfaces in the wasi:io package should be aliased to386// the wasmtime-wasi-io generated code. Note that this will also387// map the resource types to those defined in that crate as well.388"wasi:io/poll": wasmtime_wasi_io::bindings::wasi::io::poll,389"wasi:io/streams": wasmtime_wasi_io::bindings::wasi::io::streams,390"wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,391392// Configure all other resources to be concrete types defined in393// this crate394"wasi:sockets/network/network": crate::p2::network::Network,395"wasi:sockets/tcp/tcp-socket": crate::sockets::TcpSocket,396"wasi:sockets/udp/udp-socket": crate::sockets::UdpSocket,397"wasi:sockets/udp/incoming-datagram-stream": crate::p2::udp::IncomingDatagramStream,398"wasi:sockets/udp/outgoing-datagram-stream": crate::p2::udp::OutgoingDatagramStream,399"wasi:sockets/ip-name-lookup/resolve-address-stream": crate::p2::ip_name_lookup::ResolveAddressStream,400"wasi:filesystem/types/directory-entry-stream": crate::p2::filesystem::ReaddirIterator,401"wasi:filesystem/types/descriptor": crate::filesystem::Descriptor,402"wasi:cli/terminal-input/terminal-input": crate::p2::stdio::TerminalInput,403"wasi:cli/terminal-output/terminal-output": crate::p2::stdio::TerminalOutput,404},405});406}407408pub use self::async_io::LinkOptions;409pub use self::async_io::exports;410pub use self::async_io::wasi::*;411412/// Asynchronous bindings to execute and run a `wasi:cli/command`.413///414/// This structure is automatically generated by `bindgen!` and is intended to415/// be used with [`Config::async_support(true)`][async]. For the synchronous416/// version see [`bindings::sync::Command`](sync::Command).417///418/// This can be used for a more "typed" view of executing a command component419/// through the [`Command::wasi_cli_run`] method plus420/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).421///422/// [async]: wasmtime::Config::async_support423/// [`wasmtime_wasi::p2::add_to_linker_async`]: crate::p2::add_to_linker_async424///425/// # Examples426///427/// ```no_run428/// use wasmtime::{Engine, Result, Store, Config};429/// use wasmtime::component::{ResourceTable, Linker, Component};430/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};431/// use wasmtime_wasi::p2::bindings::Command;432///433/// // This example is an example shim of executing a component based on the434/// // command line arguments provided to this program.435/// #[tokio::main]436/// async fn main() -> Result<()> {437/// let args = std::env::args().skip(1).collect::<Vec<_>>();438///439/// // Configure and create `Engine`440/// let mut config = Config::new();441/// config.async_support(true);442/// let engine = Engine::new(&config)?;443///444/// // Configure a `Linker` with WASI, compile a component based on445/// // command line arguments, and then pre-instantiate it.446/// let mut linker = Linker::<MyState>::new(&engine);447/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;448/// let component = Component::from_file(&engine, &args[0])?;449///450///451/// // Configure a `WasiCtx` based on this program's environment. Then452/// // build a `Store` to instantiate into.453/// let mut builder = WasiCtx::builder();454/// builder.inherit_stdio().inherit_env().args(&args);455/// let mut store = Store::new(456/// &engine,457/// MyState {458/// ctx: builder.build(),459/// table: ResourceTable::new(),460/// },461/// );462///463/// // Instantiate the component and we're off to the races.464/// let command = Command::instantiate_async(&mut store, &component, &linker).await?;465/// let program_result = command.wasi_cli_run().call_run(&mut store).await?;466/// match program_result {467/// Ok(()) => Ok(()),468/// Err(()) => std::process::exit(1),469/// }470/// }471///472/// struct MyState {473/// ctx: WasiCtx,474/// table: ResourceTable,475/// }476///477/// impl WasiView for MyState {478/// fn ctx(&mut self) -> WasiCtxView<'_> {479/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }480/// }481/// }482/// ```483///484/// ---485pub use self::async_io::Command;486487/// Pre-instantiated analog of [`Command`]488///489/// This can be used to front-load work such as export lookup before490/// instantiation.491///492/// # Examples493///494/// ```no_run495/// use wasmtime::{Engine, Result, Store, Config};496/// use wasmtime::component::{ResourceTable, Linker, Component};497/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};498/// use wasmtime_wasi::p2::bindings::CommandPre;499///500/// // This example is an example shim of executing a component based on the501/// // command line arguments provided to this program.502/// #[tokio::main]503/// async fn main() -> Result<()> {504/// let args = std::env::args().skip(1).collect::<Vec<_>>();505///506/// // Configure and create `Engine`507/// let mut config = Config::new();508/// config.async_support(true);509/// let engine = Engine::new(&config)?;510///511/// // Configure a `Linker` with WASI, compile a component based on512/// // command line arguments, and then pre-instantiate it.513/// let mut linker = Linker::<MyState>::new(&engine);514/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;515/// let component = Component::from_file(&engine, &args[0])?;516/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;517///518///519/// // Configure a `WasiCtx` based on this program's environment. Then520/// // build a `Store` to instantiate into.521/// let mut builder = WasiCtx::builder();522/// builder.inherit_stdio().inherit_env().args(&args);523/// let mut store = Store::new(524/// &engine,525/// MyState {526/// ctx: builder.build(),527/// table: ResourceTable::new(),528/// },529/// );530///531/// // Instantiate the component and we're off to the races.532/// let command = pre.instantiate_async(&mut store).await?;533/// let program_result = command.wasi_cli_run().call_run(&mut store).await?;534/// match program_result {535/// Ok(()) => Ok(()),536/// Err(()) => std::process::exit(1),537/// }538/// }539///540/// struct MyState {541/// ctx: WasiCtx,542/// table: ResourceTable,543/// }544///545/// impl WasiView for MyState {546/// fn ctx(&mut self) -> WasiCtxView<'_> {547/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }548/// }549/// }550/// ```551///552/// ---553pub use self::async_io::CommandPre;554555pub use self::async_io::CommandIndices;556557558