From 93a510ef93e4e19abf4319319111fa24677a5e40 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 12 Sep 2018 15:24:04 -0700 Subject: [PATCH] webidl: All interfaces implicitly extend `Object` This information is embedded within the algorithm for constructing interfaces and their prototypes in the section for ECMAScript glue in the WebIDL spec... This really *should* make the `wasm_bindgen_backend::ast::ImportType::extends` member from a `Vec` into a `Vec` so that we could use `js_sys::Object` in the extends field, but that is a huge pain because then the `ImportedTypes` trait needs to be changed, and all of its implementers, etc... --- crates/web-sys/src/lib.rs | 1 + crates/web-sys/tests/wasm/event.rs | 7 ++++++- crates/webidl-tests/array.rs | 1 + crates/webidl-tests/array_buffer.rs | 1 + crates/webidl-tests/callbacks.rs | 2 +- crates/webidl-tests/consts.rs | 1 + crates/webidl-tests/enums.rs | 1 + crates/webidl-tests/global.rs | 1 + crates/webidl-tests/simple.rs | 8 ++++++++ crates/webidl-tests/throws.rs | 1 + crates/webidl/src/lib.rs | 6 +++++- 11 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/web-sys/src/lib.rs b/crates/web-sys/src/lib.rs index 4c4f6dca..bccf5838 100755 --- a/crates/web-sys/src/lib.rs +++ b/crates/web-sys/src/lib.rs @@ -15,5 +15,6 @@ extern crate wasm_bindgen; extern crate js_sys; +use js_sys::Object; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/crates/web-sys/tests/wasm/event.rs b/crates/web-sys/tests/wasm/event.rs index ca6feb66..4192e5bb 100644 --- a/crates/web-sys/tests/wasm/event.rs +++ b/crates/web-sys/tests/wasm/event.rs @@ -1,6 +1,7 @@ use futures::future::Future; -use js_sys::Promise; +use js_sys::{Object, Promise}; use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; use wasm_bindgen_futures::JsFuture; use wasm_bindgen_test::*; use web_sys::Event; @@ -15,6 +16,10 @@ fn event() -> impl Future { JsFuture::from(new_event()) .map(Event::from) .map(|event| { + // All DOM interfaces should inherit from `Object`. + assert!(event.is_instance_of::()); + let _: &Object = event.as_ref(); + // These should match `new Event`. assert!(event.bubbles()); assert!(event.cancelable()); diff --git a/crates/webidl-tests/array.rs b/crates/webidl-tests/array.rs index f44622f0..cfc2c24b 100644 --- a/crates/webidl-tests/array.rs +++ b/crates/webidl-tests/array.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/array.rs")); diff --git a/crates/webidl-tests/array_buffer.rs b/crates/webidl-tests/array_buffer.rs index 0886ff91..24018bcc 100644 --- a/crates/webidl-tests/array_buffer.rs +++ b/crates/webidl-tests/array_buffer.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/array_buffer.rs")); diff --git a/crates/webidl-tests/callbacks.rs b/crates/webidl-tests/callbacks.rs index baa36af9..9fd83181 100644 --- a/crates/webidl-tests/callbacks.rs +++ b/crates/webidl-tests/callbacks.rs @@ -1,5 +1,5 @@ use wasm_bindgen_test::*; -use js_sys::Function; +use js_sys::{Function, Object}; include!(concat!(env!("OUT_DIR"), "/callbacks.rs")); diff --git a/crates/webidl-tests/consts.rs b/crates/webidl-tests/consts.rs index 629e9f37..5ca2f60f 100644 --- a/crates/webidl-tests/consts.rs +++ b/crates/webidl-tests/consts.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/consts.rs")); diff --git a/crates/webidl-tests/enums.rs b/crates/webidl-tests/enums.rs index 5d4bd5e4..b5e0949e 100644 --- a/crates/webidl-tests/enums.rs +++ b/crates/webidl-tests/enums.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/enums.rs")); diff --git a/crates/webidl-tests/global.rs b/crates/webidl-tests/global.rs index 12a7512e..d52305fc 100644 --- a/crates/webidl-tests/global.rs +++ b/crates/webidl-tests/global.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/global.rs")); diff --git a/crates/webidl-tests/simple.rs b/crates/webidl-tests/simple.rs index 6f1c0731..7c1923f8 100644 --- a/crates/webidl-tests/simple.rs +++ b/crates/webidl-tests/simple.rs @@ -1,9 +1,17 @@ +use js_sys::Object; use wasm_bindgen_test::*; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; include!(concat!(env!("OUT_DIR"), "/simple.rs")); +#[wasm_bindgen_test] +fn interfaces_inherit_from_object() { + let m = Method::new(42.0).unwrap(); + assert!(m.is_instance_of::()); + let _: &Object = m.as_ref(); +} + #[wasm_bindgen_test] fn method() { let pi = Method::new(3.14159).unwrap(); diff --git a/crates/webidl-tests/throws.rs b/crates/webidl-tests/throws.rs index 5eff71e4..8297c709 100644 --- a/crates/webidl-tests/throws.rs +++ b/crates/webidl-tests/throws.rs @@ -1,3 +1,4 @@ +use js_sys::Object; use wasm_bindgen_test::*; include!(concat!(env!("OUT_DIR"), "/throws.rs")); diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index 46c578c6..88838266 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -120,7 +120,10 @@ fn parse(webidl_source: &str, allowed_types: Option<&[&str]>) // `AsRef` and such implementations. for import in program.imports.iter_mut() { if let backend::ast::ImportKind::Type(t) = &mut import.kind { - t.extends.retain(|n| filter(&n.to_string())); + t.extends.retain(|n| { + first_pass_record.builtin_idents.contains(n) || + filter(&n.to_string()) + }); } } @@ -445,6 +448,7 @@ impl<'src> FirstPassRecord<'src> { instanceof_shim: format!("__widl_instanceof_{}", name), extends: self.all_superclasses(name) .map(|name| Ident::new(&name, Span::call_site())) + .chain(Some(Ident::new("Object", Span::call_site()))) .collect(), }; self.append_required_features_doc(&import_type, &mut doc_comment);