Skip to content

implements Object.prototype.valueOf() binding #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/all/js_globals/Object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}