mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
feat: add Reflect.setPrototypeOf
This commit is contained in:
parent
eb3f67a36f
commit
1397f9b05a
@ -1075,6 +1075,15 @@ extern "C" {
|
||||
pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result<JsValue, JsValue>;
|
||||
#[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>;
|
||||
|
||||
/// 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
|
||||
|
@ -658,3 +658,46 @@ fn set_with_receiver() {
|
||||
)
|
||||
.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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user