mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-02 02:11:06 +00:00
Merge pull request #349 from jannikkeye/bindings-map
Add bindings for Map.xx
This commit is contained in:
commit
5ff6329d29
81
src/js.rs
81
src/js.rs
@ -309,6 +309,87 @@ extern "C" {
|
|||||||
pub fn to_string(this: &Function) -> JsString;
|
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);
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// 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
|
// Math
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
213
tests/all/js_globals/Map.rs
Normal file
213
tests/all/js_globals/Map.rs
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
#![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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
100
tests/all/js_globals/MapIterator.rs
Normal file
100
tests/all/js_globals/MapIterator.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#![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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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()
|
||||||
|
}
|
@ -8,6 +8,8 @@ mod Boolean;
|
|||||||
mod Date;
|
mod Date;
|
||||||
mod Function;
|
mod Function;
|
||||||
mod JsString;
|
mod JsString;
|
||||||
|
mod Map;
|
||||||
|
mod MapIterator;
|
||||||
mod Math;
|
mod Math;
|
||||||
mod Number;
|
mod Number;
|
||||||
mod Object;
|
mod Object;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user