Skip to content

Commit

Permalink
feat: Add the ability to set WasiRunner's stdin/stdout/stderr streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Aug 28, 2023
1 parent 9ff5d49 commit 95cedc5
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::Arc;

use anyhow::{Context, Error};
use virtual_fs::TmpFileSystem;
use virtual_fs::{TmpFileSystem, VirtualFile};
use webc::metadata::{annotations::Wasi, Command};

use crate::{
Expand All @@ -16,6 +16,9 @@ use crate::{
#[derive(Debug, Default, Clone)]
pub struct WasiRunner {
wasi: CommonWasiOptions,
stdin: Option<Arc<dyn VirtualFile + Send + Sync>>,
stdout: Option<Arc<dyn VirtualFile + Send + Sync>>,
stderr: Option<Arc<dyn VirtualFile + Send + Sync>>,
}

impl WasiRunner {
Expand Down Expand Up @@ -143,6 +146,36 @@ impl WasiRunner {
self.wasi.capabilities = capabilities;
}

pub fn with_stdin(mut self, stdin: Arc<dyn VirtualFile + Send + Sync>) -> Self {
self.set_stdin(stdin);
self
}

pub fn set_stdin(&mut self, stdin: Arc<dyn VirtualFile + Send + Sync>) -> &mut Self {
self.stdin = Some(stdin);
self
}

pub fn with_stdout(mut self, stdout: Arc<dyn VirtualFile + Send + Sync>) -> Self {
self.set_stdout(stdout);
self
}

pub fn set_stdout(&mut self, stdout: Arc<dyn VirtualFile + Send + Sync>) -> &mut Self {
self.stdout = Some(stdout);
self
}

pub fn with_stderr(mut self, stderr: Arc<dyn VirtualFile + Send + Sync>) -> Self {
self.set_stderr(stderr);
self
}

pub fn set_stderr(&mut self, stderr: Arc<dyn VirtualFile + Send + Sync>) -> &mut Self {
self.stderr = Some(stderr);
self
}

pub(crate) fn prepare_webc_env(
&self,
program_name: &str,
Expand All @@ -156,6 +189,16 @@ impl WasiRunner {
self.wasi
.prepare_webc_env(&mut builder, container_fs, wasi, root_fs)?;

if let Some(stdin) = &self.stdin {
builder.set_stdin(Box::new(Arc::clone(stdin)) as _);
}
if let Some(stdout) = &self.stdout {
builder.set_stdout(Box::new(Arc::clone(stdout)) as _);
}
if let Some(stderr) = &self.stderr {
builder.set_stderr(Box::new(Arc::clone(stderr)) as _);
}

builder.add_webc(pkg.clone());
builder.set_runtime(runtime);

Expand Down

0 comments on commit 95cedc5

Please sign in to comment.