From 1397f9b05a011f312e71050ec3fce54fcecf456f Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Wed, 4 Jul 2018 13:13:35 +0200 Subject: [PATCH] feat: add Reflect.setPrototypeOf --- src/js.rs | 9 +++++++ tests/all/js_globals/Reflect.rs | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/js.rs b/src/js.rs index d2c8eada..38ade1dd 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1075,6 +1075,15 @@ extern "C" { pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> Result; #[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; + + /// 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; } // Set diff --git a/tests/all/js_globals/Reflect.rs b/tests/all/js_globals/Reflect.rs index 5f9f5b31..6bf2e212 100644 --- a/tests/all/js_globals/Reflect.rs +++ b/tests/all/js_globals/Reflect.rs @@ -657,4 +657,47 @@ 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() } \ No newline at end of file