mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
WeakMap bindings
This commit is contained in:
parent
947dfbeae0
commit
a369f7a246
39
src/js.rs
39
src/js.rs
@ -434,6 +434,45 @@ extern {
|
|||||||
pub fn value_of(this: &Object) -> Object;
|
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
|
// JsString
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern {
|
extern {
|
||||||
|
154
tests/all/js_globals/WeakMap.rs
Normal file
154
tests/all/js_globals/WeakMap.rs
Normal file
@ -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()
|
||||||
|
}
|
@ -9,6 +9,7 @@ mod JsFunction;
|
|||||||
mod JsString;
|
mod JsString;
|
||||||
mod Number;
|
mod Number;
|
||||||
mod Math;
|
mod Math;
|
||||||
|
mod WeakMap;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user