From e25592529203876f8aec1655efbf10cc4ba1f763 Mon Sep 17 00:00:00 2001 From: belfz Date: Sat, 23 Jun 2018 12:11:46 +0200 Subject: [PATCH] implements Object.prototype.valueOf() binding --- src/js.rs | 7 +++++++ tests/all/js_globals/Object.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/js.rs b/src/js.rs index c3fa8e62..d3a5a7bf 100644 --- a/src/js.rs +++ b/src/js.rs @@ -254,6 +254,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable #[wasm_bindgen(method, js_name = propertyIsEnumerable)] pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool; + + /// The valueOf() method returns the primitive value of the + /// specified object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf + #[wasm_bindgen(method, js_name = valueOf)] + pub fn value_of(this: &Object) -> Object; } // String diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index 6e2bbb70..7772a071 100755 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -184,3 +184,31 @@ fn to_locale_string() { "#) .test() } + +#[test] +fn value_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 value_of(obj: &js::Object) -> js::Object { + obj.value_of() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const obj = { foo: 42 }; + assert.strictEqual(wasm.value_of(obj), obj); + assert.notStrictEqual(wasm.value_of(obj), { foo: 42 }); + } + "#) + .test() +}