Skip to content

Commit

Permalink
Add extends attributes for several types
Browse files Browse the repository at this point in the history
Part of rustwasm#670
  • Loading branch information
eminence committed Aug 9, 2018
1 parent 505037f commit 6f865cb
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extern "C" {
// Array
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Array;

Expand Down Expand Up @@ -347,6 +348,7 @@ extern "C" {
// ArrayBuffer
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type ArrayBuffer;

Expand Down Expand Up @@ -421,6 +423,7 @@ extern "C" {
// Boolean
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Boolean;

Expand All @@ -440,6 +443,7 @@ extern "C" {
// DataView
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type DataView;

Expand Down Expand Up @@ -587,6 +591,7 @@ extern "C" {
// Error
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Error;

Expand Down Expand Up @@ -625,6 +630,8 @@ extern "C" {
// Float32Array
#[wasm_bindgen]
extern "C" {
// TODO Uncomment this once TypedArray is added:
// #[wasm_bindgen(extends = Object, extends = TypedArray)]
#[derive(Clone, Debug)]
pub type Float32Array;

Expand Down Expand Up @@ -677,6 +684,8 @@ extern "C" {
// Float64Array
#[wasm_bindgen]
extern "C" {
// TODO Uncomment this once TypedArray is added:
// #[wasm_bindgen(extends = Object, extends = TypedArray)]
#[derive(Clone, Debug)]
pub type Float64Array;

Expand Down Expand Up @@ -729,6 +738,7 @@ extern "C" {
// Function
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Function;

Expand Down Expand Up @@ -1007,6 +1017,7 @@ extern "C" {
// Map
#[wasm_bindgen]
extern {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Map;

Expand Down Expand Up @@ -1385,6 +1396,7 @@ extern "C" {
// Number.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Number;

Expand Down Expand Up @@ -1482,6 +1494,7 @@ extern "C" {
// Date.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Date;

Expand Down Expand Up @@ -2265,6 +2278,7 @@ extern {
// Set
#[wasm_bindgen]
extern {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Set;

Expand Down Expand Up @@ -2559,6 +2573,7 @@ extern "C" {
// WeakMap
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type WeakMap;

Expand Down Expand Up @@ -2602,6 +2617,7 @@ extern "C" {
// WeakSet
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type WeakSet;

Expand Down Expand Up @@ -2941,6 +2957,7 @@ impl fmt::Debug for JsString {
// Symbol
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug)]
pub type Symbol;

Expand Down
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

macro_rules! js_array {
Expand Down Expand Up @@ -286,3 +287,10 @@ fn for_each() {
assert_eq!(sum_indices_of_evens(&js_array![1, 3, 5, 7]), 0);
assert_eq!(sum_indices_of_evens(&js_array![3, 5, 7, 10]), 3);
}

#[wasm_bindgen_test]
fn array_inheritance() {
let array = js_array![0];
assert!(array.is_instance_of::<Array>());
assert!(array.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/ArrayBuffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -34,3 +35,10 @@ fn slice_with_end() {
let slice = buf.slice_with_end(1, 2);
assert!(JsValue::from(slice).is_object());
}

#[wasm_bindgen_test]
fn arraybuffer_inheritance() {
let buf = ArrayBuffer::new(4);
assert!(buf.is_instance_of::<ArrayBuffer>());
assert!(buf.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Boolean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand All @@ -11,3 +12,10 @@ fn new_undefined() {
fn new_truely() {
assert_eq!(Boolean::new(&JsValue::from("foo")).value_of(), true);
}

#[wasm_bindgen_test]
fn boolean_inheritance() {
let b = Boolean::new(&JsValue::from(true));
assert!(b.is_instance_of::<Boolean>());
assert!(b.is_instance_of::<Object>());
}
14 changes: 14 additions & 0 deletions crates/js-sys/tests/wasm/DataView.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -37,3 +38,16 @@ fn test() {
// TODO: figure out how to do `bytes[2]`
bytes.subarray(2, 3).for_each(&mut |x, _, _| assert_eq!(x, 42));
}

#[wasm_bindgen_test]
fn dataview_inheritance() {
let bytes = Int8Array::new(&JsValue::from(10));

// TODO: figure out how to do `bytes[2] = 2`
bytes.subarray(2, 3).fill(2, 0, 1);

let v = DataView::new(&bytes.buffer(), 2, 8);

assert!(v.is_instance_of::<DataView>());
assert!(v.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Date.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -406,3 +407,10 @@ fn value_of() {
let date = Date::new(&Date::utc(2018f64, 6f64).into());
assert_eq!(date.value_of(), 1530403200000.0);
}

#[wasm_bindgen_test]
fn date_inheritance() {
let date = Date::new(&"August 19, 1975 23:15:30".into());
assert!(date.is_instance_of::<Date>());
assert!(date.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -35,3 +36,10 @@ fn to_string() {
error.set_name("error_name_1");
assert_eq!(JsValue::from(error.to_string()), "error_name_1: error message 1");
}

#[wasm_bindgen_test]
fn error_inheritance() {
let error = Error::new("test");
assert!(error.is_instance_of::<Error>());
assert!(error.is_instance_of::<Object>());
}
7 changes: 7 additions & 0 deletions crates/js-sys/tests/wasm/Function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -60,3 +61,9 @@ fn name() {
fn to_string() {
assert!(MAX.to_string().length() > 0);
}

#[wasm_bindgen_test]
fn function_inheritance() {
assert!(MAX.is_instance_of::<Function>());
assert!(MAX.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -86,3 +87,10 @@ fn size() {
map.set(&"bar".into(), &"baz".into());
assert_eq!(map.size(), 2);
}

#[wasm_bindgen_test]
fn map_inheritance() {
let map = Map::new();
assert!(map.is_instance_of::<Map>());
assert!(map.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::f64::{INFINITY, NAN};

use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen_test]
Expand Down Expand Up @@ -104,3 +105,10 @@ fn to_exponential() {
assert_eq!(Number::new(&123456.into()).to_exponential(2).unwrap(), "1.23e+5");
assert!(Number::new(&10.into()).to_exponential(101).is_err());
}

#[wasm_bindgen_test]
fn number_inheritance() {
let n = Number::new(&JsValue::from(42));
assert!(n.is_instance_of::<Number>());
assert!(n.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

fn set2vec(s: &Set) -> Vec<JsValue> {
Expand Down Expand Up @@ -80,3 +81,10 @@ fn size() {
set.add(&3.into());
assert_eq!(set.size(), 3);
}

#[wasm_bindgen_test]
fn set_inheritance() {
let set = Set::new(&JsValue::undefined());
assert!(set.is_instance_of::<Set>());
assert!(set.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/Symbol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen(module = "tests/wasm/Symbol.js")]
Expand Down Expand Up @@ -106,3 +107,10 @@ fn value_of() {
let a = gensym(JsValue::undefined());
assert_eq!(JsValue::from(a.value_of()), JsValue::from(a));
}

#[wasm_bindgen_test]
fn symbol_inheritance() {
let a = Symbol::for_("foo");
assert!(a.is_instance_of::<Symbol>());
assert!(a.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/WeakMap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -50,3 +51,10 @@ fn delete() {
map.delete(&key);
assert!(!map.has(&key));
}

#[wasm_bindgen_test]
fn weakmap_inheritance() {
let map = WeakMap::new();
assert!(map.is_instance_of::<WeakMap>());
assert!(map.is_instance_of::<Object>());
}
8 changes: 8 additions & 0 deletions crates/js-sys/tests/wasm/WeakSet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -40,3 +41,10 @@ fn delete() {
assert!(!set.has(&value));
assert!(!set.delete(&value));
}

#[wasm_bindgen_test]
fn weakset_inheritance() {
let set = WeakSet::new();
assert!(set.is_instance_of::<WeakSet>());
assert!(set.is_instance_of::<Object>());
}

0 comments on commit 6f865cb

Please sign in to comment.