Skip to content

Commit 4cc7387

Browse files
committed
add binding for keys method
1 parent 0b29721 commit 4cc7387

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/js.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,15 @@ extern {
195195
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
196196
#[wasm_bindgen(method)]
197197
pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool;
198+
}
199+
200+
#[wasm_bindgen]
201+
extern {
202+
pub type ArrayIterator;
198203

204+
/// The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
205+
///
206+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
207+
#[wasm_bindgen(method)]
208+
pub fn keys(this: &Array) -> ArrayIterator;
199209
}

tests/all/js_globals/ArrayIterator.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![allow(non_snake_case)]
2+
3+
use project;
4+
5+
#[test]
6+
fn keys() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn get_keys(this: &js::Array) -> js::ArrayIterator {
17+
this.keys()
18+
}
19+
20+
"#)
21+
.file("test.ts", r#"
22+
import * as assert from "assert";
23+
import * as wasm from "./out";
24+
25+
export function test() {
26+
let characters = [8, 5, 4, 3, 1, 2]
27+
let iterator = characters.keys();
28+
let wasmIterator = wasm.get_keys(characters);
29+
30+
assert.equal(iterator.toString(), wasmIterator.toString());
31+
}
32+
"#)
33+
.test()
34+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::project;
44

55
mod Object;
66
mod Array;
7+
mod ArrayIterator;
78

89
#[test]
910
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)