From de0ba29abcc67c36a84fa17fba5bb8778d5cd3b8 Mon Sep 17 00:00:00 2001 From: Marcin Baraniecki Date: Sun, 22 Jul 2018 19:42:10 +0200 Subject: [PATCH] implements bindings for Object.is (#537) * implements bindings for Object.is * adds counterpart test cases for non-equal values --- crates/js-sys/src/lib.rs | 6 ++++++ crates/js-sys/tests/wasm/Object.rs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index e8e546fb5..246abc8d9 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1712,6 +1712,12 @@ extern "C" { #[wasm_bindgen(method, js_name = hasOwnProperty)] pub fn has_own_property(this: &Object, property: &JsValue) -> bool; + /// The Object.is() method determines whether two values are the same value. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is + #[wasm_bindgen(static_method_of = Object)] + pub fn is(value_1: &JsValue, value_2: &JsValue) -> bool; + /// The `Object.isExtensible()` method determines if an object is extensible /// (whether it can have new properties added to it). /// diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index 69f0d2e55..802465354 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; +use std::f64::NAN; use js_sys::*; #[wasm_bindgen] @@ -48,6 +49,25 @@ fn to_string() { assert_eq!(foo_42().to_string(), "[object Object]"); } +#[wasm_bindgen_test] +fn is() { + let object = JsValue::from(Object::new()); + assert!(Object::is(&object, &object)); + assert!(Object::is(&JsValue::undefined(), &JsValue::undefined())); + assert!(Object::is(&JsValue::null(), &JsValue::null())); + assert!(Object::is(&JsValue::TRUE, &JsValue::TRUE)); + assert!(Object::is(&JsValue::FALSE, &JsValue::FALSE)); + assert!(Object::is(&"foo".into(), &"foo".into())); + assert!(Object::is(&JsValue::from(42), &JsValue::from(42))); + assert!(Object::is(&JsValue::from(NAN), &JsValue::from(NAN))); + + let another_object = JsValue::from(Object::new()); + assert!(!Object::is(&object, &another_object)); + assert!(!Object::is(&JsValue::TRUE, &JsValue::FALSE)); + assert!(!Object::is(&"foo".into(), &"bar".into())); + assert!(!Object::is(&JsValue::from(23), &JsValue::from(42))); +} + #[wasm_bindgen_test] fn is_extensible() { let object = Object::new();