From ab0546963b32f6870ab9aa8c724f24b2498b7b9a Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:51:39 +0200 Subject: [PATCH 01/10] feat(Map): add Map.clear --- src/js.rs | 12 ++++++++++++ tests/all/js_globals/Map.rs | 35 +++++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 48 insertions(+) create mode 100644 tests/all/js_globals/Map.rs diff --git a/src/js.rs b/src/js.rs index fd16b23e..bd7104a6 100644 --- a/src/js.rs +++ b/src/js.rs @@ -302,6 +302,18 @@ extern { pub fn to_string(this: &Function) -> JsString; } +// Map +#[wasm_bindgen] +extern { + pub type Map; + + /// The clear() method removes all elements from a Map object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear + #[wasm_bindgen(method)] + pub fn clear(this: &Map); +} + // Math #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs new file mode 100644 index 00000000..8b2b2df2 --- /dev/null +++ b/tests/all/js_globals/Map.rs @@ -0,0 +1,35 @@ +#![allow(non_snake_case)] + +use project; + +#[test] +fn clear() { + 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 map_clear(this: &js::Map) { + this.clear(); + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + map.set('bar', 'baz'); + assert.equal(map.size, 2); + wasm.map_clear(map); + assert.equal(map.size, 0); + + } + "#) + .test() +} \ No newline at end of file diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 4090fe04..f634773e 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -8,6 +8,7 @@ mod Boolean; mod Date; mod Function; mod JsString; +mod Map; mod Math; mod WeakMap; mod WeakSet; From f7e4019e72e5136e1e2a6f3a9edec0f87a43ac1d Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:52:27 +0200 Subject: [PATCH 02/10] feat(Map): add Map.delete --- src/js.rs | 6 ++++++ tests/all/js_globals/Map.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index bd7104a6..b6a55114 100644 --- a/src/js.rs +++ b/src/js.rs @@ -312,6 +312,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear #[wasm_bindgen(method)] pub fn clear(this: &Map); + + /// The delete() method removes the specified element from a Map object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete + #[wasm_bindgen(method)] + pub fn delete(this: &Map, key: &str) -> bool; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 8b2b2df2..a8a7ace3 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -32,4 +32,36 @@ fn clear() { } "#) .test() +} + +#[test] +fn delete() { + 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 map_delete(this: &js::Map, key: &str) -> bool { + this.delete(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + assert.equal(map.size, 1); + assert.equal(wasm.map_delete(map, 'foo'), true); + assert.equal(wasm.map_delete(map, 'bar'), false); + assert.equal(map.size, 0); + + } + "#) + .test() } \ No newline at end of file From e0b399643aa42e15462b1aaddb2b30233fe3dfd5 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:53:20 +0200 Subject: [PATCH 03/10] feat(Map): add Map.get --- src/js.rs | 6 ++++++ tests/all/js_globals/Map.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index b6a55114..e53ae6da 100644 --- a/src/js.rs +++ b/src/js.rs @@ -318,6 +318,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete #[wasm_bindgen(method)] pub fn delete(this: &Map, key: &str) -> bool; + + /// 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 + #[wasm_bindgen(method)] + pub fn get(this: &Map, key: &JsValue) -> JsValue; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index a8a7ace3..1279c482 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -64,4 +64,35 @@ fn delete() { } "#) .test() +} + +#[test] +fn get() { + 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 map_get(this: &js::Map, key: &JsValue) -> JsValue { + this.get(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + map.set(1, 2) + assert.equal(wasm.map_get(map, 'foo'), 'bar'); + assert.equal(wasm.map_get(map, 1), 2); + assert.equal(wasm.map_get(map, 2), undefined); + } + "#) + .test() } \ No newline at end of file From 07e61e11750efd2fae16d3f32afebd5a68cf9f49 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:54:10 +0200 Subject: [PATCH 04/10] feat(Map): add Map.has --- src/js.rs | 7 +++++++ tests/all/js_globals/Map.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/js.rs b/src/js.rs index e53ae6da..171c9632 100644 --- a/src/js.rs +++ b/src/js.rs @@ -324,6 +324,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get #[wasm_bindgen(method)] pub fn get(this: &Map, key: &JsValue) -> JsValue; + + /// The has() method returns a boolean indicating whether an element with + /// the specified key exists or not. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has + #[wasm_bindgen(method)] + pub fn has(this: &Map, key: &JsValue) -> bool; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 1279c482..5c31c4c2 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -95,4 +95,33 @@ fn get() { } "#) .test() +} + +#[test] +fn has() { + 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 has(this: &js::Map, key: &JsValue) -> bool { + this.has(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + assert.equal(wasm.has(map, 'foo'), true); + assert.equal(wasm.has(map, 'bar'), false); + } + "#) + .test() } \ No newline at end of file From 27ee57175ae8f41ba54c441a5d8409b4bfd12e37 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:55:10 +0200 Subject: [PATCH 05/10] feat(Map): add Map.new --- src/js.rs | 7 +++++++ tests/all/js_globals/Map.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/js.rs b/src/js.rs index 171c9632..1e5d5f97 100644 --- a/src/js.rs +++ b/src/js.rs @@ -331,6 +331,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has #[wasm_bindgen(method)] pub fn has(this: &Map, key: &JsValue) -> bool; + + /// The Map object holds key-value pairs. Any value (both objects and + /// primitive values) maybe used as either a key or a value. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map + #[wasm_bindgen(constructor)] + pub fn new() -> Map; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 5c31c4c2..2bbdddd9 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -124,4 +124,32 @@ fn has() { } "#) .test() +} + +#[test] +fn new() { + 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 new_map() -> js::Map { + js::Map::new() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = wasm.new_map(); + + assert.equal(map.size, 0); + } + "#) + .test() } \ No newline at end of file From 6f90bd677b4665418dd8dfd4f61197b496473cb7 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:55:55 +0200 Subject: [PATCH 06/10] feat(Map): add Map.set --- src/js.rs | 7 +++++++ tests/all/js_globals/Map.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/js.rs b/src/js.rs index 1e5d5f97..fae6379d 100644 --- a/src/js.rs +++ b/src/js.rs @@ -338,6 +338,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map #[wasm_bindgen(constructor)] pub fn new() -> Map; + + /// The set() method adds or updates an element with a specified key + /// and value to a Map object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set + #[wasm_bindgen(method)] + pub fn set(this: &Map, key: &JsValue, value: &JsValue) -> Map; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 2bbdddd9..049b0df6 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -152,4 +152,33 @@ fn new() { } "#) .test() +} + +#[test] +fn set() { + 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 set(this: &js::Map, key: &JsValue, value: &JsValue) -> js::Map { + this.set(key, value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + const newMap = wasm.set(map, 'foo', 'bar'); + assert.equal(map.has('foo'), true); + assert.equal(newMap.has('foo'), true); + } + "#) + .test() } \ No newline at end of file From ea19775639eedb05ef8c274efa07b8b5627d80dc Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:56:49 +0200 Subject: [PATCH 07/10] feat(Map): add Map.size --- src/js.rs | 8 ++++++++ tests/all/js_globals/Map.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index fae6379d..0b04b6d3 100644 --- a/src/js.rs +++ b/src/js.rs @@ -345,6 +345,14 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set #[wasm_bindgen(method)] pub fn set(this: &Map, key: &JsValue, value: &JsValue) -> Map; + + /// The value of size is an integer representing how many entries + /// the Map object has. A set accessor function for size is undefined; + /// you can not change this property. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size + #[wasm_bindgen(method, getter, structural)] + pub fn size(this: &Map) -> Number; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 049b0df6..522e7fde 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -181,4 +181,33 @@ fn set() { } "#) .test() +} + +#[test] +fn size() { + 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 map_size(this: &js::Map) -> js::Number { + this.size() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + map.set('bar', 'baz'); + assert.equal(wasm.map_size(map), 2); + } + "#) + .test() } \ No newline at end of file From 228abaa4ae8bb0bcf12ce984cd8d7e43eac819fa Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:58:34 +0200 Subject: [PATCH 08/10] 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 0b04b6d3..21b51203 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 00000000..54a4f4ab --- /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 f634773e..799ef023 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; From fc131ee97e6c778a2568fe89f5eccb579418be4a Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:59:11 +0200 Subject: [PATCH 09/10] feat(Map/MapIterator): add Map.keys --- src/js.rs | 7 +++++++ tests/all/js_globals/MapIterator.rs | 32 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/js.rs b/src/js.rs index 21b51203..7336d658 100644 --- a/src/js.rs +++ b/src/js.rs @@ -367,6 +367,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries #[wasm_bindgen(method)] pub fn entries(this: &Map) -> MapIterator; + + /// The keys() method returns a new Iterator object that contains the + /// keys for each element in the Map object in insertion order. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys + #[wasm_bindgen(method)] + pub fn keys(this: &Map) -> MapIterator; } // Math diff --git a/tests/all/js_globals/MapIterator.rs b/tests/all/js_globals/MapIterator.rs index 54a4f4ab..bb560e42 100644 --- a/tests/all/js_globals/MapIterator.rs +++ b/tests/all/js_globals/MapIterator.rs @@ -33,4 +33,36 @@ fn entries() { } "#) .test() +} + +#[test] +fn keys() { + 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_keys(this: &js::Map) -> js::MapIterator { + this.keys() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + const iterator = map.keys(); + const wasmIterator = wasm.get_keys(map); + map.set('foo', 'bar'); + map.set('bar', 'baz'); + + assert.equal(iterator.toString(), wasmIterator.toString()); + } + "#) + .test() } \ No newline at end of file From e0a70417ce46c792134082711c6ef4065302b07d Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 22:00:02 +0200 Subject: [PATCH 10/10] feat(Map/MapIterator): add Map.values --- src/js.rs | 7 +++++++ tests/all/js_globals/MapIterator.rs | 32 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/js.rs b/src/js.rs index 7336d658..3e59b63f 100644 --- a/src/js.rs +++ b/src/js.rs @@ -374,6 +374,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys #[wasm_bindgen(method)] pub fn keys(this: &Map) -> MapIterator; + + /// The values() method returns a new Iterator object that contains the + /// values for each element in the Map object in insertion order. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values + #[wasm_bindgen(method)] + pub fn values(this: &Map) -> MapIterator; } // Math diff --git a/tests/all/js_globals/MapIterator.rs b/tests/all/js_globals/MapIterator.rs index bb560e42..0bc79e25 100644 --- a/tests/all/js_globals/MapIterator.rs +++ b/tests/all/js_globals/MapIterator.rs @@ -65,4 +65,36 @@ fn keys() { } "#) .test() +} + +#[test] +fn values() { + 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_values(this: &js::Map) -> js::MapIterator { + this.values() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + const iterator = map.keys(); + const wasmIterator = wasm.get_values(map); + map.set('foo', 'bar'); + map.set('bar', 'baz'); + + assert.equal(iterator.toString(), wasmIterator.toString()); + } + "#) + .test() } \ No newline at end of file