Add bindings for Set.xx (#347)

* feat(Set): add Set.add

* feat(Set): add Set.clear

* feat(Set): add Set.delete

* feat(Set): add Set.has

* feat(Set): add Set.new

* feat(Set): add Set.size

* feat(Set/SetIterator): add Set.entries

* feat(Set/SetIterator): add Set.keys

* feat(Set/SetIterator): add Set.values
This commit is contained in:
Jannik Keye 2018-06-28 22:57:49 +02:00 committed by Alex Crichton
parent 9193218648
commit d868ff26ef
4 changed files with 358 additions and 0 deletions

View File

@ -619,6 +619,83 @@ extern {
pub fn value_of(this: &Object) -> Object;
}
// Set
#[wasm_bindgen]
extern {
pub type Set;
/// The add() method appends a new element with a specified value to the
/// end of a Set object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add
#[wasm_bindgen(method)]
pub fn add(this: &Set, value: &JsValue) -> Set;
/// The clear() method removes all elements from a Set object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/clear
#[wasm_bindgen(method)]
pub fn clear(this: &Set);
/// The delete() method removes the specified element from a Set object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete
#[wasm_bindgen(method)]
pub fn delete(this: &Set, value: &JsValue) -> bool;
/// The has() method returns a boolean indicating whether an element
/// with the specified value exists in a Set object or not.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has
#[wasm_bindgen(method)]
pub fn has(this: &Set, value: &JsValue) -> bool;
/// The Set object lets you store unique values of any type, whether primitive
/// values or object references.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
#[wasm_bindgen(constructor)]
pub fn new() -> Set;
/// The size accessor property returns the number of elements in a Set object.
///
/// https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Set/size
#[wasm_bindgen(method, getter, structural)]
pub fn size(this: &Set) -> Number;
}
// SetIterator
#[wasm_bindgen]
extern {
pub type SetIterator;
/// The entries() method returns a new Iterator object that contains
/// an array of [value, value] for each element in the Set object,
/// in insertion order. For Set objects there is no key like in
/// Map objects. However, to keep the API similar to the Map object,
/// each entry has the same value for its key and value here, so that
/// an array [value, value] is returned.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries
#[wasm_bindgen(method)]
pub fn entries(set: &Set) -> SetIterator;
/// The keys() method is an alias for this method (for similarity with
/// Map objects); it behaves exactly the same and returns values
/// of Set elements.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values
#[wasm_bindgen(method)]
pub fn keys(set: &Set) -> SetIterator;
/// The values() method returns a new Iterator object that contains the
/// values for each element in the Set object in insertion order.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values
#[wasm_bindgen(method)]
pub fn values(set: &Set) -> SetIterator;
}
// WeakMap
#[wasm_bindgen]
extern {

182
tests/all/js_globals/Set.rs Normal file
View File

@ -0,0 +1,182 @@
#![allow(non_snake_case)]
use project;
#[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(this: &js::Set, value: &JsValue) -> js::Set {
this.add(value)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([]);
wasm.add(set, 100);
assert.equal(set.size, 1);
assert.equal(Array.from(set)[0], 100);
}
"#)
.test()
}
#[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 clear(this: &js::Set) {
this.clear();
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([1, 2, 3]);
wasm.clear(set);
assert.equal(set.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 set_delete(this: &js::Set, value: &JsValue) -> bool {
this.delete(value)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([1, 2, 3]);
assert.equal(wasm.set_delete(set, 4), false);
assert.equal(wasm.set_delete(set, 2), true);
}
"#)
.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::Set, value: &JsValue) -> bool {
this.has(value)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([1, 2, 3]);
assert.equal(wasm.has(set, 4), false);
assert.equal(wasm.has(set, 2), true);
}
"#)
.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_set() -> js::Set {
js::Set::new()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = wasm.new_set();
assert.equal(set.size, 0);
}
"#)
.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 size(this: &js::Set) -> js::Number {
this.size()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([8, 5, 4, 3, 1, 2]);
assert.equal(wasm.size(set), 6);
}
"#)
.test()
}

View File

@ -0,0 +1,97 @@
#![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 entries(this: &js::Set) -> js::SetIterator {
this.entries()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([8, 5, 4, 3, 1, 2]);
let wasmIterator = wasm.entries(set);
let nextValue = wasmIterator.next().value;
assert.equal(nextValue[0], 8);
assert.equal(nextValue[1], 8);
}
"#)
.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 keys(this: &js::Set) -> js::SetIterator {
this.keys()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([8, 5, 4, 3, 1, 2]);
let wasmIterator = wasm.keys(set);
let nextValue = wasmIterator.next().value;
assert.equal(nextValue, 8);
}
"#)
.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 values(this: &js::Set) -> js::SetIterator {
this.values()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let set = new Set([8, 5, 4, 3, 1, 2]);
let wasmIterator = wasm.values(set);
let nextValue = wasmIterator.next().value;
assert.equal(nextValue, 8);
}
"#)
.test()
}

View File

@ -13,6 +13,8 @@ mod WeakMap;
mod WeakSet;
mod Number;
mod Object;
mod Set;
mod SetIterator;
mod TypedArray;
#[test]