Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ macro-wasmer-universal-test = { version = "6.0.0-beta.1", path = "./macro-wasmer
wasmer-types = { path = "../types", version = "=6.0.0-beta.1", default-features = false, features = [
"std",
] }
wasm-bindgen = "0.2.74"
js-sys = "0.3.51"
wasm-bindgen = "0.2.100"
js-sys = "0.3.77"
wasmer-derive = { path = "../derive", version = "=6.0.0-beta.1" }
wasmer-compiler = { path = "../compiler", version = "=6.0.0-beta.1" }
# - Optional dependencies for `js`.
wasmparser = { workspace = true, default-features = false, optional = true }
hashbrown = { version = "0.11", optional = true }
serde-wasm-bindgen = { version = "0.4.5" }
serde-wasm-bindgen = { version = "0.6.5" }
serde = { version = "1.0", features = ["derive"] }
target-lexicon = { workspace = true }

Expand Down
5 changes: 3 additions & 2 deletions lib/api/src/backend/js/entities/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use wasm_bindgen::{prelude::*, JsValue};
use wasmer_types::{GlobalType, Mutability, RawValue, Type};

use crate::{
js::{utils::polyfill::Global as JsGlobal, vm::VMGlobal},
js::vm::VMGlobal,
vm::{VMExtern, VMExternGlobal},
AsStoreMut, AsStoreRef, BackendGlobal, RuntimeError, Value,
};
use js_sys::WebAssembly;

#[derive(Debug, Clone, PartialEq, Eq)]
/// A WebAssembly `global` in `js`.
Expand Down Expand Up @@ -54,7 +55,7 @@ impl Global {
&mutability.is_mutable().into(),
)?;

let js_global = JsGlobal::new(&descriptor, &value).unwrap();
let js_global = WebAssembly::Global::new(&descriptor, &value).unwrap();
let vm_global = VMGlobal::new(js_global, global_ty);

Ok(Self::from_vm_extern(store, VMExternGlobal::Js(vm_global)))
Expand Down
9 changes: 7 additions & 2 deletions lib/api/src/backend/js/entities/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ impl Module {
binary: &[u8],
) -> Result<Self, CompileError> {
let js_bytes = Uint8Array::view(binary);
let module = WebAssembly::Module::new(&js_bytes.into())
.map_err(|e| CompileError::Validate(format!("{}", e.as_string().unwrap())))?;
let module = WebAssembly::Module::new(&js_bytes.into()).map_err(|e| {
CompileError::Validate(format!(
"{}",
e.as_string()
.unwrap_or("Unknown validation error".to_string())
))
})?;
Ok(Self::from_js_module(module, binary))
}

Expand Down
57 changes: 51 additions & 6 deletions lib/api/src/backend/js/entities/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use wasmer_types::{TagType, Type};
use crate::{
js::vm::VMTag,
vm::{VMExtern, VMExternTag},
AsStoreMut, AsStoreRef,
AsStoreMut, AsStoreRef, BackendTag, TagKind,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -21,22 +21,67 @@ unsafe impl Sync for Tag {}

impl Tag {
pub fn new<P: Into<Box<[Type]>>>(store: &mut impl AsStoreMut, params: P) -> Self {
panic!("EH not supported yet!")
let descriptor = js_sys::Object::new();
let params: Box<[Type]> = params.into();
let parameters: Vec<String> = params
.into_iter()
.map(|param| match param {
Type::I32 => "i32".to_string(),
Type::I64 => "i64".to_string(),
Type::F32 => "f32".to_string(),
Type::F64 => "f64".to_string(),
_ => unimplemented!("The type is not yet supported in the JS Global API"),
})
.collect();
js_sys::Reflect::set(&descriptor, &"parameters".into(), &parameters.into()).unwrap();

let tag = js_sys::WebAssembly::Tag::new(&descriptor);
let ty = TagType::new(TagKind::Exception, params);
let handle = VMTag::new(tag.unwrap(), ty);
Self { handle }
}

pub fn ty(&self, store: &impl AsStoreRef) -> TagType {
panic!("EH not supported yet!")
self.handle.ty.clone()
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternTag) -> Self {
panic!("EH not supported yet!")
Self {
handle: vm_extern.into_js(),
}
}

pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
panic!("EH not supported yet!")
true
}

pub(crate) fn to_vm_extern(&self) -> VMExtern {
panic!("EH not supported yet!")
VMExtern::Js(crate::js::vm::VMExtern::Tag(self.handle.clone()))
}
}

impl crate::Tag {
/// Consume [`self`] into [`crate::backend::js::tag::Tag`].
pub fn into_js(self) -> crate::backend::js::tag::Tag {
match self.0 {
BackendTag::Js(s) => s,
_ => panic!("Not a `js` tag!"),
}
}

/// Convert a reference to [`self`] into a reference [`crate::backend::js::tag::Tag`].
pub fn as_js(&self) -> &crate::backend::js::tag::Tag {
match self.0 {
BackendTag::Js(ref s) => s,
_ => panic!("Not a `js` tag!"),
}
}

/// Convert a mutable reference to [`self`] into a mutable reference [`crate::backend::js::tag::Tag`].
pub fn as_js_mut(&mut self) -> &mut crate::backend::js::tag::Tag {
match self.0 {
BackendTag::Js(ref mut s) => s,
_ => panic!("Not a `js` tag!"),
}
}
}
41 changes: 33 additions & 8 deletions lib/api/src/backend/js/utils/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, convert::TryInto};

