diff --git a/src/js.rs b/src/js.rs index 98e57928e..f77901977 100644 --- a/src/js.rs +++ b/src/js.rs @@ -85,4 +85,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty #[wasm_bindgen(method, js_name = hasOwnProperty)] pub fn has_own_property(this: &Object, property: &str) -> bool; + + /// The toString() method returns a string representing the object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString + #[wasm_bindgen(method, js_name = toString)] + pub fn to_string(this: &Object) -> String; + + } diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index 0c2f8311b..487f57441 100755 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -54,3 +54,36 @@ fn has_own_property() { "#) .test() } + +#[test] +fn to_string() { + 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 to_string(obj: &js::Object) -> String { + obj.to_string() + } + + #[wasm_bindgen] + pub fn test() { + let object = js::Object::new(); + assert_eq!(object.to_string(), "[object Object]"); + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.to_string({ foo: 42 }), "[object Object]"); + wasm.test(); + } + "#) + .test() +}