Skip to content

Commit

Permalink
Remove deprecated Export type. Updated structure to use VMExtern
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jan 26, 2023
1 parent d59f4c2 commit 6707e67
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 342 deletions.
23 changes: 9 additions & 14 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
@@ -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, 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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -321,7 +322,7 @@ 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 {
pub fn ty(&self, _store: &impl AsStoreRef) -> FunctionType {
self.handle.ty.clone()
}

Expand Down Expand Up @@ -465,13 +466,7 @@ impl Function {
}
}

pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_function: VMFunction) -> Self {
Self {
handle: vm_function,
}
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, internal: VMFunction) -> Self {
pub(crate) fn from_vm_extern(_store: &mut impl AsStoreMut, internal: VMFunction) -> Self {
Self { handle: internal }
}

Expand Down Expand Up @@ -609,7 +604,7 @@ impl Function {
}

/// Checks whether this `Function` can be used with the given context.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true
}
}
Expand Down
16 changes: 6 additions & 10 deletions lib/api/src/js/externals/global.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::js::export::VMGlobal;
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::{Extern, VMExtern};
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;
Expand Down Expand Up @@ -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`.
Expand All @@ -112,7 +112,7 @@ 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 {
pub fn ty(&self, _store: &impl AsStoreRef) -> GlobalType {
self.handle.ty
}

Expand Down Expand Up @@ -205,18 +205,14 @@ impl Global {
Ok(())
}

pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_global: VMGlobal) -> Self {
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 }
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, internal: VMGlobal) -> Self {
Self { handle: internal }
}

/// Checks whether this `Global` can be used with the given store.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true
}
}
Expand Down
20 changes: 8 additions & 12 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::js::export::VMMemory;
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::{Extern, VMExtern};
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;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Memory {
///
/// assert_eq!(m.ty(), mt);
/// ```
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType {
pub fn ty(&self, _store: &impl AsStoreRef) -> MemoryType {
self.handle.ty
}

Expand Down Expand Up @@ -234,26 +234,22 @@ impl Memory {
Ok(new_memory)
}

pub(crate) fn from_vm_export(store: &mut impl AsStoreMut, vm_memory: VMMemory) -> Self {
Self { handle: vm_memory }
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, internal: VMMemory) -> Self {
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> {
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 {
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> {
pub fn duplicate(&mut self, _store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
self.handle.duplicate()
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/js/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct MemoryView<'a> {
}

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, store: &impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, _store: &impl AsStoreRef) -> Self {
Self::new_raw(&memory.handle.memory)
}

Expand Down
108 changes: 5 additions & 103 deletions lib/api/src/js/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,102 +10,13 @@ pub use self::memory::{Memory, MemoryError};
pub use self::memory_view::MemoryView;
pub use self::table::Table;

use crate::js::error::WasmError;
use crate::js::export::{Export, VMFunction, VMGlobal, VMMemory, VMTable};
use crate::js::exports::{ExportError, Exportable};
use crate::js::store::StoreObject;
use crate::js::store::{AsStoreMut, AsStoreRef};
use crate::js::types::AsJs;
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 crate::js::vm::VMExtern;
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(VMFunction),

/// A table export value.
Table(VMTable),

/// A memory export value.
Memory(VMMemory),

/// A global export value.
Global(VMGlobal),
}

impl VMExtern {
/// Return the export as a `JSValue`.
pub fn as_jsvalue<'context>(&self, store: &'context impl AsStoreRef) -> JsValue {
match self {
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,
extern_type: ExternType,
) -> Result<Self, WasmError> {
match extern_type {
ExternType::Memory(memory_type) => {
if val.is_instance_of::<JsMemory>() {
Ok(Self::Memory(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(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(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(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.
///
Expand Down Expand Up @@ -162,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.clone()),
Self::Memory(val) => Export::Memory(val.handle.clone()),
Self::Global(val) => Export::Global(val.handle.clone()),
Self::Table(val) => Export::Table(val.handle.clone()),
}
}
}

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()
}
Expand Down
16 changes: 8 additions & 8 deletions lib/api/src/js/externals/table.rs
Original file line number Diff line number Diff line change
@@ -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::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;
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Table {
}

/// Returns the [`TableType`] of the `Table`.
pub fn ty(&self, store: &impl AsStoreRef) -> TableType {
pub fn ty(&self, _store: &impl AsStoreRef) -> TableType {
self.handle.ty
}

Expand All @@ -83,7 +83,7 @@ impl Table {
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
Expand All @@ -102,7 +102,7 @@ impl Table {
}

/// Retrieves the size of the `Table` (in elements)
pub fn size(&self, store: &impl AsStoreRef) -> u32 {
pub fn size(&self, _store: &impl AsStoreRef) -> u32 {
self.handle.table.length()
}

Expand Down Expand Up @@ -142,12 +142,12 @@ impl Table {
unimplemented!("Table.copy is not natively supported in Javascript");
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, internal: VMTable) -> Self {
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 {
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true
}

Expand All @@ -161,7 +161,7 @@ impl Table {
#[doc(hidden)]
pub unsafe fn get_vm_table<'context>(
&'context self,
store: &'context impl AsStoreRef,
_store: &'context impl AsStoreRef,
) -> &'context VMTable {
&self.handle
}
Expand Down
Loading

0 comments on commit 6707e67

Please sign in to comment.