mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-10 22:58:18 +00:00
Merge remote-tracking branch 'origin/master' into wasix
This commit is contained in:
54
lib/api/src/js/externals/function.rs
vendored
54
lib/api/src/js/externals/function.rs
vendored
@@ -1,9 +1,10 @@
|
||||
pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
|
||||
use crate::js::exports::{ExportError, Exportable};
|
||||
use crate::js::externals::{Extern, VMExtern};
|
||||
use crate::js::externals::Extern;
|
||||
use crate::js::function_env::FunctionEnvMut;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle, StoreMut};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, StoreMut};
|
||||
use crate::js::types::{param_from_js, AsJs}; /* ValFuncRef */
|
||||
use crate::js::vm::VMExtern;
|
||||
use crate::js::RuntimeError;
|
||||
use crate::js::TypedFunction;
|
||||
use crate::js::Value;
|
||||
@@ -13,7 +14,7 @@ use std::iter::FromIterator;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
|
||||
use crate::js::export::VMFunction;
|
||||
use crate::js::vm::VMFunction;
|
||||
use std::fmt;
|
||||
|
||||
#[repr(C)]
|
||||
@@ -58,11 +59,11 @@ fn results_to_js_array(values: &[Value]) -> Array {
|
||||
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840)
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct Function {
|
||||
pub(crate) handle: StoreHandle<VMFunction>,
|
||||
pub(crate) handle: VMFunction,
|
||||
}
|
||||
|
||||
impl From<StoreHandle<VMFunction>> for Function {
|
||||
fn from(handle: StoreHandle<VMFunction>) -> Self {
|
||||
impl From<VMFunction> for Function {
|
||||
fn from(handle: VMFunction) -> Self {
|
||||
Self { handle }
|
||||
}
|
||||
}
|
||||
@@ -86,7 +87,7 @@ impl Function {
|
||||
|
||||
/// To `VMExtern`.
|
||||
pub fn to_vm_extern(&self) -> VMExtern {
|
||||
VMExtern::Function(self.handle.internal_handle())
|
||||
VMExtern::Function(self.handle.clone())
|
||||
}
|
||||
|
||||
/// Creates a new host `Function` (dynamic) with the provided signature.
|
||||
@@ -190,7 +191,7 @@ impl Function {
|
||||
JSFunction::new_with_args("f", "return f(Array.prototype.slice.call(arguments, 1))");
|
||||
let binded_func = dyn_func.bind1(&JsValue::UNDEFINED, &wrapped_func);
|
||||
let vm_function = VMFunction::new(binded_func, func_ty);
|
||||
Self::from_vm_export(&mut store, vm_function)
|
||||
Self::from_vm_extern(&mut store, vm_function)
|
||||
}
|
||||
|
||||
#[deprecated(
|
||||
@@ -214,7 +215,7 @@ impl Function {
|
||||
Args: WasmTypeList,
|
||||
Rets: WasmTypeList,
|
||||
{
|
||||
let mut store = store.as_store_mut();
|
||||
let store = store.as_store_mut();
|
||||
if std::mem::size_of::<F>() != 0 {
|
||||
Self::closures_unsupported_panic();
|
||||
}
|
||||
@@ -232,7 +233,7 @@ impl Function {
|
||||
let ty = function.ty();
|
||||
let vm_function = VMFunction::new(binded_func, ty);
|
||||
Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), vm_function),
|
||||
handle: vm_function,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,7 +282,7 @@ impl Function {
|
||||
Args: WasmTypeList,
|
||||
Rets: WasmTypeList,
|
||||
{
|
||||
let mut store = store.as_store_mut();
|
||||
let store = store.as_store_mut();
|
||||
if std::mem::size_of::<F>() != 0 {
|
||||
Self::closures_unsupported_panic();
|
||||
}
|
||||
@@ -300,7 +301,7 @@ impl Function {
|
||||
let ty = function.ty();
|
||||
let vm_function = VMFunction::new(binded_func, ty);
|
||||
Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), vm_function),
|
||||
handle: vm_function,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,8 +322,8 @@ impl Function {
|
||||
/// assert_eq!(f.ty().params(), vec![Type::I32, Type::I32]);
|
||||
/// assert_eq!(f.ty().results(), vec![Type::I32]);
|
||||
/// ```
|
||||
pub fn ty(&self, store: &impl AsStoreRef) -> FunctionType {
|
||||
self.handle.get(store.as_store_ref().objects()).ty.clone()
|
||||
pub fn ty(&self, _store: &impl AsStoreRef) -> FunctionType {
|
||||
self.handle.ty.clone()
|
||||
}
|
||||
|
||||
/// Returns the number of parameters that this function takes.
|
||||
@@ -422,7 +423,7 @@ impl Function {
|
||||
// TODO: This loop is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
|
||||
loop {
|
||||
r = js_sys::Reflect::apply(
|
||||
&self.handle.get(store.as_store_ref().objects()).function,
|
||||
&self.handle.function,
|
||||
&wasm_bindgen::JsValue::NULL,
|
||||
&arr,
|
||||
);
|
||||
@@ -446,7 +447,7 @@ impl Function {
|
||||
r?
|
||||
};
|
||||
|
||||
let result_types = self.handle.get(store.as_store_ref().objects()).ty.results();
|
||||
let result_types = self.handle.ty.results();
|
||||
match result_types.len() {
|
||||
0 => Ok(Box::new([])),
|
||||
1 => {
|
||||
@@ -465,21 +466,8 @@ impl Function {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self {
|
||||
Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), vm_function),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_extern(
|
||||
store: &mut impl AsStoreMut,
|
||||
internal: InternalStoreHandle<VMFunction>,
|
||||
) -> Self {
|
||||
Self {
|
||||
handle: unsafe {
|
||||
StoreHandle::from_internal(store.as_store_ref().objects().id(), internal)
|
||||
},
|
||||
}
|
||||
pub(crate) fn from_vm_extern(_store: &mut impl AsStoreMut, internal: VMFunction) -> Self {
|
||||
Self { handle: internal }
|
||||
}
|
||||
|
||||
#[deprecated(since = "3.0.0", note = "native() has been renamed to typed().")]
|
||||
@@ -616,8 +604,8 @@ impl Function {
|
||||
}
|
||||
|
||||
/// Checks whether this `Function` can be used with the given context.
|
||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||
self.handle.store_id() == store.as_store_ref().objects().id()
|
||||
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
54
lib/api/src/js/externals/global.rs
vendored
54
lib/api/src/js/externals/global.rs
vendored
@@ -1,8 +1,8 @@
|
||||
use crate::js::export::VMGlobal;
|
||||
use crate::js::exports::{ExportError, Exportable};
|
||||
use crate::js::externals::{Extern, VMExtern};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle};
|
||||
use crate::js::externals::Extern;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::value::Value;
|
||||
use crate::js::vm::{VMExtern, VMGlobal};
|
||||
use crate::js::wasm_bindgen_polyfill::Global as JSGlobal;
|
||||
use crate::js::GlobalType;
|
||||
use crate::js::Mutability;
|
||||
@@ -17,7 +17,7 @@ use wasm_bindgen::JsValue;
|
||||
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Global {
|
||||
pub(crate) handle: StoreHandle<VMGlobal>,
|
||||
pub(crate) handle: VMGlobal,
|
||||
}
|
||||
|
||||
impl Global {
|
||||
@@ -57,7 +57,7 @@ impl Global {
|
||||
|
||||
/// To `VMExtern`.
|
||||
pub(crate) fn to_vm_extern(&self) -> VMExtern {
|
||||
VMExtern::Global(self.handle.internal_handle())
|
||||
VMExtern::Global(self.handle.clone())
|
||||
}
|
||||
|
||||
/// Create a `Global` with the initial value [`Value`] and the provided [`Mutability`].
|
||||
@@ -95,7 +95,7 @@ impl Global {
|
||||
let js_global = JSGlobal::new(&descriptor, &value).unwrap();
|
||||
let vm_global = VMGlobal::new(js_global, global_ty);
|
||||
|
||||
Ok(Self::from_vm_export(store, vm_global))
|
||||
Ok(Self::from_vm_extern(store, vm_global))
|
||||
}
|
||||
|
||||
/// Returns the [`GlobalType`] of the `Global`.
|
||||
@@ -112,8 +112,8 @@ impl Global {
|
||||
/// assert_eq!(c.ty(), &GlobalType::new(Type::I32, Mutability::Const));
|
||||
/// assert_eq!(v.ty(), &GlobalType::new(Type::I64, Mutability::Var));
|
||||
/// ```
|
||||
pub fn ty(&self, store: &impl AsStoreRef) -> GlobalType {
|
||||
self.handle.get(store.as_store_ref().objects()).ty
|
||||
pub fn ty(&self, _store: &impl AsStoreRef) -> GlobalType {
|
||||
self.handle.ty
|
||||
}
|
||||
|
||||
/// Retrieves the current value [`Value`] that the Global has.
|
||||
@@ -130,14 +130,8 @@ impl Global {
|
||||
/// ```
|
||||
pub fn get(&self, store: &impl AsStoreRef) -> Value {
|
||||
unsafe {
|
||||
let raw = self
|
||||
.handle
|
||||
.get(store.as_store_ref().objects())
|
||||
.global
|
||||
.value()
|
||||
.as_f64()
|
||||
.unwrap();
|
||||
let ty = self.handle.get(store.as_store_ref().objects()).ty;
|
||||
let raw = self.handle.global.value().as_f64().unwrap();
|
||||
let ty = self.handle.ty;
|
||||
Value::from_raw(store, ty.ty, raw)
|
||||
}
|
||||
}
|
||||
@@ -207,33 +201,19 @@ impl Global {
|
||||
))
|
||||
}
|
||||
};
|
||||
self.handle
|
||||
.get_mut(store.objects_mut())
|
||||
.global
|
||||
.set_value(&new_value);
|
||||
self.handle.global.set_value(&new_value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_global: VMGlobal) -> Self {
|
||||
Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), vm_global),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_extern(
|
||||
store: &mut impl AsStoreMut,
|
||||
internal: InternalStoreHandle<VMGlobal>,
|
||||
) -> Self {
|
||||
Self {
|
||||
handle: unsafe {
|
||||
StoreHandle::from_internal(store.as_store_ref().objects().id(), internal)
|
||||
},
|
||||
}
|
||||
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_global: VMGlobal) -> Self {
|
||||
use crate::js::store::StoreObject;
|
||||
VMGlobal::list_mut(store.objects_mut()).push(vm_global.clone());
|
||||
Self { handle: vm_global }
|
||||
}
|
||||
|
||||
/// Checks whether this `Global` can be used with the given store.
|
||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||
self.handle.store_id() == store.as_store_ref().objects().id()
|
||||
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
55
lib/api/src/js/externals/memory.rs
vendored
55
lib/api/src/js/externals/memory.rs
vendored
@@ -1,7 +1,7 @@
|
||||
use crate::js::export::VMMemory;
|
||||
use crate::js::exports::{ExportError, Exportable};
|
||||
use crate::js::externals::{Extern, VMExtern};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle, StoreObjects};
|
||||
use crate::js::externals::Extern;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, StoreObjects};
|
||||
use crate::js::vm::{VMExtern, VMMemory};
|
||||
use crate::js::{MemoryAccessError, MemoryType};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
@@ -65,7 +65,7 @@ extern "C" {
|
||||
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances>
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Memory {
|
||||
pub(crate) handle: StoreHandle<VMMemory>,
|
||||
pub(crate) handle: VMMemory,
|
||||
}
|
||||
|
||||
unsafe impl Send for Memory {}
|
||||
@@ -87,7 +87,7 @@ impl Memory {
|
||||
/// ```
|
||||
pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result<Self, MemoryError> {
|
||||
let vm_memory = VMMemory::new(Self::new_internal(ty.clone())?, ty);
|
||||
Ok(Self::from_vm_export(store, vm_memory))
|
||||
Ok(Self::from_vm_extern(store, vm_memory))
|
||||
}
|
||||
|
||||
pub(crate) fn new_internal(ty: MemoryType) -> Result<js_sys::WebAssembly::Memory, MemoryError> {
|
||||
@@ -115,19 +115,17 @@ impl Memory {
|
||||
ty: MemoryType,
|
||||
) -> Result<Self, MemoryError> {
|
||||
let vm_memory = VMMemory::new(js_memory, ty);
|
||||
let handle = StoreHandle::new(store.objects_mut(), vm_memory);
|
||||
Ok(Self::from_vm_extern(store, handle.internal_handle()))
|
||||
Ok(Self::from_vm_extern(store, vm_memory))
|
||||
}
|
||||
|
||||
/// Create a memory object from an existing memory and attaches it to the store
|
||||
pub fn new_from_existing(new_store: &mut impl AsStoreMut, memory: VMMemory) -> Self {
|
||||
let handle = StoreHandle::new(new_store.objects_mut(), memory);
|
||||
Self::from_vm_extern(new_store, handle.internal_handle())
|
||||
Self::from_vm_extern(new_store, memory)
|
||||
}
|
||||
|
||||
/// To `VMExtern`.
|
||||
pub(crate) fn to_vm_extern(&self) -> VMExtern {
|
||||
VMExtern::Memory(self.handle.internal_handle())
|
||||
VMExtern::Memory(self.handle.clone())
|
||||
}
|
||||
|
||||
/// Returns the [`MemoryType`] of the `Memory`.
|
||||
@@ -143,8 +141,8 @@ impl Memory {
|
||||
///
|
||||
/// assert_eq!(m.ty(), mt);
|
||||
/// ```
|
||||
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType {
|
||||
self.handle.get(store.as_store_ref().objects()).ty
|
||||
pub fn ty(&self, _store: &impl AsStoreRef) -> MemoryType {
|
||||
self.handle.ty
|
||||
}
|
||||
|
||||
/// Creates a view into the memory that then allows for
|
||||
@@ -192,7 +190,7 @@ impl Memory {
|
||||
IntoPages: Into<Pages>,
|
||||
{
|
||||
let pages = delta.into();
|
||||
let js_memory = &self.handle.get_mut(store.objects_mut()).memory;
|
||||
let js_memory = &self.handle.memory;
|
||||
let our_js_memory: &JSMemory = JsCast::unchecked_from_js_ref(js_memory);
|
||||
let new_pages = our_js_memory.grow(pages.0).map_err(|err| {
|
||||
if err.is_instance_of::<js_sys::RangeError>() {
|
||||
@@ -236,38 +234,23 @@ impl Memory {
|
||||
Ok(new_memory)
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_memory: VMMemory) -> Self {
|
||||
Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), vm_memory),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_extern(
|
||||
store: &mut impl AsStoreMut,
|
||||
internal: InternalStoreHandle<VMMemory>,
|
||||
) -> Self {
|
||||
Self {
|
||||
handle: unsafe {
|
||||
StoreHandle::from_internal(store.as_store_ref().objects().id(), internal)
|
||||
},
|
||||
}
|
||||
pub(crate) fn from_vm_extern(_store: &mut impl AsStoreMut, internal: VMMemory) -> Self {
|
||||
Self { handle: internal }
|
||||
}
|
||||
|
||||
/// Attempts to clone this memory (if its clonable)
|
||||
pub fn try_clone(&self, store: &impl AsStoreRef) -> Option<VMMemory> {
|
||||
let mem = self.handle.get(store.as_store_ref().objects());
|
||||
mem.try_clone()
|
||||
pub fn try_clone(&self, _store: &impl AsStoreRef) -> Option<VMMemory> {
|
||||
self.handle.try_clone()
|
||||
}
|
||||
|
||||
/// Checks whether this `Global` can be used with the given context.
|
||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||
self.handle.store_id() == store.as_store_ref().objects().id()
|
||||
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Copies this memory to a new memory
|
||||
pub fn duplicate(&mut self, store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
|
||||
let mem = self.handle.get(store.as_store_ref().objects());
|
||||
mem.duplicate()
|
||||
pub fn duplicate(&mut self, _store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
|
||||
self.handle.duplicate()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
5
lib/api/src/js/externals/memory_view.rs
vendored
5
lib/api/src/js/externals/memory_view.rs
vendored
@@ -26,9 +26,8 @@ pub struct MemoryView<'a> {
|
||||
}
|
||||
|
||||
impl<'a> MemoryView<'a> {
|
||||
pub(crate) fn new(memory: &Memory, store: &impl AsStoreRef) -> Self {
|
||||
let memory = memory.handle.get(store.as_store_ref().objects());
|
||||
Self::new_raw(&memory.memory)
|
||||
pub(crate) fn new(memory: &Memory, _store: &impl AsStoreRef) -> Self {
|
||||
Self::new_raw(&memory.handle.memory)
|
||||
}
|
||||
|
||||
pub(crate) fn new_raw(memory: &js_sys::WebAssembly::Memory) -> Self {
|
||||
|
||||
133
lib/api/src/js/externals/mod.rs
vendored
133
lib/api/src/js/externals/mod.rs
vendored
@@ -10,123 +10,13 @@ pub use self::memory::{Memory, MemoryError};
|
||||
pub use self::memory_view::MemoryView;
|
||||
pub use self::table::Table;
|
||||
|
||||
use crate::js::export::{Export, VMFunction, VMGlobal, VMMemory, VMTable};
|
||||
use crate::js::exports::{ExportError, Exportable};
|
||||
use crate::js::store::StoreObject;
|
||||
use crate::js::types::AsJs;
|
||||
|
||||
/*
|
||||
|
||||
|
||||
use crate::js::store::InternalStoreHandle;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::ExternType;
|
||||
use crate::js::types::AsJs;
|
||||
use crate::js::vm::VMExtern;
|
||||
use std::fmt;
|
||||
*/
|
||||
use crate::js::error::WasmError;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle};
|
||||
use crate::js::wasm_bindgen_polyfill::Global as JsGlobal;
|
||||
use js_sys::Function as JsFunction;
|
||||
use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable};
|
||||
use std::fmt;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use wasmer_types::ExternType;
|
||||
|
||||
/// The value of an export passed from one instance to another.
|
||||
pub enum VMExtern {
|
||||
/// A function export value.
|
||||
Function(InternalStoreHandle<VMFunction>),
|
||||
|
||||
/// A table export value.
|
||||
Table(InternalStoreHandle<VMTable>),
|
||||
|
||||
/// A memory export value.
|
||||
Memory(InternalStoreHandle<VMMemory>),
|
||||
|
||||
/// A global export value.
|
||||
Global(InternalStoreHandle<VMGlobal>),
|
||||
}
|
||||
|
||||
impl VMExtern {
|
||||
/// Return the export as a `JSValue`.
|
||||
pub fn as_jsvalue<'context>(&self, store: &'context impl AsStoreRef) -> &'context JsValue {
|
||||
match self {
|
||||
Self::Memory(js_wasm_memory) => js_wasm_memory
|
||||
.get(store.as_store_ref().objects())
|
||||
.memory
|
||||
.as_ref(),
|
||||
Self::Function(js_func) => js_func
|
||||
.get(store.as_store_ref().objects())
|
||||
.function
|
||||
.as_ref(),
|
||||
Self::Table(js_wasm_table) => js_wasm_table
|
||||
.get(store.as_store_ref().objects())
|
||||
.table
|
||||
.as_ref(),
|
||||
Self::Global(js_wasm_global) => js_wasm_global
|
||||
.get(store.as_store_ref().objects())
|
||||
.global
|
||||
.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a `JsValue` into an `Export` within a given `Context`.
|
||||
pub fn from_js_value(
|
||||
val: JsValue,
|
||||
store: &mut impl AsStoreMut,
|
||||
extern_type: ExternType,
|
||||
) -> Result<Self, WasmError> {
|
||||
match extern_type {
|
||||
ExternType::Memory(memory_type) => {
|
||||
if val.is_instance_of::<JsMemory>() {
|
||||
Ok(Self::Memory(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMMemory::new(val.unchecked_into::<JsMemory>(), memory_type),
|
||||
)))
|
||||
} else {
|
||||
Err(WasmError::TypeMismatch(
|
||||
val.js_typeof()
|
||||
.as_string()
|
||||
.map(Into::into)
|
||||
.unwrap_or("unknown".into()),
|
||||
"Memory".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
ExternType::Global(global_type) => {
|
||||
if val.is_instance_of::<JsGlobal>() {
|
||||
Ok(Self::Global(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMGlobal::new(val.unchecked_into::<JsGlobal>(), global_type),
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
}
|
||||
}
|
||||
ExternType::Function(function_type) => {
|
||||
if val.is_instance_of::<JsFunction>() {
|
||||
Ok(Self::Function(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMFunction::new(val.unchecked_into::<JsFunction>(), function_type),
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
}
|
||||
}
|
||||
ExternType::Table(table_type) => {
|
||||
if val.is_instance_of::<JsTable>() {
|
||||
Ok(Self::Table(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMTable::new(val.unchecked_into::<JsTable>(), table_type),
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An `Extern` is the runtime representation of an entity that
|
||||
/// can be imported or exported.
|
||||
///
|
||||
@@ -183,24 +73,15 @@ impl Extern {
|
||||
Self::Table(val) => val.is_from_store(store),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_export(&self) -> Export {
|
||||
match self {
|
||||
Self::Function(val) => Export::Function(val.handle.internal_handle()),
|
||||
Self::Memory(val) => Export::Memory(val.handle.internal_handle()),
|
||||
Self::Global(val) => Export::Global(val.handle.internal_handle()),
|
||||
Self::Table(val) => Export::Table(val.handle.internal_handle()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsJs for Extern {
|
||||
fn as_jsvalue(&self, store: &impl AsStoreRef) -> wasm_bindgen::JsValue {
|
||||
match self {
|
||||
Self::Function(_) => self.to_export().as_jsvalue(store),
|
||||
Self::Global(_) => self.to_export().as_jsvalue(store),
|
||||
Self::Table(_) => self.to_export().as_jsvalue(store),
|
||||
Self::Memory(_) => self.to_export().as_jsvalue(store),
|
||||
Self::Function(_) => self.to_vm_extern().as_jsvalue(store),
|
||||
Self::Global(_) => self.to_vm_extern().as_jsvalue(store),
|
||||
Self::Table(_) => self.to_vm_extern().as_jsvalue(store),
|
||||
Self::Memory(_) => self.to_vm_extern().as_jsvalue(store),
|
||||
}
|
||||
.clone()
|
||||
}
|
||||
@@ -213,8 +94,6 @@ impl<'a> Exportable<'a> for Extern {
|
||||
}
|
||||
}
|
||||
|
||||
impl StoreObject for Extern {}
|
||||
|
||||
impl fmt::Debug for Extern {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
|
||||
65
lib/api/src/js/externals/table.rs
vendored
65
lib/api/src/js/externals/table.rs
vendored
@@ -1,8 +1,8 @@
|
||||
use crate::js::export::{VMFunction, VMTable};
|
||||
use crate::js::exports::{ExportError, Exportable};
|
||||
use crate::js::externals::{Extern, VMExtern};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle};
|
||||
use crate::js::externals::Extern;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::value::Value;
|
||||
use crate::js::vm::{VMExtern, VMFunction, VMTable};
|
||||
use crate::js::RuntimeError;
|
||||
use crate::js::{FunctionType, TableType};
|
||||
use js_sys::Function;
|
||||
@@ -18,7 +18,7 @@ use js_sys::Function;
|
||||
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#table-instances>
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Table {
|
||||
pub(crate) handle: StoreHandle<VMTable>,
|
||||
pub(crate) handle: VMTable,
|
||||
}
|
||||
|
||||
fn set_table_item(table: &VMTable, item_index: u32, item: &Function) -> Result<(), RuntimeError> {
|
||||
@@ -30,12 +30,7 @@ fn get_function(store: &mut impl AsStoreMut, val: Value) -> Result<Function, Run
|
||||
return Err(RuntimeError::new("cannot pass Value across contexts"));
|
||||
}
|
||||
match val {
|
||||
Value::FuncRef(Some(ref func)) => Ok(func
|
||||
.handle
|
||||
.get(&store.as_store_ref().objects())
|
||||
.function
|
||||
.clone()
|
||||
.into()),
|
||||
Value::FuncRef(Some(ref func)) => Ok(func.handle.function.clone().into()),
|
||||
// Only funcrefs is supported by the spec atm
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
@@ -70,33 +65,25 @@ impl Table {
|
||||
set_table_item(&table, i, &func)?;
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
handle: StoreHandle::new(store.objects_mut(), table),
|
||||
})
|
||||
Ok(Self { handle: table })
|
||||
}
|
||||
|
||||
/// To `VMExtern`.
|
||||
pub fn to_vm_extern(&self) -> VMExtern {
|
||||
VMExtern::Table(self.handle.internal_handle())
|
||||
VMExtern::Table(self.handle.clone())
|
||||
}
|
||||
|
||||
/// Returns the [`TableType`] of the `Table`.
|
||||
pub fn ty(&self, store: &impl AsStoreRef) -> TableType {
|
||||
self.handle.get(store.as_store_ref().objects()).ty
|
||||
pub fn ty(&self, _store: &impl AsStoreRef) -> TableType {
|
||||
self.handle.ty
|
||||
}
|
||||
|
||||
/// Retrieves an element of the table at the provided `index`.
|
||||
pub fn get(&self, store: &mut impl AsStoreMut, index: u32) -> Option<Value> {
|
||||
if let Some(func) = self
|
||||
.handle
|
||||
.get(store.as_store_ref().objects())
|
||||
.table
|
||||
.get(index)
|
||||
.ok()
|
||||
{
|
||||
if let Some(func) = self.handle.table.get(index).ok() {
|
||||
let ty = FunctionType::new(vec![], vec![]);
|
||||
let vm_function = VMFunction::new(func, ty);
|
||||
let function = crate::js::externals::Function::from_vm_export(store, vm_function);
|
||||
let function = crate::js::externals::Function::from_vm_extern(store, vm_function);
|
||||
Some(Value::FuncRef(Some(function)))
|
||||
} else {
|
||||
None
|
||||
@@ -111,15 +98,12 @@ impl Table {
|
||||
val: Value,
|
||||
) -> Result<(), RuntimeError> {
|
||||
let item = get_function(store, val)?;
|
||||
set_table_item(self.handle.get_mut(store.objects_mut()), index, &item)
|
||||
set_table_item(&self.handle, index, &item)
|
||||
}
|
||||
|
||||
/// Retrieves the size of the `Table` (in elements)
|
||||
pub fn size(&self, store: &impl AsStoreRef) -> u32 {
|
||||
self.handle
|
||||
.get(store.as_store_ref().objects())
|
||||
.table
|
||||
.length()
|
||||
pub fn size(&self, _store: &impl AsStoreRef) -> u32 {
|
||||
self.handle.table.length()
|
||||
}
|
||||
|
||||
/// Grows the size of the `Table` by `delta`, initializating
|
||||
@@ -158,20 +142,13 @@ impl Table {
|
||||
unimplemented!("Table.copy is not natively supported in Javascript");
|
||||
}
|
||||
|
||||
pub(crate) fn from_vm_extern(
|
||||
store: &mut impl AsStoreMut,
|
||||
internal: InternalStoreHandle<VMTable>,
|
||||
) -> Self {
|
||||
Self {
|
||||
handle: unsafe {
|
||||
StoreHandle::from_internal(store.as_store_ref().objects().id(), internal)
|
||||
},
|
||||
}
|
||||
pub(crate) fn from_vm_extern(_store: &mut impl AsStoreMut, internal: VMTable) -> Self {
|
||||
Self { handle: internal }
|
||||
}
|
||||
|
||||
/// Checks whether this `Table` can be used with the given context.
|
||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||
self.handle.store_id() == store.as_store_ref().objects().id()
|
||||
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Get access to the backing VM value for this extern. This function is for
|
||||
@@ -183,10 +160,10 @@ impl Table {
|
||||
/// make breaking changes to it at any time or remove this method.
|
||||
#[doc(hidden)]
|
||||
pub unsafe fn get_vm_table<'context>(
|
||||
&self,
|
||||
store: &'context impl AsStoreRef,
|
||||
&'context self,
|
||||
_store: &'context impl AsStoreRef,
|
||||
) -> &'context VMTable {
|
||||
self.handle.get(store.as_store_ref().objects())
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::js::exports::Exports;
|
||||
use crate::js::module::Module;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::types::AsJs;
|
||||
use crate::js::vm::VMExtern;
|
||||
use crate::js::ExternType;
|
||||
use crate::Extern;
|
||||
use std::collections::HashMap;
|
||||
@@ -206,7 +207,6 @@ impl Imports {
|
||||
module: &Module,
|
||||
object: js_sys::Object,
|
||||
) -> Result<Self, WasmError> {
|
||||
use crate::js::externals::VMExtern;
|
||||
let module_imports: HashMap<(String, String), ExternType> = module
|
||||
.imports()
|
||||
.map(|import| {
|
||||
@@ -457,162 +457,4 @@ mod test {
|
||||
assert!(happy.is_some());
|
||||
assert!(small.is_some());
|
||||
}
|
||||
// fn namespace() {
|
||||
// let mut store = Store::default();
|
||||
// let g1 = Global::new(&store, Val::I32(0));
|
||||
// let namespace = namespace! {
|
||||
// "happy" => g1
|
||||
// };
|
||||
// let imports1 = imports! {
|
||||
// "dog" => namespace
|
||||
// };
|
||||
|
||||
// let happy_dog_entry = imports1.get_export("dog", "happy").unwrap();
|
||||
|
||||
// assert!(
|
||||
// if let Export::Global(happy_dog_global) = happy_dog_entry.to_export() {
|
||||
// happy_dog_global.ty.ty == Type::I32
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
|
||||
// fn imports_macro_allows_trailing_comma_and_none() {
|
||||
// use crate::js::Function;
|
||||
|
||||
// let mut store = Default::default();
|
||||
|
||||
// fn func(arg: i32) -> i32 {
|
||||
// arg + 1
|
||||
// }
|
||||
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func" => Function::new_typed(&store, func),
|
||||
// },
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func" => Function::new_typed(&store, func),
|
||||
// }
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func" => Function::new_typed(&store, func),
|
||||
// },
|
||||
// "abc" => {
|
||||
// "def" => Function::new_typed(&store, func),
|
||||
// }
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func" => Function::new_typed(&store, func)
|
||||
// },
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func" => Function::new_typed(&store, func)
|
||||
// }
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func1" => Function::new_typed(&store, func),
|
||||
// "func2" => Function::new_typed(&store, func)
|
||||
// }
|
||||
// };
|
||||
// let _ = imports! {
|
||||
// "env" => {
|
||||
// "func1" => Function::new_typed(&store, func),
|
||||
// "func2" => Function::new_typed(&store, func),
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
// fn chaining_works() {
|
||||
// let mut store = Store::default();
|
||||
// let g = Global::new(&store, Val::I32(0));
|
||||
|
||||
// let mut imports1 = imports! {
|
||||
// "dog" => {
|
||||
// "happy" => g.clone()
|
||||
// }
|
||||
// };
|
||||
|
||||
// let imports2 = imports! {
|
||||
// "dog" => {
|
||||
// "small" => g.clone()
|
||||
// },
|
||||
// "cat" => {
|
||||
// "small" => g.clone()
|
||||
// }
|
||||
// };
|
||||
|
||||
// imports1.extend(&imports2);
|
||||
|
||||
// let small_cat_export = imports1.get_export("cat", "small");
|
||||
// assert!(small_cat_export.is_some());
|
||||
|
||||
// let happy = imports1.get_export("dog", "happy");
|
||||
// let small = imports1.get_export("dog", "small");
|
||||
// assert!(happy.is_some());
|
||||
// assert!(small.is_some());
|
||||
// }
|
||||
|
||||
// fn extending_conflict_overwrites() {
|
||||
// let mut store = Store::default();
|
||||
// let g1 = Global::new(&store, Val::I32(0));
|
||||
// let g2 = Global::new(&store, Val::F32(0.));
|
||||
|
||||
// let mut imports1 = imports! {
|
||||
// "dog" => {
|
||||
// "happy" => g1,
|
||||
// },
|
||||
// };
|
||||
|
||||
// let imports2 = imports! {
|
||||
// "dog" => {
|
||||
// "happy" => g2,
|
||||
// },
|
||||
// };
|
||||
|
||||
// imports1.extend(&imports2);
|
||||
// let happy_dog_entry = imports1.get_export("dog", "happy").unwrap();
|
||||
|
||||
// assert!(
|
||||
// if let Export::Global(happy_dog_global) = happy_dog_entry.to_export() {
|
||||
// happy_dog_global.ty.ty == Type::F32
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// );
|
||||
|
||||
// // now test it in reverse
|
||||
// let mut store = Store::default();
|
||||
// let g1 = Global::new(&store, Val::I32(0));
|
||||
// let g2 = Global::new(&store, Val::F32(0.));
|
||||
|
||||
// let imports1 = imports! {
|
||||
// "dog" => {
|
||||
// "happy" => g1,
|
||||
// },
|
||||
// };
|
||||
|
||||
// let mut imports2 = imports! {
|
||||
// "dog" => {
|
||||
// "happy" => g2,
|
||||
// },
|
||||
// };
|
||||
|
||||
// imports2.extend(&imports1);
|
||||
// let happy_dog_entry = imports2.get_export("dog", "happy").unwrap();
|
||||
|
||||
// assert!(
|
||||
// if let Export::Global(happy_dog_global) = happy_dog_entry.to_export() {
|
||||
// happy_dog_global.ty.ty == Type::I32
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ use crate::js::exports::Exports;
|
||||
use crate::js::externals::Extern;
|
||||
use crate::js::imports::Imports;
|
||||
use crate::js::module::Module;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, StoreHandle};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::vm::{VMExtern, VMInstance};
|
||||
use js_sys::WebAssembly;
|
||||
use std::fmt;
|
||||
|
||||
@@ -17,7 +18,7 @@ use std::fmt;
|
||||
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
|
||||
#[derive(Clone)]
|
||||
pub struct Instance {
|
||||
_handle: StoreHandle<WebAssembly::Instance>,
|
||||
handle: VMInstance,
|
||||
module: Module,
|
||||
/// The exports for an instance.
|
||||
pub exports: Exports,
|
||||
@@ -62,14 +63,11 @@ impl Instance {
|
||||
module: &Module,
|
||||
imports: &Imports,
|
||||
) -> Result<Self, InstantiationError> {
|
||||
let (instance, externs) = module
|
||||
let instance = module
|
||||
.instantiate(&mut store, imports)
|
||||
.map_err(|e| InstantiationError::Start(e))?;
|
||||
|
||||
let instance = instance.get(store.objects_mut()).clone();
|
||||
let mut self_instance = Self::from_module_and_instance(store, module, instance)?;
|
||||
self_instance.ensure_memory_export(store, externs);
|
||||
//self_instance.init_envs(&imports.iter().map(Extern::to_export).collect::<Vec<_>>())?;
|
||||
let self_instance = Self::from_module_and_instance(store, module, instance)?;
|
||||
Ok(self_instance)
|
||||
}
|
||||
|
||||
@@ -109,8 +107,6 @@ impl Instance {
|
||||
module: &Module,
|
||||
instance: WebAssembly::Instance,
|
||||
) -> Result<Self, InstantiationError> {
|
||||
use crate::js::externals::VMExtern;
|
||||
|
||||
let instance_exports = instance.exports();
|
||||
|
||||
let exports = module
|
||||
@@ -131,29 +127,13 @@ impl Instance {
|
||||
})
|
||||
.collect::<Result<Exports, InstantiationError>>()?;
|
||||
|
||||
let handle = StoreHandle::new(store.as_store_mut().objects_mut(), instance);
|
||||
Ok(Self {
|
||||
_handle: handle,
|
||||
handle: instance,
|
||||
module: module.clone(),
|
||||
exports,
|
||||
})
|
||||
}
|
||||
|
||||
/// This will check the memory is correctly setup
|
||||
/// If the memory is imported then also export it for backwards compatibility reasons
|
||||
/// (many will assume the memory is always exported) - later we can remove this
|
||||
pub fn ensure_memory_export(&mut self, store: &mut impl AsStoreMut, externs: Vec<Extern>) {
|
||||
if self.exports.get_memory("memory").is_err() {
|
||||
if let Some(memory) = externs
|
||||
.iter()
|
||||
.filter(|a| a.ty(store).memory().is_some())
|
||||
.next()
|
||||
{
|
||||
self.exports.insert("memory", memory.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the [`Module`] associated with this instance.
|
||||
pub fn module(&self) -> &Module {
|
||||
&self.module
|
||||
@@ -162,10 +142,10 @@ impl Instance {
|
||||
/// Returns the inner WebAssembly Instance
|
||||
#[doc(hidden)]
|
||||
pub fn raw<'context>(
|
||||
&self,
|
||||
store: &'context impl AsStoreRef,
|
||||
&'context self,
|
||||
_store: &'context impl AsStoreRef,
|
||||
) -> &'context WebAssembly::Instance {
|
||||
&self._handle.get(store.as_store_ref().objects())
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ mod lib {
|
||||
}
|
||||
|
||||
pub(crate) mod error;
|
||||
mod export;
|
||||
mod exports;
|
||||
mod externals;
|
||||
mod function_env;
|
||||
@@ -41,10 +40,10 @@ mod store;
|
||||
mod trap;
|
||||
mod types;
|
||||
mod value;
|
||||
mod vm;
|
||||
mod wasm_bindgen_polyfill;
|
||||
|
||||
pub use crate::js::error::{DeserializeError, InstantiationError, SerializeError};
|
||||
pub use crate::js::export::Export;
|
||||
pub use crate::js::exports::{ExportError, Exportable, Exports, ExportsIterator};
|
||||
pub use crate::js::externals::{
|
||||
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView,
|
||||
@@ -61,7 +60,7 @@ pub use crate::js::ptr::{Memory32, Memory64, MemorySize, WasmPtr, WasmPtr64};
|
||||
pub use crate::js::trap::RuntimeError;
|
||||
|
||||
pub use crate::js::store::{
|
||||
AsStoreMut, AsStoreRef, Store, StoreHandle, StoreMut, StoreObject, StoreObjects, StoreRef,
|
||||
AsStoreMut, AsStoreRef, Store, StoreHandle, StoreMut, StoreObjects, StoreRef,
|
||||
};
|
||||
pub use crate::js::types::ValType as Type;
|
||||
pub use crate::js::types::{
|
||||
@@ -71,11 +70,6 @@ pub use crate::js::types::{
|
||||
pub use crate::js::value::Value;
|
||||
pub use crate::js::value::Value as Val;
|
||||
|
||||
pub mod vm {
|
||||
//! The `vm` module re-exports wasmer-vm types.
|
||||
pub use crate::js::export::VMMemory;
|
||||
}
|
||||
|
||||
pub use wasmer_types::is_wasm;
|
||||
// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
|
||||
pub use wasmer_types::{
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::js::externals::Extern;
|
||||
use crate::js::imports::Imports;
|
||||
use crate::js::store::AsStoreMut;
|
||||
use crate::js::types::{AsJs, ExportType, ImportType};
|
||||
use crate::js::vm::VMInstance;
|
||||
use crate::js::RuntimeError;
|
||||
use crate::AsStoreRef;
|
||||
use crate::IntoBytes;
|
||||
@@ -246,7 +247,7 @@ impl Module {
|
||||
&self,
|
||||
store: &mut impl AsStoreMut,
|
||||
imports: &Imports,
|
||||
) -> Result<(crate::StoreHandle<WebAssembly::Instance>, Vec<Extern>), RuntimeError> {
|
||||
) -> Result<VMInstance, RuntimeError> {
|
||||
// Ensure all imports come from the same store.
|
||||
if imports
|
||||
.into_iter()
|
||||
@@ -323,14 +324,8 @@ impl Module {
|
||||
// in case the import is not found, the JS Wasm VM will handle
|
||||
// the error for us, so we don't need to handle it
|
||||
}
|
||||
Ok((
|
||||
crate::StoreHandle::new(
|
||||
store.as_store_mut().objects_mut(),
|
||||
WebAssembly::Instance::new(&self.module, &imports_object)
|
||||
.map_err(|e: JsValue| -> RuntimeError { e.into() })?,
|
||||
),
|
||||
import_externs,
|
||||
))
|
||||
Ok(WebAssembly::Instance::new(&self.module, &imports_object)
|
||||
.map_err(|e: JsValue| -> RuntimeError { e.into() })?)
|
||||
}
|
||||
|
||||
/// Returns the name of the current module.
|
||||
|
||||
@@ -10,12 +10,12 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::js::externals::Function;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, StoreHandle};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::{FromToNativeWasmType, RuntimeError, WasmTypeList};
|
||||
// use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use crate::js::export::VMFunction;
|
||||
use crate::js::types::param_from_js;
|
||||
use crate::js::types::AsJs;
|
||||
use crate::js::vm::VMFunction;
|
||||
use js_sys::Array;
|
||||
use std::iter::FromIterator;
|
||||
use wasm_bindgen::JsValue;
|
||||
@@ -25,7 +25,7 @@ use wasmer_types::RawValue;
|
||||
/// (using the Native ABI).
|
||||
#[derive(Clone)]
|
||||
pub struct TypedFunction<Args = (), Rets = ()> {
|
||||
pub(crate) handle: StoreHandle<VMFunction>,
|
||||
pub(crate) handle: VMFunction,
|
||||
_phantom: PhantomData<(Args, Rets)>,
|
||||
}
|
||||
|
||||
@@ -38,9 +38,9 @@ where
|
||||
Rets: WasmTypeList,
|
||||
{
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn new<T>(store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self {
|
||||
pub(crate) fn new<T>(_store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self {
|
||||
Self {
|
||||
handle: StoreHandle::new(store.as_store_mut().objects_mut(), vm_function),
|
||||
handle: vm_function,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ macro_rules! impl_native_traits {
|
||||
let mut r;
|
||||
// TODO: This loop is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
|
||||
loop {
|
||||
r = self.handle.get(store.as_store_ref().objects()).function.apply(
|
||||
r = self.handle.function.apply(
|
||||
&JsValue::UNDEFINED,
|
||||
&Array::from_iter(params_list.iter())
|
||||
);
|
||||
|
||||
@@ -78,14 +78,6 @@ impl fmt::Debug for Store {
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait represinting any object that lives in the `Store`.
|
||||
pub trait StoreObject {
|
||||
/// Return true if the object `Store` is the same as the provided `Store`.
|
||||
fn comes_from_same_store(&self, _store: &Store) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl AsStoreRef for Store {
|
||||
fn as_store_ref(&self) -> StoreRef<'_> {
|
||||
StoreRef { inner: &self.inner }
|
||||
@@ -207,21 +199,17 @@ impl<T: AsStoreMut> AsStoreMut for &'_ mut T {
|
||||
}
|
||||
}
|
||||
|
||||
pub use objects::*;
|
||||
pub(crate) use objects::{InternalStoreHandle, StoreObject};
|
||||
pub use objects::{StoreHandle, StoreId, StoreObjects};
|
||||
|
||||
mod objects {
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
use crate::js::{
|
||||
export::{VMFunction, VMGlobal, VMMemory, VMTable},
|
||||
function_env::VMFunctionEnvironment,
|
||||
};
|
||||
use crate::js::{function_env::VMFunctionEnvironment, vm::VMGlobal};
|
||||
use std::{
|
||||
cell::UnsafeCell,
|
||||
fmt,
|
||||
marker::PhantomData,
|
||||
num::{NonZeroU64, NonZeroUsize},
|
||||
ptr::NonNull,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
};
|
||||
|
||||
@@ -266,11 +254,15 @@ mod objects {
|
||||
}
|
||||
|
||||
impl_store_object! {
|
||||
functions => VMFunction,
|
||||
tables => VMTable,
|
||||
// Note: we store the globals in order to be able to access them later via
|
||||
// `StoreObjects::iter_globals`.
|
||||
globals => VMGlobal,
|
||||
memories => VMMemory,
|
||||
instances => js_sys::WebAssembly::Instance,
|
||||
// functions => VMFunction,
|
||||
// tables => VMTable,
|
||||
// memories => VMMemory,
|
||||
// The function environments are the only things attached to a store,
|
||||
// since the other JS objects (table, globals, memory and functions)
|
||||
// live in the JS VM Store by default
|
||||
function_environments => VMFunctionEnvironment,
|
||||
}
|
||||
|
||||
@@ -278,11 +270,7 @@ mod objects {
|
||||
#[derive(Default)]
|
||||
pub struct StoreObjects {
|
||||
id: StoreId,
|
||||
memories: Vec<VMMemory>,
|
||||
tables: Vec<VMTable>,
|
||||
globals: Vec<VMGlobal>,
|
||||
functions: Vec<VMFunction>,
|
||||
instances: Vec<js_sys::WebAssembly::Instance>,
|
||||
function_environments: Vec<VMFunctionEnvironment>,
|
||||
}
|
||||
|
||||
@@ -482,28 +470,4 @@ mod objects {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Data used by the generated code is generally located inline within the
|
||||
/// `VMContext` for items defined in an instance. Host-defined objects are
|
||||
/// allocated separately and owned directly by the context.
|
||||
#[allow(dead_code)]
|
||||
pub enum MaybeInstanceOwned<T> {
|
||||
/// The data is owned here.
|
||||
Host(Box<UnsafeCell<T>>),
|
||||
|
||||
/// The data is stored inline in the `VMContext` of an instance.
|
||||
Instance(NonNull<T>),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl<T> MaybeInstanceOwned<T> {
|
||||
/// Returns underlying pointer to the VM data.
|
||||
#[allow(dead_code)]
|
||||
pub fn as_ptr(&self) -> NonNull<T> {
|
||||
match self {
|
||||
MaybeInstanceOwned::Host(p) => unsafe { NonNull::new_unchecked(p.get()) },
|
||||
MaybeInstanceOwned::Instance(p) => *p,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,19 +37,14 @@ pub fn param_from_js(ty: &ValType, js_val: &JsValue) -> Value {
|
||||
}
|
||||
|
||||
impl AsJs for Value {
|
||||
fn as_jsvalue(&self, store: &impl AsStoreRef) -> JsValue {
|
||||
fn as_jsvalue(&self, _store: &impl AsStoreRef) -> JsValue {
|
||||
match self {
|
||||
Self::I32(i) => JsValue::from_f64(*i as f64),
|
||||
Self::I64(i) => JsValue::from_f64(*i as f64),
|
||||
Self::F32(f) => JsValue::from_f64(*f as f64),
|
||||
Self::F64(f) => JsValue::from_f64(*f),
|
||||
Self::V128(f) => JsValue::from_f64(*f as f64),
|
||||
Self::FuncRef(Some(func)) => func
|
||||
.handle
|
||||
.get(store.as_store_ref().objects())
|
||||
.function
|
||||
.clone()
|
||||
.into(),
|
||||
Self::FuncRef(Some(func)) => func.handle.function.clone().into(),
|
||||
Self::FuncRef(None) => JsValue::null(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,19 +87,14 @@ impl Value {
|
||||
}
|
||||
|
||||
/// Converts the `Value` into a `f64`.
|
||||
pub fn as_raw(&self, store: &impl AsStoreRef) -> f64 {
|
||||
pub fn as_raw(&self, _store: &impl AsStoreRef) -> f64 {
|
||||
match *self {
|
||||
Self::I32(v) => v as f64,
|
||||
Self::I64(v) => v as f64,
|
||||
Self::F32(v) => v as f64,
|
||||
Self::F64(v) => v,
|
||||
Self::V128(v) => v as f64,
|
||||
Self::FuncRef(Some(ref f)) => f
|
||||
.handle
|
||||
.get(store.as_store_ref().objects())
|
||||
.function
|
||||
.as_f64()
|
||||
.unwrap_or(0_f64), //TODO is this correct?
|
||||
Self::FuncRef(Some(ref f)) => f.handle.function.as_f64().unwrap_or(0_f64), //TODO is this correct?
|
||||
|
||||
Self::FuncRef(None) => 0_f64,
|
||||
//Self::ExternRef(Some(ref e)) => unsafe { *e.address().0 } as .into_raw(),
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
/// This module is mainly used to create the `VM` types that will hold both
|
||||
/// the JS values of the `Memory`, `Table`, `Global` and `Function` and also
|
||||
/// it's types.
|
||||
/// This module should not be needed any longer (with the exception of the memory)
|
||||
/// once the type reflection is added to the WebAssembly JS API.
|
||||
/// https://github.com/WebAssembly/js-types/
|
||||
use crate::js::error::WasmError;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle};
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef};
|
||||
use crate::js::wasm_bindgen_polyfill::Global;
|
||||
use crate::js::wasm_bindgen_polyfill::Global as JsGlobal;
|
||||
use crate::MemoryView;
|
||||
use js_sys::Function;
|
||||
use js_sys::Function as JsFunction;
|
||||
use js_sys::WebAssembly;
|
||||
use js_sys::WebAssembly::{Memory, Table};
|
||||
use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
#[cfg(feature = "tracing")]
|
||||
@@ -162,56 +172,43 @@ impl fmt::Debug for VMFunction {
|
||||
}
|
||||
|
||||
/// The value of an export passed from one instance to another.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Export {
|
||||
pub enum VMExtern {
|
||||
/// A function export value.
|
||||
Function(InternalStoreHandle<VMFunction>),
|
||||
Function(VMFunction),
|
||||
|
||||
/// A table export value.
|
||||
Table(InternalStoreHandle<VMTable>),
|
||||
Table(VMTable),
|
||||
|
||||
/// A memory export value.
|
||||
Memory(InternalStoreHandle<VMMemory>),
|
||||
Memory(VMMemory),
|
||||
|
||||
/// A global export value.
|
||||
Global(InternalStoreHandle<VMGlobal>),
|
||||
Global(VMGlobal),
|
||||
}
|
||||
|
||||
impl Export {
|
||||
impl VMExtern {
|
||||
/// Return the export as a `JSValue`.
|
||||
pub fn as_jsvalue<'context>(&self, store: &'context impl AsStoreRef) -> &'context JsValue {
|
||||
pub fn as_jsvalue<'context>(&self, _store: &'context impl AsStoreRef) -> JsValue {
|
||||
match self {
|
||||
Self::Memory(js_wasm_memory) => js_wasm_memory
|
||||
.get(store.as_store_ref().objects())
|
||||
.memory
|
||||
.as_ref(),
|
||||
Self::Function(js_func) => js_func
|
||||
.get(store.as_store_ref().objects())
|
||||
.function
|
||||
.as_ref(),
|
||||
Self::Table(js_wasm_table) => js_wasm_table
|
||||
.get(store.as_store_ref().objects())
|
||||
.table
|
||||
.as_ref(),
|
||||
Self::Global(js_wasm_global) => js_wasm_global
|
||||
.get(store.as_store_ref().objects())
|
||||
.global
|
||||
.as_ref(),
|
||||
Self::Memory(js_wasm_memory) => js_wasm_memory.memory.clone().into(),
|
||||
Self::Function(js_func) => js_func.function.clone().into(),
|
||||
Self::Table(js_wasm_table) => js_wasm_table.table.clone().into(),
|
||||
Self::Global(js_wasm_global) => js_wasm_global.global.clone().into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a `JsValue` into an `Export` within a given `Context`.
|
||||
pub fn from_js_value(
|
||||
val: JsValue,
|
||||
store: &mut impl AsStoreMut,
|
||||
_store: &mut impl AsStoreMut,
|
||||
extern_type: ExternType,
|
||||
) -> Result<Self, WasmError> {
|
||||
match extern_type {
|
||||
ExternType::Memory(memory_type) => {
|
||||
if val.is_instance_of::<Memory>() {
|
||||
Ok(Self::Memory(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMMemory::new(val.unchecked_into::<Memory>(), memory_type),
|
||||
if val.is_instance_of::<JsMemory>() {
|
||||
Ok(Self::Memory(VMMemory::new(
|
||||
val.unchecked_into::<JsMemory>(),
|
||||
memory_type,
|
||||
)))
|
||||
} else {
|
||||
Err(WasmError::TypeMismatch(
|
||||
@@ -224,30 +221,30 @@ impl Export {
|
||||
}
|
||||
}
|
||||
ExternType::Global(global_type) => {
|
||||
if val.is_instance_of::<Global>() {
|
||||
Ok(Self::Global(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMGlobal::new(val.unchecked_into::<Global>(), global_type),
|
||||
if val.is_instance_of::<JsGlobal>() {
|
||||
Ok(Self::Global(VMGlobal::new(
|
||||
val.unchecked_into::<JsGlobal>(),
|
||||
global_type,
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
}
|
||||
}
|
||||
ExternType::Function(function_type) => {
|
||||
if val.is_instance_of::<Function>() {
|
||||
Ok(Self::Function(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMFunction::new(val.unchecked_into::<Function>(), function_type),
|
||||
if val.is_instance_of::<JsFunction>() {
|
||||
Ok(Self::Function(VMFunction::new(
|
||||
val.unchecked_into::<JsFunction>(),
|
||||
function_type,
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
}
|
||||
}
|
||||
ExternType::Table(table_type) => {
|
||||
if val.is_instance_of::<Table>() {
|
||||
Ok(Self::Table(InternalStoreHandle::new(
|
||||
&mut store.objects_mut(),
|
||||
VMTable::new(val.unchecked_into::<Table>(), table_type),
|
||||
if val.is_instance_of::<JsTable>() {
|
||||
Ok(Self::Table(VMTable::new(
|
||||
val.unchecked_into::<JsTable>(),
|
||||
table_type,
|
||||
)))
|
||||
} else {
|
||||
panic!("Extern type doesn't match js value type");
|
||||
@@ -256,3 +253,5 @@ impl Export {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type VMInstance = WebAssembly::Instance;
|
||||
@@ -368,6 +368,9 @@
|
||||
//! to compile it with [`wasm-pack`] and [`wasm-bindgen`]:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! use wasm_bindgen::prelude::*;
|
||||
//! use wasmer::{imports, Instance, Module, Store, Value};
|
||||
//!
|
||||
//! #[wasm_bindgen]
|
||||
//! pub extern fn do_add_one_in_wasmer() -> i32 {
|
||||
//! let module_wat = r#"
|
||||
@@ -382,10 +385,10 @@
|
||||
//! let module = Module::new(&store, &module_wat).unwrap();
|
||||
//! // The module doesn't import anything, so we create an empty import object.
|
||||
//! let import_object = imports! {};
|
||||
//! let instance = Instance::new(&module, &import_object).unwrap();
|
||||
//! let instance = Instance::new(&mut store, &module, &import_object).unwrap();
|
||||
//!
|
||||
//! let add_one = instance.exports.get_function("add_one").unwrap();
|
||||
//! let result = add_one.call(&[Value::I32(42)]).unwrap();
|
||||
//! let result = add_one.call(&mut store, &[Value::I32(42)]).unwrap();
|
||||
//! assert_eq!(result[0], Value::I32(43));
|
||||
//!
|
||||
//! result[0].unwrap_i32()
|
||||
|
||||
Reference in New Issue
Block a user