feat: add Reflect.getPrototypeOf

This commit is contained in:
Jannik Keye 2018-07-04 12:39:56 +02:00
parent 2422c5e945
commit edddd4b08e
2 changed files with 53 additions and 0 deletions

View File

@ -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<JsValue, JsValue>;
/// 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<JsValue, JsValue>;
}
// Set

View File

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