wasmtime_wasi/p3/
bindings.rs

1//! Auto-generated bindings for WASI interfaces.
2//!
3//! This module contains the output of the [`bindgen!`] macro when run over
4//! the `wasi:cli/imports` world.
5//!
6//! [`bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html
7//!
8//! # Examples
9//!
10//! If you have a WIT world which refers to WASI interfaces you probably want to
11//! use this modules's bindings rather than generate fresh bindings. That can be
12//! done using the `with` option to [`bindgen!`]:
13//!
14//! ```rust
15//! use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
16//! use wasmtime::{Result, Engine, Config};
17//! use wasmtime::component::{Linker, HasSelf, ResourceTable};
18//!
19//! wasmtime::component::bindgen!({
20//!     inline: "
21//!         package example:wasi;
22//!
23//!         // An example of extending the `wasi:cli/command` world with a
24//!         // custom host interface.
25//!         world my-world {
26//!             include wasi:cli/command@0.3.0-rc-2025-09-16;
27//!
28//!             import custom-host;
29//!         }
30//!
31//!         interface custom-host {
32//!             my-custom-function: func();
33//!         }
34//!     ",
35//!     path: "src/p3/wit",
36//!     with: {
37//!         "wasi": wasmtime_wasi::p3::bindings,
38//!     },
39//!     require_store_data_send: true,
40//! });
41//!
42//! struct MyState {
43//!     ctx: WasiCtx,
44//!     table: ResourceTable,
45//! }
46//!
47//! impl example::wasi::custom_host::Host for MyState {
48//!     fn my_custom_function(&mut self) {
49//!         // ..
50//!     }
51//! }
52//!
53//! impl WasiView for MyState {
54//!     fn ctx(&mut self) -> WasiCtxView<'_> {
55//!         WasiCtxView{
56//!             ctx: &mut self.ctx,
57//!             table: &mut self.table,
58//!         }
59//!     }
60//! }
61//!
62//! fn main() -> Result<()> {
63//!     let mut config = Config::default();
64//!     config.async_support(true);
65//!     config.wasm_component_model_async(true);
66//!     let engine = Engine::new(&config)?;
67//!     let mut linker: Linker<MyState> = Linker::new(&engine);
68//!     wasmtime_wasi::p3::add_to_linker(&mut linker)?;
69//!     example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;
70//!
71//!     // .. use `Linker` to instantiate component ...
72//!
73//!     Ok(())
74//! }
75//! ```
76
77mod generated {
78    wasmtime::component::bindgen!({
79        path: "src/p3/wit",
80        world: "wasi:cli/command",
81        imports: {
82            "wasi:cli/stdin": async | store | tracing | trappable,
83            "wasi:cli/stdout": async | store | tracing | trappable,
84            "wasi:cli/stderr": async | store | tracing | trappable,
85            "wasi:filesystem/types/[method]descriptor.read-via-stream": async | store | tracing | trappable,
86            "wasi:sockets/types/[method]tcp-socket.bind": async | store | tracing | trappable,
87            "wasi:sockets/types/[method]tcp-socket.listen": async | store | tracing | trappable,
88            "wasi:sockets/types/[method]tcp-socket.receive": async | store | tracing | trappable,
89            "wasi:sockets/types/[method]udp-socket.bind": async | store | tracing | trappable,
90            "wasi:sockets/types/[method]udp-socket.connect": async | store | tracing | trappable,
91            default: tracing | trappable,
92        },
93        exports: { default: async | store },
94        with: {
95            "wasi:cli/terminal-input/terminal-input": crate::p3::cli::TerminalInput,
96            "wasi:cli/terminal-output/terminal-output": crate::p3::cli::TerminalOutput,
97            "wasi:filesystem/types/descriptor": crate::filesystem::Descriptor,
98            "wasi:sockets/types/tcp-socket": crate::sockets::TcpSocket,
99            "wasi:sockets/types/udp-socket": crate::sockets::UdpSocket,
100        },
101        trappable_error_type: {
102            "wasi:filesystem/types/error-code" => crate::p3::filesystem::FilesystemError,
103            "wasi:sockets/types/error-code" => crate::p3::sockets::SocketError,
104        },
105    });
106}
107pub use self::generated::LinkOptions;
108pub use self::generated::exports;
109pub use self::generated::wasi::*;
110
111/// Bindings to execute and run a `wasi:cli/command`.
112///
113/// This structure is automatically generated by `bindgen!`.
114///
115/// This can be used for a more "typed" view of executing a command component
116/// through the [`Command::wasi_cli_run`] method plus
117/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).
118///
119/// # Examples
120///
121/// ```no_run
122/// use wasmtime::{Engine, Result, Store, Config};
123/// use wasmtime::component::{Component, Linker, ResourceTable};
124/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
125/// use wasmtime_wasi::p3::bindings::Command;
126///
127/// // This example is an example shim of executing a component based on the
128/// // command line arguments provided to this program.
129/// #[tokio::main]
130/// async fn main() -> Result<()> {
131///     let args = std::env::args().skip(1).collect::<Vec<_>>();
132///
133///     // Configure and create `Engine`
134///     let mut config = Config::new();
135///     config.async_support(true);
136///     config.wasm_component_model_async(true);
137///     let engine = Engine::new(&config)?;
138///
139///     // Configure a `Linker` with WASI, compile a component based on
140///     // command line arguments, and then pre-instantiate it.
141///     let mut linker = Linker::<MyState>::new(&engine);
142///     wasmtime_wasi::p3::add_to_linker(&mut linker)?;
143///     let component = Component::from_file(&engine, &args[0])?;
144///
145///
146///     // Configure a `WasiCtx` based on this program's environment. Then
147///     // build a `Store` to instantiate into.
148///     let mut builder = WasiCtx::builder();
149///     builder.inherit_stdio().inherit_env().args(&args);
150///     let mut store = Store::new(
151///         &engine,
152///         MyState {
153///             ctx: builder.build(),
154///             table: ResourceTable::default(),
155///         },
156///     );
157///
158///     // Instantiate the component and we're off to the races.
159///     let command = Command::instantiate_async(&mut store, &component, &linker).await?;
160///     let program_result = store.run_concurrent(async move |store| {
161///         command.wasi_cli_run().call_run(store).await
162///     }).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;
186
187/// Pre-instantiated analog of [`Command`]
188///
189/// This can be used to front-load work such as export lookup before
190/// instantiation.
191///
192/// # Examples
193///
194/// ```no_run
195/// 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 the
201/// // 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 on
213///     // 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. Then
221///     // 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/11249
236///     //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 for
261// calling exports
262// https://github.com/bytecodealliance/wasmtime/issues/11249
263#[doc(hidden)]
264pub use self::generated::CommandPre;
265
266pub use self::generated::CommandIndices;