From 36e15149c7877839b7dbd0afd8877e7213b2eb9b Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 10 Aug 2018 13:45:39 -0700 Subject: [PATCH] js-sys: Add bindings for `TypeError` --- crates/js-sys/src/lib.rs | 19 +++++++++++++++++++ crates/js-sys/tests/wasm/TypeError.rs | 14 ++++++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 3 files changed, 34 insertions(+) create mode 100644 crates/js-sys/tests/wasm/TypeError.rs diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 3bdb3a0f..aaa3080b 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2517,6 +2517,25 @@ extern { pub fn new(message: &str) -> SyntaxError; } +// TypeError +#[wasm_bindgen] +extern { + /// The TypeError object represents an error when a value is not of the + /// expected type. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError + #[wasm_bindgen(extends = Error)] + #[derive(Clone, Debug)] + pub type TypeError; + + /// The TypeError object represents an error when a value is not of the + /// expected type. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError + #[wasm_bindgen(constructor)] + pub fn new(message: &str) -> TypeError; +} + // Uint8Array #[wasm_bindgen] extern "C" { diff --git a/crates/js-sys/tests/wasm/TypeError.rs b/crates/js-sys/tests/wasm/TypeError.rs new file mode 100644 index 00000000..e82cf091 --- /dev/null +++ b/crates/js-sys/tests/wasm/TypeError.rs @@ -0,0 +1,14 @@ +use wasm_bindgen::JsValue; +use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; +use js_sys::*; + +#[wasm_bindgen_test] +fn type_error() { + let error = TypeError::new("msg"); + assert!(error.is_instance_of::()); + assert!(error.is_instance_of::()); + + let base: &Error = error.as_ref(); + assert_eq!(JsValue::from(base.message()), "msg"); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index e3b6ec90..faef8e52 100755 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -33,6 +33,7 @@ pub mod Set; pub mod SetIterator; pub mod Symbol; pub mod SyntaxError; +pub mod TypeError; pub mod TypedArray; pub mod UriError; pub mod WeakMap;