use js_sys::{
Function as JsFunction,
WebAssembly::{Memory as JsMemory, Table as JsTable},
WebAssembly::{Global as JsGlobal, Memory as JsMemory, Table as JsTable, Tag as JsTag},
};
use wasm_bindgen::{JsCast, JsError, JsValue};
use wasmer_types::ExternType;
Expand All @@ -12,12 +12,11 @@ use crate::{
instance::Instance,
js::{
instance::Instance as JsInstance,
utils::polyfill::Global as JsGlobal,
vm::{VMFunction, VMGlobal, VMMemory, VMTable},
vm::{VMFunction, VMGlobal, VMMemory, VMTable, VMTag},
},
store::{AsStoreMut, AsStoreRef},
value::Value,
Extern, Function, Global, Memory, Table, Type,
Extern, Function, Global, Memory, Table, Tag, Type,
};

/// Convert the given type to a [`JsValue`].
Expand Down Expand Up @@ -193,7 +192,7 @@ impl AsJs for Extern {
Self::Function(function) => function.as_js().handle.function.clone().into(),
Self::Table(table) => table.as_js().handle.table.clone().into(),
Self::Global(global) => global.as_js().handle.global.clone().into(),
Self::Tag(_) => unimplemented!("Tags are not yet supported in the JS Function API"),
Self::Tag(tag) => tag.as_js().handle.tag.clone().into(),
}
}

Expand All @@ -219,9 +218,7 @@ impl AsJs for Extern {
ExternType::Table(table_type) => {
Ok(Self::Table(Table::from_jsvalue(store, table_type, val)?))
}
ExternType::Tag(_) => {
unimplemented!("Tags are not yet supported in the JS Function API")
}
ExternType::Tag(tag_type) => Ok(Self::Tag(Tag::from_jsvalue(store, tag_type, val)?)),
}
}
}
Expand Down Expand Up @@ -303,6 +300,34 @@ impl AsJs for Function {
}
}

impl AsJs for Tag {
type DefinitionType = crate::TagType;
fn as_jsvalue(&self, _store: &impl AsStoreRef) -> wasm_bindgen::JsValue {
self.as_js().handle.tag.clone().into()
}

fn from_jsvalue(
store: &mut impl AsStoreMut,
tag: &Self::DefinitionType,
value: &JsValue,
) -> Result<Self, JsError> {
if value.is_instance_of::<JsTag>() {
Ok(Tag::from_vm_extern(
store,
crate::vm::VMExternTag::Js(VMTag::new(
value.clone().unchecked_into::<JsTag>(),
tag.clone(),
)),
))
} else {
Err(JsError::new(&format!(
"Extern expect to be of type Tag, but received {:?}",
value
)))
}
}
}

