Reimplement wasmtime-wasi on top of wasmtime (#899)

* Reimplement `wasmtime-wasi` on top of `wasmtime`

This commit reimplements the `wasmtime-wasi` crate on top of the
`wasmtime` API crate, instead of being placed on top of the `wasmtime-*`
family of internal crates. The purpose here is to continue to exercise
the API as well as avoid usage of internals wherever possible and
instead use the safe API as much as possible.

The `wasmtime-wasi` crate's API has been updated as part of this PR as
well. The general outline of it is now:

* Each module snapshot has a `WasiCtxBuilder`, `WasiCtx`, and `Wasi`
  type.
  * The `WasiCtx*` types are reexported from `wasi-common`.
  * The `Wasi` type is synthesized by the `wig` crate's procedural macro
* The `Wasi` type exposes one constructor which takes a `Store` and a
  `WasiCtx`, and produces a `Wasi`
* Each `Wasi` struct fields for all the exported functions in that wasi
  module. They're all public an they all have type `wasmtime::Func`
* The `Wasi` type has a `get_export` method to fetch an struct field by
  name.

The intention here is that we can continue to make progress on #727 by
integrating WASI construction into the `Instance::new` experience, but
it requires everything to be part of the same system!

The main oddity required by the `wasmtime-wasi` crate is that it needs
access to the caller's `memory` export, if any. This is currently done
with a bit of a hack and is expected to go away once interface types are
more fully baked in.

* Remove now no-longer-necessary APIs from `wasmtime`

* rustfmt

* Rename to from_abi
This commit is contained in:
Alex Crichton
2020-02-06 09:23:06 -06:00
committed by GitHub
parent c9dce98ba2
commit 3dd5a3cb3f
20 changed files with 284 additions and 488 deletions

View File

@@ -45,16 +45,19 @@ macro_rules! wrappers {
unsafe extern "C" fn shim<F, $($args,)* R>(
vmctx: *mut VMContext,
_caller_vmctx: *mut VMContext,
$($args: $args,)*
$($args: $args::Abi,)*
) -> R::Abi
where
F: Fn($($args),*) -> R + 'static,
$($args: WasmArg,)*
R: WasmRet,
{
let ret = {
let instance = InstanceHandle::from_vmctx(vmctx);
let func = instance.host_state().downcast_ref::<F>().expect("state");
panic::catch_unwind(AssertUnwindSafe(|| func($($args),*)))
panic::catch_unwind(AssertUnwindSafe(|| {
func($($args::from_abi(_caller_vmctx, $args)),*)
}))
};
match ret {
Ok(ret) => ret.into_abi(),
@@ -166,6 +169,36 @@ impl Func {
///
/// For more information about this function, see [`Func::wrap1`].
(wrap5, A, B, C, D, E)
/// Creates a new `Func` from the given Rust closure, which takes 6
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap6, A, B, C, D, E, G)
/// Creates a new `Func` from the given Rust closure, which takes 7
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap7, A, B, C, D, E, G, H)
/// Creates a new `Func` from the given Rust closure, which takes 8
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap8, A, B, C, D, E, G, H, I)
/// Creates a new `Func` from the given Rust closure, which takes 9
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap9, A, B, C, D, E, G, H, I, J)
/// Creates a new `Func` from the given Rust closure, which takes 10
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap10, A, B, C, D, E, G, H, I, J, K)
}
fn from_wrapped(
@@ -248,36 +281,65 @@ impl fmt::Debug for Func {
///
/// For more information see [`Func::wrap1`]
pub trait WasmArg {
#[doc(hidden)]
type Abi;
#[doc(hidden)]
fn push(dst: &mut Vec<ValType>);
#[doc(hidden)]
fn from_abi(vmctx: *mut VMContext, abi: Self::Abi) -> Self;
}
impl WasmArg for () {
type Abi = ();
fn push(_dst: &mut Vec<ValType>) {}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
}
impl WasmArg for i32 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::I32);
}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
}
impl WasmArg for i64 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::I64);
}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
}
impl WasmArg for f32 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::F32);
}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
}
impl WasmArg for f64 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::F64);
}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
}
/// A trait implemented for types which can be returned from closures passed to