Skip to content

Commit a95476a

Browse files
committed
add binding for entries method
1 parent 4cc7387 commit a95476a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/js.rs

+7
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ extern {
197197
pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool;
198198
}
199199

200+
// Array Iterator
200201
#[wasm_bindgen]
201202
extern {
202203
pub type ArrayIterator;
@@ -206,4 +207,10 @@ extern {
206207
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
207208
#[wasm_bindgen(method)]
208209
pub fn keys(this: &Array) -> ArrayIterator;
210+
211+
/// The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
212+
///
213+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
214+
#[wasm_bindgen(method)]
215+
pub fn entries(this: &Array) -> ArrayIterator;
209216
}

tests/all/js_globals/ArrayIterator.rs

+35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,41 @@ fn keys() {
2828
let wasmIterator = wasm.get_keys(characters);
2929
3030
assert.equal(iterator.toString(), wasmIterator.toString());
31+
assert.equal(Array.from(iterator)[0], Array.from(wasmIterator)[0]);
32+
}
33+
"#)
34+
.test()
35+
}
36+
37+
#[test]
38+
fn entries() {
39+
project()
40+
.file("src/lib.rs", r#"
41+
#![feature(proc_macro, wasm_custom_section)]
42+
43+
extern crate wasm_bindgen;
44+
use wasm_bindgen::prelude::*;
45+
use wasm_bindgen::js;
46+
47+
#[wasm_bindgen]
48+
pub fn get_entries(this: &js::Array) -> js::ArrayIterator {
49+
this.entries()
50+
}
51+
52+
"#)
53+
.file("test.ts", r#"
54+
import * as assert from "assert";
55+
import * as wasm from "./out";
56+
57+
export function test() {
58+
let characters = [8, 5, 4, 3, 1, 2]
59+
let iterator = characters.entries();
60+
let wasmIterator = wasm.get_entries(characters);
61+
let jsItem = iterator.next();
62+
let wasmItem = wasmIterator.next();
63+
64+
assert.equal(iterator.toString(), wasmIterator.toString());
65+
assert.equal(jsItem.value[1], wasmItem.value[1]);
3166
}
3267
"#)
3368
.test()

0 commit comments

Comments
 (0)