From 4cc73877a6a45f1f73b6a48cf6866a5dc38c915c Mon Sep 17 00:00:00 2001 From: Matt Long Date: Thu, 21 Jun 2018 17:00:02 -0400 Subject: [PATCH 1/2] add binding for keys method --- src/js.rs | 10 ++++++++ tests/all/js_globals/ArrayIterator.rs | 34 +++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 45 insertions(+) create mode 100644 tests/all/js_globals/ArrayIterator.rs diff --git a/src/js.rs b/src/js.rs index 561451a0a..86980e643 100644 --- a/src/js.rs +++ b/src/js.rs @@ -195,5 +195,15 @@ extern { /// 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; +} +#[wasm_bindgen] +extern { + pub type ArrayIterator; + + /// The keys() method returns a new Array Iterator object that contains the keys for each index in the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys + #[wasm_bindgen(method)] + pub fn keys(this: &Array) -> ArrayIterator; } \ No newline at end of file diff --git a/tests/all/js_globals/ArrayIterator.rs b/tests/all/js_globals/ArrayIterator.rs new file mode 100644 index 000000000..7afedf749 --- /dev/null +++ b/tests/all/js_globals/ArrayIterator.rs @@ -0,0 +1,34 @@ +#![allow(non_snake_case)] + +use project; + +#[test] +fn keys() { + 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_keys(this: &js::Array) -> js::ArrayIterator { + this.keys() + } + + "#) + .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 iterator = characters.keys(); + let wasmIterator = wasm.get_keys(characters); + + assert.equal(iterator.toString(), wasmIterator.toString()); + } + "#) + .test() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 8ab08574d..d9c58b5c4 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -4,6 +4,7 @@ use super::project; mod Object; mod Array; +mod ArrayIterator; #[test] #[cfg(feature = "std")] From a95476a8eed547406776d198277ecdb1b67bb08d Mon Sep 17 00:00:00 2001 From: Matt Long Date: Thu, 21 Jun 2018 17:11:08 -0400 Subject: [PATCH 2/2] add binding for entries method --- src/js.rs | 7 ++++++ tests/all/js_globals/ArrayIterator.rs | 35 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/js.rs b/src/js.rs index 86980e643..db35dd623 100644 --- a/src/js.rs +++ b/src/js.rs @@ -197,6 +197,7 @@ extern { pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool; } +// Array Iterator #[wasm_bindgen] extern { pub type ArrayIterator; @@ -206,4 +207,10 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys #[wasm_bindgen(method)] pub fn keys(this: &Array) -> ArrayIterator; + + /// The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries + #[wasm_bindgen(method)] + pub fn entries(this: &Array) -> ArrayIterator; } \ No newline at end of file diff --git a/tests/all/js_globals/ArrayIterator.rs b/tests/all/js_globals/ArrayIterator.rs index 7afedf749..09755cc8c 100644 --- a/tests/all/js_globals/ArrayIterator.rs +++ b/tests/all/js_globals/ArrayIterator.rs @@ -28,6 +28,41 @@ fn keys() { let wasmIterator = wasm.get_keys(characters); assert.equal(iterator.toString(), wasmIterator.toString()); + assert.equal(Array.from(iterator)[0], Array.from(wasmIterator)[0]); + } + "#) + .test() +} + +#[test] +fn entries() { + 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_entries(this: &js::Array) -> js::ArrayIterator { + this.entries() + } + + "#) + .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 iterator = characters.entries(); + let wasmIterator = wasm.get_entries(characters); + let jsItem = iterator.next(); + let wasmItem = wasmIterator.next(); + + assert.equal(iterator.toString(), wasmIterator.toString()); + assert.equal(jsItem.value[1], wasmItem.value[1]); } "#) .test()