Js binding for to_string

This commit is contained in:
Jonathan Sundqvist 2018-06-20 20:46:04 +02:00
parent 5eda5504e9
commit d89c7958eb
2 changed files with 41 additions and 0 deletions

View File

@ -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;
}

View File

@ -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()
}