From 4a96ba3c72f163cb54f749caebc70c6bf520a51c Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:16:57 -0400 Subject: [PATCH 01/13] add binding for indexOf --- src/js.rs | 12 ++++++++++ tests/all/js_globals/Array.rs | 41 +++++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 54 insertions(+) create mode 100644 tests/all/js_globals/Array.rs diff --git a/src/js.rs b/src/js.rs index f7790197..0d39a508 100644 --- a/src/js.rs +++ b/src/js.rs @@ -94,3 +94,15 @@ extern { } + +// Array +#[wasm_bindgen] +extern { + pub type Array; + + /// The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf + #[wasm_bindgen(method, js_name = indexOf)] + pub fn index_of(this: &Array, value: JsValue, from_index: i32) -> i32; +} \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs new file mode 100644 index 00000000..f4699253 --- /dev/null +++ b/tests/all/js_globals/Array.rs @@ -0,0 +1,41 @@ +#![allow(non_snake_case)] + +use project; + +#[test] +fn index_of() { + 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 get_index_of(this: &js::Array, value: JsValue, from_index: i32) -> i32 { + this.index_of(value, from_index) + } + + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = ["a", "c", "x", "n"]; + let index = wasm.get_index_of(characters, "x", 0); + let notFoundIndex = wasm.get_index_of(characters, "z", 0); + + assert.equal(index, 2); + assert.equal(notFoundIndex, -1); + + let withFromIndex = wasm.get_index_of(characters, "x", -3); + let withFromIndexNotFound = wasm.get_index_of(characters, "a", -2); + + assert.equal(withFromIndex, 2); + assert.equal(withFromIndexNotFound, -1); + } + "#) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index a7eaaeeb..8ab08574 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -3,6 +3,7 @@ use super::project; mod Object; +mod Array; #[test] #[cfg(feature = "std")] From 667733e9290ce81e56bcdfe0fbbd8e3ac29993d4 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:36:35 -0400 Subject: [PATCH 02/13] add binding for lastIndexOf --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/js.rs b/src/js.rs index 0d39a508..7fd0737c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -105,4 +105,11 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf #[wasm_bindgen(method, js_name = indexOf)] pub fn index_of(this: &Array, value: JsValue, from_index: i32) -> i32; + + /// The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. + /// The array is searched backwards, starting at fromIndex. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf + #[wasm_bindgen(method, js_name = lastIndexOf)] + pub fn last_index_of(this: &Array, value: JsValue, from_index: i32) -> i32; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index f4699253..98354206 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -39,3 +39,41 @@ fn index_of() { "#) .test() } + +#[test] +fn last_index_of() { + 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 get_last_index_of(this: &js::Array, value: JsValue, from_index: i32) -> i32 { + this.last_index_of(value, from_index) + } + + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = ["a", "x", "c", "x", "n"]; + let index = wasm.get_last_index_of(characters, "x", 5); + let notFoundIndex = wasm.get_last_index_of(characters, "z", 5); + + assert.equal(index, 3); + assert.equal(notFoundIndex, -1); + + let withFromIndex = wasm.get_last_index_of(characters, "x", 2); + let withFromIndexNotFound = wasm.get_last_index_of(characters, "x", 0); + + assert.equal(withFromIndex, 1); + assert.equal(withFromIndexNotFound, -1); + } + "#) + .test() +} \ No newline at end of file From 0b80888c0d2bb14d0c6766929bf894c6dc116eb0 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:38:47 -0400 Subject: [PATCH 03/13] add binding for join --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 7fd0737c..079d65af 100644 --- a/src/js.rs +++ b/src/js.rs @@ -112,4 +112,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf #[wasm_bindgen(method, js_name = lastIndexOf)] pub fn last_index_of(this: &Array, value: JsValue, from_index: i32) -> i32; + + /// The join() method joins all elements of an array (or an array-like object) into a string and returns this string. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join + #[wasm_bindgen(method)] + pub fn join(this: &Array, delimiter: &str) -> String; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 98354206..da37a2da 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -76,4 +76,36 @@ fn last_index_of() { } "#) .test() -} \ No newline at end of file +} + +#[test] +fn join() { + 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 join_array(this: &js::Array, delimiter: &str) -> String { + this.join(delimiter) + } + + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = ["a", "c", "x", "n"]; + let stringValue = wasm.join_array(characters, ", "); + + assert.equal("a, c, x, n", stringValue); + let withForwardSlash = wasm.join_array(characters, "/"); + assert.equal("a/c/x/n", withForwardSlash); + } + "#) + .test() +} From e8bb0c2f98119579a9bf907358b122407cf3abe1 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:46:10 -0400 Subject: [PATCH 04/13] add binding for slice --- src/js.rs | 8 ++++++++ tests/all/js_globals/Array.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index 079d65af..e3b0f217 100644 --- a/src/js.rs +++ b/src/js.rs @@ -118,4 +118,12 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join #[wasm_bindgen(method)] pub fn join(this: &Array, delimiter: &str) -> String; + + /// The slice() method returns a shallow copy of a portion of an array into a new array + /// object selected from begin to end (end not included). + /// The original array will not be modified. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice + #[wasm_bindgen(method)] + pub fn slice(this: &Array, start: u32, end: u32) -> Array; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index da37a2da..8e89f3e8 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -109,3 +109,33 @@ fn join() { "#) .test() } + +#[test] +fn slice() { + 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 create_slice(this: &js::Array, start: u32, end: u32) -> js::Array { + this.slice(start, end) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = ["a", "c", "x", "n", 1, "8"]; + let subset = wasm.create_slice(characters, 1, 3); + + assert.equal(subset[0], "c"); + assert.equal(subset[1], "x"); + } + "#) + .test() +} \ No newline at end of file From d155136f0eddf747b48b8218c48f62237fad0c55 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:49:34 -0400 Subject: [PATCH 05/13] add binding for fill --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index e3b0f217..fc614e4c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -126,4 +126,11 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice #[wasm_bindgen(method)] pub fn slice(this: &Array, start: u32, end: u32) -> Array; + + /// The fill() method fills all the elements of an array from a start index to an end index with a static value. + /// The end index is not included. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill + #[wasm_bindgen(method)] + pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 8e89f3e8..f83f56e2 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -138,4 +138,34 @@ fn slice() { } "#) .test() +} + +#[test] +fn fill() { + 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 fill_with(this: &js::Array, value: JsValue, start: u32, end: u32) -> js::Array { + this.fill(value, start, end) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = ["a", "c", "x", "n", 1, "8"]; + let subset = wasm.fill_with(characters, 0, 0, 3); + + assert.equal(subset[0], 0); + assert.equal(subset[4], 1); + } + "#) + .test() } \ No newline at end of file From 2f6f734216d81bb850002d4c5066d03c56defe79 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:51:02 -0400 Subject: [PATCH 06/13] add binding for copyWithin --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index fc614e4c..91166f2f 100644 --- a/src/js.rs +++ b/src/js.rs @@ -133,4 +133,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill #[wasm_bindgen(method)] pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array; + + /// The copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin + #[wasm_bindgen(method, js_name = copyWithin)] + pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index f83f56e2..84297be0 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -168,4 +168,38 @@ fn fill() { } "#) .test() -} \ No newline at end of file +} + +#[test] +fn copy_within() { + 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 copy_values_within_array(this: &js::Array, target: i32, start: i32, end: i32) -> js::Array { + this.copy_within(target, start, end) + } + + "#) + .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] + wasm.copy_values_within_array(characters, 1, 4, 5); + + assert.equal(characters[1], 1); + + // if negatives were used + wasm.copy_values_within_array(characters, -1, -3, -2); + assert.equal(characters[5], 3); + } + "#) + .test() +} From 73081180cdb74b64dc274fd051a17d3eca03bd81 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:55:25 -0400 Subject: [PATCH 07/13] add binding for pop --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/js.rs b/src/js.rs index 91166f2f..4f7b8cc1 100644 --- a/src/js.rs +++ b/src/js.rs @@ -139,4 +139,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin #[wasm_bindgen(method, js_name = copyWithin)] pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array; + + /// The pop() method removes the last element from an array and returns that element. This method changes the length of the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop + #[wasm_bindgen(method)] + pub fn pop(this: &Array) -> JsValue; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 84297be0..7b888955 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -203,3 +203,33 @@ fn copy_within() { "#) .test() } + +#[test] +fn pop() { + 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 pop_in_it(this: &js::Array) -> JsValue { + this.pop() + } + + "#) + .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 item = wasm.pop_in_it(characters); + assert.equal(item, 2); + assert.equal(characters.length, 5); + } + "#) + .test() +} From 4611d7bdba38879fb4058b4bd5be505c1c2e7ac1 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:58:15 -0400 Subject: [PATCH 08/13] add binding for push --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index 4f7b8cc1..0fd94ea9 100644 --- a/src/js.rs +++ b/src/js.rs @@ -145,4 +145,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop #[wasm_bindgen(method)] pub fn pop(this: &Array) -> JsValue; + + /// The push() method adds one or more elements to the end of an array and returns the new length of the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push + #[wasm_bindgen(method)] + pub fn push(this: &Array, value: JsValue) -> u32; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 7b888955..ff28217d 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -233,3 +233,34 @@ fn pop() { "#) .test() } + + +#[test] +fn push() { + 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 push_it_along(this: &js::Array, value: JsValue) -> u32 { + this.push(value) + } + + "#) + .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 length = wasm.push_it_along(characters, "a"); + assert.equal(length, 7); + assert.equal(characters[6], "a"); + } + "#) + .test() +} \ No newline at end of file From d705cd8bbfadeb1a2a7bdb7f0a16c94e6b37444c Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:00:58 -0400 Subject: [PATCH 09/13] add binding for reverse --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index 0fd94ea9..246fa759 100644 --- a/src/js.rs +++ b/src/js.rs @@ -151,4 +151,11 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push #[wasm_bindgen(method)] pub fn push(this: &Array, value: JsValue) -> u32; + + /// The reverse() method reverses an array in place. + /// The first array element becomes the last, and the last array element becomes the first. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse + #[wasm_bindgen(method)] + pub fn reverse(this: &Array) -> Array; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index ff28217d..175267b4 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -263,4 +263,34 @@ fn push() { } "#) .test() +} + +#[test] +fn reverse() { + 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 reverse_array(this: &js::Array) -> js::Array { + this.reverse() + } + + "#) + .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 reversed = wasm.reverse_array(characters); + assert.equal(reversed[0], 2); + assert.equal(reversed[5], 8); + } + "#) + .test() } \ No newline at end of file From 68acbeab25f4d94ff35769e9d37e735dd2bc2d33 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:03:26 -0400 Subject: [PATCH 10/13] add binding for shift --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index 246fa759..ca1cbc3e 100644 --- a/src/js.rs +++ b/src/js.rs @@ -158,4 +158,11 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse #[wasm_bindgen(method)] pub fn reverse(this: &Array) -> Array; + + /// The shift() method removes the first element from an array and returns that removed element. + /// This method changes the length of the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift + #[wasm_bindgen(method)] + pub fn shift(this: &Array) -> JsValue; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 175267b4..2efa8150 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -293,4 +293,35 @@ fn reverse() { } "#) .test() +} + +#[test] +fn shift() { + 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 shift_item(this: &js::Array) -> JsValue { + this.shift() + } + + "#) + .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 shiftedItem = wasm.shift_item(characters); + + assert.equal(shiftedItem, 8); + assert.equal(characters.length, 5); + } + "#) + .test() } \ No newline at end of file From 3cf522d2df4f4ca30570714f591191fd1dd41282 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:06:08 -0400 Subject: [PATCH 11/13] add bindings for unshift --- src/js.rs | 6 ++++++ tests/all/js_globals/Array.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index ca1cbc3e..bb6fa8fe 100644 --- a/src/js.rs +++ b/src/js.rs @@ -165,4 +165,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift #[wasm_bindgen(method)] pub fn shift(this: &Array) -> JsValue; + + /// The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. + /// + /// 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; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 2efa8150..b9879f93 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -324,4 +324,35 @@ fn shift() { } "#) .test() +} + +#[test] +fn unshift() { + 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 unshift_item(this: &js::Array, value: JsValue) -> u32 { + this.unshift(value) + } + + "#) + .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 length = wasm.unshift_item(characters, "abba"); + + assert.equal(length, 7); + assert.equal(characters[0], "abba"); + } + "#) + .test() } \ No newline at end of file From eb6c2a239cdfafbcdd0cbce7951b5a20e7d46910 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:23:26 -0400 Subject: [PATCH 12/13] 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() +} From a8cd428850a5bb0129841052c19b218618bc9593 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:27:10 -0400 Subject: [PATCH 13/13] add binding for includes --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/js.rs b/src/js.rs index 52168b70..8c9e9deb 100644 --- a/src/js.rs +++ b/src/js.rs @@ -177,4 +177,11 @@ extern { /// 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; + + /// The includes() method determines whether an array includes a certain element, returning true or false as appropriate. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes + #[wasm_bindgen(method)] + pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool; + } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 44aa4ca4..ae6f893e 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -386,3 +386,39 @@ fn to_string() { "#) .test() } + + +#[test] +fn includes() { + 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_includes(this: &js::Array, value: JsValue, from_index: i32) -> bool { + this.includes(value, from_index) + } + + "#) + .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 isTwoIncluded = wasm.array_includes(characters, 2, 0); + let isNineIncluded = wasm.array_includes(characters, 9, 0); + + assert.ok(isTwoIncluded); + assert.ok(!isNineIncluded); + + let isThreeIncluded = wasm.array_includes(characters, 3, 4); + assert.ok(!isThreeIncluded); + } + "#) + .test() +}