From 879fd43edbda008d8f8fe5d175e1d24ab309e76e Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 10 Aug 2018 13:03:44 -0700 Subject: [PATCH] js-sys: Add bindings to `RangeError` --- crates/js-sys/src/lib.rs | 19 +++++++++++++++++++ crates/js-sys/tests/wasm/RangeError.rs | 14 ++++++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 3 files changed, 34 insertions(+) create mode 100644 crates/js-sys/tests/wasm/RangeError.rs diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index c30913326..ed212618e 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2084,6 +2084,25 @@ extern { pub fn revocable(target: &JsValue, handler: &Object) -> Object; } +// RangeError +#[wasm_bindgen] +extern { + /// The RangeError object indicates an error when a value is not in the set + /// or range of allowed values. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError + #[wasm_bindgen(extends = Error)] + #[derive(Clone, Debug)] + pub type RangeError; + + /// The RangeError object indicates an error when a value is not in the set + /// or range of allowed values. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError + #[wasm_bindgen(constructor)] + pub fn new(message: &str) -> RangeError; +} + // Reflect #[wasm_bindgen] extern "C" { diff --git a/crates/js-sys/tests/wasm/RangeError.rs b/crates/js-sys/tests/wasm/RangeError.rs new file mode 100644 index 000000000..c3470d263 --- /dev/null +++ b/crates/js-sys/tests/wasm/RangeError.rs @@ -0,0 +1,14 @@ +use wasm_bindgen::JsValue; +use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; +use js_sys::*; + +#[wasm_bindgen_test] +fn range_error() { + let error = RangeError::new("out of range yo"); + assert!(error.is_instance_of::()); + assert!(error.is_instance_of::()); + + let base: &Error = error.as_ref(); + assert_eq!(JsValue::from(base.message()), "out of range yo"); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index 9b8b2f536..49319b4da 100755 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -25,6 +25,7 @@ pub mod Math; pub mod Number; pub mod Object; pub mod Proxy; +pub mod RangeError; pub mod Reflect; pub mod RegExp; pub mod Set;