xref: /wasmtime-44.0.1/crates/wasi/src/p3/cli/mod.rs (revision cc8d04f4)
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.wasm_component_model_async(true);
30 ///     let engine = Engine::new(&config)?;
31 ///
32 ///     let mut linker = Linker::<MyState>::new(&engine);
33 ///     wasmtime_wasi::p3::cli::add_to_linker(&mut linker)?;
34 ///     // ... add any further functionality to `linker` if desired ...
35 ///
36 ///     let mut store = Store::new(
37 ///         &engine,
38 ///         MyState::default(),
39 ///     );
40 ///
41 ///     // ... use `linker` to instantiate within `store` ...
42 ///
43 ///     Ok(())
44 /// }
45 ///
46 /// #[derive(Default)]
47 /// struct MyState {
48 ///     cli: WasiCliCtx,
49 ///     table: ResourceTable,
50 /// }
51 ///
52 /// impl WasiCliView for MyState {
53 ///     fn cli(&mut self) -> WasiCliCtxView<'_> {
54 ///         WasiCliCtxView {
55 ///             ctx: &mut self.cli,
56 ///             table: &mut self.table,
57 ///         }
58 ///     }
59 /// }
60 /// ```
add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()> where T: WasiCliView + 'static,61 pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
62 where
63     T: WasiCliView + 'static,
64 {
65     let exit_options = exit::LinkOptions::default();
66     add_to_linker_with_options(linker, &exit_options)
67 }
68 
69 /// Similar to [`add_to_linker`], but with the ability to enable unstable features.
add_to_linker_with_options<T>( linker: &mut Linker<T>, exit_options: &exit::LinkOptions, ) -> wasmtime::Result<()> where T: WasiCliView + 'static,70 pub fn add_to_linker_with_options<T>(
71     linker: &mut Linker<T>,
72     exit_options: &exit::LinkOptions,
73 ) -> wasmtime::Result<()>
74 where
75     T: WasiCliView + 'static,
76 {
77     exit::add_to_linker::<_, WasiCli>(linker, exit_options, T::cli)?;
78     environment::add_to_linker::<_, WasiCli>(linker, T::cli)?;
79     stdin::add_to_linker::<_, WasiCli>(linker, T::cli)?;
80     stdout::add_to_linker::<_, WasiCli>(linker, T::cli)?;
81     stderr::add_to_linker::<_, WasiCli>(linker, T::cli)?;
82     terminal_input::add_to_linker::<_, WasiCli>(linker, T::cli)?;
83     terminal_output::add_to_linker::<_, WasiCli>(linker, T::cli)?;
84     terminal_stdin::add_to_linker::<_, WasiCli>(linker, T::cli)?;
85     terminal_stdout::add_to_linker::<_, WasiCli>(linker, T::cli)?;
86     terminal_stderr::add_to_linker::<_, WasiCli>(linker, T::cli)?;
87     Ok(())
88 }
89 
90 pub struct TerminalInput;
91 pub struct TerminalOutput;
92