Skip to main content

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