Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p3/cli/mod.rs
1692 views
1
mod host;
2
3
use crate::cli::{WasiCli, WasiCliView};
4
use crate::p3::bindings::cli::{
5
environment, exit, stderr, stdin, stdout, terminal_input, terminal_output, terminal_stderr,
6
terminal_stdin, terminal_stdout,
7
};
8
use wasmtime::component::Linker;
9
10
/// Add all WASI interfaces from this module into the `linker` provided.
11
///
12
/// This function will add all interfaces implemented by this module to the
13
/// [`Linker`], which corresponds to the `wasi:cli/imports` world supported by
14
/// this module.
15
///
16
/// This is low-level API for advanced use cases,
17
/// [`wasmtime_wasi::p3::add_to_linker`](crate::p3::add_to_linker) can be used instead
18
/// to add *all* wasip3 interfaces (including the ones from this module) to the `linker`.
19
///
20
/// # Example
21
///
22
/// ```
23
/// use wasmtime::{Engine, Result, Store, Config};
24
/// use wasmtime::component::{Linker, ResourceTable};
25
/// use wasmtime_wasi::cli::{WasiCliCtx, WasiCliView, WasiCliCtxView};
26
///
27
/// fn main() -> Result<()> {
28
/// let mut config = Config::new();
29
/// config.async_support(true);
30
/// config.wasm_component_model_async(true);
31
/// let engine = Engine::new(&config)?;
32
///
33
/// let mut linker = Linker::<MyState>::new(&engine);
34
/// wasmtime_wasi::p3::cli::add_to_linker(&mut linker)?;
35
/// // ... add any further functionality to `linker` if desired ...
36
///
37
/// let mut store = Store::new(
38
/// &engine,
39
/// MyState::default(),
40
/// );
41
///
42
/// // ... use `linker` to instantiate within `store` ...
43
///
44
/// Ok(())
45
/// }
46
///
47
/// #[derive(Default)]
48
/// struct MyState {
49
/// cli: WasiCliCtx,
50
/// table: ResourceTable,
51
/// }
52
///
53
/// impl WasiCliView for MyState {
54
/// fn cli(&mut self) -> WasiCliCtxView<'_> {
55
/// WasiCliCtxView {
56
/// ctx: &mut self.cli,
57
/// table: &mut self.table,
58
/// }
59
/// }
60
/// }
61
/// ```
62
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
63
where
64
T: WasiCliView + 'static,
65
{
66
let exit_options = exit::LinkOptions::default();
67
add_to_linker_with_options(linker, &exit_options)
68
}
69
70
/// Similar to [`add_to_linker`], but with the ability to enable unstable features.
71
pub fn add_to_linker_with_options<T>(
72
linker: &mut Linker<T>,
73
exit_options: &exit::LinkOptions,
74
) -> anyhow::Result<()>
75
where
76
T: WasiCliView + 'static,
77
{
78
exit::add_to_linker::<_, WasiCli>(linker, exit_options, T::cli)?;
79
environment::add_to_linker::<_, WasiCli>(linker, T::cli)?;
80
stdin::add_to_linker::<_, WasiCli>(linker, T::cli)?;
81
stdout::add_to_linker::<_, WasiCli>(linker, T::cli)?;
82
stderr::add_to_linker::<_, WasiCli>(linker, T::cli)?;
83
terminal_input::add_to_linker::<_, WasiCli>(linker, T::cli)?;
84
terminal_output::add_to_linker::<_, WasiCli>(linker, T::cli)?;
85
terminal_stdin::add_to_linker::<_, WasiCli>(linker, T::cli)?;
86
terminal_stdout::add_to_linker::<_, WasiCli>(linker, T::cli)?;
87
terminal_stderr::add_to_linker::<_, WasiCli>(linker, T::cli)?;
88
Ok(())
89
}
90
91
pub struct TerminalInput;
92
pub struct TerminalOutput;
93
94