From 70406fe18a84ad6502f68a129ece2e699fddfa8a Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Thu, 9 Aug 2018 19:21:06 -0400 Subject: [PATCH] Add support and tests for EvalError --- crates/js-sys/src/lib.rs | 17 +++++++++ crates/js-sys/tests/wasm/EvalError.rs | 55 +++++++++++++++++++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 3 files changed, 73 insertions(+) create mode 100644 crates/js-sys/tests/wasm/EvalError.rs diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index e0f01f7bc..b6b6e36af 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -627,6 +627,23 @@ extern "C" { pub fn to_string(this: &Error) -> JsString; } +// EvalError +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(extends = Object, extends = Error)] + #[derive(Clone, Debug)] + pub type EvalError; + + /// The EvalError object indicates an error regarding the global eval() function. This + /// exception is not thrown by JavaScript anymore, however the EvalError object remains for + /// compatibility. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError + #[wasm_bindgen(constructor)] + pub fn new(message: &str) -> EvalError; +} + + // Float32Array #[wasm_bindgen] extern "C" { diff --git a/crates/js-sys/tests/wasm/EvalError.rs b/crates/js-sys/tests/wasm/EvalError.rs new file mode 100644 index 000000000..d1deb73ae --- /dev/null +++ b/crates/js-sys/tests/wasm/EvalError.rs @@ -0,0 +1,55 @@ +use wasm_bindgen::JsValue; +use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; +use js_sys::*; + +// Note: This error is not thrown any more, so there are no tests that will generate this error. +// Instead we just have to manually construct it + +#[wasm_bindgen_test] +fn new() { + let error = EvalError::new("some message"); + let base_error: &Error = error.dyn_ref().unwrap(); + assert_eq!(JsValue::from(base_error.message()), "some message"); +} + +#[wasm_bindgen_test] +fn set_message() { + let error = EvalError::new("test"); + let base_error: &Error = error.dyn_ref().unwrap(); + base_error.set_message("another"); + assert_eq!(JsValue::from(base_error.message()), "another"); +} + +#[wasm_bindgen_test] +fn name() { + let error = EvalError::new("test"); + let base_error: &Error = error.dyn_ref().unwrap(); + assert_eq!(JsValue::from(base_error.name()), "EvalError"); +} + +#[wasm_bindgen_test] +fn set_name() { + let error = EvalError::new("test"); + let base_error: &Error = error.dyn_ref().unwrap(); + base_error.set_name("different"); + assert_eq!(JsValue::from(base_error.name()), "different"); +} + +#[wasm_bindgen_test] +fn to_string() { + let error = EvalError::new("error message 1"); + let base_error: &Error = error.dyn_ref().unwrap(); + assert_eq!(JsValue::from(base_error.to_string()), "EvalError: error message 1"); + base_error.set_name("error_name_1"); + assert_eq!(JsValue::from(base_error.to_string()), "error_name_1: error message 1"); +} + + +#[wasm_bindgen_test] +fn evalerror_inheritance() { + let error = EvalError::new("some message"); + assert!(error.is_instance_of::()); + assert!(error.is_instance_of::()); + assert!(error.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index 1c4414b07..9b8b2f536 100755 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -14,6 +14,7 @@ pub mod Boolean; pub mod DataView; pub mod Date; pub mod Error; +pub mod EvalError; pub mod Function; pub mod Generator; pub mod Intl;