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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C

### Changed
- #[3131](https://github.com/wasmerio/wasmer/pull/3131) Update migration docs for MemoryView changes
- #[3129](https://github.com/wasmerio/wasmer/pull/3129) Fix differences between -sys and -js API

### Fixed
- #[3130](https://github.com/wasmerio/wasmer/pull/3130) Remove panics from Artifact::deserialize
Expand Down
36 changes: 34 additions & 2 deletions lib/api/src/js/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::js::trap::RuntimeError;
use std::borrow::Cow;
#[cfg(feature = "std")]
use thiserror::Error;
use wasmer_types::ImportError;

// Compilation Errors
//
Expand Down Expand Up @@ -144,6 +145,28 @@ pub enum DeserializeError {
Compiler(CompileError),
}

/// The WebAssembly.LinkError object indicates an error during
/// module instantiation (besides traps from the start function).
///
/// This is based on the [link error][link-error] API.
///
/// [link-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError
#[derive(Error, Debug)]
#[error("Link error: {0}")]
pub enum LinkError {
/// An error occurred when checking the import types.
#[error("Error while importing {0:?}.{1:?}: {2}")]
Import(String, String, ImportError),

#[cfg(not(target_arch = "wasm32"))]
/// A trap ocurred during linking.
#[error("RuntimeError occurred during linking: {0}")]
Trap(#[source] RuntimeError),
/// Insufficient resources available for linking.
#[error("Insufficient resources: {0}")]
Resource(String),
}

/// An error while instantiating a module.
///
/// This is not a common WebAssembly error, however
Expand All @@ -156,13 +179,18 @@ pub enum DeserializeError {
#[cfg_attr(feature = "std", derive(Error))]
pub enum InstantiationError {
/// A linking ocurred during instantiation.
#[cfg_attr(feature = "std", error("Link error: {0}"))]
Link(String),
#[cfg_attr(feature = "std", error(transparent))]
Link(LinkError),

/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),

/// The module was compiled with a CPU feature that is not available on
/// the current host.
#[cfg_attr(feature = "std", error("missing required CPU features: {0:?}"))]
CpuFeature(String),

/// Import from a different [`Store`].
/// This error occurs when an import from a different store is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))]
Expand All @@ -171,6 +199,10 @@ pub enum InstantiationError {
/// A generic error occured while invoking API functions
#[cfg_attr(feature = "std", error(transparent))]
Wasm(WasmError),

/// Insufficient resources available for execution.
#[cfg_attr(feature = "std", error("Can't get {0} from the instance exports"))]
NotInExports(String),
}

impl From<WasmError> for InstantiationError {
Expand Down
42 changes: 31 additions & 11 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::Extern;
use crate::js::externals::{Extern, VMExtern};
use crate::js::function_env::FunctionEnvMut;
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle, StoreMut};
use crate::js::types::{param_from_js, AsJs}; /* ValFuncRef */
Expand All @@ -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 @@ -77,6 +78,11 @@ impl Function {
Self::new_with_env(store, &env, ty, wrapped_func)
}

/// To `VMExtern`.
pub fn to_vm_extern(&self) -> VMExtern {
VMExtern::Function(self.handle.internal_handle())
}

/// Creates a new host `Function` (dynamic) with the provided signature.
///
/// If you know the signature of the host function at compile time,
Expand Down Expand Up @@ -229,13 +235,13 @@ impl Function {
note = "new_native_with_env() has been renamed to new_typed_with_env()."
)]
/// Creates a new host `Function` with an environment from a typed function.
pub fn new_native_with_env<T, F, Args, Rets>(
pub fn new_native_with_env<T: Send + 'static, F, Args, Rets>(
store: &mut impl AsStoreMut,
env: &FunctionEnv<T>,
func: F,
) -> Self
where
F: HostFunction<T, Args, Rets, WithEnv>,
F: HostFunction<T, Args, Rets, WithEnv> + 'static + Send + Sync,
Args: WasmTypeList,
Rets: WasmTypeList,
{
Expand Down Expand Up @@ -309,8 +315,8 @@ impl Function {
/// assert_eq!(f.ty().params(), vec![Type::I32, Type::I32]);
/// assert_eq!(f.ty().results(), vec![Type::I32]);
/// ```
pub fn ty<'context>(&self, store: &'context impl AsStoreRef) -> &'context FunctionType {
&self.handle.get(store.as_store_ref().objects()).ty
pub fn ty(&self, store: &impl AsStoreRef) -> FunctionType {
self.handle.get(store.as_store_ref().objects()).ty.clone()
}

/// Returns the number of parameters that this function takes.
Expand Down Expand Up @@ -598,7 +604,7 @@ mod inner {
use super::RuntimeError;
use super::VMFunctionBody;
use crate::js::function_env::{FunctionEnvMut, VMFunctionEnvironment};
use crate::js::store::{AsStoreMut, InternalStoreHandle, StoreHandle, StoreMut};
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle, StoreMut};
use crate::js::FunctionEnv;
use crate::js::NativeWasmTypeInto;
use std::array::TryFromSliceError;
Expand Down Expand Up @@ -639,6 +645,9 @@ mod inner {
/// This method panics if `self` cannot fit in the
/// `Self::Native` type.
fn to_native(self) -> Self::Native;

/// Returns whether this native type belongs to the given store
fn is_from_store(&self, _store: &impl AsStoreRef) -> bool;
}

macro_rules! from_to_native_wasm_type {
Expand All @@ -657,6 +666,11 @@ mod inner {
fn to_native(self) -> Self::Native {
self as Self::Native
}

#[inline]
fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true // Javascript only has one store
}
}
)*
};
Expand All @@ -678,6 +692,11 @@ mod inner {
fn to_native(self) -> Self::Native {
Self::Native::from_ne_bytes(Self::to_ne_bytes(self))
}

#[inline]
fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true // Javascript only has one store
}
}
)*
};
Expand Down Expand Up @@ -887,11 +906,12 @@ mod inner {
Args: WasmTypeList,
Rets: WasmTypeList,
Kind: HostFunctionKind,
T: Sized,
Self: Sized,
{
/// Get the pointer to the function body.
fn function_body_ptr(self) -> *const VMFunctionBody;
fn function_body_ptr(&self) -> *const VMFunctionBody;

// /// Get the pointer to the function call trampoline.
// fn call_trampoline_address() -> VMTrampoline;
}

/// Empty trait to specify the kind of `HostFunction`: With or
Expand Down Expand Up @@ -1102,7 +1122,7 @@ mod inner {
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
{
#[allow(non_snake_case)]
fn function_body_ptr(self) -> *const VMFunctionBody {
fn function_body_ptr(&self) -> *const VMFunctionBody {
/// This is a function that wraps the real host
/// function. Its address will be used inside the
/// runtime.
Expand Down Expand Up @@ -1150,7 +1170,7 @@ mod inner {
Func: Fn($( $x , )*) -> RetsAsResult + 'static,
{
#[allow(non_snake_case)]
fn function_body_ptr(self) -> *const VMFunctionBody {
fn function_body_ptr(&self) -> *const VMFunctionBody {
/// This is a function that wraps the real host
/// function. Its address will be used inside the
/// runtime.
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,6 +1,6 @@
use crate::js::export::VMGlobal;
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::Extern;
use crate::js::externals::{Extern, VMExtern};
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle};
use crate::js::value::Value;
use crate::js::wasm_bindgen_polyfill::Global as JSGlobal;
Expand Down Expand Up @@ -55,6 +55,11 @@ impl Global {
Self::from_value(store, val, Mutability::Var).unwrap()
}

/// To `VMExtern`.
pub(crate) fn to_vm_extern(&self) -> VMExtern {
VMExtern::Global(self.handle.internal_handle())
}

/// Create a `Global` with the initial value [`Value`] and the provided [`Mutability`].
fn from_value(
store: &mut impl AsStoreMut,
Expand Down Expand Up @@ -135,15 +140,6 @@ impl Global {
let ty = self.handle.get(store.as_store_ref().objects()).ty;
Value::from_raw(store, ty.ty, raw)
}
/*
match self.vm_global.ty.ty {
ValType::I32 => Value::I32(self.vm_global.global.value().as_f64().unwrap() as _),
ValType::I64 => Value::I64(self.vm_global.global.value().as_f64().unwrap() as _),
ValType::F32 => Value::F32(self.vm_global.global.value().as_f64().unwrap() as _),
ValType::F64 => Value::F64(self.vm_global.global.value().as_f64().unwrap()),
_ => unimplemented!("The type is not yet supported in the JS Global API"),
}
*/
}

/// Sets a custom value [`Value`] to the runtime Global.
Expand Down
13 changes: 12 additions & 1 deletion lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::js::export::VMMemory;
use crate::js::exports::{ExportError, Exportable};
use crate::js::externals::Extern;
use crate::js::externals::{Extern, VMExtern};
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle, StoreHandle, StoreObjects};
use crate::js::{MemoryAccessError, MemoryType};
use std::marker::PhantomData;
Expand Down Expand Up @@ -118,6 +118,11 @@ impl Memory {
Self::from_vm_extern(new_store, handle.internal_handle())
}

/// To `VMExtern`.
pub(crate) fn to_vm_extern(&self) -> VMExtern {
VMExtern::Memory(self.handle.internal_handle())
}

/// Returns the [`MemoryType`] of the `Memory`.
///
/// # Example
Expand Down Expand Up @@ -218,6 +223,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
Loading