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

Initial port of make test-js-core (port wasmer API to core) #3165

Merged
merged 8 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ test-packages:

test-js: test-js-api test-js-wasi

test-js-core:
cd lib/api && wasm-pack test --node -- --no-default-features --features js,core,wasm-types-polyfill,wat
fschutt marked this conversation as resolved.
Show resolved Hide resolved

test-js-api:
cd lib/api && wasm-pack test --node -- --no-default-features --features js-default,wat

Expand Down
9 changes: 6 additions & 3 deletions lib/api/src/js/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::js::lib::std::string::String;
use crate::js::trap::RuntimeError;
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "core")]
use crate::alloc::borrow::Cow;
#[cfg(feature = "std")]
use thiserror::Error;

Expand Down Expand Up @@ -115,7 +117,7 @@ impl From<wasm_bindgen::JsValue> for WasmError {
pub enum SerializeError {
/// An IO error
#[cfg_attr(feature = "std", error(transparent))]
Io(#[from] std::io::Error),
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
/// A generic serialization error
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
Expand All @@ -124,11 +126,12 @@ pub enum SerializeError {
/// The Deserialize error can occur when loading a
/// compiled Module from a binary.
/// Copied from wasmer_compiler::DeSerializeError
#[derive(Error, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum DeserializeError {
/// An IO error
#[cfg_attr(feature = "std", error(transparent))]
Io(#[from] std::io::Error),
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
/// A generic deserialization error
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
Expand Down
6 changes: 3 additions & 3 deletions lib/api/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ compile_error!(
compile_error!("Both the `std` and `core` features are disabled. Please enable one of them.");

#[cfg(feature = "core")]
extern crate alloc;
pub(crate) extern crate alloc;

mod lib {
#[cfg(feature = "core")]
pub mod std {
pub use alloc::{borrow, boxed, str, string, sync, vec};
pub use crate::alloc::{borrow, boxed, str, string, sync, vec};
pub use core::fmt;
pub use hashbrown as collections;
}
Expand All @@ -23,7 +23,7 @@ mod lib {
}
}

mod error;
pub(crate) mod error;
mod export;
mod exports;
mod externals;
Expand Down
19 changes: 19 additions & 0 deletions lib/api/src/js/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use wasm_bindgen::convert::FromWasmAbi;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsValue;

pub trait CoreError: fmt::Debug + fmt::Display + core::any::Any { }

impl<T: fmt::Debug + fmt::Display + core::any::Any> CoreError for T { }

/// A struct representing an aborted instruction execution, with a message
/// indicating the cause.
#[wasm_bindgen]
Expand All @@ -30,7 +34,10 @@ impl PartialEq for RuntimeError {
#[derive(Debug)]
enum RuntimeErrorSource {
Generic(String),
#[cfg(feature = "std")]
User(Box<dyn Error + Send + Sync>),
#[cfg(feature = "core")]
User(Box<dyn CoreError + Send + Sync>),
Js(JsValue),
}

Expand Down Expand Up @@ -74,6 +81,7 @@ impl RuntimeError {
///
/// This error object can be passed through Wasm frames and later retrieved
/// using the `downcast` method.
#[cfg(feature = "std")]
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
match error.downcast::<Self>() {
// The error is already a RuntimeError, we return it directly
Expand All @@ -84,6 +92,17 @@ impl RuntimeError {
}
}

#[cfg(feature = "core")]
pub fn user(error: Box<dyn CoreError + Send + Sync>) -> Self {
match error.downcast::<Self>() {
// The error is already a RuntimeError, we return it directly
Ok(runtime_error) => *runtime_error,
Err(error) => RuntimeError {
inner: Arc::new(RuntimeErrorSource::User(error)),
},
}
}

/// Returns a reference the `message` stored in `Trap`.
pub fn message(&self) -> String {
format!("{}", self.inner)
Expand Down
29 changes: 29 additions & 0 deletions lib/compiler/src/engine/trap/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ pub struct RuntimeError {
inner: Arc<RuntimeErrorInner>,
}

pub trait CoreError: fmt::Debug + fmt::Display + core::any::Any { }

impl<T: fmt::Debug + fmt::Display + core::any::Any> CoreError for T { }

/// The source of the `RuntimeError`.
#[derive(Debug)]
enum RuntimeErrorSource {
Generic(String),
OutOfMemory,
#[cfg(feature = "std")]
User(Box<dyn Error + Send + Sync>),
#[cfg(feature = "core")]
User(Box<dyn CoreError + Send + Sync>),
Trap(TrapCode),
}

Expand Down Expand Up @@ -110,6 +117,7 @@ impl RuntimeError {
///
/// This error object can be passed through Wasm frames and later retrieved
/// using the `downcast` method.
#[cfg(feature = "std")]
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
match error.downcast::<Self>() {
// The error is already a RuntimeError, we return it directly
Expand All @@ -126,6 +134,27 @@ impl RuntimeError {
}
}

/// Creates a custom user Error.
///
/// This error object can be passed through Wasm frames and later retrieved
/// using the `downcast` method.
#[cfg(feature = "core")]
pub fn user(error: Box<dyn CoreError + Send + Sync>) -> Self {
match error.downcast::<Self>() {
// The error is already a RuntimeError, we return it directly
Ok(runtime_error) => *runtime_error,
Err(error) => {
let info = FRAME_INFO.read().unwrap();
Self::new_with_trace(
&info,
None,
RuntimeErrorSource::User(error),
Backtrace::new_unresolved(),
)
}
}
}

fn new_with_trace(
info: &GlobalFrameInfo,
trap_pc: Option<usize>,
Expand Down