From 77ad68673c688dcbe28572cafc95a3d53363d3ea Mon Sep 17 00:00:00 2001 From: belfz Date: Thu, 21 Jun 2018 07:36:24 +0200 Subject: [PATCH] implements Object.isPrototypeOf binding --- src/js.rs | 7 ++++++- tests/all/js_globals/Object.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 8c9e9deb..6aac7093 100644 --- a/src/js.rs +++ b/src/js.rs @@ -92,7 +92,12 @@ extern { #[wasm_bindgen(method, js_name = toString)] pub fn to_string(this: &Object) -> String; - + /// The isPrototypeOf() method checks if an object exists in another + /// object's prototype chain. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf + #[wasm_bindgen(method, js_name = isPrototypeOf)] + pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool; } // Array diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index 487f5744..80ac388d 100755 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -87,3 +87,34 @@ fn to_string() { "#) .test() } + +#[test] +fn is_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 obj_is_prototype_of_value(obj: &js::Object, value: &JsValue) -> bool { + obj.is_prototype_of(&value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + class Foo {} + class Bar {} + + export function test() { + const foo = new Foo(); + assert(wasm.obj_is_prototype_of_value(Foo.prototype, foo)); + assert(!wasm.obj_is_prototype_of_value(Bar.prototype, foo)); + } + "#) + .test() +}