Improved traps, now fully working

This commit is contained in:
Syrus Akbary
2021-07-14 12:26:18 -07:00
parent 93b84110e9
commit 40df4455da
3 changed files with 27 additions and 24 deletions

View File

@@ -11,7 +11,7 @@
#![warn(unused_import_braces)]
#![cfg_attr(
feature = "cargo-clippy",
allow(clippy::new_without_default, vtable_address_comparisons)
allow(clippy::new_without_default, clippy::vtable_address_comparisons)
)]
#![cfg_attr(
feature = "cargo-clippy",
@@ -19,8 +19,7 @@
clippy::float_arithmetic,
clippy::mut_mut,
clippy::nonminimal_bool,
clippy::option_map_unwrap_or,
clippy::option_map_unwrap_or_else,
clippy::map_unwrap_or,
clippy::print_stdout,
clippy::unicode_not_nfc,
clippy::use_self

View File

@@ -1,10 +1,9 @@
// use super::frame_info::{FrameInfo, GlobalFrameInfo, FRAME_INFO};
use std::convert::TryInto;
use std::error::Error;
use std::fmt;
use std::sync::Arc;
use wasm_bindgen::convert::FromWasmAbi;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
/// A struct representing an aborted instruction execution, with a message
@@ -120,23 +119,29 @@ impl std::error::Error for RuntimeError {
}
}
impl From<JsValue> for RuntimeError {
fn from(original: JsValue) -> Self {
RuntimeError {
inner: Arc::new(RuntimeErrorSource::Js(original)),
}
// let into_runtime: Result<RuntimeError, _> = original.clone().try_into();
// match into_runtime {
// Ok(rt) => rt,
// Err(_) => RuntimeError {
// inner: Arc::new(RuntimeErrorSource::Js(original)),
// }
// }
// match original.dyn_into::<RuntimeError>() {
// Ok(rt) => rt,
// Err(original) => RuntimeError {
// inner: Arc::new(RuntimeErrorSource::Js(original)),
// }
// }
pub fn generic_of_jsval<T: FromWasmAbi<Abi = u32>>(
js: JsValue,
classname: &str,
) -> Result<T, JsValue> {
use js_sys::{Object, Reflect};
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)
} else {
Err(js)
}
}
impl From<JsValue> for RuntimeError {
fn from(original: JsValue) -> Self {
// We try to downcast the error and see if it's
// an instance of RuntimeError instead, so we don't need
// to re-wrap it.
generic_of_jsval(original, "RuntimeError").unwrap_or_else(|js| RuntimeError {
inner: Arc::new(RuntimeErrorSource::Js(js)),
})
}
}

View File

@@ -642,7 +642,6 @@ fn test_custom_error() {
);
}
Err(e) => {
assert!(false, "Unknown error `{:?}`", e);
match e.downcast::<ExitCode>() {
// We found the exit code used to terminate execution.
Ok(exit_code) => {