Improved traps

# Conflicts:
#	lib/js-api/tests/instance.rs
This commit is contained in:
Syrus Akbary
2021-07-14 18:54:14 -07:00
parent 8047e3eb6a
commit ed9134af86
2 changed files with 92 additions and 23 deletions

View File

@@ -10,10 +10,16 @@ use wasm_bindgen::JsValue;
/// indicating the cause.
#[wasm_bindgen]
#[derive(Clone)]
pub struct RuntimeError {
pub struct WasmerRuntimeError {
inner: Arc<RuntimeErrorSource>,
}
/// This type is the same as `WasmerRuntimeError`.
///
/// We use the `WasmerRuntimeError` name to not collide with the
/// `RuntimeError` in JS.
pub type RuntimeError = WasmerRuntimeError;
impl PartialEq for RuntimeError {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
@@ -56,6 +62,13 @@ impl RuntimeError {
}
}
/// Creates a new user `RuntimeError` with the given `error`.
pub fn user(error: impl Error + 'static) -> Self {
RuntimeError {
inner: Arc::new(RuntimeErrorSource::User(Box::new(error))),
}
}
/// Raises a custom user Error
pub fn raise(error: Box<dyn Error + Send + Sync>) -> ! {
let error = if error.is::<RuntimeError>() {
@@ -127,9 +140,16 @@ pub fn generic_of_jsval<T: FromWasmAbi<Abi = u32>>(
let ctor_name = Object::get_prototype_of(&js).constructor().name();
if ctor_name == classname {
let ptr = Reflect::get(&js, &JsValue::from_str("ptr"))?;
let ptr_u32: u32 = ptr.as_f64().ok_or(JsValue::NULL)? as u32;
let foo = unsafe { T::from_abi(ptr_u32) };
Ok(foo)
match ptr.as_f64() {
Some(ptr_f64) => {
let foo = unsafe { T::from_abi(ptr_f64 as u32) };
Ok(foo)
}
None => {
// We simply relay the js value
Err(js)
}
}
} else {
Err(js)
}