Skip to content

Commit

Permalink
Merge pull request #4186 from wasmerio/wasi-runner-fixes
Browse files Browse the repository at this point in the history
Add stdin/stdout/stderr streams to `WasiRunner` and only use async threading when requested
  • Loading branch information
Michael Bryan authored Aug 28, 2023
2 parents 417e5bc + 2b88305 commit a4d23a3
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions 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::{ArcBoxFile, 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<ArcBoxFile>,
stdout: Option<ArcBoxFile>,
stderr: Option<ArcBoxFile>,
}

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

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

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

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

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

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

pub fn set_stderr(&mut self, stderr: Box<dyn VirtualFile + Send + Sync>) -> &mut Self {
self.stderr = Some(ArcBoxFile::new(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(stdin.clone()));
}
if let Some(stdout) = &self.stdout {
builder.set_stdout(Box::new(stdout.clone()));
}
if let Some(stderr) = &self.stderr {
builder.set_stderr(Box::new(stderr.clone()));
}

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

Expand Down Expand Up @@ -186,11 +229,22 @@ impl crate::runners::Runner for WasiRunner {
.unwrap_or_else(|| Wasi::new(command_name));

let module = runtime.load_module_sync(cmd.atom())?;
let store = runtime.new_store();

self.prepare_webc_env(command_name, &wasi, pkg, runtime, None)
.context("Unable to prepare the WASI environment")?
.run_with_store_async(module, store)?;
let mut store = runtime.new_store();

let env = self
.prepare_webc_env(command_name, &wasi, pkg, runtime, None)
.context("Unable to prepare the WASI environment")?;

if self
.wasi
.capabilities
.threading
.enable_asynchronous_threading
{
env.run_with_store_async(module, store)?;
} else {
env.run_with_store(module, &mut store)?;
}

Ok(())
}
Expand Down

0 comments on commit a4d23a3

Please sign in to comment.