From ce9b95635d24657a5616e3ecfe09125a832e42e4 Mon Sep 17 00:00:00 2001 From: belfz Date: Sun, 1 Jul 2018 11:52:22 +0200 Subject: [PATCH] implements Object.setPrototypeOf() binding --- src/js.rs | 7 +++++++ tests/all/js_globals/Object.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index ced943054..96b542e29 100644 --- a/src/js.rs +++ b/src/js.rs @@ -782,6 +782,13 @@ extern "C" { #[wasm_bindgen(static_method_of = Object)] pub fn seal(value: &JsValue) -> JsValue; + /// The Object.setPrototypeOf() method sets the prototype (i.e., the internal + /// [[Prototype]] property) of a specified object to another object or null. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf + #[wasm_bindgen(static_method_of = Object, js_name = setPrototypeOf)] + pub fn set_prototype_of(object: &Object, prototype: &Object) -> Object; + /// The toLocaleString() method returns a string representing the object. /// This method is meant to be overridden by derived objects for /// locale-specific purposes. diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index 6b2689561..8787cf917 100644 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -272,6 +272,36 @@ fn seal() { .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(object: &js::Object, prototype: &js::Object) -> js::Object { + js::Object::set_prototype_of(&object, &prototype) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const object = { foo: 42 }; + const newPrototype = { bar: 'baz' }; + + const modifiedObject = wasm.set_prototype_of(object, newPrototype); + assert(newPrototype.isPrototypeOf(modifiedObject)); + } + "#) + .test() +} + #[test] fn to_locale_string() { project()