From 326e4c02624b5cd1e050c14dabf1670d618787c2 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 21:10:40 +0200 Subject: [PATCH] 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()));