add binding for toString

This commit is contained in:
Matt Long 2018-06-20 18:23:26 -04:00
parent 3cf522d2df
commit eb6c2a239c
2 changed files with 37 additions and 1 deletions

View File

@ -171,4 +171,10 @@ extern {
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
#[wasm_bindgen(method)]
pub fn unshift(this: &Array, value: JsValue) -> u32;
/// The toString() method returns a string representing the specified array and its elements.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
#[wasm_bindgen(method, js_name = toString)]
pub fn to_string(this: &Array) -> String;
}

View File

@ -355,4 +355,34 @@ fn unshift() {
}
"#)
.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 array_to_string(this: &js::Array) -> String {
this.to_string()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let characters = [8, 5, 4, 3, 1, 2]
let arrayString = wasm.array_to_string(characters);
assert.equal(arrayString, "8,5,4,3,1,2");
}
"#)
.test()
}