Skip to content

Commit e255925

Browse files
committed
implements Object.prototype.valueOf() binding
1 parent d79f982 commit e255925

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ extern {
254254
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
255255
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
256256
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
257+
258+
/// The valueOf() method returns the primitive value of the
259+
/// specified object.
260+
///
261+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
262+
#[wasm_bindgen(method, js_name = valueOf)]
263+
pub fn value_of(this: &Object) -> Object;
257264
}
258265

259266
// String

tests/all/js_globals/Object.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,31 @@ fn to_locale_string() {
184184
"#)
185185
.test()
186186
}
187+
188+
#[test]
189+
fn value_of() {
190+
project()
191+
.file("src/lib.rs", r#"
192+
#![feature(proc_macro, wasm_custom_section)]
193+
194+
extern crate wasm_bindgen;
195+
use wasm_bindgen::prelude::*;
196+
use wasm_bindgen::js;
197+
198+
#[wasm_bindgen]
199+
pub fn value_of(obj: &js::Object) -> js::Object {
200+
obj.value_of()
201+
}
202+
"#)
203+
.file("test.ts", r#"
204+
import * as assert from "assert";
205+
import * as wasm from "./out";
206+
207+
export function test() {
208+
const obj = { foo: 42 };
209+
assert.strictEqual(wasm.value_of(obj), obj);
210+
assert.notStrictEqual(wasm.value_of(obj), { foo: 42 });
211+
}
212+
"#)
213+
.test()
214+
}

0 commit comments

Comments
 (0)