Skip to main content

wasmtime_wasi/p2/
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/command` world. That means this module has all the generated
5//! types for WASI for all of its base interfaces used by the CLI world. This
6//! module itself by default contains bindings for `async`-related traits. The
7//! [`sync`] module contains bindings for a non-`async` version of types.
8//!
9//! [`bindgen!`]: https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html
10//!
11//! # Examples
12//!
13//! If you have a WIT world which refers to WASI interfaces you probably want to
14//! use this modules's bindings rather than generate fresh bindings. That can be
15//! done using the `with` option to [`bindgen!`]:
16//!
17//! ```rust
18//! use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
19//! use wasmtime::{Result, Engine};
20//! use wasmtime::component::{Linker, ResourceTable, HasSelf};
21//!
22//! wasmtime::component::bindgen!({
23//!     inline: "
24//!         package example:wasi;
25//!
26//!         // An example of extending the `wasi:cli/command` world with a
27//!         // custom host interface.
28//!         world my-world {
29//!             include wasi:cli/command@0.2.6;
30//!
31//!             import custom-host;
32//!         }
33//!
34//!         interface custom-host {
35//!             my-custom-function: func();
36//!         }
37//!     ",
38//!     path: "src/p2/wit",
39//!     with: {
40//!         "wasi": wasmtime_wasi::p2::bindings,
41//!     },
42//!     imports: { default: async },
43//! });
44//!
45//! struct MyState {
46//!     table: ResourceTable,
47//!     ctx: WasiCtx,
48//! }
49//!
50//! impl example::wasi::custom_host::Host for MyState {
51//!     async fn my_custom_function(&mut self) {
52//!         // ..
53//!     }
54//! }
55//!
56//! impl WasiView for MyState {
57//!     fn ctx(&mut self) -> WasiCtxView<'_> {
58//!         WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
59//!     }
60//! }
61//!
62//! fn main() -> Result<()> {
63//!     let engine = Engine::default();
64//!     let mut linker: Linker<MyState> = Linker::new(&engine);
65//!     wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
66//!     example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;
67//!
68//!     // .. use `Linker` to instantiate component ...
69//!
70//!     Ok(())
71//! }
72//! ```
73
74/// Synchronous-generated bindings for WASI interfaces.
75///
76/// This is the same as the top-level [`bindings`](crate::p2::bindings) submodule of
77/// this module except that it's for synchronous calls.
78///
79/// # Examples
80///
81/// If you have a WIT world which refers to WASI interfaces you probably want to
82/// use this modules's bindings rather than generate fresh bindings. That can be
83/// done using the `with` option to `bindgen!`:
84///
85/// ```rust
86/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
87/// use wasmtime::{Result, Engine};
88/// use wasmtime::component::{Linker, ResourceTable, HasSelf};
89///
90/// wasmtime::component::bindgen!({
91///     inline: "
92///         package example:wasi;
93///
94///         // An example of extending the `wasi:cli/command` world with a
95///         // custom host interface.
96///         world my-world {
97///             include wasi:cli/command@0.2.6;
98///
99///             import custom-host;
100///         }
101///
102///         interface custom-host {
103///             my-custom-function: func();
104///         }
105///     ",
106///     path: "src/p2/wit",
107///     with: {
108///         "wasi": wasmtime_wasi::p2::bindings::sync,
109///     },
110///     // This is required for bindings using `wasmtime-wasi` and it otherwise
111///     // isn't the default for non-async bindings.
112///     require_store_data_send: true,
113/// });
114///
115/// struct MyState {
116///     table: ResourceTable,
117///     ctx: WasiCtx,
118/// }
119///
120/// impl example::wasi::custom_host::Host for MyState {
121///     fn my_custom_function(&mut self) {
122///         // ..
123///     }
124/// }
125///
126/// impl WasiView for MyState {
127///     fn ctx(&mut self) -> WasiCtxView<'_> {
128///         WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
129///     }
130/// }
131///
132/// fn main() -> Result<()> {
133///     let engine = Engine::default();
134///     let mut linker: Linker<MyState> = Linker::new(&engine);
135///     wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;
136///     example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;
137///
138///     // .. use `Linker` to instantiate component ...
139///
140///     Ok(())
141/// }
142/// ```
143pub mod sync {
144    mod generated {
145        use crate::p2::{FsError, SocketError};
146        use wasmtime_wasi_io::streams::StreamError;
147
148        wasmtime::component::bindgen!({
149            path: "src/p2/wit",
150            world: "wasi:cli/command",
151            trappable_error_type: {
152                "wasi:io/streams.stream-error" => StreamError,
153                "wasi:filesystem/types.error-code" => FsError,
154                "wasi:sockets/network.error-code" => SocketError,
155            },
156            imports: { default: tracing | trappable },
157            with: {
158                // These interfaces contain only synchronous methods, so they
159                // can be aliased directly
160                "wasi:clocks": crate::p2::bindings::clocks,
161                "wasi:random": crate::p2::bindings::random,
162                "wasi:cli": crate::p2::bindings::cli,
163                "wasi:filesystem/preopens": crate::p2::bindings::filesystem::preopens,
164                "wasi:sockets/network": crate::p2::bindings::sockets::network,
165
166                // Configure the resource types of the bound interfaces here
167                // to be the same as the async versions of the resources, that
168                // way everything has the same type.
169                "wasi:filesystem/types.descriptor": crate::filesystem::Descriptor,
170                "wasi:filesystem/types.directory-entry-stream": super::super::filesystem::types::DirectoryEntryStream,
171                "wasi:sockets/tcp.tcp-socket": super::super::sockets::tcp::TcpSocket,
172                "wasi:sockets/udp.incoming-datagram-stream": super::super::sockets::udp::IncomingDatagramStream,
173                "wasi:sockets/udp.outgoing-datagram-stream": super::super::sockets::udp::OutgoingDatagramStream,
174                "wasi:sockets/udp.udp-socket": crate::sockets::UdpSocket,
175
176                // Error host trait from wasmtime-wasi-io is synchronous, so we can alias it
177                "wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,
178                // Configure the resource types from wasmtime-wasi-io, though
179                // this bindgen will make a new synchronous Host traits
180                "wasi:io/poll.pollable": wasmtime_wasi_io::poll::DynPollable,
181                "wasi:io/streams.input-stream": wasmtime_wasi_io::streams::DynInputStream,
182                "wasi:io/streams.output-stream": wasmtime_wasi_io::streams::DynOutputStream,
183
184            },
185            require_store_data_send: true,
186        });
187    }
188    pub use self::generated::exports;
189    pub use self::generated::wasi::*;
190
191    /// Synchronous bindings to execute and run a `wasi:cli/command`.
192    ///
193    /// This structure is automatically generated by `bindgen!`. For the
194    /// asynchronous version see [`bindings::Command`](super::Command).
195    ///
196    /// This can be used for a more "typed" view of executing a command
197    /// component through the [`Command::wasi_cli_run`] method plus
198    /// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).
199    ///
200    /// [`wasmtime_wasi::p2::add_to_linker_sync`]: crate::p2::add_to_linker_sync
201    ///
202    /// # Examples
203    ///
204    /// ```no_run
205    /// use wasmtime::{Engine, Result, Store, Config};
206    /// use wasmtime::component::{ResourceTable, Linker, Component};
207    /// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
208    /// use wasmtime_wasi::p2::bindings::sync::Command;
209    ///
210    /// // This example is an example shim of executing a component based on the
211    /// // command line arguments provided to this program.
212    /// fn main() -> Result<()> {
213    ///     let args = std::env::args().skip(1).collect::<Vec<_>>();
214    ///
215    ///     // Configure and create `Engine`
216    ///     let engine = Engine::default();
217    ///
218    ///     // Configure a `Linker` with WASI, compile a component based on
219    ///     // command line arguments.
220    ///     let mut linker = Linker::<MyState>::new(&engine);
221    ///     wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;
222    ///     let component = Component::from_file(&engine, &args[0])?;
223    ///
224    ///
225    ///     // Configure a `WasiCtx` based on this program's environment. Then
226    ///     // build a `Store` to instantiate into.
227    ///     let mut builder = WasiCtx::builder();
228    ///     builder.inherit_stdio().inherit_env().args(&args[2..]);
229    ///     let mut store = Store::new(
230    ///         &engine,
231    ///         MyState {
232    ///             ctx: builder.build(),
233    ///             table: ResourceTable::new(),
234    ///         },
235    ///     );
236    ///
237    ///     // Instantiate the component and we're off to the races.
238    ///     let command = Command::instantiate(&mut store, &component, &linker)?;
239    ///     let program_result = command.wasi_cli_run().call_run(&mut store)?;
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 { ctx: &mut self.ctx, table: &mut self.table }
254    ///     }
255    /// }
256    /// ```
257    ///
258    /// ---
259    pub use self::generated::Command;
260
261    /// Pre-instantiated analogue of [`Command`].
262    ///
263    /// This works the same as [`Command`] but enables front-loading work such
264    /// as export lookup to before instantiation.
265    ///
266    /// # Examples
267    ///
268    /// ```no_run
269    /// use wasmtime::{Engine, Result, Store, Config};
270    /// use wasmtime::component::{ResourceTable, Linker, Component};
271    /// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
272    /// use wasmtime_wasi::p2::bindings::sync::CommandPre;
273    ///
274    /// // This example is an example shim of executing a component based on the
275    /// // command line arguments provided to this program.
276    /// fn main() -> Result<()> {
277    ///     let args = std::env::args().skip(1).collect::<Vec<_>>();
278    ///
279    ///     // Configure and create `Engine`
280    ///     let engine = Engine::default();
281    ///
282    ///     // Configure a `Linker` with WASI, compile a component based on
283    ///     // command line arguments, and then pre-instantiate it.
284    ///     let mut linker = Linker::<MyState>::new(&engine);
285    ///     wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;
286    ///     let component = Component::from_file(&engine, &args[0])?;
287    ///     let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;
288    ///
289    ///
290    ///     // Configure a `WasiCtx` based on this program's environment. Then
291    ///     // build a `Store` to instantiate into.
292    ///     let mut builder = WasiCtx::builder();
293    ///     builder.inherit_stdio().inherit_env().args(&args);
294    ///     let mut store = Store::new(
295    ///         &engine,
296    ///         MyState {
297    ///             ctx: builder.build(),
298    ///             table: ResourceTable::new(),
299    ///         },
300    ///     );
301    ///
302    ///     // Instantiate the component and we're off to the races.
303    ///     let command = pre.instantiate(&mut store)?;
304    ///     let program_result = command.wasi_cli_run().call_run(&mut store)?;
305    ///     match program_result {
306    ///         Ok(()) => Ok(()),
307    ///         Err(()) => std::process::exit(1),
308    ///     }
309    /// }
310    ///
311    /// struct MyState {
312    ///     ctx: WasiCtx,
313    ///     table: ResourceTable,
314    /// }
315    ///
316    /// impl WasiView for MyState {
317    ///     fn ctx(&mut self) -> WasiCtxView<'_> {
318    ///         WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
319    ///     }
320    /// }
321    /// ```
322    ///
323    /// ---
324    pub use self::generated::CommandPre;
325
326    pub use self::generated::CommandIndices;
327
328    pub use self::generated::LinkOptions;
329}
330
331mod async_io {
332    wasmtime::component::bindgen!({
333        path: "src/p2/wit",
334        world: "wasi:cli/command",
335        imports: {
336            // Only these functions are `async` and everything else is sync
337            // meaning that it basically doesn't need to block. These functions
338            // are the only ones that need to block.
339            //
340            // Note that at this time `only_imports` works on function names
341            // which in theory can be shared across interfaces, so this may
342            // need fancier syntax in the future.
343            "wasi:filesystem/types.[method]descriptor.advise": async | tracing | trappable,
344            "wasi:filesystem/types.[method]descriptor.create-directory-at": async | tracing | trappable,
345            "wasi:filesystem/types.[method]descriptor.get-flags": async | tracing | trappable,
346            "wasi:filesystem/types.[method]descriptor.get-type": async | tracing | trappable,
347            "wasi:filesystem/types.[method]descriptor.is-same-object": async | tracing | trappable,
348            "wasi:filesystem/types.[method]descriptor.link-at": async | tracing | trappable,
349            "wasi:filesystem/types.[method]descriptor.metadata-hash": async | tracing | trappable,
350            "wasi:filesystem/types.[method]descriptor.metadata-hash-at": async | tracing | trappable,
351            "wasi:filesystem/types.[method]descriptor.open-at": async | tracing | trappable,
352            "wasi:filesystem/types.[method]descriptor.read": async | tracing | trappable,
353            "wasi:filesystem/types.[method]descriptor.read-directory": async | tracing | trappable,
354            "wasi:filesystem/types.[method]descriptor.readlink-at": async | tracing | trappable,
355            "wasi:filesystem/types.[method]descriptor.remove-directory-at": async | tracing | trappable,
356            "wasi:filesystem/types.[method]descriptor.rename-at": async | tracing | trappable,
357            "wasi:filesystem/types.[method]descriptor.set-size": async | tracing | trappable,
358            "wasi:filesystem/types.[method]descriptor.set-times": async | tracing | trappable,
359            "wasi:filesystem/types.[method]descriptor.set-times-at": async | tracing | trappable,
360            "wasi:filesystem/types.[method]descriptor.stat": async | tracing | trappable,
361            "wasi:filesystem/types.[method]descriptor.stat-at": async | tracing | trappable,
362            "wasi:filesystem/types.[method]descriptor.symlink-at": async | tracing | trappable,
363            "wasi:filesystem/types.[method]descriptor.sync": async | tracing | trappable,
364            "wasi:filesystem/types.[method]descriptor.sync-data": async | tracing | trappable,
365            "wasi:filesystem/types.[method]descriptor.unlink-file-at": async | tracing | trappable,
366            "wasi:filesystem/types.[method]descriptor.write": async | tracing | trappable,
367            "wasi:filesystem/types.[method]directory-entry-stream.read-directory-entry": async | tracing | trappable,
368            "wasi:sockets/tcp.[method]tcp-socket.start-bind": async | tracing | trappable,
369            "wasi:sockets/tcp.[method]tcp-socket.start-connect": async | tracing | trappable,
370            "wasi:sockets/udp.[method]udp-socket.start-bind": async | tracing | trappable,
371            "wasi:sockets/udp.[method]udp-socket.stream": async | tracing | trappable,
372            "wasi:sockets/udp.[method]outgoing-datagram-stream.send": async | tracing | trappable,
373            default: tracing | trappable,
374        },
375        exports: { default: async },
376        trappable_error_type: {
377            "wasi:io/streams.stream-error" => wasmtime_wasi_io::streams::StreamError,
378            "wasi:filesystem/types.error-code" => crate::p2::FsError,
379            "wasi:sockets/network.error-code" => crate::p2::SocketError,
380        },
381        with: {
382            // All interfaces in the wasi:io package should be aliased to
383            // the wasmtime-wasi-io generated code. Note that this will also
384            // map the resource types to those defined in that crate as well.
385            "wasi:io/poll": wasmtime_wasi_io::bindings::wasi::io::poll,
386            "wasi:io/streams": wasmtime_wasi_io::bindings::wasi::io::streams,
387            "wasi:io/error": wasmtime_wasi_io::bindings::wasi::io::error,
388
389            // Configure all other resources to be concrete types defined in
390            // this crate
391            "wasi:sockets/network.network": crate::p2::network::Network,
392            "wasi:sockets/tcp.tcp-socket": crate::sockets::TcpSocket,
393            "wasi:sockets/udp.udp-socket": crate::sockets::UdpSocket,
394            "wasi:sockets/udp.incoming-datagram-stream": crate::p2::udp::IncomingDatagramStream,
395            "wasi:sockets/udp.outgoing-datagram-stream": crate::p2::udp::OutgoingDatagramStream,
396            "wasi:sockets/ip-name-lookup.resolve-address-stream": crate::p2::ip_name_lookup::ResolveAddressStream,
397            "wasi:filesystem/types.directory-entry-stream": crate::p2::filesystem::ReaddirIterator,
398            "wasi:filesystem/types.descriptor": crate::filesystem::Descriptor,
399            "wasi:cli/terminal-input.terminal-input": crate::p2::stdio::TerminalInput,
400            "wasi:cli/terminal-output.terminal-output": crate::p2::stdio::TerminalOutput,
401        },
402    });
403}
404
405pub use self::async_io::LinkOptions;
406pub use self::async_io::exports;
407pub use self::async_io::wasi::*;
408
409/// Asynchronous bindings to execute and run a `wasi:cli/command`.
410///
411/// This structure is automatically generated by `bindgen!`. For the synchronous
412/// version see [`bindings::sync::Command`](sync::Command).
413///
414/// This can be used for a more "typed" view of executing a command component
415/// through the [`Command::wasi_cli_run`] method plus
416/// [`Guest::call_run`](exports::wasi::cli::run::Guest::call_run).
417///
418/// [`wasmtime_wasi::p2::add_to_linker_async`]: crate::p2::add_to_linker_async
419///
420/// # Examples
421///
422/// ```no_run
423/// use wasmtime::{Engine, Result, Store, Config};
424/// use wasmtime::component::{ResourceTable, Linker, Component};
425/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
426/// use wasmtime_wasi::p2::bindings::Command;
427///
428/// // This example is an example shim of executing a component based on the
429/// // command line arguments provided to this program.
430/// #[tokio::main]
431/// async fn main() -> Result<()> {
432///     let args = std::env::args().skip(1).collect::<Vec<_>>();
433///
434///     // Configure and create `Engine`
435///     let engine = Engine::default();
436///
437///     // Configure a `Linker` with WASI, compile a component based on
438///     // command line arguments, and then pre-instantiate it.
439///     let mut linker = Linker::<MyState>::new(&engine);
440///     wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
441///     let component = Component::from_file(&engine, &args[0])?;
442///
443///
444///     // Configure a `WasiCtx` based on this program's environment. Then
445///     // build a `Store` to instantiate into.
446///     let mut builder = WasiCtx::builder();
447///     builder.inherit_stdio().inherit_env().args(&args);
448///     let mut store = Store::new(
449///         &engine,
450///         MyState {
451///             ctx: builder.build(),
452///             table: ResourceTable::new(),
453///         },
454///     );
455///
456///     // Instantiate the component and we're off to the races.
457///     let command = Command::instantiate_async(&mut store, &component, &linker).await?;
458///     let program_result = command.wasi_cli_run().call_run(&mut store).await?;
459///     match program_result {
460///         Ok(()) => Ok(()),
461///         Err(()) => std::process::exit(1),
462///     }
463/// }
464///
465/// struct MyState {
466///     ctx: WasiCtx,
467///     table: ResourceTable,
468/// }
469///
470/// impl WasiView for MyState {
471///     fn ctx(&mut self) -> WasiCtxView<'_> {
472///         WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
473///     }
474/// }
475/// ```
476///
477/// ---
478pub use self::async_io::Command;
479
480/// Pre-instantiated analog of [`Command`]
481///
482/// This can be used to front-load work such as export lookup before
483/// instantiation.
484///
485/// # Examples
486///
487/// ```no_run
488/// use wasmtime::{Engine, Result, Store};
489/// use wasmtime::component::{ResourceTable, Linker, Component};
490/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
491/// use wasmtime_wasi::p2::bindings::CommandPre;
492///
493/// // This example is an example shim of executing a component based on the
494/// // command line arguments provided to this program.
495/// #[tokio::main]
496/// async fn main() -> Result<()> {
497///     let args = std::env::args().skip(1).collect::<Vec<_>>();
498///
499///     // Configure and create `Engine`
500///     let engine = Engine::default();
501///
502///     // Configure a `Linker` with WASI, compile a component based on
503///     // command line arguments, and then pre-instantiate it.
504///     let mut linker = Linker::<MyState>::new(&engine);
505///     wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
506///     let component = Component::from_file(&engine, &args[0])?;
507///     let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;
508///
509///
510///     // Configure a `WasiCtx` based on this program's environment. Then
511///     // build a `Store` to instantiate into.
512///     let mut builder = WasiCtx::builder();
513///     builder.inherit_stdio().inherit_env().args(&args);
514///     let mut store = Store::new(
515///         &engine,
516///         MyState {
517///             ctx: builder.build(),
518///             table: ResourceTable::new(),
519///         },
520///     );
521///
522///     // Instantiate the component and we're off to the races.
523///     let command = pre.instantiate_async(&mut store).await?;
524///     let program_result = command.wasi_cli_run().call_run(&mut store).await?;
525///     match program_result {
526///         Ok(()) => Ok(()),
527///         Err(()) => std::process::exit(1),
528///     }
529/// }
530///
531/// struct MyState {
532///     ctx: WasiCtx,
533///     table: ResourceTable,
534/// }
535///
536/// impl WasiView for MyState {
537///     fn ctx(&mut self) -> WasiCtxView<'_> {
538///         WasiCtxView { ctx: &mut self.ctx, table: &mut self.table }
539///     }
540/// }
541/// ```
542///
543/// ---
544pub use self::async_io::CommandPre;
545
546pub use self::async_io::CommandIndices;