From eb6c2a239cdfafbcdd0cbce7951b5a20e7d46910 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:23:26 -0400 Subject: [PATCH] add binding for toString --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index bb6fa8fe..52168b70 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index b9879f93..44aa4ca4 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -355,4 +355,34 @@ fn unshift() { } "#) .test() -} \ No newline at end of file +} + +#[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() +}