only wasi_cap_std_sync and wasi_tokio need to define WasiCtxBuilders (#2917)

* wasmtime-wasi: re-exporting this WasiCtxBuilder was shadowing the right one

wasi-common's WasiCtxBuilder is really only useful wasi_cap_std_sync and
wasi_tokio to implement their own Builder on top of.

This re-export of wasi-common's is 1. not useful and 2. shadow's the
re-export of the right one in sync::*.

* wasi-common: eliminate WasiCtxBuilder, make the builder methods on WasiCtx instead

* delete wasi-common::WasiCtxBuilder altogether

just put those methods directly on &mut WasiCtx.

As a bonus, the sync and tokio WasiCtxBuilder::build functions
are no longer fallible!

* bench fixes

* more test fixes
This commit is contained in:
Pat Hickey
2021-05-21 10:59:39 -07:00
committed by GitHub
parent 817d72a7b7
commit 0f5bdc6497
19 changed files with 114 additions and 138 deletions

View File

@@ -8,68 +8,67 @@ use std::future::Future;
use std::path::Path;
use std::rc::Rc;
pub use wasi_cap_std_sync::{clocks_ctx, random_ctx};
use wasi_common::{Error, Table, WasiCtx};
use wasi_common::{Error, Table, WasiCtx, WasiFile};
pub use dir::Dir;
pub use file::File;
use crate::sched::sched_ctx;
pub struct WasiCtxBuilder(wasi_common::WasiCtxBuilder);
pub struct WasiCtxBuilder(WasiCtx);
impl WasiCtxBuilder {
pub fn new() -> Self {
WasiCtxBuilder(WasiCtx::builder(
WasiCtxBuilder(WasiCtx::new(
random_ctx(),
clocks_ctx(),
sched_ctx(),
Rc::new(RefCell::new(Table::new())),
))
}
pub fn env(self, var: &str, value: &str) -> Result<Self, wasi_common::StringArrayError> {
let s = self.0.env(var, value)?;
Ok(WasiCtxBuilder(s))
pub fn env(mut self, var: &str, value: &str) -> Result<Self, wasi_common::StringArrayError> {
self.0.push_env(var, value)?;
Ok(self)
}
pub fn envs(self, env: &[(String, String)]) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self;
pub fn envs(mut self, env: &[(String, String)]) -> Result<Self, wasi_common::StringArrayError> {
for (k, v) in env {
s = s.env(k, v)?;
self.0.push_env(k, v)?;
}
Ok(s)
Ok(self)
}
pub fn inherit_env(self) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self.0;
pub fn inherit_env(mut self) -> Result<Self, wasi_common::StringArrayError> {
for (key, value) in std::env::vars() {
s = s.env(&key, &value)?;
self.0.push_env(&key, &value)?;
}
Ok(WasiCtxBuilder(s))
Ok(self)
}
pub fn arg(self, arg: &str) -> Result<Self, wasi_common::StringArrayError> {
let s = self.0.arg(arg)?;
Ok(WasiCtxBuilder(s))
pub fn arg(mut self, arg: &str) -> Result<Self, wasi_common::StringArrayError> {
self.0.push_arg(arg)?;
Ok(self)
}
pub fn args(self, arg: &[String]) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self;
pub fn args(mut self, arg: &[String]) -> Result<Self, wasi_common::StringArrayError> {
for a in arg {
s = s.arg(&a)?;
self.0.push_arg(&a)?;
}
Ok(s)
Ok(self)
}
pub fn inherit_args(self) -> Result<Self, wasi_common::StringArrayError> {
let mut s = self.0;
pub fn inherit_args(mut self) -> Result<Self, wasi_common::StringArrayError> {
for arg in std::env::args() {
s = s.arg(&arg)?;
self.0.push_arg(&arg)?;
}
Ok(WasiCtxBuilder(s))
Ok(self)
}
pub fn stdin(self, f: Box<dyn wasi_common::WasiFile>) -> Self {
WasiCtxBuilder(self.0.stdin(f))
pub fn stdin(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stdin(f);
self
}
pub fn stdout(self, f: Box<dyn wasi_common::WasiFile>) -> Self {
WasiCtxBuilder(self.0.stdout(f))
pub fn stdout(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stdout(f);
self
}
pub fn stderr(self, f: Box<dyn wasi_common::WasiFile>) -> Self {
WasiCtxBuilder(self.0.stderr(f))
pub fn stderr(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.set_stderr(f);
self
}
pub fn inherit_stdin(self) -> Self {
self.stdin(Box::new(crate::stdio::stdin()))
@@ -84,15 +83,16 @@ impl WasiCtxBuilder {
self.inherit_stdin().inherit_stdout().inherit_stderr()
}
pub fn preopened_dir(
self,
mut self,
dir: cap_std::fs::Dir,
guest_path: impl AsRef<Path>,
) -> Result<Self, wasi_common::Error> {
let dir = Box::new(Dir::from_cap_std(dir));
Ok(WasiCtxBuilder(self.0.preopened_dir(dir, guest_path)?))
) -> Result<Self, Error> {
let dir = Box::new(crate::dir::Dir::from_cap_std(dir));
self.0.push_preopened_dir(dir, guest_path)?;
Ok(self)
}
pub fn build(self) -> Result<WasiCtx, wasi_common::Error> {
self.0.build()
pub fn build(self) -> WasiCtx {
self.0
}
}