implements bindings for Object.is (#537)

* implements bindings for Object.is

* adds counterpart test cases for non-equal values
This commit is contained in:
Marcin Baraniecki 2018-07-22 19:42:10 +02:00 committed by Alex Crichton
parent 82c2dfa7b2
commit de0ba29abc
2 changed files with 26 additions and 0 deletions

View File

@ -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).
///

View File

@ -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();