Skip to content

Commit

Permalink
Merge pull request #2776 from bytecodealliance/pch/wasmtime_wasi_usab…
Browse files Browse the repository at this point in the history
…ility

`wasmtime-wasi` usability: re-exports of common siblings
  • Loading branch information
Pat Hickey authored Mar 27, 2021
2 parents 550c774 + ae4c5a9 commit 3abc29e
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ wasmtime-wast = { path = "crates/wast", version = "0.25.0" }
wasmtime-wasi = { path = "crates/wasi", version = "0.25.0" }
wasmtime-wasi-crypto = { path = "crates/wasi-crypto", version = "0.25.0", optional = true }
wasmtime-wasi-nn = { path = "crates/wasi-nn", version = "0.25.0", optional = true }
wasi-common = { path = "crates/wasi-common", version = "0.25.0" }
wasi-cap-std-sync = { path = "crates/wasi-common/cap-std-sync", version = "0.25.0" }
structopt = { version = "0.3.5", features = ["color", "suggestions"] }
object = { version = "0.23.0", default-features = false, features = ["write"] }
anyhow = "1.0.19"
Expand All @@ -46,7 +44,6 @@ log = "0.4.8"
rayon = "1.2.1"
humantime = "2.0.0"
wasmparser = "0.77.0"
cap-std = "0.13"

[dev-dependencies]
env_logger = "0.8.1"
Expand Down
10 changes: 5 additions & 5 deletions crates/c-api/src/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! The WASI embedding API definitions for Wasmtime.
use crate::{wasm_extern_t, wasm_importtype_t, wasm_store_t, wasm_trap_t};
use anyhow::Result;
use cap_std::fs::Dir;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::CStr;
Expand All @@ -11,11 +10,12 @@ use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::slice;
use std::str;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::WasiCtx;
use wasmtime::{Extern, Linker, Trap};
use wasmtime_wasi::{
snapshots::preview_0::Wasi as WasiSnapshot0, snapshots::preview_1::Wasi as WasiPreview1,
snapshots::preview_0::Wasi as WasiSnapshot0,
snapshots::preview_1::Wasi as WasiPreview1,
sync::{Dir, WasiCtxBuilder},
WasiCtx,
};

unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> {
Expand Down Expand Up @@ -186,7 +186,7 @@ pub unsafe extern "C" fn wasi_config_preopen_dir(
};

let dir = match cstr_to_path(path) {
Some(p) => match cap_std::fs::Dir::open_ambient_dir(p) {
Some(p) => match Dir::open_ambient_dir(p) {
Ok(d) => d,
Err(_) => return false,
},
Expand Down
13 changes: 7 additions & 6 deletions crates/wasi-common/cap-std-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
//! `cap_std::fs::Dir`, and provides convenience methods for inheriting the
//! parent process's stdio, args, and env.
//!
//! For the convenience of consumers, `cap_std::fs::Dir` is re-exported from
//! this crate. This saves consumers tracking an additional dep on the exact
//! version of cap_std used by this crate, if they want to avoid it.
//!
//! The only place we expect to run into long-term compatibility issues
//! between `wasi-cap-std-sync` and the other impl crates that will come later
//! is in the `Sched` abstraction. Once we can build an async scheduler based
Expand All @@ -33,6 +37,7 @@ pub mod file;
pub mod sched;
pub mod stdio;

pub use cap_std::fs::Dir;
pub use clocks::clocks_ctx;
pub use sched::sched_ctx;

Expand Down Expand Up @@ -110,13 +115,9 @@ impl WasiCtxBuilder {
pub fn inherit_stdio(self) -> Self {
self.inherit_stdin().inherit_stdout().inherit_stderr()
}
pub fn preopened_dir(
self,
dir: cap_std::fs::Dir,
path: impl AsRef<Path>,
) -> Result<Self, Error> {
pub fn preopened_dir(self, dir: Dir, guest_path: impl AsRef<Path>) -> Result<Self, Error> {
let dir = Box::new(crate::dir::Dir::from_cap_std(dir));
Ok(WasiCtxBuilder(self.0.preopened_dir(dir, path)?))
Ok(WasiCtxBuilder(self.0.preopened_dir(dir, guest_path)?))
}
pub fn build(self) -> Result<WasiCtx, Error> {
self.0.build()
Expand Down
5 changes: 5 additions & 0 deletions crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ build = "build.rs"

[dependencies]
wasi-common = { path = "../wasi-common", version = "0.25.0" }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync", version = "0.25.0", optional = true }
wiggle = { path = "../wiggle", default-features = false, version = "0.25.0" }
wasmtime-wiggle = { path = "../wiggle/wasmtime", default-features = false, version = "0.25.0" }
wasmtime = { path = "../wasmtime", default-features = false, version = "0.25.0" }
anyhow = "1.0"

[features]
default = ["sync"]
sync = ["wasi-cap-std-sync"]
8 changes: 8 additions & 0 deletions crates/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ use std::rc::Rc;
pub use wasi_common::{Error, WasiCtx, WasiCtxBuilder, WasiDir, WasiFile};
use wasmtime::{Config, Linker, Store};

/// Re-export the commonly used wasi-cap-std-sync crate here. This saves
/// consumers of this library from having to keep additional dependencies
/// in sync.
#[cfg(feature = "sync")]
pub mod sync {
pub use wasi_cap_std_sync::*;
}

/// An instantiated instance of all available wasi exports. Presently includes
/// both the "preview1" snapshot and the "unstable" (preview0) snapshot.
pub struct Wasi {
Expand Down
3 changes: 1 addition & 2 deletions examples/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// You can execute this example with `cargo run --example linking`

use anyhow::Result;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmtime::*;
use wasmtime_wasi::Wasi;
use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi};

fn main() -> Result<()> {
let engine = Engine::default();
Expand Down
3 changes: 1 addition & 2 deletions examples/wasi/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
// You can execute this example with `cargo run --example wasi`

use anyhow::Result;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmtime::*;
use wasmtime_wasi::Wasi;
use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi};

fn main() -> Result<()> {
tracing_subscriber::FmtSubscriber::builder()
Expand Down
7 changes: 4 additions & 3 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::{init_file_per_thread_logger, CommonOptions};
use anyhow::{bail, Context as _, Result};
use cap_std::fs::Dir;
use std::thread;
use std::time::Duration;
use std::{
Expand All @@ -11,9 +10,11 @@ use std::{
process,
};
use structopt::{clap::AppSettings, StructOpt};
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType};
use wasmtime_wasi::Wasi;
use wasmtime_wasi::{
sync::{Dir, WasiCtxBuilder},
Wasi,
};

#[cfg(feature = "wasi-nn")]
use wasmtime_wasi_nn::{WasiNn, WasiNnCtx};
Expand Down
3 changes: 1 addition & 2 deletions tests/all/host_funcs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow::Result;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmtime::*;
use wasmtime_wasi::Wasi;
use wasmtime_wasi::{sync::WasiCtxBuilder, Wasi};

#[test]
fn async_required() {
Expand Down
2 changes: 1 addition & 1 deletion tests/all/traps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn parse_dwarf_info() -> Result<()> {
let mut linker = Linker::new(&store);
wasmtime_wasi::Wasi::new(
&store,
wasi_cap_std_sync::WasiCtxBuilder::new()
wasmtime_wasi::sync::WasiCtxBuilder::new()
.inherit_stdio()
.build()?,
)
Expand Down

0 comments on commit 3abc29e

Please sign in to comment.