mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-12 21:38:39 +00:00
Initial port of make test-js-core (port wasmer API to core)
This commit is contained in:
3
Makefile
3
Makefile
@@ -486,6 +486,9 @@ test-packages:
|
|||||||
|
|
||||||
test-js: test-js-api test-js-wasi
|
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
|
||||||
|
|
||||||
test-js-api:
|
test-js-api:
|
||||||
cd lib/api && wasm-pack test --node -- --no-default-features --features js-default,wat
|
cd lib/api && wasm-pack test --node -- --no-default-features --features js-default,wat
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use crate::js::lib::std::string::String;
|
|||||||
use crate::js::trap::RuntimeError;
|
use crate::js::trap::RuntimeError;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
#[cfg(feature = "core")]
|
||||||
|
use crate::alloc::borrow::Cow;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -115,7 +117,7 @@ impl From<wasm_bindgen::JsValue> for WasmError {
|
|||||||
pub enum SerializeError {
|
pub enum SerializeError {
|
||||||
/// An IO error
|
/// An IO error
|
||||||
#[cfg_attr(feature = "std", error(transparent))]
|
#[cfg_attr(feature = "std", error(transparent))]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
|
||||||
/// A generic serialization error
|
/// A generic serialization error
|
||||||
#[cfg_attr(feature = "std", error("{0}"))]
|
#[cfg_attr(feature = "std", error("{0}"))]
|
||||||
Generic(String),
|
Generic(String),
|
||||||
@@ -124,11 +126,12 @@ pub enum SerializeError {
|
|||||||
/// The Deserialize error can occur when loading a
|
/// The Deserialize error can occur when loading a
|
||||||
/// compiled Module from a binary.
|
/// compiled Module from a binary.
|
||||||
/// Copied from wasmer_compiler::DeSerializeError
|
/// Copied from wasmer_compiler::DeSerializeError
|
||||||
#[derive(Error, Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "std", derive(Error))]
|
||||||
pub enum DeserializeError {
|
pub enum DeserializeError {
|
||||||
/// An IO error
|
/// An IO error
|
||||||
#[cfg_attr(feature = "std", error(transparent))]
|
#[cfg_attr(feature = "std", error(transparent))]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
|
||||||
/// A generic deserialization error
|
/// A generic deserialization error
|
||||||
#[cfg_attr(feature = "std", error("{0}"))]
|
#[cfg_attr(feature = "std", error("{0}"))]
|
||||||
Generic(String),
|
Generic(String),
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ compile_error!(
|
|||||||
compile_error!("Both the `std` and `core` features are disabled. Please enable one of them.");
|
compile_error!("Both the `std` and `core` features are disabled. Please enable one of them.");
|
||||||
|
|
||||||
#[cfg(feature = "core")]
|
#[cfg(feature = "core")]
|
||||||
extern crate alloc;
|
pub(crate) extern crate alloc;
|
||||||
|
|
||||||
mod lib {
|
mod lib {
|
||||||
#[cfg(feature = "core")]
|
#[cfg(feature = "core")]
|
||||||
pub mod std {
|
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 core::fmt;
|
||||||
pub use hashbrown as collections;
|
pub use hashbrown as collections;
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ mod lib {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod error;
|
pub(crate) mod error;
|
||||||
mod export;
|
mod export;
|
||||||
mod exports;
|
mod exports;
|
||||||
mod externals;
|
mod externals;
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ use wasm_bindgen::convert::FromWasmAbi;
|
|||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_bindgen::JsValue;
|
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
|
/// A struct representing an aborted instruction execution, with a message
|
||||||
/// indicating the cause.
|
/// indicating the cause.
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
@@ -30,7 +34,10 @@ impl PartialEq for RuntimeError {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum RuntimeErrorSource {
|
enum RuntimeErrorSource {
|
||||||
Generic(String),
|
Generic(String),
|
||||||
|
#[cfg(feature = "std")]
|
||||||
User(Box<dyn Error + Send + Sync>),
|
User(Box<dyn Error + Send + Sync>),
|
||||||
|
#[cfg(feature = "core")]
|
||||||
|
User(Box<dyn CoreError + Send + Sync>),
|
||||||
Js(JsValue),
|
Js(JsValue),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +81,7 @@ impl RuntimeError {
|
|||||||
///
|
///
|
||||||
/// This error object can be passed through Wasm frames and later retrieved
|
/// This error object can be passed through Wasm frames and later retrieved
|
||||||
/// using the `downcast` method.
|
/// using the `downcast` method.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
|
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
|
||||||
match error.downcast::<Self>() {
|
match error.downcast::<Self>() {
|
||||||
// The error is already a RuntimeError, we return it directly
|
// The error is already a RuntimeError, we return it directly
|
||||||
@@ -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`.
|
/// Returns a reference the `message` stored in `Trap`.
|
||||||
pub fn message(&self) -> String {
|
pub fn message(&self) -> String {
|
||||||
format!("{}", self.inner)
|
format!("{}", self.inner)
|
||||||
|
|||||||
@@ -12,12 +12,19 @@ pub struct RuntimeError {
|
|||||||
inner: Arc<RuntimeErrorInner>,
|
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`.
|
/// The source of the `RuntimeError`.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum RuntimeErrorSource {
|
enum RuntimeErrorSource {
|
||||||
Generic(String),
|
Generic(String),
|
||||||
OutOfMemory,
|
OutOfMemory,
|
||||||
|
#[cfg(feature = "std")]
|
||||||
User(Box<dyn Error + Send + Sync>),
|
User(Box<dyn Error + Send + Sync>),
|
||||||
|
#[cfg(feature = "core")]
|
||||||
|
User(Box<dyn CoreError + Send + Sync>),
|
||||||
Trap(TrapCode),
|
Trap(TrapCode),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +117,7 @@ impl RuntimeError {
|
|||||||
///
|
///
|
||||||
/// This error object can be passed through Wasm frames and later retrieved
|
/// This error object can be passed through Wasm frames and later retrieved
|
||||||
/// using the `downcast` method.
|
/// using the `downcast` method.
|
||||||
|
#[cfg(feature = "std")]
|
||||||
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
|
pub fn user(error: Box<dyn Error + Send + Sync>) -> Self {
|
||||||
match error.downcast::<Self>() {
|
match error.downcast::<Self>() {
|
||||||
// The error is already a RuntimeError, we return it directly
|
// The error is already a RuntimeError, we return it directly
|
||||||
@@ -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(
|
fn new_with_trace(
|
||||||
info: &GlobalFrameInfo,
|
info: &GlobalFrameInfo,
|
||||||
trap_pc: Option<usize>,
|
trap_pc: Option<usize>,
|
||||||
|
|||||||
Reference in New Issue
Block a user