Skip to content

Commit

Permalink
Redesign wasi ctx a bit (bytecodealliance#156)
Browse files Browse the repository at this point in the history
* redesign wasi ctx and builder

* in ctx, resources are only ever owned by the table
* preopen dirs and stdio are just indexes into the table that we hand to the caller
* builder requires less boxing, in general, takes more impl traits

* tests: rewrite the way we generate tests to not construct store

* uncomment WasiCtx's pool methods
  • Loading branch information
pchickey authored Apr 27, 2023
1 parent c8fa55e commit 3e888f7
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 412 deletions.
1 change: 1 addition & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["e
test-programs-macros = { path = "../test-programs/macros" }
test-log = { version = "0.2", default-features = false, features = ["trace"] }
tempfile = "3.3.0"
lazy_static = "1"
14 changes: 4 additions & 10 deletions host/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ impl wasi::environment::Host for WasiCtx {
impl wasi::preopens::Host for WasiCtx {
async fn get_stdio(&mut self) -> Result<wasi::preopens::StdioPreopens, anyhow::Error> {
Ok(wasi::preopens::StdioPreopens {
stdin: 0,
stdout: 1,
stderr: 2,
stdin: self.stdin,
stdout: self.stdout,
stderr: self.stderr,
})
}
async fn get_directories(
&mut self,
) -> Result<Vec<(wasi::filesystem::Descriptor, String)>, anyhow::Error> {
// Create new handles to the preopens.
let mut results = Vec::new();
for (handle, name) in &self.preopens {
let desc = self.table.push(Box::new(handle.dup()))?;
results.push((desc, name.clone()));
}
Ok(results)
Ok(self.preopens.clone())
}
}
6 changes: 3 additions & 3 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ async fn main() -> Result<()> {

let mut builder = WasiCtxBuilder::new()
.inherit_stdio()
.inherit_network()
.inherit_network(cap_std::ambient_authority())
.args(&argv);

for (guest, host) in args.map_dirs {
let dir = cap_std::fs::Dir::open_ambient_dir(&host, cap_std::ambient_authority())
.context(format!("opening directory {host:?}"))?;
builder = builder.preopened_dir(dir, &guest)?;
builder = builder.preopened_dir(dir, &guest);
}

let wasi_ctx = builder.build();
let wasi_ctx = builder.build()?;

if args.world == "command" {
run_command(&mut linker, &engine, &component, wasi_ctx).await?;
Expand Down
Loading

0 comments on commit 3e888f7

Please sign in to comment.