From a0dda505d954a29c07b3f596a2024f5d0819aebb Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 13:15:47 +0500 Subject: [PATCH 1/5] Added WeakSet constructor --- src/js.rs | 11 +++++++++++ tests/all/js_globals/WeakSet.rs | 29 +++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 41 insertions(+) create mode 100644 tests/all/js_globals/WeakSet.rs diff --git a/src/js.rs b/src/js.rs index 6ece49ac3..934000f43 100644 --- a/src/js.rs +++ b/src/js.rs @@ -619,6 +619,17 @@ extern { pub fn delete(this: &WeakMap, key: Object) -> bool; } +#[wasm_bindgen] +extern { + pub type WeakSet; + + /// The WeakSet object lets you store weakly held objects in a collection. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet + #[wasm_bindgen(constructor)] + pub fn new() -> WeakSet; +} + // JsString #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/WeakSet.rs b/tests/all/js_globals/WeakSet.rs new file mode 100644 index 000000000..46f829fd5 --- /dev/null +++ b/tests/all/js_globals/WeakSet.rs @@ -0,0 +1,29 @@ +#![allow(non_snake_case)] + +use project; + +#[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_weak_set() -> js::WeakSet { + js::WeakSet::new() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(typeof wasm.new_weak_set(), "object"); + } + "#) + .test() +} \ No newline at end of file diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 453c407ce..5f1ea8fee 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -9,6 +9,7 @@ mod Function; mod JsString; mod Math; mod WeakMap; +mod WeakSet; mod Number; mod Object; mod TypedArray; From 846e5aaacccf73161de6b88dfacbce0ef022f074 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 13:26:53 +0500 Subject: [PATCH 2/5] Added WeakSet has method --- src/js.rs | 6 ++++++ tests/all/js_globals/WeakSet.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index 934000f43..b35ef08a5 100644 --- a/src/js.rs +++ b/src/js.rs @@ -628,6 +628,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet #[wasm_bindgen(constructor)] pub fn new() -> WeakSet; + + /// The has() method returns a boolean indicating whether an object exists in a WeakSet or not. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/has + #[wasm_bindgen(method)] + pub fn has(this: &WeakSet, value: Object) -> bool; } // JsString diff --git a/tests/all/js_globals/WeakSet.rs b/tests/all/js_globals/WeakSet.rs index 46f829fd5..40b4be5af 100644 --- a/tests/all/js_globals/WeakSet.rs +++ b/tests/all/js_globals/WeakSet.rs @@ -26,4 +26,36 @@ fn new() { } "#) .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_value(this: &js::WeakSet, value: js::Object) -> bool { + this.has(value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let set = new WeakSet(); + let value = {some: "value"}; + set.add(value); + assert.equal(wasm.has_value(set, value), true); + + let nonex = {nonexistent: "value"}; + assert.equal(wasm.has_value(set, nonex), false); + } + "#) + .test() } \ No newline at end of file From 6b798a350841cf200811f2ebcb531ce158a707e4 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 13:38:33 +0500 Subject: [PATCH 3/5] Added WeakSet add method --- src/js.rs | 6 ++++++ tests/all/js_globals/WeakSet.rs | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js.rs b/src/js.rs index b35ef08a5..c39d37166 100644 --- a/src/js.rs +++ b/src/js.rs @@ -634,6 +634,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/has #[wasm_bindgen(method)] pub fn has(this: &WeakSet, value: Object) -> bool; + + /// The add() method appends a new object to the end of a WeakSet object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/add + #[wasm_bindgen(method)] + pub fn add(this: &WeakSet, value: Object) -> WeakSet; } // JsString diff --git a/tests/all/js_globals/WeakSet.rs b/tests/all/js_globals/WeakSet.rs index 40b4be5af..2788acc36 100644 --- a/tests/all/js_globals/WeakSet.rs +++ b/tests/all/js_globals/WeakSet.rs @@ -58,4 +58,39 @@ fn has() { } "#) .test() +} + +#[test] +fn add() { + 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 add_value(this: &js::WeakSet, value: js::Object) -> js::WeakSet { + this.add(value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let set = new WeakSet(); + let value = {some: "value"}; + wasm.add_value(set, value); + assert.equal(set.has(value), true); + + assert.throws(() => { wasm.add_value(set, 1) }, TypeError); + assert.throws(() => { wasm.add_value(set, true) }, TypeError); + assert.throws(() => { wasm.add_value(set, "fail") }, TypeError); + assert.throws(() => { wasm.add_value(set, null) }, TypeError); + assert.throws(() => { wasm.add_value(set, undefined) }, TypeError); + } + "#) + .test() } \ No newline at end of file From 761a9272caaa29fa5ea0047264f7032fa5226ea9 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 13:44:01 +0500 Subject: [PATCH 4/5] Added WeakSet delete method --- src/js.rs | 6 ++++++ tests/all/js_globals/WeakSet.rs | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/js.rs b/src/js.rs index c39d37166..2c8ce61ef 100644 --- a/src/js.rs +++ b/src/js.rs @@ -640,6 +640,12 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/add #[wasm_bindgen(method)] pub fn add(this: &WeakSet, value: Object) -> WeakSet; + + /// The delete() method removes the specified element from a WeakSet object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet/delete + #[wasm_bindgen(method)] + pub fn delete(this: &WeakSet, value: Object) -> bool; } // JsString diff --git a/tests/all/js_globals/WeakSet.rs b/tests/all/js_globals/WeakSet.rs index 2788acc36..5201df1af 100644 --- a/tests/all/js_globals/WeakSet.rs +++ b/tests/all/js_globals/WeakSet.rs @@ -93,4 +93,41 @@ fn add() { } "#) .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 delete_value(this: &js::WeakSet, value: js::Object) -> bool { + this.delete(value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let set = new WeakSet(); + let value = {some: "value"}; + set.add(value); + assert.equal(wasm.delete_value(set, value), true); + assert.equal(set.has(value), false); + assert.equal(wasm.delete_value(set, value), false); + + assert.equal(wasm.delete_value(set, 1), false); + assert.equal(wasm.delete_value(set, true), false); + assert.equal(wasm.delete_value(set, "false"), false); + assert.equal(wasm.delete_value(set, null), false); + assert.equal(wasm.delete_value(set, undefined), false); + } + "#) + .test() } \ No newline at end of file From 72be16c8ff9d60f0d69c4f8b8ef0b04b162d2959 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 13:48:32 +0500 Subject: [PATCH 5/5] Forgotten comment --- src/js.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/js.rs b/src/js.rs index 2c8ce61ef..5a3a13bef 100644 --- a/src/js.rs +++ b/src/js.rs @@ -619,6 +619,7 @@ extern { pub fn delete(this: &WeakMap, key: Object) -> bool; } +// WeakSet #[wasm_bindgen] extern { pub type WeakSet;