Map.prototype.forEach binding. (#501)

This commit is contained in:
data-pup 2018-07-18 11:30:52 -04:00 committed by Alex Crichton
parent bc474aceba
commit f0dcdc249c
2 changed files with 52 additions and 0 deletions

View File

@ -771,6 +771,13 @@ extern {
#[wasm_bindgen(method)]
pub fn delete(this: &Map, key: &str) -> bool;
/// The forEach() method executes a provided function once per each
/// key/value pair in the Map object, in insertion order.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
#[wasm_bindgen(method, js_name = forEach)]
pub fn for_each(this: &Map, callback: &mut FnMut(JsValue, JsValue));
/// The get() method returns a specified element from a Map object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

View File

@ -66,6 +66,51 @@ fn delete() {
.test()
}
#[test]
fn for_each() {
project()
.file("src/lib.rs", r#"
#![feature(use_extern_macros, wasm_custom_section)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use wasm_bindgen::js;
#[wasm_bindgen]
pub fn get_bool_vals(this: &js::Map) -> js::Map {
let res = js::Map::new();
this.for_each(&mut |value, key| {
if value.as_bool().is_some() {
res.set(&key, &value);
}
});
res
}
"#)
.file("test.js", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const map = new Map();
map.set(1, true);
map.set(2, false);
map.set(3, "awoo");
map.set(4, 100);
map.set(5, []);
map.set(6, {});
const res = wasm.get_bool_vals(map);
assert.equal(map.size, 6);
assert.equal(res.size, 2);
assert.equal(res.get(1), true);
assert.equal(res.get(2), false);
}
"#)
.test()
}
#[test]
fn get() {
project()