From 228abaa4ae8bb0bcf12ce984cd8d7e43eac819fa Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:58:34 +0200 Subject: [PATCH] feat(Map/MapIterator): add Map.entries --- src/js.rs | 14 +++++++++++ tests/all/js_globals/MapIterator.rs | 36 +++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 51 insertions(+) create mode 100644 tests/all/js_globals/MapIterator.rs diff --git a/src/js.rs b/src/js.rs index 0b04b6d33..21b512033 100644 --- a/src/js.rs +++ b/src/js.rs @@ -355,6 +355,20 @@ extern { pub fn size(this: &Map) -> Number; } +// Map Iterator +#[wasm_bindgen] +extern { + pub type MapIterator; + + /// The entries() method returns a new Iterator object that contains + /// the [key, value] pairs for each element in the Map object in + /// insertion order. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries + #[wasm_bindgen(method)] + pub fn entries(this: &Map) -> MapIterator; +} + // Math #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/MapIterator.rs b/tests/all/js_globals/MapIterator.rs new file mode 100644 index 000000000..54a4f4abe --- /dev/null +++ b/tests/all/js_globals/MapIterator.rs @@ -0,0 +1,36 @@ +#![allow(non_snake_case)] + +use project; + + +#[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::Map) -> js::MapIterator { + this.entries() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + const iterator = map.entries(); + const wasmIterator = wasm.get_entries(map); + map.set('foo', 'bar'); + map.set('bar', 'baz'); + + assert.equal(iterator.toString(), wasmIterator.toString()); + } + "#) + .test() +} \ No newline at end of file diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index f634773ee..799ef0232 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -9,6 +9,7 @@ mod Date; mod Function; mod JsString; mod Map; +mod MapIterator; mod Math; mod WeakMap; mod WeakSet;