Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix differences between -sys and -js API #3129

Merged
merged 38 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1fb3fbe
Start fixing diff between -sys and -js API
fschutt Aug 23, 2022
fd80f77
Add u128 support to js/Value
fschutt Aug 23, 2022
b1fac30
Fix f64 -> RawValue in JS API
fschutt Aug 23, 2022
161aa62
More f64 -> RawValue conversion
fschutt Aug 23, 2022
34ddd28
Fix js::Value::from_raw
fschutt Aug 23, 2022
e09e706
Fix NativeWasmTypeInto::from_raw
fschutt Aug 23, 2022
e026812
Fix Function::new to not take self argument
fschutt Aug 23, 2022
59bc9a7
Revert f64 -> RawValue
fschutt Aug 23, 2022
c410d79
Add WasmSlice::is_empty
fschutt Aug 23, 2022
cd34245
Merge branch 'master' into fix-sync-js-sys-api
fschutt Aug 23, 2022
4540e3b
Add Module::custom_sections and FromToNativeWasmType::is_from_store
fschutt Aug 24, 2022
4966ec4
Always return true for is_from_store
fschutt Aug 24, 2022
11de772
Merge branch 'master' into fix-sync-js-sys-api
fschutt Aug 24, 2022
a52856d
Add LinkError to JS API
fschutt Aug 24, 2022
e589318
Added missing cases to InstantiationError
fschutt Aug 24, 2022
93abcce
Add Send + 'static bounds for Function::new_native_with_env
fschutt Aug 24, 2022
4e93a76
Add Instance::new_by_index function
fschutt Aug 24, 2022
55cea22
Add From<TypedFunction> for Function
fschutt Aug 24, 2022
66d293a
Fix Function::ty
fschutt Aug 24, 2022
88e06c4
Fix Table::copy signature
fschutt Aug 24, 2022
2fa85bd
Fix HostFunction trait
fschutt Aug 24, 2022
2e9a58c
Add to_vm_extern / from_vm_extern
fschutt Aug 24, 2022
355be4b
Reexport wasmparser from js
fschutt Aug 24, 2022
bafe1a9
Fix some compilation errors in make test-js
fschutt Aug 24, 2022
037ceca
Remove impl<Args, Rets> From<TypedFunction<Args, Rets>> for Function
fschutt Aug 24, 2022
e788bbc
Fix errors in "make lint"
fschutt Aug 24, 2022
e64882a
Fix error in conversion from u128 to f64 in JS bindings
fschutt Aug 24, 2022
63e2046
Return DefaultCustomSectionsIterator for custom_sections
fschutt Aug 25, 2022
42cbe14
Fix to_vm_extern function in JS
fschutt Aug 25, 2022
14ddcfd
Fix all errors up to the last one
fschutt Aug 26, 2022
2ee6614
cargo fmt && fix issue with function_body_ptr
fschutt Aug 26, 2022
eb91f1b
Merge branch 'master' into fix-sync-js-sys-api
fschutt Aug 26, 2022
329bfb2
Add to_jsvalue / from_jsvalue
fschutt Aug 29, 2022
f2b13f1
make test-js passes
fschutt Aug 29, 2022
68939d6
cargo fmt
fschutt Aug 29, 2022
cdf85a2
Update CHANGELOG
fschutt Aug 29, 2022
a5d0944
Merge branch 'master' into fix-sync-js-sys-api
fschutt Aug 31, 2022
3dc2e26
Fix merge errors
fschutt Aug 31, 2022
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
18 changes: 10 additions & 8 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn result_to_js(val: &Value) -> JsValue {
Value::I64(i) => JsValue::from_f64(*i as _),
Value::F32(f) => JsValue::from_f64(*f as _),
Value::F64(f) => JsValue::from_f64(*f),
Value::V128(f) => JsValue::from_f64(*f as _),
val => unimplemented!(
"The value `{:?}` is not yet supported in the JS Function API",
val
Expand Down Expand Up @@ -607,7 +608,7 @@ mod inner {
use std::marker::PhantomData;
use std::panic::{self, AssertUnwindSafe};

use wasmer_types::{FunctionType, NativeWasmType, Type};
use wasmer_types::{FunctionType, RawValue, NativeWasmType, Type};
fschutt marked this conversation as resolved.
Show resolved Hide resolved
// use wasmer::{raise_user_trap, resume_panic};

/// A trait to convert a Rust value to a `WasmNativeType` value,
Expand Down Expand Up @@ -757,7 +758,7 @@ mod inner {
/// # Safety
unsafe fn from_slice(
store: &mut impl AsStoreMut,
slice: &[f64],
slice: &[RawValue],
) -> Result<Self, TryFromSliceError>;

/// Builds and returns an array of type `Array` from a tuple
Expand All @@ -783,10 +784,10 @@ mod inner {
/// # Safety
unsafe fn into_c_struct(self, store: &mut impl AsStoreMut) -> Self::CStruct;

/// Writes the contents of a C struct to an array of `f64`.
/// Writes the contents of a C struct to an array of `RawValue`.
///
/// # Safety
unsafe fn write_c_struct_to_ptr(c_struct: Self::CStruct, ptr: *mut f64);
unsafe fn write_c_struct_to_ptr(c_struct: Self::CStruct, ptr: *mut RawValue);

/// Get the Wasm types for the tuple (list) of currently
/// represented values.
Expand Down Expand Up @@ -838,6 +839,7 @@ mod inner {
mod test_into_result {
use super::*;
use std::convert::Infallible;
use wasmer_types::RawValue;

#[test]
fn test_into_result_over_t() {
Expand Down Expand Up @@ -1013,7 +1015,7 @@ mod inner {
}

#[allow(clippy::missing_safety_doc)]
unsafe fn from_slice(store: &mut impl AsStoreMut, slice: &[f64]) -> Result<Self, TryFromSliceError> {
unsafe fn from_slice(store: &mut impl AsStoreMut, slice: &[RawValue]) -> Result<Self, TryFromSliceError> {
Ok(Self::from_array(store, slice.try_into()?))
}

Expand Down Expand Up @@ -1067,7 +1069,7 @@ mod inner {
}

#[allow(non_snake_case)]
unsafe fn write_c_struct_to_ptr(c_struct: Self::CStruct, _ptr: *mut f64) {
unsafe fn write_c_struct_to_ptr(c_struct: Self::CStruct, _ptr: *mut RawValue) {
// Unpack items of the tuple.
let $c_struct_name( $( $x ),* ) = c_struct;

Expand Down Expand Up @@ -1243,7 +1245,7 @@ mod inner {

unsafe fn from_slice(
_: &mut impl AsStoreMut,
_: &[f64],
_: &[RawValue],
) -> Result<Self, TryFromSliceError> {
unreachable!()
}
Expand All @@ -1264,7 +1266,7 @@ mod inner {
self
}

unsafe fn write_c_struct_to_ptr(_: Self::CStruct, _: *mut f64) {}
unsafe fn write_c_struct_to_ptr(_: Self::CStruct, _: *mut RawValue) {}

fn wasm_types() -> &'static [Type] {
&[]
Expand Down
6 changes: 6 additions & 0 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ impl Memory {
}
}

impl std::cmp::PartialEq for Memory {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl<'a> Exportable<'a> for Memory {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Expand Down
27 changes: 27 additions & 0 deletions lib/api/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,33 @@ impl Module {
Self::new(_store, bytes).map_err(|e| DeserializeError::Compiler(e))
}


#[cfg(feature = "compiler")]
/// Deserializes a a serialized Module located in a `Path` into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method.
///
/// # Safety
///
/// Please check [`Module::deserialize`].
///
/// # Usage
///
/// ```ignore
/// # use wasmer::*;
/// # let mut store = Store::default();
/// # fn main() -> anyhow::Result<()> {
/// let module = Module::deserialize_from_file(&store, path)?;
/// # Ok(())
/// # }
/// ```
pub unsafe fn deserialize_from_file(
store: &impl AsStoreRef,
path: impl AsRef<Path>,
) -> Result<Self, DeserializeError> {
let artifact = std::fs::read(path.as_ref())?;
Ok(Self::new(store, bytes).map_err(|e| DeserializeError::Compiler(e)))
}

/// Sets the name of the current module.
/// This is normally useful for stacktraces and debugging.
///
Expand Down
42 changes: 32 additions & 10 deletions lib/api/src/js/native_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module permits to create native functions
//! easily in Rust, thanks to its advanced typing system.

use wasmer_types::{NativeWasmType, Type};
use wasmer_types::{NativeWasmType, RawValue, Type};

use crate::js::Function;

Expand All @@ -23,7 +23,7 @@ pub trait NativeWasmTypeInto: NativeWasmType + Sized {
///
/// # Safety
///
unsafe fn from_raw(store: &mut impl AsStoreMut, raw: f64) -> Self;
unsafe fn from_raw(store: &mut impl AsStoreMut, raw: RawValue) -> Self;
}

impl NativeWasmTypeInto for i32 {
Expand All @@ -43,8 +43,8 @@ impl NativeWasmTypeInto for i32 {
}

#[inline]
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self {
raw as _
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self {
raw.i32
}
}

Expand All @@ -65,8 +65,8 @@ impl NativeWasmTypeInto for i64 {
}

#[inline]
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self {
raw as _
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self {
raw.i64
}
}

Expand All @@ -87,8 +87,8 @@ impl NativeWasmTypeInto for f32 {
}

#[inline]
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self {
raw as _
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self {
raw.f32
}
}

Expand All @@ -109,8 +109,30 @@ impl NativeWasmTypeInto for f64 {
}

#[inline]
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: f64) -> Self {
raw
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self {
raw.f64
}
}

impl NativeWasmTypeInto for u128 {
#[inline]
unsafe fn from_abi(_store: &mut impl AsStoreMut, abi: Self::Abi) -> Self {
abi
}

#[inline]
fn into_abi(self, _store: &mut impl AsStoreMut) -> Self::Abi {
self
}

#[inline]
fn into_raw(self, _store: &mut impl AsStoreMut) -> f64 {
self.into()
}

#[inline]
unsafe fn from_raw(_store: &mut impl AsStoreMut, raw: RawValue) -> Self {
raw.u128
}
}

Expand Down
40 changes: 32 additions & 8 deletions lib/api/src/js/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::convert::TryFrom;
use std::fmt;
use std::string::{String, ToString};

use wasmer_types::Type;
use wasmer_types::{Type, RawValue};

//use crate::ExternRef;
use crate::js::externals::function::Function;

use super::store::AsStoreRef;
use super::store::{AsStoreRef, AsStoreMut};

/// WebAssembly computations manipulate values of basic value types:
/// * Integers (32 or 64 bit width)
Expand Down Expand Up @@ -37,6 +37,9 @@ pub enum Value {

/// A first-class reference to a WebAssembly function.
FuncRef(Option<Function>),

/// A 128-bit number
V128(u128),
}

macro_rules! accessors {
Expand Down Expand Up @@ -76,6 +79,7 @@ impl Value {
Self::I64(_) => Type::I64,
Self::F32(_) => Type::F32,
Self::F64(_) => Type::F64,
Self::V128(_) => Type::V128,
//Self::ExternRef(_) => Type::ExternRef,
Self::FuncRef(_) => Type::FuncRef,
}
Expand All @@ -88,6 +92,7 @@ impl Value {
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())
Expand All @@ -105,14 +110,14 @@ impl Value {
///
/// # Safety
///
pub unsafe fn from_raw(_store: &impl AsStoreRef, ty: Type, raw: f64) -> Self {
pub unsafe fn from_raw(store: &mut impl AsStoreMut, ty: Type, raw: RawValue) -> Self {
match ty {
Type::I32 => Self::I32(raw as i32),
Type::I64 => Self::I64(raw as i64),
Type::F32 => Self::F32(raw as f32),
Type::F64 => Self::F64(raw),
Type::I32 => Self::I32(raw.i32),
Type::I64 => Self::I64(raw.i64),
Type::F32 => Self::F32(raw.f32),
Type::F64 => Self::F64(raw.f64),
Type::V128 => Self::V128(raw.u128),
Type::FuncRef => todo!(),
Type::V128 => todo!(),
Type::ExternRef => todo!(),
//Self::ExternRef(
//{
Expand All @@ -134,6 +139,7 @@ impl Value {
| Self::I64(_)
| Self::F32(_)
| Self::F64(_)
| Self::V128(_)
//| Self::ExternRef(None)
| Self::FuncRef(None) => true,
//Self::ExternRef(Some(e)) => e.is_from_store(store),
Expand All @@ -147,6 +153,7 @@ impl Value {
(I64(i64) i64 unwrap_i64 *e)
(F32(f32) f32 unwrap_f32 *e)
(F64(f64) f64 unwrap_f64 *e)
(V128(u128) v128 unwrap_v128 *e)
//(ExternRef(&Option<ExternRef>) externref unwrap_externref e)
(FuncRef(&Option<Function>) funcref unwrap_funcref e)
}
Expand All @@ -159,6 +166,7 @@ impl fmt::Debug for Value {
Self::I64(v) => write!(f, "I64({:?})", v),
Self::F32(v) => write!(f, "F32({:?})", v),
Self::F64(v) => write!(f, "F64({:?})", v),
Self::V128(v) => write!(f, "V128({:?})", v),
//Self::ExternRef(None) => write!(f, "Null ExternRef"),
//Self::ExternRef(Some(v)) => write!(f, "ExternRef({:?})", v),
Self::FuncRef(None) => write!(f, "Null FuncRef"),
Expand All @@ -174,12 +182,19 @@ impl ToString for Value {
Self::I64(v) => v.to_string(),
Self::F32(v) => v.to_string(),
Self::F64(v) => v.to_string(),
Self::V128(v) => v.to_string(),
//Self::ExternRef(_) => "externref".to_string(),
Self::FuncRef(_) => "funcref".to_string(),
}
}
}

impl From<u128> for Value {
fn from(val: u128) -> Self {
Self::V128(val)
}
}

impl From<i32> for Value {
fn from(val: i32) -> Self {
Self::I32(val)
Expand Down Expand Up @@ -246,9 +261,18 @@ const NOT_I32: &str = "Value is not of Wasm type i32";
const NOT_I64: &str = "Value is not of Wasm type i64";
const NOT_F32: &str = "Value is not of Wasm type f32";
const NOT_F64: &str = "Value is not of Wasm type f64";
const NOT_V128: &str = "Value is not of Wasm type u128";
const NOT_FUNCREF: &str = "Value is not of Wasm type funcref";
//const NOT_EXTERNREF: &str = "Value is not of Wasm type externref";

impl TryFrom<Value> for u128 {
type Error = &'static str;

fn try_from(value: Value) -> Result<Self, Self::Error> {
value.v128().ok_or(NOT_V128)
}
}

impl TryFrom<Value> for i32 {
type Error = &'static str;

Expand Down