diff --git a/src/js.rs b/src/js.rs index 8c9e9deb4..6aac7093e 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 487f57441..80ac388d1 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() +}