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

View File

@@ -1,10 +1,9 @@
// use super::frame_info::{FrameInfo, GlobalFrameInfo, FRAME_INFO}; // use super::frame_info::{FrameInfo, GlobalFrameInfo, FRAME_INFO};
use std::convert::TryInto;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::sync::Arc; use std::sync::Arc;
use wasm_bindgen::convert::FromWasmAbi;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
/// A struct representing an aborted instruction execution, with a message /// 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 { pub fn generic_of_jsval<T: FromWasmAbi<Abi = u32>>(
fn from(original: JsValue) -> Self { js: JsValue,
RuntimeError { classname: &str,
inner: Arc::new(RuntimeErrorSource::Js(original)), ) -> Result<T, JsValue> {
} use js_sys::{Object, Reflect};
// let into_runtime: Result<RuntimeError, _> = original.clone().try_into(); let ctor_name = Object::get_prototype_of(&js).constructor().name();
// match into_runtime { if ctor_name == classname {
// Ok(rt) => rt, let ptr = Reflect::get(&js, &JsValue::from_str("ptr"))?;
// Err(_) => RuntimeError { let ptr_u32: u32 = ptr.as_f64().ok_or(JsValue::NULL)? as u32;
// inner: Arc::new(RuntimeErrorSource::Js(original)), let foo = unsafe { T::from_abi(ptr_u32) };
// } Ok(foo)
// } } else {
// match original.dyn_into::<RuntimeError>() { Err(js)
// Ok(rt) => rt, }
// Err(original) => RuntimeError { }
// inner: Arc::new(RuntimeErrorSource::Js(original)),
// } 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) => { Err(e) => {
assert!(false, "Unknown error `{:?}`", e);
match e.downcast::<ExitCode>() { match e.downcast::<ExitCode>() {
// We found the exit code used to terminate execution. // We found the exit code used to terminate execution.
Ok(exit_code) => { Ok(exit_code) => {