impl AsJs for Global {
type DefinitionType = crate::GlobalType;

Expand Down
1 change: 0 additions & 1 deletion lib/api/src/backend/js/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub(crate) mod convert;
pub(crate) mod js_handle;
pub(crate) mod polyfill;

pub use convert::*;
pub use js_handle::*;
33 changes: 0 additions & 33 deletions lib/api/src/backend/js/utils/polyfill.rs

This file was deleted.

9 changes: 8 additions & 1 deletion lib/api/src/backend/js/vm/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasmer_types::RawValue;

use crate::{AsStoreMut, Extern, VMExternToExtern};

use super::{function::VMFunction, global::VMGlobal, memory::VMMemory, table::VMTable};
use super::{function::VMFunction, global::VMGlobal, memory::VMMemory, table::VMTable, tag::VMTag};

/// The value of an export passed from one instance to another in the `js` VM.
pub enum VMExtern {
Expand All @@ -17,6 +17,9 @@ pub enum VMExtern {

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

/// A tag export value.
Tag(VMTag),
}

impl VMExternToExtern for VMExtern {
Expand All @@ -38,6 +41,10 @@ impl VMExternToExtern for VMExtern {
store,
crate::vm::VMExternTable::Js(t),
)),
Self::Tag(t) => Extern::Tag(crate::Tag::from_vm_extern(
store,
crate::vm::VMExternTag::Js(t),
)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/backend/js/vm/global.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::backend::js::utils::polyfill::Global as JsGlobal;
use js_sys::WebAssembly::Global as JsGlobal;
use wasmer_types::GlobalType;

/// The VM Global type
Expand Down
5 changes: 3 additions & 2 deletions lib/api/src/backend/js/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ pub(crate) mod function;
pub(crate) mod global;
pub(crate) mod memory;
pub(crate) mod table;
pub(crate) mod tag;
pub use super::error::Trap;

pub use external::*;
pub use function::*;
pub use global::*;
pub use memory::*;
pub use table::*;
pub use tag::*;

/// The type of instances in the `js` VM.
pub type VMInstance = js_sys::WebAssembly::Instance;
Expand All @@ -27,8 +29,7 @@ pub(crate) type VMExternFunction = VMFunction;

// No EH for now.
pub(crate) type VMException = ();
pub(crate) type VMTag = ();
pub(crate) type VMExternTag = ();
pub(crate) type VMExternTag = VMTag;

pub struct VMExceptionRef;
impl VMExceptionRef {
Expand Down
18 changes: 18 additions & 0 deletions lib/api/src/backend/js/vm/tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use js_sys::WebAssembly::Tag as JsTag;
use wasmer_types::TagType;

/// The VM Tag type
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VMTag {
pub(crate) tag: JsTag,
pub(crate) ty: TagType,
}

impl VMTag {
pub(crate) fn new(tag: JsTag, ty: TagType) -> Self {
Self { tag, ty }
}
}

unsafe impl Send for VMTag {}
unsafe impl Sync for VMTag {}
5 changes: 3 additions & 2 deletions lib/api/src/backend/sys/entities/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ impl Tag {
pub fn ty(&self, store: &impl AsStoreRef) -> TagType {
TagType {
kind: wasmer_types::TagKind::Exception,
ty: self
params: self
.handle
.get(store.as_store_ref().objects().as_sys())
.signature
.clone(),
.params()
.into(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/backend/v8/entities/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Tag {
res
};

TagType::new(wasmer_types::TagKind::Exception, params, vec![])
TagType::new(wasmer_types::TagKind::Exception, params)
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_tag: VMExternTag) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ pub use wasmer_types::{
is_wasm, Bytes, CompileError, DeserializeError, ExportIndex, ExportType, ExternType, FrameInfo,
FunctionType, GlobalInit, GlobalType, ImportType, LocalFunctionIndex, MemoryError, MemoryStyle,
MemoryType, Mutability, OnCalledAction, Pages, ParseCpuFeatureError, SerializeError,
TableStyle, TableType, Type, ValueType, WasmError, WasmResult, WASM_MAX_PAGES, WASM_MIN_PAGES,
WASM_PAGE_SIZE,
TableStyle, TableType, TagKind, TagType, Type, ValueType, WasmError, WasmResult,
WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
};

#[cfg(feature = "wasmparser")]
Expand Down
Loading
Loading