feat: add Reflect.setPrototypeOf

This commit is contained in:
Jannik Keye 2018-07-04 13:13:35 +02:00
parent eb3f67a36f
commit 1397f9b05a
2 changed files with 52 additions and 0 deletions

View File

@ -1075,6 +1075,15 @@ extern "C" {
pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result<JsValue, JsValue>; pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result<JsValue, JsValue>;
#[wasm_bindgen(static_method_of = Reflect, js_name = set, catch)] #[wasm_bindgen(static_method_of = Reflect, js_name = set, catch)]
pub fn set_with_receiver(target: &JsValue, property_key: &JsValue, value: &JsValue, receiver: &JsValue) -> Result<JsValue, JsValue>; pub fn set_with_receiver(target: &JsValue, property_key: &JsValue, value: &JsValue, receiver: &JsValue) -> Result<JsValue, JsValue>;
/// The static Reflect.setPrototypeOf() method is the same
/// method as Object.setPrototypeOf(). It sets the prototype
/// (i.e., the internal [[Prototype]] property) of a specified
/// object to another object or to null.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/setPrototypeOf
#[wasm_bindgen(static_method_of = Reflect, js_name = setPrototypeOf, catch)]
pub fn set_prototype_of(target: &JsValue, prototype: &JsValue) -> Result<JsValue, JsValue>;
} }
// Set // Set

View File

@ -657,4 +657,47 @@ fn set_with_receiver() {
"#, "#,
) )
.test() .test()
}
#[test]
fn set_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 set_prototype_of(target: &JsValue, prototype: &JsValue) -> JsValue {
let result = js::Reflect::set_prototype_of(target, prototype);
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 = {};
assert.equal(wasm.set_prototype_of(object, Object.prototype), true);
assert.equal(Object.getPrototypeOf(object), Object.prototype);
assert.equal(wasm.set_prototype_of(object, null), true);
assert.equal(Object.getPrototypeOf(object), null);
assert.equal(wasm.set_prototype_of("", Object.prototype), "TypeError");
}
"#,
)
.test()
} }