Skip to content

Commit

Permalink
chore: sync WasiCtxBuilder with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Aug 23, 2023
1 parent 23a4386 commit fe39245
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions crates/wasm-bridge-js/src/wasi/preview2/wasi_ctx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use rand_core::RngCore;
use super::*;
use crate::Result;

pub enum IsAtty {
Yes,
No,
}

#[derive(Default)]
pub struct WasiCtxBuilder {
stdin: Option<Box<dyn InputStream>>,
Expand All @@ -20,86 +25,94 @@ impl WasiCtxBuilder {
Default::default()
}

pub fn build(self, _table: &mut Table) -> Result<WasiCtx> {
pub fn build(&mut self, _table: &mut Table) -> Result<WasiCtx> {
let v = mem::take(self);

Ok(WasiCtx::new(
self.stdin,
self.stdout,
self.stderr,
self.random,
self.wall_clock,
self.monotonic_clock,
v.stdin,
v.stdout,
v.stderr,
v.random,
v.wall_clock,
v.monotonic_clock,
))
}

pub fn set_stdin(self, in_stream: impl InputStream + 'static) -> Self {
/// *NOTE*: is_atty is currently ignored
pub fn stdin(&mut self, in_stream: impl InputStream + 'static, _is_atty: IsATTY) -> &mut Self {
Self {
stdin: Some(Box::new(in_stream)),
..self
}
}

pub fn set_stdout(self, out: impl OutputStream + 'static) -> Self {
/// *NOTE*: is_atty is currently ignored
pub fn stdout(&mut self, out: impl OutputStream + 'static, _is_atty: IsATTY) -> &mut Self {
Self {
stdout: Some(Box::new(out)),
..self
}
}

pub fn set_stderr(self, err: impl OutputStream + 'static) -> Self {
/// *NOTE*: is_atty is currently ignored
pub fn stderr(&mut self, err: impl OutputStream + 'static, _is_atty: IsATTY) -> &mut Self {
Self {
stderr: Some(Box::new(err)),
..self
}
}

pub fn inherit_stdin(self) -> Self {
pub fn inherit_stdin(&mut self) -> &mut Self {
// TODO: could be implemented at least on node, but readline is asynchronous
self
}

pub fn inherit_stdout(self) -> Self {
pub fn inherit_stdout(&mut self) -> &mut Self {
Self {
stdout: Some(Box::new(console_log_stream())),
..self
}
}

pub fn inherit_stderr(self) -> Self {
pub fn inherit_stderr(&mut self) -> &mut Self {
Self {
stderr: Some(Box::new(console_error_stream())),
..self
}
}

pub fn inherit_stdio(self) -> Self {
pub fn inherit_stdio(&mut self) -> &mut Self {
self.inherit_stdin().inherit_stdout().inherit_stderr()
}

pub fn set_secure_random(self) -> Self {
pub fn set_secure_random(&mut self) -> &mut Self {
Self {
random: None, // Will be filled later
..self
}
}

pub fn set_secure_random_to_custom_generator(
self,
&mut self,
random: impl RngCore + Send + Sync + 'static,
) -> Self {
) -> &mut Self {
Self {
random: Some(Box::new(random)),
..self
}
}

pub fn set_wall_clock(self, wall_clock: impl HostWallClock + 'static) -> Self {
pub fn set_wall_clock(&mut self, wall_clock: impl HostWallClock + 'static) -> &mut Self {
Self {
wall_clock: Some(Box::new(wall_clock)),
..self
}
}

pub fn set_monotonic_clock(self, monotonic_clock: impl HostMonotonicClock + 'static) -> Self {
pub fn set_monotonic_clock(
&mut self,
monotonic_clock: impl HostMonotonicClock + 'static,
) -> &mut Self {
Self {
monotonic_clock: Some(Box::new(monotonic_clock)),
..self
Expand Down

0 comments on commit fe39245

Please sign in to comment.