Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p3/bindings.rs
1692 views
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/[email protected];
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
77
mod 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
}
107
pub use self::generated::LinkOptions;
108
pub use self::generated::exports;
109
pub 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 instance = linker.instantiate_async(&mut store, &component).await?;
160
/// let command = Command::new(&mut store, &instance)?;
161
/// let program_result = instance.run_concurrent(&mut store, async move |store| {
162
/// command.wasi_cli_run().call_run(store).await
163
/// }).await??;
164
/// match program_result {
165
/// Ok(()) => Ok(()),
166
/// Err(()) => std::process::exit(1),
167
/// }
168
/// }
169
///
170
/// struct MyState {
171
/// ctx: WasiCtx,
172
/// table: ResourceTable,
173
/// }
174
///
175
/// impl WasiView for MyState {
176
/// fn ctx(&mut self) -> WasiCtxView<'_> {
177
/// WasiCtxView{
178
/// ctx: &mut self.ctx,
179
/// table: &mut self.table,
180
/// }
181
/// }
182
/// }
183
/// ```
184
///
185
/// ---
186
pub use self::generated::Command;
187
188
/// Pre-instantiated analog of [`Command`]
189
///
190
/// This can be used to front-load work such as export lookup before
191
/// instantiation.
192
///
193
/// # Examples
194
///
195
/// ```no_run
196
/// use wasmtime::{Engine, Result, Store, Config};
197
/// use wasmtime::component::{Linker, Component, ResourceTable};
198
/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
199
/// use wasmtime_wasi::p3::bindings::CommandPre;
200
///
201
/// // This example is an example shim of executing a component based on the
202
/// // command line arguments provided to this program.
203
/// #[tokio::main]
204
/// async fn main() -> Result<()> {
205
/// let args = std::env::args().skip(1).collect::<Vec<_>>();
206
///
207
/// // Configure and create `Engine`
208
/// let mut config = Config::new();
209
/// config.async_support(true);
210
/// config.wasm_component_model_async(true);
211
/// let engine = Engine::new(&config)?;
212
///
213
/// // Configure a `Linker` with WASI, compile a component based on
214
/// // command line arguments, and then pre-instantiate it.
215
/// let mut linker = Linker::<MyState>::new(&engine);
216
/// wasmtime_wasi::p3::add_to_linker(&mut linker)?;
217
/// let component = Component::from_file(&engine, &args[0])?;
218
/// let pre = CommandPre::new(linker.instantiate_pre(&component)?)?;
219
///
220
///
221
/// // Configure a `WasiCtx` based on this program's environment. Then
222
/// // build a `Store` to instantiate into.
223
/// let mut builder = WasiCtx::builder();
224
/// builder.inherit_stdio().inherit_env().args(&args);
225
/// let mut store = Store::new(
226
/// &engine,
227
/// MyState {
228
/// ctx: builder.build(),
229
/// table: ResourceTable::default(),
230
/// },
231
/// );
232
///
233
/// // Instantiate the component and we're off to the races.
234
/// let command = pre.instantiate_async(&mut store).await?;
235
/// // TODO: Construct an accessor from `store` to call `run`
236
/// // https://github.com/bytecodealliance/wasmtime/issues/11249
237
/// //let program_result = command.wasi_cli_run().call_run(&mut store).await?;
238
/// let program_result = todo!();
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{
253
/// ctx: &mut self.ctx,
254
/// table: &mut self.table,
255
/// }
256
/// }
257
/// }
258
/// ```
259
///
260
/// ---
261
// TODO: Make this public, once `CommandPre` can be used for
262
// calling exports
263
// https://github.com/bytecodealliance/wasmtime/issues/11249
264
#[doc(hidden)]
265
pub use self::generated::CommandPre;
266
267
pub use self::generated::CommandIndices;
268
269