From 4e18493fd7207996d65480b21f4d2ce2e33ace1c Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 20:43:50 +0200 Subject: [PATCH 1/4] Add binding for Object.getOwnPropertyNames() --- crates/js-sys/src/lib.rs | 8 ++++++++ crates/js-sys/tests/wasm/Object.rs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index d72bbcac..86b9d05b 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2040,6 +2040,14 @@ extern "C" { #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyDescriptors)] pub fn get_own_property_descriptors(obj: &Object) -> JsValue; + /// The Object.getOwnPropertyNames() method returns an array of + /// all properties (including non-enumerable properties except for + /// those which use Symbol) found directly upon a given object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) + #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyNames)] + pub fn get_own_property_names(obj: &Object) -> Array; + /// The `hasOwnProperty()` method returns a boolean indicating whether the /// object has the specified property as its own property (as opposed to /// inheriting it). diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index 525bfab3..ccd555cb 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -122,6 +122,15 @@ fn get_own_property_descriptors() { assert_eq!(PropertyDescriptor::from(foo_desc).value(), 42); } +#[wasm_bindgen_test] +fn get_own_property_names() { + let names = Object::get_own_property_names(&foo_42()); + assert_eq!(names.length(), 1); + names.for_each(&mut |x, _, _| { + assert_eq!(x, "foo"); + }); +} + #[wasm_bindgen_test] fn has_own_property() { assert!(foo_42().has_own_property(&"foo".into())); From 76969bd1e35adf91059e777ed836df983379f374 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 20:58:46 +0200 Subject: [PATCH 2/4] Add binding for Object.getOwnPropertySymbols() --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/Object.rs | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 86b9d05b..da8591f5 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2048,6 +2048,13 @@ extern "C" { #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyNames)] pub fn get_own_property_names(obj: &Object) -> Array; + /// The Object.getOwnPropertySymbols() method returns an array of + /// all symbol properties found directly upon a given object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols) + #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertySymbols)] + pub fn get_own_property_symbols(obj: &Object) -> Array; + /// The `hasOwnProperty()` method returns a boolean indicating whether the /// object has the specified property as its own property (as opposed to /// inheriting it). diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index ccd555cb..1304fd0b 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -131,6 +131,12 @@ fn get_own_property_names() { }); } +#[wasm_bindgen_test] +fn get_own_property_symbols() { + let symbols = Object::get_own_property_symbols(&map_with_symbol_key()); + assert_eq!(symbols.length(), 1); +} + #[wasm_bindgen_test] fn has_own_property() { assert!(foo_42().has_own_property(&"foo".into())); From 326e4c02624b5cd1e050c14dabf1670d618787c2 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 21:10:40 +0200 Subject: [PATCH 3/4] Add binding for Object.getPrototypeOf() --- crates/js-sys/src/lib.rs | 8 ++++++++ crates/js-sys/tests/wasm/Object.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index da8591f5..08531aa4 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2055,6 +2055,14 @@ extern "C" { #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertySymbols)] pub fn get_own_property_symbols(obj: &Object) -> Array; + /// The Object.getPrototypeOf() method returns the prototype + /// (i.e. the value of the internal [[Prototype]] property) of the + /// specified object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf) + #[wasm_bindgen(static_method_of = Object, js_name = getPrototypeOf)] + pub fn get_prototype_of(obj: &JsValue) -> Object; + /// The `hasOwnProperty()` method returns a boolean indicating whether the /// object has the specified property as its own property (as opposed to /// inheriting it). diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index 1304fd0b..ccd37a9a 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -10,6 +10,11 @@ extern "C" { #[wasm_bindgen(method, setter, structural)] fn set_foo(this: &Foo42, val: JsValue); + #[wasm_bindgen(js_name = prototype, js_namespace = Object)] + static OBJECT_PROTOTYPE: JsValue; + #[wasm_bindgen(js_name = prototype, js_namespace = Array)] + static ARRAY_PROTOTYPE: JsValue; + type DefinePropertyAttrs; #[wasm_bindgen(method, setter, structural)] fn set_value(this: &DefinePropertyAttrs, val: &JsValue); @@ -137,6 +142,14 @@ fn get_own_property_symbols() { assert_eq!(symbols.length(), 1); } +#[wasm_bindgen_test] +fn get_prototype_of() { + let proto = JsValue::from(Object::get_prototype_of(&Object::new().into())); + assert_eq!(proto, *OBJECT_PROTOTYPE); + let proto = JsValue::from(Object::get_prototype_of(&Array::new().into())); + assert_eq!(proto, *ARRAY_PROTOTYPE); +} + #[wasm_bindgen_test] fn has_own_property() { assert!(foo_42().has_own_property(&"foo".into())); From f7b511588b24aa0312a49ecbebe8d0f4be2f42d6 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 21:32:05 +0200 Subject: [PATCH 4/4] Add binding for Object.entries() --- crates/js-sys/src/lib.rs | 10 ++++++++++ crates/js-sys/tests/wasm/Object.rs | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 08531aa4..b09f582d 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2014,6 +2014,16 @@ extern "C" { #[wasm_bindgen(static_method_of = Object, js_name = defineProperties)] pub fn define_properties(obj: &Object, props: &Object) -> Object; + /// The Object.entries() method returns an array of a given + /// object's own enumerable property [key, value] pairs, in the + /// same order as that provided by a for...in loop (the difference + /// being that a for-in loop enumerates properties in the + /// prototype chain as well). + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) + #[wasm_bindgen(static_method_of = Object)] + pub fn entries(object: &Object) -> Array; + /// The `Object.freeze()` method freezes an object: that is, prevents new /// properties from being added to it; prevents existing properties from /// being removed; and prevents existing properties, or their enumerability, diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index ccd37a9a..52e6fa50 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -110,6 +110,19 @@ fn define_properties() { assert!(foo.has_own_property(&"car".into())); } +#[wasm_bindgen_test] +fn entries() { + let entries = Object::entries(&foo_42()); + assert_eq!(entries.length(), 1); + entries.for_each(&mut |x, _, _| { + assert!(x.is_object()); + let array: Array = x.into(); + assert_eq!(array.shift(), "foo"); + assert_eq!(array.shift(), 42); + assert_eq!(array.length(), 0); + }); +} + #[wasm_bindgen_test] fn get_own_property_descriptor() { let foo = foo_42();