From e74b5c9c354fef35e95a191065e24a3a043a33ab Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 15 Nov 2023 12:05:49 -0600 Subject: [PATCH] Defer isatty in the adapter til needed (#7544) 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. --- .../src/descriptors.rs | 42 ++++++++----------- .../src/lib.rs | 8 ++-- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/crates/wasi-preview1-component-adapter/src/descriptors.rs b/crates/wasi-preview1-component-adapter/src/descriptors.rs index 72d211cf26f8..287c7e518de4 100644 --- a/crates/wasi-preview1-component-adapter/src/descriptors.rs +++ b/crates/wasi-preview1-component-adapter/src/descriptors.rs @@ -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 } } } @@ -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(val: T) -> OnceCell { let cell = OnceCell::new(); let _ = cell.set(val); @@ -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(); diff --git a/crates/wasi-preview1-component-adapter/src/lib.rs b/crates/wasi-preview1-component-adapter/src/lib.rs index a701c1d71fcf..ff0c8e2d8c32 100644 --- a/crates/wasi-preview1-component-adapter/src/lib.rs +++ b/crates/wasi-preview1-component-adapter/src/lib.rs @@ -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; @@ -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, @@ -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,