Skip to content

Commit

Permalink
Defer isatty in the adapter til needed (bytecodealliance#7544)
Browse files Browse the repository at this point in the history
This commit slims down the default WASI interfaces needed by the adapter
by deferring the need to infer whether a stdio stream is a tty until
it's requested.
  • Loading branch information
alexcrichton authored Nov 15, 2023
1 parent 82c3e0a commit e74b5c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
42 changes: 18 additions & 24 deletions crates/wasi-preview1-component-adapter/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,29 @@ impl Streams {

pub enum StreamType {
/// Streams for implementing stdio.
Stdio(IsATTY),
Stdio(Stdio),

/// Streaming data with a file.
File(File),
}

pub enum IsATTY {
Yes,
No,
pub enum Stdio {
Stdin,
Stdout,
Stderr,
}

impl IsATTY {
impl Stdio {
pub fn filetype(&self) -> wasi::Filetype {
match self {
IsATTY::Yes => wasi::FILETYPE_CHARACTER_DEVICE,
IsATTY::No => wasi::FILETYPE_UNKNOWN,
let is_terminal = match self {
Stdio::Stdin => terminal_stdin::get_terminal_stdin().is_some(),
Stdio::Stdout => terminal_stdout::get_terminal_stdout().is_some(),
Stdio::Stderr => terminal_stderr::get_terminal_stderr().is_some(),
};
if is_terminal {
wasi::FILETYPE_CHARACTER_DEVICE
} else {
wasi::FILETYPE_UNKNOWN
}
}
}
Expand Down Expand Up @@ -138,19 +145,6 @@ impl Descriptors {
preopens: Cell::new(None),
};

let stdin_isatty = match terminal_stdin::get_terminal_stdin() {
Some(_) => IsATTY::Yes,
None => IsATTY::No,
};
let stdout_isatty = match terminal_stdout::get_terminal_stdout() {
Some(_) => IsATTY::Yes,
None => IsATTY::No,
};
let stderr_isatty = match terminal_stderr::get_terminal_stderr() {
Some(_) => IsATTY::Yes,
None => IsATTY::No,
};

fn new_once<T>(val: T) -> OnceCell<T> {
let cell = OnceCell::new();
let _ = cell.set(val);
Expand All @@ -160,19 +154,19 @@ impl Descriptors {
d.push(Descriptor::Streams(Streams {
input: new_once(stdin::get_stdin()),
output: OnceCell::new(),
type_: StreamType::Stdio(stdin_isatty),
type_: StreamType::Stdio(Stdio::Stdin),
}))
.trapping_unwrap();
d.push(Descriptor::Streams(Streams {
input: OnceCell::new(),
output: new_once(stdout::get_stdout()),
type_: StreamType::Stdio(stdout_isatty),
type_: StreamType::Stdio(Stdio::Stdout),
}))
.trapping_unwrap();
d.push(Descriptor::Streams(Streams {
input: OnceCell::new(),
output: new_once(stderr::get_stderr()),
type_: StreamType::Stdio(stderr_isatty),
type_: StreamType::Stdio(Stdio::Stderr),
}))
.trapping_unwrap();

Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-preview1-component-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ pub unsafe extern "C" fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno {
Descriptor::Streams(Streams {
input,
output,
type_: StreamType::Stdio(isatty),
type_: StreamType::Stdio(stdio),
}) => {
let fs_flags = 0;
let mut fs_rights_base = 0;
Expand All @@ -580,7 +580,7 @@ pub unsafe extern "C" fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno {
}
let fs_rights_inheriting = fs_rights_base;
stat.write(Fdstat {
fs_filetype: isatty.filetype(),
fs_filetype: stdio.filetype(),
fs_flags,
fs_rights_base,
fs_rights_inheriting,
Expand Down Expand Up @@ -663,13 +663,13 @@ pub unsafe extern "C" fn fd_filestat_get(fd: Fd, buf: *mut Filestat) -> Errno {
}
// Stdio is all zero fields, except for filetype character device
Descriptor::Streams(Streams {
type_: StreamType::Stdio(isatty),
type_: StreamType::Stdio(stdio),
..
}) => {
*buf = Filestat {
dev: 0,
ino: 0,
filetype: isatty.filetype(),
filetype: stdio.filetype(),
nlink: 0,
size: 0,
atim: 0,
Expand Down

0 comments on commit e74b5c9

Please sign in to comment.