feat(api) Merge js-api into api.

This patch takes the entire `wasmer-js` crate and merges it into the
`wasmer` crate.

Inside the `lib/api/src/` directory, there are 2 new directories:

1. a new `sys` directory, which contains the usual `wasmer` crate
   implementation,
2. a new directory `js`, which contains the implementation of
   `wasmer-js`.

The `Cargo.toml` file is still compatible. The `default` feature
fallbacks to `sys-default`, which enables the `sys` feature. All
features related to compilers or engines or anything else prior this
patch, activates the `sys` feature.

Parallel to that, there is a `js-default` and `js` features.

The `Cargo.toml` file is extensively documented to explain what are
dependencies, dev-dependencies, features and other sections related to
`sys` or to `js`.

There is a bug with `wasm_bindgen_test` where it doesn't compile or
look for tests in `tests/*/<test>.rs`. The hack is to name files
`tests/js_<test>.rs`. Ugly, but it works.
This commit is contained in:
Ivan Enderlin
2021-07-23 12:10:49 +02:00
parent 6b2ec7209d
commit b30284897e
65 changed files with 3808 additions and 3783 deletions

113
lib/api/src/js/externals/mod.rs vendored Normal file
View File

@@ -0,0 +1,113 @@
pub(crate) mod function;
mod global;
mod memory;
mod table;
pub use self::function::{
FromToNativeWasmType, Function, HostFunction, WasmTypeList, WithEnv, WithoutEnv,
};
pub use self::global::Global;
pub use self::memory::{Memory, MemoryError};
pub use self::table::Table;
use crate::js::export::Export;
use crate::js::exports::{ExportError, Exportable};
use crate::js::store::{Store, StoreObject};
use crate::js::ExternType;
use std::fmt;
/// An `Extern` is the runtime representation of an entity that
/// can be imported or exported.
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#external-values>
#[derive(Clone)]
pub enum Extern {
/// A external [`Function`].
Function(Function),
/// A external [`Global`].
Global(Global),
/// A external [`Table`].
Table(Table),
/// A external [`Memory`].
Memory(Memory),
}
impl Extern {
/// Return the underlying type of the inner `Extern`.
pub fn ty(&self) -> ExternType {
match self {
Self::Function(ft) => ExternType::Function(ft.ty().clone()),
Self::Memory(ft) => ExternType::Memory(ft.ty()),
Self::Table(tt) => ExternType::Table(*tt.ty()),
Self::Global(gt) => ExternType::Global(*gt.ty()),
}
}
/// Create an `Extern` from an `wasmer_engine::Export`.
pub fn from_vm_export(store: &Store, export: Export) -> Self {
match export {
Export::Function(f) => Self::Function(Function::from_vm_export(store, f)),
Export::Memory(m) => Self::Memory(Memory::from_vm_export(store, m)),
Export::Global(g) => Self::Global(Global::from_vm_export(store, g)),
Export::Table(t) => Self::Table(Table::from_vm_export(store, t)),
}
}
}
impl<'a> Exportable<'a> for Extern {
fn to_export(&self) -> Export {
match self {
Self::Function(f) => f.to_export(),
Self::Global(g) => g.to_export(),
Self::Memory(m) => m.to_export(),
Self::Table(t) => t.to_export(),
}
}
fn get_self_from_extern(_extern: &'a Self) -> Result<&'a Self, ExportError> {
// Since this is already an extern, we can just return it.
Ok(_extern)
}
}
impl StoreObject for Extern {}
impl fmt::Debug for Extern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Function(_) => "Function(...)",
Self::Global(_) => "Global(...)",
Self::Memory(_) => "Memory(...)",
Self::Table(_) => "Table(...)",
}
)
}
}
impl From<Function> for Extern {
fn from(r: Function) -> Self {
Self::Function(r)
}
}
impl From<Global> for Extern {
fn from(r: Global) -> Self {
Self::Global(r)
}
}
impl From<Memory> for Extern {
fn from(r: Memory) -> Self {
Self::Memory(r)
}
}
impl From<Table> for Extern {
fn from(r: Table) -> Self {
Self::Table(r)
}
}