From a369f7a2467c7d0ff398471809c46ea73bc0ce91 Mon Sep 17 00:00:00 2001 From: xeqlol Date: Tue, 26 Jun 2018 13:12:32 +0500 Subject: [PATCH 1/3] WeakMap bindings --- src/js.rs | 39 ++++++++ tests/all/js_globals/WeakMap.rs | 154 ++++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 194 insertions(+) create mode 100644 tests/all/js_globals/WeakMap.rs diff --git a/src/js.rs b/src/js.rs index cef43b16..71bc5a39 100644 --- a/src/js.rs +++ b/src/js.rs @@ -434,6 +434,45 @@ extern { pub fn value_of(this: &Object) -> Object; } +// WeakMap +#[wasm_bindgen] +extern { + pub type WeakMap; + + /// Returns the function that created an WeakMap wrapper. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap + #[wasm_bindgen(constructor)] + pub fn new() -> WeakMap; + + /// The set() method sets the value for the key in the WeakMap object. Returns + /// the WeakMap object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/set + #[wasm_bindgen(method, js_class="WeakMap")] + pub fn set(this: &WeakMap, key: JsValue, value: JsValue) -> WeakMap; + + /// The get() method returns a specified by key element + /// from a WeakMap object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/get + #[wasm_bindgen(method)] + pub fn get(this: &WeakMap, key: JsValue) -> JsValue; + + /// The has() method returns a boolean indicating whether an element with + /// the specified key exists in the WeakMap object or not. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/has + #[wasm_bindgen(method)] + pub fn has(this: &WeakMap, key: JsValue) -> bool; + + /// The delete() method removes the specified element from a WeakMap object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/delete + #[wasm_bindgen(method)] + pub fn delete(this: &WeakMap, key: JsValue) -> bool; +} + // JsString #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/WeakMap.rs b/tests/all/js_globals/WeakMap.rs new file mode 100644 index 00000000..47206ea8 --- /dev/null +++ b/tests/all/js_globals/WeakMap.rs @@ -0,0 +1,154 @@ +#![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_map() -> js::WeakMap { + js::WeakMap::new() + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(typeof wasm.new_weak_map(), "object"); + } + "#) + .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 get_value(this: &js::WeakMap, key: JsValue) -> JsValue { + this.get(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let map = new WeakMap(); + let key = {some: "key"}; + map.set(key, "value"); + assert.equal(wasm.get_value(map, key), "value"); + + let undef = "unexisting_key"; + assert.equal(typeof wasm.get_value(map, undef), "undefined"); + } + "#) + .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_value(this: &js::WeakMap, key: JsValue, value: JsValue) -> js::WeakMap { + this.set(key, value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let map = new WeakMap(); + let key = {some: "key"}; + wasm.set_value(map, key, "value"); + assert.equal(map.get(key), "value"); + } + "#) + .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::WeakMap, key: JsValue) -> bool { + this.has(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let map = new WeakMap(); + let key = {some: "key"}; + map.set(key, "value"); + assert.equal(wasm.has_value(map, key), true); + + let undef = "unexisting_key"; + assert.equal(wasm.has_value(map, undef), false); + } + "#) + .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_key(this: &js::WeakMap, key: JsValue) -> bool { + this.delete(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let map = new WeakMap(); + let key = {some: "key"}; + map.set(key, "value"); + assert.equal(wasm.delete_key(map, key), true); + assert.equal(map.has(key), false); + assert.equal(wasm.delete_key(map, key), false); + } + "#) + .test() +} \ No newline at end of file diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 3fd54d80..025f27e4 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -9,6 +9,7 @@ mod JsFunction; mod JsString; mod Number; mod Math; +mod WeakMap; #[test] #[cfg(feature = "std")] From a6c7b4b69f14a7b2cee49b56f1ae4c2fead50c64 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Tue, 26 Jun 2018 23:50:31 +0500 Subject: [PATCH 2/3] Changed keys' type JsValue -> Object, changed description of WeakMap constructor --- src/js.rs | 11 ++++++----- tests/all/js_globals/WeakMap.rs | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/js.rs b/src/js.rs index 09754469..e870f729 100644 --- a/src/js.rs +++ b/src/js.rs @@ -565,7 +565,8 @@ extern { extern { pub type WeakMap; - /// Returns the function that created an WeakMap wrapper. + /// The WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. + /// The keys must be objects and the values can be arbitrary values. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap #[wasm_bindgen(constructor)] @@ -576,27 +577,27 @@ extern { /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/set #[wasm_bindgen(method, js_class="WeakMap")] - pub fn set(this: &WeakMap, key: JsValue, value: JsValue) -> WeakMap; + pub fn set(this: &WeakMap, key: Object, value: JsValue) -> WeakMap; /// The get() method returns a specified by key element /// from a WeakMap object. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/get #[wasm_bindgen(method)] - pub fn get(this: &WeakMap, key: JsValue) -> JsValue; + pub fn get(this: &WeakMap, key: Object) -> JsValue; /// The has() method returns a boolean indicating whether an element with /// the specified key exists in the WeakMap object or not. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/has #[wasm_bindgen(method)] - pub fn has(this: &WeakMap, key: JsValue) -> bool; + pub fn has(this: &WeakMap, key: Object) -> bool; /// The delete() method removes the specified element from a WeakMap object. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/delete #[wasm_bindgen(method)] - pub fn delete(this: &WeakMap, key: JsValue) -> bool; + pub fn delete(this: &WeakMap, key: Object) -> bool; } // JsString diff --git a/tests/all/js_globals/WeakMap.rs b/tests/all/js_globals/WeakMap.rs index 47206ea8..6bad21bf 100644 --- a/tests/all/js_globals/WeakMap.rs +++ b/tests/all/js_globals/WeakMap.rs @@ -40,7 +40,7 @@ fn get() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn get_value(this: &js::WeakMap, key: JsValue) -> JsValue { + pub fn get_value(this: &js::WeakMap, key: js::Object) -> JsValue { this.get(key) } "#) @@ -72,7 +72,7 @@ fn set() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn set_value(this: &js::WeakMap, key: JsValue, value: JsValue) -> js::WeakMap { + pub fn set_value(this: &js::WeakMap, key: js::Object, value: js::JsValue) -> js::WeakMap { this.set(key, value) } "#) @@ -101,7 +101,7 @@ fn has() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn has_value(this: &js::WeakMap, key: JsValue) -> bool { + pub fn has_value(this: &js::WeakMap, key: js::Object) -> bool { this.has(key) } "#) @@ -133,7 +133,7 @@ fn delete() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn delete_key(this: &js::WeakMap, key: JsValue) -> bool { + pub fn delete_key(this: &js::WeakMap, key: js::Object) -> bool { this.delete(key) } "#) From ec8ed3a48d42e0e811561ee82121300be4029654 Mon Sep 17 00:00:00 2001 From: Dimitrii Nemkov Date: Wed, 27 Jun 2018 00:20:36 +0500 Subject: [PATCH 3/3] Fixed missed error in WeakMap::set --- tests/all/js_globals/WeakMap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all/js_globals/WeakMap.rs b/tests/all/js_globals/WeakMap.rs index 6bad21bf..94b02a6b 100644 --- a/tests/all/js_globals/WeakMap.rs +++ b/tests/all/js_globals/WeakMap.rs @@ -72,7 +72,7 @@ fn set() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn set_value(this: &js::WeakMap, key: js::Object, value: js::JsValue) -> js::WeakMap { + pub fn set_value(this: &js::WeakMap, key: js::Object, value: JsValue) -> js::WeakMap { this.set(key, value) } "#)