From 6f865cb418be386f32a58811b63139ca3fbf78e6 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Wed, 8 Aug 2018 22:49:06 -0400 Subject: [PATCH] Add extends attributes for several types Part of #670 --- crates/js-sys/src/lib.rs | 17 +++++++++++++++++ crates/js-sys/tests/wasm/Array.rs | 8 ++++++++ crates/js-sys/tests/wasm/ArrayBuffer.rs | 8 ++++++++ crates/js-sys/tests/wasm/Boolean.rs | 8 ++++++++ crates/js-sys/tests/wasm/DataView.rs | 14 ++++++++++++++ crates/js-sys/tests/wasm/Date.rs | 8 ++++++++ crates/js-sys/tests/wasm/Error.rs | 8 ++++++++ crates/js-sys/tests/wasm/Function.rs | 7 +++++++ crates/js-sys/tests/wasm/Map.rs | 8 ++++++++ crates/js-sys/tests/wasm/Number.rs | 8 ++++++++ crates/js-sys/tests/wasm/Set.rs | 8 ++++++++ crates/js-sys/tests/wasm/Symbol.rs | 8 ++++++++ crates/js-sys/tests/wasm/WeakMap.rs | 8 ++++++++ crates/js-sys/tests/wasm/WeakSet.rs | 8 ++++++++ 14 files changed, 126 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 30ec0855377..5922eeab197 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -125,6 +125,7 @@ extern "C" { // Array #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Array; @@ -347,6 +348,7 @@ extern "C" { // ArrayBuffer #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type ArrayBuffer; @@ -421,6 +423,7 @@ extern "C" { // Boolean #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Boolean; @@ -440,6 +443,7 @@ extern "C" { // DataView #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type DataView; @@ -587,6 +591,7 @@ extern "C" { // Error #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Error; @@ -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; @@ -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; @@ -729,6 +738,7 @@ extern "C" { // Function #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Function; @@ -1007,6 +1017,7 @@ extern "C" { // Map #[wasm_bindgen] extern { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Map; @@ -1385,6 +1396,7 @@ extern "C" { // Number. #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Number; @@ -1482,6 +1494,7 @@ extern "C" { // Date. #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Date; @@ -2265,6 +2278,7 @@ extern { // Set #[wasm_bindgen] extern { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Set; @@ -2559,6 +2573,7 @@ extern "C" { // WeakMap #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type WeakMap; @@ -2602,6 +2617,7 @@ extern "C" { // WeakSet #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type WeakSet; @@ -2941,6 +2957,7 @@ impl fmt::Debug for JsString { // Symbol #[wasm_bindgen] extern "C" { + #[wasm_bindgen(extends = Object)] #[derive(Clone, Debug)] pub type Symbol; diff --git a/crates/js-sys/tests/wasm/Array.rs b/crates/js-sys/tests/wasm/Array.rs index 9525a913bdc..ae171bb7cea 100644 --- a/crates/js-sys/tests/wasm/Array.rs +++ b/crates/js-sys/tests/wasm/Array.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; macro_rules! js_array { @@ -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::()); + assert!(array.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/ArrayBuffer.rs b/crates/js-sys/tests/wasm/ArrayBuffer.rs index ddfcdeef5d2..21fa0491751 100644 --- a/crates/js-sys/tests/wasm/ArrayBuffer.rs +++ b/crates/js-sys/tests/wasm/ArrayBuffer.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(buf.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Boolean.rs b/crates/js-sys/tests/wasm/Boolean.rs index 6f0befaa7be..34c53e01f39 100644 --- a/crates/js-sys/tests/wasm/Boolean.rs +++ b/crates/js-sys/tests/wasm/Boolean.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(b.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/DataView.rs b/crates/js-sys/tests/wasm/DataView.rs index b7d952ae11e..8efe0c6dac7 100644 --- a/crates/js-sys/tests/wasm/DataView.rs +++ b/crates/js-sys/tests/wasm/DataView.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(v.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Date.rs b/crates/js-sys/tests/wasm/Date.rs index 2228679694f..c4026b47c4e 100644 --- a/crates/js-sys/tests/wasm/Date.rs +++ b/crates/js-sys/tests/wasm/Date.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(date.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Error.rs b/crates/js-sys/tests/wasm/Error.rs index 7c258b522d6..abe856f18d3 100644 --- a/crates/js-sys/tests/wasm/Error.rs +++ b/crates/js-sys/tests/wasm/Error.rs @@ -1,5 +1,6 @@ use wasm_bindgen::JsValue; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(error.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Function.rs b/crates/js-sys/tests/wasm/Function.rs index f2339496053..c7fed2835d4 100644 --- a/crates/js-sys/tests/wasm/Function.rs +++ b/crates/js-sys/tests/wasm/Function.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen] @@ -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::()); + assert!(MAX.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Map.rs b/crates/js-sys/tests/wasm/Map.rs index 6395b6bf519..f9ba77409e1 100644 --- a/crates/js-sys/tests/wasm/Map.rs +++ b/crates/js-sys/tests/wasm/Map.rs @@ -1,4 +1,5 @@ use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen_test] @@ -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::()); + assert!(map.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Number.rs b/crates/js-sys/tests/wasm/Number.rs index fee72abc1f3..9d43f8a2bba 100644 --- a/crates/js-sys/tests/wasm/Number.rs +++ b/crates/js-sys/tests/wasm/Number.rs @@ -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] @@ -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::()); + assert!(n.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Set.rs b/crates/js-sys/tests/wasm/Set.rs index 92cca443daf..419d66b8ac2 100644 --- a/crates/js-sys/tests/wasm/Set.rs +++ b/crates/js-sys/tests/wasm/Set.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; fn set2vec(s: &Set) -> Vec { @@ -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::()); + assert!(set.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/Symbol.rs b/crates/js-sys/tests/wasm/Symbol.rs index a34a1ca2a3a..cbd7083e6e7 100644 --- a/crates/js-sys/tests/wasm/Symbol.rs +++ b/crates/js-sys/tests/wasm/Symbol.rs @@ -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")] @@ -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::()); + assert!(a.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/WeakMap.rs b/crates/js-sys/tests/wasm/WeakMap.rs index b6c4c405b4e..8cfe464c681 100644 --- a/crates/js-sys/tests/wasm/WeakMap.rs +++ b/crates/js-sys/tests/wasm/WeakMap.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen] @@ -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::()); + assert!(map.is_instance_of::()); +} diff --git a/crates/js-sys/tests/wasm/WeakSet.rs b/crates/js-sys/tests/wasm/WeakSet.rs index 0d9963340fb..dc70ca8b1f2 100644 --- a/crates/js-sys/tests/wasm/WeakSet.rs +++ b/crates/js-sys/tests/wasm/WeakSet.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; use wasm_bindgen_test::*; +use wasm_bindgen::JsCast; use js_sys::*; #[wasm_bindgen] @@ -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::()); + assert!(set.is_instance_of::()); +}