From b005e96fd9bd46ba67ab91b8ba3d750eeff72978 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Sun, 16 Sep 2018 22:55:15 +0200 Subject: [PATCH] Add binding for Object.getOwnPropertyDescriptor() --- crates/js-sys/src/lib.rs | 9 +++++++++ crates/js-sys/tests/wasm/Object.rs | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 512d0d9e..f074dd20 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2022,6 +2022,15 @@ extern "C" { #[wasm_bindgen(static_method_of = Object)] pub fn freeze(value: &Object) -> Object; + /// The Object.getOwnPropertyDescriptor() method returns a + /// property descriptor for an own property (that is, one directly + /// present on an object and not in the object's prototype chain) + /// of a given object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor) + #[wasm_bindgen(static_method_of = Object, js_name = getOwnPropertyDescriptor)] + pub fn get_own_property_descriptor(obj: &Object, prop: &JsValue) -> JsValue; + /// 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 832985c3..de4dfffe 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -13,6 +13,10 @@ extern "C" { type DefinePropertyAttrs; #[wasm_bindgen(method, setter, structural)] fn set_value(this: &DefinePropertyAttrs, val: &JsValue); + + type PropertyDescriptor; + #[wasm_bindgen(method, getter, structural)] + fn value(this: &PropertyDescriptor) -> JsValue; } #[wasm_bindgen(module = "tests/wasm/Object.js")] @@ -101,6 +105,15 @@ fn define_properties() { assert!(foo.has_own_property(&"car".into())); } +#[wasm_bindgen_test] +fn get_own_property_descriptor() { + let foo = foo_42(); + let desc = Object::get_own_property_descriptor(&foo, &"foo".into()); + assert_eq!(PropertyDescriptor::from(desc).value(), 42); + let desc = Object::get_own_property_descriptor(&foo, &"bar".into()); + assert!(PropertyDescriptor::from(desc).value().is_undefined()); +} + #[wasm_bindgen_test] fn has_own_property() { assert!(foo_42().has_own_property(&"foo".into()));