Merge pull request #2776 from bytecodealliance/pch/wasmtime_wasi_usability

`wasmtime-wasi` usability: re-exports of common siblings
This commit is contained in:
Pat Hickey
2021-03-26 17:36:44 -07:00
committed by GitHub
11 changed files with 34 additions and 27 deletions

View File

@@ -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;
@@ -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> {
@@ -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,
},

View File

@@ -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
@@ -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;
@@ -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()

View File

@@ -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"]

View File

@@ -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 {