mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-13 13:58:38 +00:00
Unified Extern in js/sys
This commit is contained in:
8
lib/api/src/externals/function.rs
vendored
Normal file
8
lib/api/src/externals/function.rs
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#[cfg(feature = "js")]
|
||||||
|
pub use crate::js::externals::function::{
|
||||||
|
FromToNativeWasmType, Function, HostFunction, WasmTypeList,
|
||||||
|
};
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
pub use crate::sys::externals::function::{
|
||||||
|
FromToNativeWasmType, Function, HostFunction, WasmTypeList,
|
||||||
|
};
|
||||||
4
lib/api/src/externals/global.rs
vendored
Normal file
4
lib/api/src/externals/global.rs
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[cfg(feature = "js")]
|
||||||
|
pub use crate::js::externals::global::Global;
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
pub use crate::sys::externals::global::Global;
|
||||||
4
lib/api/src/externals/memory.rs
vendored
Normal file
4
lib/api/src/externals/memory.rs
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[cfg(feature = "js")]
|
||||||
|
pub use crate::js::externals::memory::Memory;
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
pub use crate::sys::externals::memory::Memory;
|
||||||
4
lib/api/src/externals/memory_view.rs
vendored
Normal file
4
lib/api/src/externals/memory_view.rs
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[cfg(feature = "js")]
|
||||||
|
pub use crate::js::externals::memory_view::MemoryView;
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
pub use crate::sys::externals::memory_view::MemoryView;
|
||||||
126
lib/api/src/externals/mod.rs
vendored
Normal file
126
lib/api/src/externals/mod.rs
vendored
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
mod function;
|
||||||
|
mod global;
|
||||||
|
mod memory;
|
||||||
|
mod memory_view;
|
||||||
|
mod table;
|
||||||
|
|
||||||
|
pub use self::function::{FromToNativeWasmType, Function, HostFunction, WasmTypeList};
|
||||||
|
pub use self::global::Global;
|
||||||
|
pub use self::memory::Memory;
|
||||||
|
pub use self::memory_view::MemoryView;
|
||||||
|
pub use self::table::Table;
|
||||||
|
|
||||||
|
use crate::exports::{ExportError, Exportable};
|
||||||
|
use crate::ExternType;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[cfg(feature = "js")]
|
||||||
|
use crate::js::vm::VMExtern;
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
use wasmer_vm::VMExtern;
|
||||||
|
|
||||||
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
|
|
||||||
|
/// 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, store: &impl AsStoreRef) -> ExternType {
|
||||||
|
match self {
|
||||||
|
Self::Function(ft) => ExternType::Function(ft.ty(store)),
|
||||||
|
Self::Memory(ft) => ExternType::Memory(ft.ty(store)),
|
||||||
|
Self::Table(tt) => ExternType::Table(tt.ty(store)),
|
||||||
|
Self::Global(gt) => ExternType::Global(gt.ty(store)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an `Extern` from an `wasmer_engine::Export`.
|
||||||
|
pub fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExtern) -> Self {
|
||||||
|
match vm_extern {
|
||||||
|
VMExtern::Function(f) => Self::Function(Function::from_vm_extern(store, f)),
|
||||||
|
VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(store, m)),
|
||||||
|
VMExtern::Global(g) => Self::Global(Global::from_vm_extern(store, g)),
|
||||||
|
VMExtern::Table(t) => Self::Table(Table::from_vm_extern(store, t)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks whether this `Extern` can be used with the given context.
|
||||||
|
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Function(f) => f.is_from_store(store),
|
||||||
|
Self::Global(g) => g.is_from_store(store),
|
||||||
|
Self::Memory(m) => m.is_from_store(store),
|
||||||
|
Self::Table(t) => t.is_from_store(store),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// To `VMExtern`.
|
||||||
|
pub fn to_vm_extern(&self) -> VMExtern {
|
||||||
|
match self {
|
||||||
|
Self::Function(f) => f.to_vm_extern(),
|
||||||
|
Self::Global(g) => g.to_vm_extern(),
|
||||||
|
Self::Memory(m) => m.to_vm_extern(),
|
||||||
|
Self::Table(t) => t.to_vm_extern(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Exportable<'a> for Extern {
|
||||||
|
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 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
4
lib/api/src/externals/table.rs
vendored
Normal file
4
lib/api/src/externals/table.rs
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[cfg(feature = "js")]
|
||||||
|
pub use crate::js::externals::table::Table;
|
||||||
|
#[cfg(feature = "sys")]
|
||||||
|
pub use crate::sys::externals::table::Table;
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
// use crate::store::{Store, StoreObject};
|
// use crate::store::{Store, StoreObject};
|
||||||
// use crate::js::RuntimeError;
|
// use crate::js::RuntimeError;
|
||||||
use crate::imports::Imports;
|
use crate::imports::Imports;
|
||||||
use crate::js::externals::{Extern, Function, Global, Memory, Table};
|
|
||||||
use crate::js::instance::Instance;
|
use crate::js::instance::Instance;
|
||||||
use crate::js::vm::{VMExtern, VMFunction, VMGlobal, VMMemory, VMTable};
|
use crate::js::vm::{VMExtern, VMFunction, VMGlobal, VMMemory, VMTable};
|
||||||
use crate::js::wasm_bindgen_polyfill::Global as JsGlobal;
|
use crate::js::wasm_bindgen_polyfill::Global as JsGlobal;
|
||||||
@@ -10,6 +9,7 @@ use crate::store::{AsStoreMut, AsStoreRef};
|
|||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use crate::Exports;
|
use crate::Exports;
|
||||||
use crate::Type;
|
use crate::Type;
|
||||||
|
use crate::{Extern, Function, Global, Memory, Table};
|
||||||
use js_sys::Function as JsFunction;
|
use js_sys::Function as JsFunction;
|
||||||
use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable};
|
use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|||||||
2
lib/api/src/js/externals/function.rs
vendored
2
lib/api/src/js/externals/function.rs
vendored
@@ -2,11 +2,11 @@ pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv,
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::function_env::{FunctionEnv, FunctionEnvMut};
|
use crate::function_env::{FunctionEnv, FunctionEnvMut};
|
||||||
use crate::js::as_js::{param_from_js, AsJs}; /* ValFuncRef */
|
use crate::js::as_js::{param_from_js, AsJs}; /* ValFuncRef */
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::VMExtern;
|
use crate::js::vm::VMExtern;
|
||||||
use crate::js::RuntimeError;
|
use crate::js::RuntimeError;
|
||||||
use crate::store::{AsStoreMut, AsStoreRef, StoreMut};
|
use crate::store::{AsStoreMut, AsStoreRef, StoreMut};
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
use crate::Extern;
|
||||||
use crate::FunctionType;
|
use crate::FunctionType;
|
||||||
use crate::TypedFunction;
|
use crate::TypedFunction;
|
||||||
use js_sys::{Array, Function as JSFunction};
|
use js_sys::{Array, Function as JSFunction};
|
||||||
|
|||||||
2
lib/api/src/js/externals/global.rs
vendored
2
lib/api/src/js/externals/global.rs
vendored
@@ -1,10 +1,10 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::{VMExtern, VMGlobal};
|
use crate::js::vm::{VMExtern, VMGlobal};
|
||||||
use crate::js::wasm_bindgen_polyfill::Global as JSGlobal;
|
use crate::js::wasm_bindgen_polyfill::Global as JSGlobal;
|
||||||
use crate::js::RuntimeError;
|
use crate::js::RuntimeError;
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
use crate::Extern;
|
||||||
use crate::GlobalType;
|
use crate::GlobalType;
|
||||||
use crate::Mutability;
|
use crate::Mutability;
|
||||||
use wasm_bindgen::JsValue;
|
use wasm_bindgen::JsValue;
|
||||||
|
|||||||
4
lib/api/src/js/externals/memory.rs
vendored
4
lib/api/src/js/externals/memory.rs
vendored
@@ -1,8 +1,8 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::{VMExtern, VMMemory};
|
use crate::js::vm::{VMExtern, VMMemory};
|
||||||
use crate::mem_access::MemoryAccessError;
|
use crate::mem_access::MemoryAccessError;
|
||||||
use crate::store::{AsStoreMut, AsStoreRef, StoreObjects};
|
use crate::store::{AsStoreMut, AsStoreRef, StoreObjects};
|
||||||
|
use crate::Extern;
|
||||||
use crate::MemoryType;
|
use crate::MemoryType;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
@@ -14,7 +14,7 @@ use wasm_bindgen::prelude::*;
|
|||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use wasmer_types::{Pages, WASM_PAGE_SIZE};
|
use wasmer_types::{Pages, WASM_PAGE_SIZE};
|
||||||
|
|
||||||
use super::MemoryView;
|
use super::memory_view::MemoryView;
|
||||||
|
|
||||||
pub use wasmer_types::MemoryError;
|
pub use wasmer_types::MemoryError;
|
||||||
|
|
||||||
|
|||||||
3
lib/api/src/js/externals/memory_view.rs
vendored
3
lib/api/src/js/externals/memory_view.rs
vendored
@@ -9,8 +9,7 @@ use tracing::warn;
|
|||||||
|
|
||||||
use wasmer_types::{Bytes, Pages};
|
use wasmer_types::{Bytes, Pages};
|
||||||
|
|
||||||
use super::memory::MemoryBuffer;
|
use super::memory::{Memory, MemoryBuffer};
|
||||||
use super::Memory;
|
|
||||||
|
|
||||||
/// A WebAssembly `memory` view.
|
/// A WebAssembly `memory` view.
|
||||||
///
|
///
|
||||||
|
|||||||
116
lib/api/src/js/externals/mod.rs
vendored
116
lib/api/src/js/externals/mod.rs
vendored
@@ -3,119 +3,3 @@ pub(crate) mod global;
|
|||||||
pub(crate) mod memory;
|
pub(crate) mod memory;
|
||||||
pub(crate) mod memory_view;
|
pub(crate) mod memory_view;
|
||||||
pub(crate) mod table;
|
pub(crate) mod table;
|
||||||
|
|
||||||
pub use self::function::{FromToNativeWasmType, Function, HostFunction, WasmTypeList};
|
|
||||||
pub use self::global::Global;
|
|
||||||
pub use self::memory::{Memory, MemoryError};
|
|
||||||
pub use self::memory_view::MemoryView;
|
|
||||||
pub use self::table::Table;
|
|
||||||
|
|
||||||
use crate::exports::{ExportError, Exportable};
|
|
||||||
use crate::js::vm::VMExtern;
|
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
|
||||||
use std::fmt;
|
|
||||||
use wasmer_types::ExternType;
|
|
||||||
|
|
||||||
/// 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, store: &impl AsStoreRef) -> ExternType {
|
|
||||||
match self {
|
|
||||||
Self::Function(ft) => ExternType::Function(ft.ty(store)),
|
|
||||||
Self::Memory(ft) => ExternType::Memory(ft.ty(store)),
|
|
||||||
Self::Table(tt) => ExternType::Table(tt.ty(store)),
|
|
||||||
Self::Global(gt) => ExternType::Global(gt.ty(store)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an `Extern` from an `wasmer_engine::Export`.
|
|
||||||
pub fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExtern) -> Self {
|
|
||||||
match vm_extern {
|
|
||||||
VMExtern::Function(f) => Self::Function(Function::from_vm_extern(store, f)),
|
|
||||||
VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(store, m)),
|
|
||||||
VMExtern::Global(g) => Self::Global(Global::from_vm_extern(store, g)),
|
|
||||||
VMExtern::Table(t) => Self::Table(Table::from_vm_extern(store, t)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// To `VMExtern`.
|
|
||||||
pub fn to_vm_extern(&self) -> VMExtern {
|
|
||||||
match self {
|
|
||||||
Self::Function(f) => f.to_vm_extern(),
|
|
||||||
Self::Global(g) => g.to_vm_extern(),
|
|
||||||
Self::Memory(m) => m.to_vm_extern(),
|
|
||||||
Self::Table(t) => t.to_vm_extern(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks whether this `Extern` can be used with the given context.
|
|
||||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Function(val) => val.is_from_store(store),
|
|
||||||
Self::Memory(val) => val.is_from_store(store),
|
|
||||||
Self::Global(val) => val.is_from_store(store),
|
|
||||||
Self::Table(val) => val.is_from_store(store),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Exportable<'a> for Extern {
|
|
||||||
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 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
4
lib/api/src/js/externals/table.rs
vendored
4
lib/api/src/js/externals/table.rs
vendored
@@ -1,9 +1,9 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::{VMExtern, VMFunction, VMTable};
|
use crate::js::vm::{VMExtern, VMFunction, VMTable};
|
||||||
use crate::js::RuntimeError;
|
use crate::js::RuntimeError;
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
use crate::Extern;
|
||||||
use crate::{FunctionType, TableType};
|
use crate::{FunctionType, TableType};
|
||||||
use js_sys::Function;
|
use js_sys::Function;
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ impl Table {
|
|||||||
if let Some(func) = self.handle.table.get(index).ok() {
|
if let Some(func) = self.handle.table.get(index).ok() {
|
||||||
let ty = FunctionType::new(vec![], vec![]);
|
let ty = FunctionType::new(vec![], vec![]);
|
||||||
let vm_function = VMFunction::new(func, ty);
|
let vm_function = VMFunction::new(func, ty);
|
||||||
let function = crate::js::externals::Function::from_vm_extern(store, vm_function);
|
let function = crate::Function::from_vm_extern(store, vm_function);
|
||||||
Some(Value::FuncRef(Some(function)))
|
Some(Value::FuncRef(Some(function)))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ use crate::errors::InstantiationError;
|
|||||||
use crate::exports::Exports;
|
use crate::exports::Exports;
|
||||||
use crate::imports::Imports;
|
use crate::imports::Imports;
|
||||||
use crate::js::as_js::AsJs;
|
use crate::js::as_js::AsJs;
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::{VMExtern, VMInstance};
|
use crate::js::vm::{VMExtern, VMInstance};
|
||||||
use crate::module::Module;
|
use crate::module::Module;
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
|
use crate::Extern;
|
||||||
use crate::{LinkError, RuntimeError};
|
use crate::{LinkError, RuntimeError};
|
||||||
use js_sys::WebAssembly;
|
use js_sys::WebAssembly;
|
||||||
|
|
||||||
|
|||||||
@@ -40,10 +40,6 @@ mod wasm_bindgen_polyfill;
|
|||||||
|
|
||||||
pub use crate::js::as_js::AsJs;
|
pub use crate::js::as_js::AsJs;
|
||||||
pub use crate::js::error::{DeserializeError, LinkError, SerializeError};
|
pub use crate::js::error::{DeserializeError, LinkError, SerializeError};
|
||||||
pub use crate::js::externals::{
|
|
||||||
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView,
|
|
||||||
Table, WasmTypeList,
|
|
||||||
};
|
|
||||||
pub use crate::js::module::{Module, ModuleTypeHints};
|
pub use crate::js::module::{Module, ModuleTypeHints};
|
||||||
pub use crate::js::store::StoreObjects;
|
pub use crate::js::store::StoreObjects;
|
||||||
pub use crate::js::trap::RuntimeError;
|
pub use crate::js::trap::RuntimeError;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use crate::errors::InstantiationError;
|
use crate::errors::InstantiationError;
|
||||||
use crate::imports::Imports;
|
use crate::imports::Imports;
|
||||||
use crate::js::externals::Extern;
|
|
||||||
use crate::js::vm::VMInstance;
|
use crate::js::vm::VMInstance;
|
||||||
use crate::js::AsJs;
|
use crate::js::AsJs;
|
||||||
use crate::js::RuntimeError;
|
use crate::js::RuntimeError;
|
||||||
use crate::module::IoCompileError;
|
use crate::module::IoCompileError;
|
||||||
use crate::store::AsStoreMut;
|
use crate::store::AsStoreMut;
|
||||||
|
use crate::Extern;
|
||||||
use crate::IntoBytes;
|
use crate::IntoBytes;
|
||||||
use crate::{AsEngineRef, ExportType, ImportType};
|
use crate::{AsEngineRef, ExportType, ImportType};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|||||||
@@ -9,10 +9,10 @@
|
|||||||
//! ```
|
//! ```
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use crate::js::externals::Function;
|
|
||||||
use crate::js::{FromToNativeWasmType, RuntimeError, WasmTypeList};
|
|
||||||
use crate::native_type::NativeWasmTypeInto;
|
use crate::native_type::NativeWasmTypeInto;
|
||||||
|
use crate::Function;
|
||||||
use crate::{AsStoreMut, AsStoreRef, TypedFunction};
|
use crate::{AsStoreMut, AsStoreRef, TypedFunction};
|
||||||
|
use crate::{FromToNativeWasmType, RuntimeError, WasmTypeList};
|
||||||
// use std::panic::{catch_unwind, AssertUnwindSafe};
|
// use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||||
use crate::js::as_js::{param_from_js, AsJs};
|
use crate::js::as_js::{param_from_js, AsJs};
|
||||||
use js_sys::Array;
|
use js_sys::Array;
|
||||||
|
|||||||
@@ -433,6 +433,7 @@ mod engine;
|
|||||||
mod errors;
|
mod errors;
|
||||||
mod exports;
|
mod exports;
|
||||||
mod extern_ref;
|
mod extern_ref;
|
||||||
|
mod externals;
|
||||||
mod function_env;
|
mod function_env;
|
||||||
mod imports;
|
mod imports;
|
||||||
mod instance;
|
mod instance;
|
||||||
@@ -457,6 +458,10 @@ mod js;
|
|||||||
#[cfg(feature = "js")]
|
#[cfg(feature = "js")]
|
||||||
pub use js::*;
|
pub use js::*;
|
||||||
|
|
||||||
|
pub use crate::externals::{
|
||||||
|
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table,
|
||||||
|
WasmTypeList,
|
||||||
|
};
|
||||||
pub use engine::{AsEngineRef, Engine};
|
pub use engine::{AsEngineRef, Engine};
|
||||||
pub use errors::InstantiationError;
|
pub use errors::InstantiationError;
|
||||||
pub use exports::{ExportError, Exportable, Exports, ExportsIterator};
|
pub use exports::{ExportError, Exportable, Exports, ExportsIterator};
|
||||||
@@ -481,10 +486,10 @@ pub use wasmer_derive::ValueType;
|
|||||||
// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
|
// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
|
||||||
pub use wasmer_types::{
|
pub use wasmer_types::{
|
||||||
is_wasm, Bytes, CompileError, CpuFeature, DeserializeError, ExportIndex, ExportType,
|
is_wasm, Bytes, CompileError, CpuFeature, DeserializeError, ExportIndex, ExportType,
|
||||||
ExternType, FunctionType, GlobalInit, GlobalType, ImportType, LocalFunctionIndex, MemoryType,
|
ExternType, FunctionType, GlobalInit, GlobalType, ImportType, LocalFunctionIndex, MemoryError,
|
||||||
MiddlewareError, Mutability, OnCalledAction, Pages, ParseCpuFeatureError, SerializeError,
|
MemoryType, MiddlewareError, Mutability, OnCalledAction, Pages, ParseCpuFeatureError,
|
||||||
TableType, Target, Type, ValueType, WasmError, WasmResult, WASM_MAX_PAGES, WASM_MIN_PAGES,
|
SerializeError, TableType, Target, Type, ValueType, WasmError, WasmResult, WASM_MAX_PAGES,
|
||||||
WASM_PAGE_SIZE,
|
WASM_MIN_PAGES, WASM_PAGE_SIZE,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "wat")]
|
#[cfg(feature = "wat")]
|
||||||
pub use wat::parse_bytes as wat2wasm;
|
pub use wat::parse_bytes as wat2wasm;
|
||||||
|
|||||||
2
lib/api/src/sys/externals/function.rs
vendored
2
lib/api/src/sys/externals/function.rs
vendored
@@ -7,7 +7,7 @@ use wasmer_vm::{
|
|||||||
|
|
||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::sys::externals::Extern;
|
use crate::Extern;
|
||||||
use crate::FunctionEnv;
|
use crate::FunctionEnv;
|
||||||
use crate::{FunctionType, RuntimeError, TypedFunction};
|
use crate::{FunctionType, RuntimeError, TypedFunction};
|
||||||
|
|
||||||
|
|||||||
2
lib/api/src/sys/externals/global.rs
vendored
2
lib/api/src/sys/externals/global.rs
vendored
@@ -1,8 +1,8 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::sys::externals::Extern;
|
|
||||||
use crate::sys::RuntimeError;
|
use crate::sys::RuntimeError;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
use crate::Extern;
|
||||||
use crate::GlobalType;
|
use crate::GlobalType;
|
||||||
use crate::Mutability;
|
use crate::Mutability;
|
||||||
use wasmer_vm::{InternalStoreHandle, StoreHandle, VMExtern, VMGlobal};
|
use wasmer_vm::{InternalStoreHandle, StoreHandle, VMExtern, VMGlobal};
|
||||||
|
|||||||
4
lib/api/src/sys/externals/memory.rs
vendored
4
lib/api/src/sys/externals/memory.rs
vendored
@@ -1,6 +1,6 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::sys::externals::Extern;
|
use crate::Extern;
|
||||||
use crate::MemoryAccessError;
|
use crate::MemoryAccessError;
|
||||||
use crate::MemoryType;
|
use crate::MemoryType;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
@@ -13,7 +13,7 @@ use tracing::warn;
|
|||||||
use wasmer_types::Pages;
|
use wasmer_types::Pages;
|
||||||
use wasmer_vm::{InternalStoreHandle, LinearMemory, MemoryError, StoreHandle, VMExtern, VMMemory};
|
use wasmer_vm::{InternalStoreHandle, LinearMemory, MemoryError, StoreHandle, VMExtern, VMMemory};
|
||||||
|
|
||||||
use super::MemoryView;
|
use super::memory_view::MemoryView;
|
||||||
|
|
||||||
/// A WebAssembly `memory` instance.
|
/// A WebAssembly `memory` instance.
|
||||||
///
|
///
|
||||||
|
|||||||
3
lib/api/src/sys/externals/memory_view.rs
vendored
3
lib/api/src/sys/externals/memory_view.rs
vendored
@@ -7,8 +7,7 @@ use std::slice;
|
|||||||
use wasmer_types::Pages;
|
use wasmer_types::Pages;
|
||||||
use wasmer_vm::LinearMemory;
|
use wasmer_vm::LinearMemory;
|
||||||
|
|
||||||
use super::memory::MemoryBuffer;
|
use super::memory::{Memory, MemoryBuffer};
|
||||||
use super::Memory;
|
|
||||||
|
|
||||||
/// A WebAssembly `memory` view.
|
/// A WebAssembly `memory` view.
|
||||||
///
|
///
|
||||||
|
|||||||
118
lib/api/src/sys/externals/mod.rs
vendored
118
lib/api/src/sys/externals/mod.rs
vendored
@@ -3,121 +3,3 @@ pub(crate) mod global;
|
|||||||
pub(crate) mod memory;
|
pub(crate) mod memory;
|
||||||
pub(crate) mod memory_view;
|
pub(crate) mod memory_view;
|
||||||
pub(crate) mod table;
|
pub(crate) mod table;
|
||||||
|
|
||||||
pub use self::function::{FromToNativeWasmType, Function, HostFunction, WasmTypeList};
|
|
||||||
|
|
||||||
pub use self::global::Global;
|
|
||||||
pub use self::memory::Memory;
|
|
||||||
pub use self::memory_view::MemoryView;
|
|
||||||
pub use self::table::Table;
|
|
||||||
|
|
||||||
use crate::exports::{ExportError, Exportable};
|
|
||||||
use crate::ExternType;
|
|
||||||
use std::fmt;
|
|
||||||
use wasmer_vm::VMExtern;
|
|
||||||
|
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
|
||||||
|
|
||||||
/// 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, store: &impl AsStoreRef) -> ExternType {
|
|
||||||
match self {
|
|
||||||
Self::Function(ft) => ExternType::Function(ft.ty(store)),
|
|
||||||
Self::Memory(ft) => ExternType::Memory(ft.ty(store)),
|
|
||||||
Self::Table(tt) => ExternType::Table(tt.ty(store)),
|
|
||||||
Self::Global(gt) => ExternType::Global(gt.ty(store)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an `Extern` from an `wasmer_engine::Export`.
|
|
||||||
pub fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExtern) -> Self {
|
|
||||||
match vm_extern {
|
|
||||||
VMExtern::Function(f) => Self::Function(Function::from_vm_extern(store, f)),
|
|
||||||
VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(store, m)),
|
|
||||||
VMExtern::Global(g) => Self::Global(Global::from_vm_extern(store, g)),
|
|
||||||
VMExtern::Table(t) => Self::Table(Table::from_vm_extern(store, t)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks whether this `Extern` can be used with the given context.
|
|
||||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::Function(f) => f.is_from_store(store),
|
|
||||||
Self::Global(g) => g.is_from_store(store),
|
|
||||||
Self::Memory(m) => m.is_from_store(store),
|
|
||||||
Self::Table(t) => t.is_from_store(store),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// To `VMExtern`.
|
|
||||||
pub fn to_vm_extern(&self) -> VMExtern {
|
|
||||||
match self {
|
|
||||||
Self::Function(f) => f.to_vm_extern(),
|
|
||||||
Self::Global(g) => g.to_vm_extern(),
|
|
||||||
Self::Memory(m) => m.to_vm_extern(),
|
|
||||||
Self::Table(t) => t.to_vm_extern(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Exportable<'a> for Extern {
|
|
||||||
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 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
2
lib/api/src/sys/externals/table.rs
vendored
2
lib/api/src/sys/externals/table.rs
vendored
@@ -1,6 +1,6 @@
|
|||||||
use crate::exports::{ExportError, Exportable};
|
use crate::exports::{ExportError, Exportable};
|
||||||
use crate::store::{AsStoreMut, AsStoreRef};
|
use crate::store::{AsStoreMut, AsStoreRef};
|
||||||
use crate::sys::externals::Extern;
|
use crate::Extern;
|
||||||
use crate::TableType;
|
use crate::TableType;
|
||||||
use crate::Value;
|
use crate::Value;
|
||||||
use crate::{sys::RuntimeError, ExternRef, Function};
|
use crate::{sys::RuntimeError, ExternRef, Function};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use wasmer_vm::{StoreHandle, VMInstance};
|
|||||||
|
|
||||||
use crate::imports::Imports;
|
use crate::imports::Imports;
|
||||||
use crate::store::AsStoreMut;
|
use crate::store::AsStoreMut;
|
||||||
use crate::sys::externals::Extern;
|
use crate::Extern;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
|
|||||||
@@ -6,11 +6,6 @@ pub(crate) mod module;
|
|||||||
mod tunables;
|
mod tunables;
|
||||||
pub(crate) mod typed_function;
|
pub(crate) mod typed_function;
|
||||||
|
|
||||||
pub use crate::sys::externals::{
|
|
||||||
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table,
|
|
||||||
WasmTypeList,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub use crate::sys::tunables::BaseTunables;
|
pub use crate::sys::tunables::BaseTunables;
|
||||||
pub use target_lexicon::{Architecture, CallingConvention, OperatingSystem, Triple, HOST};
|
pub use target_lexicon::{Architecture, CallingConvention, OperatingSystem, Triple, HOST};
|
||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ pub use wasmer_compiler::BaseTunables;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::sys::TableType;
|
use crate::TableType;
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
use wasmer_compiler::Tunables;
|
use wasmer_compiler::Tunables;
|
||||||
|
|||||||
Reference in New Issue
Block a user