Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p2/stdio.rs
1692 views
1
use crate::cli::{IsTerminal, WasiCliCtxView};
2
use crate::p2::bindings::cli::{
3
stderr, stdin, stdout, terminal_input, terminal_output, terminal_stderr, terminal_stdin,
4
terminal_stdout,
5
};
6
use wasmtime::component::Resource;
7
use wasmtime_wasi_io::streams;
8
9
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10
pub enum IsATTY {
11
Yes,
12
No,
13
}
14
15
impl stdin::Host for WasiCliCtxView<'_> {
16
fn get_stdin(&mut self) -> Result<Resource<streams::DynInputStream>, anyhow::Error> {
17
let stream = self.ctx.stdin.p2_stream();
18
Ok(self.table.push(stream)?)
19
}
20
}
21
22
impl stdout::Host for WasiCliCtxView<'_> {
23
fn get_stdout(&mut self) -> Result<Resource<streams::DynOutputStream>, anyhow::Error> {
24
let stream = self.ctx.stdout.p2_stream();
25
Ok(self.table.push(stream)?)
26
}
27
}
28
29
impl stderr::Host for WasiCliCtxView<'_> {
30
fn get_stderr(&mut self) -> Result<Resource<streams::DynOutputStream>, anyhow::Error> {
31
let stream = self.ctx.stderr.p2_stream();
32
Ok(self.table.push(stream)?)
33
}
34
}
35
36
pub struct TerminalInput;
37
pub struct TerminalOutput;
38
39
impl terminal_input::Host for WasiCliCtxView<'_> {}
40
impl terminal_input::HostTerminalInput for WasiCliCtxView<'_> {
41
fn drop(&mut self, r: Resource<TerminalInput>) -> anyhow::Result<()> {
42
self.table.delete(r)?;
43
Ok(())
44
}
45
}
46
impl terminal_output::Host for WasiCliCtxView<'_> {}
47
impl terminal_output::HostTerminalOutput for WasiCliCtxView<'_> {
48
fn drop(&mut self, r: Resource<TerminalOutput>) -> anyhow::Result<()> {
49
self.table.delete(r)?;
50
Ok(())
51
}
52
}
53
impl terminal_stdin::Host for WasiCliCtxView<'_> {
54
fn get_terminal_stdin(&mut self) -> anyhow::Result<Option<Resource<TerminalInput>>> {
55
if self.ctx.stdin.is_terminal() {
56
let fd = self.table.push(TerminalInput)?;
57
Ok(Some(fd))
58
} else {
59
Ok(None)
60
}
61
}
62
}
63
impl terminal_stdout::Host for WasiCliCtxView<'_> {
64
fn get_terminal_stdout(&mut self) -> anyhow::Result<Option<Resource<TerminalOutput>>> {
65
if self.ctx.stdout.is_terminal() {
66
let fd = self.table.push(TerminalOutput)?;
67
Ok(Some(fd))
68
} else {
69
Ok(None)
70
}
71
}
72
}
73
impl terminal_stderr::Host for WasiCliCtxView<'_> {
74
fn get_terminal_stderr(&mut self) -> anyhow::Result<Option<Resource<TerminalOutput>>> {
75
if self.ctx.stderr.is_terminal() {
76
let fd = self.table.push(TerminalOutput)?;
77
Ok(Some(fd))
78
} else {
79
Ok(None)
80
}
81
}
82
}
83
84