Add Array.prototype.values binding (#395)

This commit is contained in:
Michael Hoffmann 2018-07-09 19:42:30 +02:00 committed by Alex Crichton
parent 21cb50ef05
commit bae324c951
2 changed files with 52 additions and 6 deletions

View File

@ -339,6 +339,13 @@ extern "C" {
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
#[wasm_bindgen(method)]
pub fn entries(this: &Array) -> ArrayIterator;
/// The values() method returns a new Array Iterator object that
/// contains the values for each index in the array.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values
#[wasm_bindgen(method)]
pub fn values(this: &Array) -> ArrayIterator;
}
// Boolean

View File

@ -28,9 +28,9 @@ fn keys() {
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);
let numbers = [8, 5, 4, 3, 1, 2];
let iterator = numbers.keys();
let wasmIterator = wasm.get_keys(numbers);
assert.equal(iterator.toString(), wasmIterator.toString());
assert.equal(Array.from(iterator)[0], Array.from(wasmIterator)[0]);
@ -66,9 +66,9 @@ fn entries() {
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 numbers = [8, 5, 4, 3, 1, 2];
let iterator = numbers.entries();
let wasmIterator = wasm.get_entries(numbers);
let jsItem = iterator.next();
let wasmItem = wasmIterator.next();
@ -79,3 +79,42 @@ fn entries() {
)
.test()
}
#[test]
fn values() {
project()
.headless(true) // Node.js does not have values()
.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_values(this: &js::Array) -> js::ArrayIterator {
this.values()
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let numbers = [8, 3, 2];
let wasmIterator = wasm.get_values(numbers);
assert.equal(wasmIterator.next().value, 8);
assert.equal(wasmIterator.next().value, 3);
assert.equal(wasmIterator.next().value, 2);
assert.ok(wasmIterator.next().done);
}
"#,
)
.test()
}