Path: blob/main/crates/wasi/src/p2/bindings.rs
3090 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};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 engine = Engine::default();63//! let mut linker: Linker<MyState> = Linker::new(&engine);64//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;65//! example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;66//!67//! // .. use `Linker` to instantiate component ...68//!69//! Ok(())70//! }71//! ```7273/// Synchronous-generated bindings for WASI interfaces.74///75/// This is the same as the top-level [`bindings`](crate::p2::bindings) submodule of76/// this module except that it's for synchronous calls.77///78/// # Examples79///80/// If you have a WIT world which refers to WASI interfaces you probably want to81/// use this modules's bindings rather than generate fresh bindings. That can be82/// done using the `with` option to `bindgen!`:83///84/// ```rust85/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};86/// use wasmtime::{Result, Engine};87/// use wasmtime::component::{Linker, ResourceTable, HasSelf};88///89/// wasmtime::component::bindgen!({90/// inline: "91/// package example:wasi;92///93/// // An example of extending the `wasi:cli/command` world with a94/// // custom host interface.95/// world my-world {96/// include wasi:cli/[email protected];97///98/// import custom-host;99/// }100///101/// interface custom-host {102/// my-custom-function: func();103/// }104/// ",105/// path: "src/p2/wit",106/// with: {107/// "wasi": wasmtime_wasi::p2::bindings::sync,108/// },109/// // This is required for bindings using `wasmtime-wasi` and it otherwise110/// // isn't the default for non-async bindings.111/// require_store_data_send: true,112/// });113///114/// struct MyState {115/// table: ResourceTable,116/// ctx: WasiCtx,117/// }118///119/// impl example::wasi::custom_host::Host for MyState {120/// fn my_custom_function(&mut self) {121/// // ..122/// }123/// }124///125/// impl WasiView for MyState {126/// fn ctx(&mut self) -> WasiCtxView<'_> {127/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }128/// }129/// }130///131/// fn main() -> Result<()> {132/// let engine = Engine::default();133/// let mut linker: Linker<MyState> = Linker::new(&engine);134/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;135/// example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;136///137/// // .. use `Linker` to instantiate component ...138///139/// Ok(())140/// }141/// ```142pub mod sync {143mod generated {144use crate::p2::{FsError, SocketError};145use wasmtime_wasi_io::streams::StreamError;146147wasmtime::component::bindgen!({148path: "src/p2/wit",149world: "wasi:cli/command",150trappable_error_type: {151"wasi:io/streams.stream-error" => StreamError,152"wasi:filesystem/types.error-code" => FsError,153"wasi:sockets/network.error-code" => SocketError,154},155imports: { default: tracing | trappable },156with: {157// These interfaces contain only synchronous methods, so they158// can be aliased directly159"wasi:clocks": crate::p2::bindings::clocks,160"wasi:random": crate::p2::bindings::random,161"wasi:cli": crate::p2::bindings::cli,162"wasi:filesystem/preopens": crate::p2::bindings::filesystem::preopens,163"wasi:sockets/network": crate::p2::bindings::sockets::network,164165// Configure the resource types of the bound interfaces here166// to be the same as the async versions of the resources, that167// way everything has the same type.168"wasi:filesystem/types.descriptor": crate::filesystem::Descriptor,169"wasi:filesystem/types.directory-entry-stream": super::super::filesystem::types::DirectoryEntryStream,170"wasi:sockets/tcp.tcp-socket": super::super::sockets::tcp::TcpSocket,171"wasi:sockets/udp.incoming-datagram-stream": super::super::sockets::udp::IncomingDatagramStream,172"wasi:sockets/udp.outgoing-datagram-stream": super::super::sockets::udp::OutgoingDatagramStream,173"wasi:sockets/udp.udp-socket": crate::sockets::UdpSocket,174175// Error host trait from wasmtime-wasi-io is synchronous, so we can alias it176"wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,177// Configure the resource types from wasmtime-wasi-io, though178// this bindgen will make a new synchronous Host traits179"wasi:io/poll.pollable": wasmtime_wasi_io::poll::DynPollable,180"wasi:io/streams.input-stream": wasmtime_wasi_io::streams::DynInputStream,181"wasi:io/streams.output-stream": wasmtime_wasi_io::streams::DynOutputStream,182183},184require_store_data_send: true,185});186}187pub use self::generated::exports;188pub use self::generated::wasi::*;189190/// Synchronous bindings to execute and run a `wasi:cli/command`.191///192/// This structure is automatically generated by `bindgen!`. For the193/// asynchronous version see [`bindings::Command`](super::Command).194///195/// This can be used for a more "typed" view of executing a command196/// component through the [`Command::wasi_cli_run`] method plus197/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).198///199/// [`wasmtime_wasi::p2::add_to_linker_sync`]: crate::p2::add_to_linker_sync200///201/// # Examples202///203/// ```no_run204/// use wasmtime::{Engine, Result, Store, Config};205/// use wasmtime::component::{ResourceTable, Linker, Component};206/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};207/// use wasmtime_wasi::p2::bindings::sync::Command;208///209/// // This example is an example shim of executing a component based on the210/// // command line arguments provided to this program.211/// fn main() -> Result<()> {212/// let args = std::env::args().skip(1).collect::<Vec<_>>();213///214/// // Configure and create `Engine`215/// let engine = Engine::default();216///217/// // Configure a `Linker` with WASI, compile a component based on218/// // command line arguments.219/// let mut linker = Linker::<MyState>::new(&engine);220/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;221/// let component = Component::from_file(&engine, &args[0])?;222///223///224/// // Configure a `WasiCtx` based on this program's environment. Then225/// // build a `Store` to instantiate into.226/// let mut builder = WasiCtx::builder();227/// builder.inherit_stdio().inherit_env().args(&args[2..]);228/// let mut store = Store::new(229/// &engine,230/// MyState {231/// ctx: builder.build(),232/// table: ResourceTable::new(),233/// },234/// );235///236/// // Instantiate the component and we're off to the races.237/// let command = Command::instantiate(&mut store, &component, &linker)?;238/// let program_result = command.wasi_cli_run().call_run(&mut store)?;239/// match program_result {240/// Ok(()) => Ok(()),241/// Err(()) => std::process::exit(1),242/// }243/// }244///245/// struct MyState {246/// ctx: WasiCtx,247/// table: ResourceTable,248/// }249///250/// impl WasiView for MyState {251/// fn ctx(&mut self) -> WasiCtxView<'_> {252/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }253/// }254/// }255/// ```256///257/// ---258pub use self::generated::Command;259260/// Pre-instantiated analogue of [`Command`].261///262/// This works the same as [`Command`] but enables front-loading work such263/// as export lookup to before instantiation.264///265/// # Examples266///267/// ```no_run268/// use wasmtime::{Engine, Result, Store, Config};269/// use wasmtime::component::{ResourceTable, Linker, Component};270/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};271/// use wasmtime_wasi::p2::bindings::sync::CommandPre;272///273/// // This example is an example shim of executing a component based on the274/// // command line arguments provided to this program.275/// fn main() -> Result<()> {276/// let args = std::env::args().skip(1).collect::<Vec<_>>();277///278/// // Configure and create `Engine`279/// let engine = Engine::default();280///281/// // Configure a `Linker` with WASI, compile a component based on282/// // command line arguments, and then pre-instantiate it.283/// let mut linker = Linker::<MyState>::new(&engine);284/// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;285/// let component = Component::from_file(&engine, &args[0])?;286/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;287///288///289/// // Configure a `WasiCtx` based on this program's environment. Then290/// // build a `Store` to instantiate into.291/// let mut builder = WasiCtx::builder();292/// builder.inherit_stdio().inherit_env().args(&args);293/// let mut store = Store::new(294/// &engine,295/// MyState {296/// ctx: builder.build(),297/// table: ResourceTable::new(),298/// },299/// );300///301/// // Instantiate the component and we're off to the races.302/// let command = pre.instantiate(&mut store)?;303/// let program_result = command.wasi_cli_run().call_run(&mut store)?;304/// match program_result {305/// Ok(()) => Ok(()),306/// Err(()) => std::process::exit(1),307/// }308/// }309///310/// struct MyState {311/// ctx: WasiCtx,312/// table: ResourceTable,313/// }314///315/// impl WasiView for MyState {316/// fn ctx(&mut self) -> WasiCtxView<'_> {317/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }318/// }319/// }320/// ```321///322/// ---323pub use self::generated::CommandPre;324325pub use self::generated::CommandIndices;326327pub use self::generated::LinkOptions;328}329330mod async_io {331wasmtime::component::bindgen!({332path: "src/p2/wit",333world: "wasi:cli/command",334imports: {335// Only these functions are `async` and everything else is sync336// meaning that it basically doesn't need to block. These functions337// are the only ones that need to block.338//339// Note that at this time `only_imports` works on function names340// which in theory can be shared across interfaces, so this may341// need fancier syntax in the future.342"wasi:filesystem/types.[method]descriptor.advise": async | tracing | trappable,343"wasi:filesystem/types.[method]descriptor.create-directory-at": async | tracing | trappable,344"wasi:filesystem/types.[method]descriptor.get-flags": async | tracing | trappable,345"wasi:filesystem/types.[method]descriptor.get-type": async | tracing | trappable,346"wasi:filesystem/types.[method]descriptor.is-same-object": async | tracing | trappable,347"wasi:filesystem/types.[method]descriptor.link-at": async | tracing | trappable,348"wasi:filesystem/types.[method]descriptor.metadata-hash": async | tracing | trappable,349"wasi:filesystem/types.[method]descriptor.metadata-hash-at": async | tracing | trappable,350"wasi:filesystem/types.[method]descriptor.open-at": async | tracing | trappable,351"wasi:filesystem/types.[method]descriptor.read": async | tracing | trappable,352"wasi:filesystem/types.[method]descriptor.read-directory": async | tracing | trappable,353"wasi:filesystem/types.[method]descriptor.readlink-at": async | tracing | trappable,354"wasi:filesystem/types.[method]descriptor.remove-directory-at": async | tracing | trappable,355"wasi:filesystem/types.[method]descriptor.rename-at": async | tracing | trappable,356"wasi:filesystem/types.[method]descriptor.set-size": async | tracing | trappable,357"wasi:filesystem/types.[method]descriptor.set-times": async | tracing | trappable,358"wasi:filesystem/types.[method]descriptor.set-times-at": async | tracing | trappable,359"wasi:filesystem/types.[method]descriptor.stat": async | tracing | trappable,360"wasi:filesystem/types.[method]descriptor.stat-at": async | tracing | trappable,361"wasi:filesystem/types.[method]descriptor.symlink-at": async | tracing | trappable,362"wasi:filesystem/types.[method]descriptor.sync": async | tracing | trappable,363"wasi:filesystem/types.[method]descriptor.sync-data": async | tracing | trappable,364"wasi:filesystem/types.[method]descriptor.unlink-file-at": async | tracing | trappable,365"wasi:filesystem/types.[method]descriptor.write": async | tracing | trappable,366"wasi:filesystem/types.[method]directory-entry-stream.read-directory-entry": async | tracing | trappable,367"wasi:sockets/tcp.[method]tcp-socket.start-bind": async | tracing | trappable,368"wasi:sockets/tcp.[method]tcp-socket.start-connect": async | tracing | trappable,369"wasi:sockets/udp.[method]udp-socket.start-bind": async | tracing | trappable,370"wasi:sockets/udp.[method]udp-socket.stream": async | tracing | trappable,371"wasi:sockets/udp.[method]outgoing-datagram-stream.send": async | tracing | trappable,372default: tracing | trappable,373},374exports: { default: async },375trappable_error_type: {376"wasi:io/streams.stream-error" => wasmtime_wasi_io::streams::StreamError,377"wasi:filesystem/types.error-code" => crate::p2::FsError,378"wasi:sockets/network.error-code" => crate::p2::SocketError,379},380with: {381// All interfaces in the wasi:io package should be aliased to382// the wasmtime-wasi-io generated code. Note that this will also383// map the resource types to those defined in that crate as well.384"wasi:io/poll": wasmtime_wasi_io::bindings::wasi::io::poll,385"wasi:io/streams": wasmtime_wasi_io::bindings::wasi::io::streams,386"wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,387388// Configure all other resources to be concrete types defined in389// this crate390"wasi:sockets/network.network": crate::p2::network::Network,391"wasi:sockets/tcp.tcp-socket": crate::sockets::TcpSocket,392"wasi:sockets/udp.udp-socket": crate::sockets::UdpSocket,393"wasi:sockets/udp.incoming-datagram-stream": crate::p2::udp::IncomingDatagramStream,394"wasi:sockets/udp.outgoing-datagram-stream": crate::p2::udp::OutgoingDatagramStream,395"wasi:sockets/ip-name-lookup.resolve-address-stream": crate::p2::ip_name_lookup::ResolveAddressStream,396"wasi:filesystem/types.directory-entry-stream": crate::p2::filesystem::ReaddirIterator,397"wasi:filesystem/types.descriptor": crate::filesystem::Descriptor,398"wasi:cli/terminal-input.terminal-input": crate::p2::stdio::TerminalInput,399"wasi:cli/terminal-output.terminal-output": crate::p2::stdio::TerminalOutput,400},401});402}403404pub use self::async_io::LinkOptions;405pub use self::async_io::exports;406pub use self::async_io::wasi::*;407408/// Asynchronous bindings to execute and run a `wasi:cli/command`.409///410/// This structure is automatically generated by `bindgen!`. For the synchronous411/// version see [`bindings::sync::Command`](sync::Command).412///413/// This can be used for a more "typed" view of executing a command component414/// through the [`Command::wasi_cli_run`] method plus415/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).416///417/// [`wasmtime_wasi::p2::add_to_linker_async`]: crate::p2::add_to_linker_async418///419/// # Examples420///421/// ```no_run422/// use wasmtime::{Engine, Result, Store, Config};423/// use wasmtime::component::{ResourceTable, Linker, Component};424/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};425/// use wasmtime_wasi::p2::bindings::Command;426///427/// // This example is an example shim of executing a component based on the428/// // command line arguments provided to this program.429/// #[tokio::main]430/// async fn main() -> Result<()> {431/// let args = std::env::args().skip(1).collect::<Vec<_>>();432///433/// // Configure and create `Engine`434/// let engine = Engine::default();435///436/// // Configure a `Linker` with WASI, compile a component based on437/// // command line arguments, and then pre-instantiate it.438/// let mut linker = Linker::<MyState>::new(&engine);439/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;440/// let component = Component::from_file(&engine, &args[0])?;441///442///443/// // Configure a `WasiCtx` based on this program's environment. Then444/// // build a `Store` to instantiate into.445/// let mut builder = WasiCtx::builder();446/// builder.inherit_stdio().inherit_env().args(&args);447/// let mut store = Store::new(448/// &engine,449/// MyState {450/// ctx: builder.build(),451/// table: ResourceTable::new(),452/// },453/// );454///455/// // Instantiate the component and we're off to the races.456/// let command = Command::instantiate_async(&mut store, &component, &linker).await?;457/// let program_result = command.wasi_cli_run().call_run(&mut store).await?;458/// match program_result {459/// Ok(()) => Ok(()),460/// Err(()) => std::process::exit(1),461/// }462/// }463///464/// struct MyState {465/// ctx: WasiCtx,466/// table: ResourceTable,467/// }468///469/// impl WasiView for MyState {470/// fn ctx(&mut self) -> WasiCtxView<'_> {471/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }472/// }473/// }474/// ```475///476/// ---477pub use self::async_io::Command;478479/// Pre-instantiated analog of [`Command`]480///481/// This can be used to front-load work such as export lookup before482/// instantiation.483///484/// # Examples485///486/// ```no_run487/// use wasmtime::{Engine, Result, Store};488/// use wasmtime::component::{ResourceTable, Linker, Component};489/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};490/// use wasmtime_wasi::p2::bindings::CommandPre;491///492/// // This example is an example shim of executing a component based on the493/// // command line arguments provided to this program.494/// #[tokio::main]495/// async fn main() -> Result<()> {496/// let args = std::env::args().skip(1).collect::<Vec<_>>();497///498/// // Configure and create `Engine`499/// let engine = Engine::default();500///501/// // Configure a `Linker` with WASI, compile a component based on502/// // command line arguments, and then pre-instantiate it.503/// let mut linker = Linker::<MyState>::new(&engine);504/// wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;505/// let component = Component::from_file(&engine, &args[0])?;506/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;507///508///509/// // Configure a `WasiCtx` based on this program's environment. Then510/// // build a `Store` to instantiate into.511/// let mut builder = WasiCtx::builder();512/// builder.inherit_stdio().inherit_env().args(&args);513/// let mut store = Store::new(514/// &engine,515/// MyState {516/// ctx: builder.build(),517/// table: ResourceTable::new(),518/// },519/// );520///521/// // Instantiate the component and we're off to the races.522/// let command = pre.instantiate_async(&mut store).await?;523/// let program_result = command.wasi_cli_run().call_run(&mut store).await?;524/// match program_result {525/// Ok(()) => Ok(()),526/// Err(()) => std::process::exit(1),527/// }528/// }529///530/// struct MyState {531/// ctx: WasiCtx,532/// table: ResourceTable,533/// }534///535/// impl WasiView for MyState {536/// fn ctx(&mut self) -> WasiCtxView<'_> {537/// WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }538/// }539/// }540/// ```541///542/// ---543pub use self::async_io::CommandPre;544545pub use self::async_io::CommandIndices;546547548