diff --git a/src/js.rs b/src/js.rs index 499fefcd..849c75a1 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1027,6 +1027,15 @@ extern "C" { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor #[wasm_bindgen(static_method_of = Reflect, js_name = getOwnPropertyDescriptor, catch)] pub fn get_own_property_descriptor(target: &JsValue, property_key: &JsValue) -> Result; + + /// The static Reflect.getPrototypeOf() method is almost the same + /// method as Object.getPrototypeOf(). It returns the prototype + /// (i.e. the value of the internal [[Prototype]] property) of + /// the specified object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getPrototypeOf + #[wasm_bindgen(static_method_of = Reflect, js_name = getPrototypeOf, catch)] + pub fn get_prototype_of(target: &JsValue) -> Result; } // Set diff --git a/tests/all/js_globals/Reflect.rs b/tests/all/js_globals/Reflect.rs index 7d730885..5f676576 100644 --- a/tests/all/js_globals/Reflect.rs +++ b/tests/all/js_globals/Reflect.rs @@ -343,4 +343,48 @@ fn get_own_property_descriptor() { "#, ) .test() +} + +#[test] +fn get_prototype_of() { + project() + .file( + "src/lib.rs", + r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn get_prototype_of(target: &JsValue) -> JsValue { + let result = js::Reflect::get_prototype_of(target); + let result = match result { + Ok(val) => val, + Err(_err) => "TypeError".into() + }; + result + } + "#, + ) + .file( + "test.ts", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const object = { + property: 42 + }; + const array: number[] = [1, 2, 3]; + + assert.equal(wasm.get_prototype_of(object), Object.prototype); + assert.equal(wasm.get_prototype_of(array), Array.prototype); + assert.equal(wasm.get_prototype_of(""), "TypeError"); + } + "#, + ) + .test() } \ No newline at end of file