Make WebIDL constructors take a more specific global if possible (fixes #14071)

This commit is contained in:
Rohan Prinja 2016-11-10 02:24:01 -05:00 committed by Anthony Ramine
parent 8af2327e95
commit 973f77c006
17 changed files with 50 additions and 49 deletions

View file

@ -3168,7 +3168,7 @@ class CGCallGenerator(CGThing):
if isFallible:
if static:
glob = "&global"
glob = "global.upcast::<GlobalScope>()"
else:
glob = "&this.global()"
@ -5245,12 +5245,14 @@ class CGClassConstructHook(CGAbstractExternMethod):
assert constructor
CGAbstractExternMethod.__init__(self, descriptor, name, 'bool', args)
self.constructor = constructor
self.exposureSet = descriptor.interface.exposureSet
def definition_body(self):
preamble = CGGeneric("""\
let global = GlobalScope::from_object(JS_CALLEE(cx, vp).to_object());
let args = CallArgs::from_vp(vp, argc);
""")
preamble = """let global = GlobalScope::from_object(JS_CALLEE(cx, vp).to_object());\n"""
if len(self.exposureSet) == 1:
preamble += "let global = Root::downcast::<dom::types::%s>(global).unwrap();\n" % list(self.exposureSet)[0]
preamble += """let args = CallArgs::from_vp(vp, argc);\n"""
preamble = CGGeneric(preamble)
name = self.constructor.identifier.name
nativeName = MakeNativeName(self.descriptor.binaryNameFor(name))
callGenerator = CGMethodCall(["&global"], nativeName, True,
@ -5582,6 +5584,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'dom::bindings::error::throw_dom_exception',
'dom::bindings::guard::Condition',
'dom::bindings::guard::Guard',
'dom::bindings::inheritance::Castable',
'dom::bindings::proxyhandler',
'dom::bindings::proxyhandler::ensure_expando_object',
'dom::bindings::proxyhandler::fill_property_descriptor',

View file

@ -9,8 +9,8 @@ use dom::bindings::js::Root;
use dom::bindings::str::DOMString;
use dom::characterdata::CharacterData;
use dom::document::Document;
use dom::globalscope::GlobalScope;
use dom::node::Node;
use dom::window::Window;
/// An HTML comment.
#[dom_struct]
@ -31,8 +31,8 @@ impl Comment {
CommentBinding::Wrap)
}
pub fn Constructor(global: &GlobalScope, data: DOMString) -> Fallible<Root<Comment>> {
let document = global.as_window().Document();
pub fn Constructor(window: &Window, data: DOMString) -> Fallible<Root<Comment>> {
let document = window.Document();
Ok(Comment::new(data, &document))
}
}

View file

@ -1884,11 +1884,10 @@ impl Document {
}
// https://dom.spec.whatwg.org/#dom-document
pub fn Constructor(global: &GlobalScope) -> Fallible<Root<Document>> {
let win = global.as_window();
let doc = win.Document();
pub fn Constructor(window: &Window) -> Fallible<Root<Document>> {
let doc = window.Document();
let docloader = DocumentLoader::new(&*doc.loader());
Ok(Document::new(win,
Ok(Document::new(window,
None,
None,
IsHTMLDocument::NonHTMLDocument,

View file

@ -12,10 +12,10 @@ use dom::bindings::js::Root;
use dom::bindings::str::DOMString;
use dom::document::Document;
use dom::element::Element;
use dom::globalscope::GlobalScope;
use dom::htmlcollection::HTMLCollection;
use dom::node::{Node, window_from_node};
use dom::nodelist::NodeList;
use dom::window::Window;
use servo_atoms::Atom;
// https://dom.spec.whatwg.org/#documentfragment
@ -38,8 +38,8 @@ impl DocumentFragment {
DocumentFragmentBinding::Wrap)
}
pub fn Constructor(global: &GlobalScope) -> Fallible<Root<DocumentFragment>> {
let document = global.as_window().Document();
pub fn Constructor(window: &Window) -> Fallible<Root<DocumentFragment>> {
let document = window.Document();
Ok(DocumentFragment::new(&document))
}

View file

@ -17,7 +17,6 @@ use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::document::{Document, IsHTMLDocument};
use dom::document::DocumentSource;
use dom::globalscope::GlobalScope;
use dom::servoparser::ServoParser;
use dom::window::Window;
@ -41,8 +40,8 @@ impl DOMParser {
DOMParserBinding::Wrap)
}
pub fn Constructor(global: &GlobalScope) -> Fallible<Root<DOMParser>> {
Ok(DOMParser::new(global.as_window()))
pub fn Constructor(window: &Window) -> Fallible<Root<DOMParser>> {
Ok(DOMParser::new(window))
}
}

View file

@ -10,7 +10,7 @@ use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::Event;
use dom::globalscope::GlobalScope;
use dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use js::jsapi::{HandleValue, JSContext};
use servo_atoms::Atom;
@ -28,12 +28,12 @@ impl ExtendableEvent {
extensions_allowed: true
}
}
pub fn new(global: &GlobalScope,
pub fn new(worker: &ServiceWorkerGlobalScope,
type_: Atom,
bubbles: bool,
cancelable: bool)
-> Root<ExtendableEvent> {
let ev = reflect_dom_object(box ExtendableEvent::new_inherited(), global, ExtendableEventBinding::Wrap);
let ev = reflect_dom_object(box ExtendableEvent::new_inherited(), worker, ExtendableEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bubbles, cancelable);
@ -41,10 +41,10 @@ impl ExtendableEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(worker: &ServiceWorkerGlobalScope,
type_: DOMString,
init: &ExtendableEventBinding::ExtendableEventInit) -> Fallible<Root<ExtendableEvent>> {
Ok(ExtendableEvent::new(global,
Ok(ExtendableEvent::new(worker,
Atom::from(type_),
init.parent.bubbles,
init.parent.cancelable))

View file

@ -13,6 +13,7 @@ use dom::event::Event;
use dom::eventtarget::EventTarget;
use dom::extendableevent::ExtendableEvent;
use dom::globalscope::GlobalScope;
use dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use js::jsapi::{HandleValue, Heap, JSContext};
use js::jsval::JSVal;
use servo_atoms::Atom;
@ -46,10 +47,11 @@ impl ExtendableMessageEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(worker: &ServiceWorkerGlobalScope,
type_: DOMString,
init: &ExtendableMessageEventBinding::ExtendableMessageEventInit)
-> Fallible<Root<ExtendableMessageEvent>> {
let global = worker.upcast::<GlobalScope>();
rooted!(in(global.get_cx()) let data = init.data);
let ev = ExtendableMessageEvent::new(global,
Atom::from(type_),

View file

@ -53,12 +53,12 @@ impl FocusEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &FocusEventBinding::FocusEventInit) -> Fallible<Root<FocusEvent>> {
let bubbles = EventBubbles::from(init.parent.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.parent.cancelable);
let event = FocusEvent::new(global.as_window(),
let event = FocusEvent::new(window,
type_,
bubbles,
cancelable,

View file

@ -16,11 +16,11 @@ use dom::bindings::str::DOMString;
use dom::document::Document;
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use dom::htmlelement::HTMLElement;
use dom::node::{Node, NodeDamage, document_from_node, window_from_node};
use dom::values::UNSIGNED_LONG_MAX;
use dom::virtualmethods::VirtualMethods;
use dom::window::Window;
use html5ever_atoms::LocalName;
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
@ -220,10 +220,10 @@ impl HTMLImageElement {
HTMLImageElementBinding::Wrap)
}
pub fn Image(global: &GlobalScope,
pub fn Image(window: &Window,
width: Option<u32>,
height: Option<u32>) -> Fallible<Root<HTMLImageElement>> {
let document = global.as_window().Document();
let document = window.Document();
let image = HTMLImageElement::new(local_name!("img"), None, &document);
if let Some(w) = width {
image.SetWidth(w);

View file

@ -12,7 +12,6 @@ use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::Event;
use dom::globalscope::GlobalScope;
use dom::uievent::UIEvent;
use dom::window::Window;
use msg::constellation_msg;
@ -101,10 +100,10 @@ impl KeyboardEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Root<KeyboardEvent>> {
let event = KeyboardEvent::new(global.as_window(),
let event = KeyboardEvent::new(window,
type_,
init.parent.parent.parent.bubbles,
init.parent.parent.parent.cancelable,

View file

@ -12,7 +12,6 @@ use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use dom::uievent::UIEvent;
use dom::window::Window;
use std::cell::Cell;
@ -82,12 +81,12 @@ impl MouseEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &MouseEventBinding::MouseEventInit) -> Fallible<Root<MouseEvent>> {
let bubbles = EventBubbles::from(init.parent.parent.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.parent.parent.cancelable);
let event = MouseEvent::new(global.as_window(),
let event = MouseEvent::new(window,
type_,
bubbles,
cancelable,

View file

@ -22,11 +22,11 @@ use dom::characterdata::CharacterData;
use dom::document::Document;
use dom::documentfragment::DocumentFragment;
use dom::element::Element;
use dom::globalscope::GlobalScope;
use dom::htmlbodyelement::HTMLBodyElement;
use dom::htmlscriptelement::HTMLScriptElement;
use dom::node::{Node, UnbindContext};
use dom::text::Text;
use dom::window::Window;
use heapsize::HeapSizeOf;
use js::jsapi::JSTracer;
use std::cell::{Cell, UnsafeCell};
@ -70,8 +70,8 @@ impl Range {
}
// https://dom.spec.whatwg.org/#dom-range
pub fn Constructor(global: &GlobalScope) -> Fallible<Root<Range>> {
let document = global.as_window().Document();
pub fn Constructor(window: &Window) -> Fallible<Root<Range>> {
let document = window.Document();
Ok(Range::new_with_doc(&document))
}

View file

@ -319,7 +319,7 @@ impl ServiceWorkerGlobalScope {
}
fn dispatch_activate(&self) {
let event = ExtendableEvent::new(self.upcast(), atom!("activate"), false, false);
let event = ExtendableEvent::new(self, atom!("activate"), false, false);
let event = (&*event).upcast::<Event>();
self.upcast::<EventTarget>().dispatch_event(event);
}

View file

@ -14,8 +14,8 @@ use dom::bindings::js::RootedReference;
use dom::bindings::str::DOMString;
use dom::characterdata::CharacterData;
use dom::document::Document;
use dom::globalscope::GlobalScope;
use dom::node::Node;
use dom::window::Window;
/// An HTML text node.
#[dom_struct]
@ -35,8 +35,8 @@ impl Text {
document, TextBinding::Wrap)
}
pub fn Constructor(global: &GlobalScope, text: DOMString) -> Fallible<Root<Text>> {
let document = global.as_window().Document();
pub fn Constructor(window: &Window, text: DOMString) -> Fallible<Root<Text>> {
let document = window.Document();
Ok(Text::new(text, &document))
}
}

View file

@ -13,6 +13,7 @@ use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::Event;
use dom::globalscope::GlobalScope;
use dom::window::Window;
use servo_atoms::Atom;
#[dom_struct]
@ -46,9 +47,10 @@ impl TransitionEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &TransitionEventInit) -> Fallible<Root<TransitionEvent>> {
let global = window.upcast::<GlobalScope>();
Ok(TransitionEvent::new(global, Atom::from(type_), init))
}
}

View file

@ -12,7 +12,6 @@ use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::globalscope::GlobalScope;
use dom::window::Window;
use servo_atoms::Atom;
use std::cell::Cell;
@ -52,12 +51,12 @@ impl UIEvent {
ev
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &UIEventBinding::UIEventInit) -> Fallible<Root<UIEvent>> {
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
let event = UIEvent::new(global.as_window(),
let event = UIEvent::new(window,
type_,
bubbles, cancelable,
init.view.r(), init.detail);

View file

@ -12,7 +12,6 @@ use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::globalscope::GlobalScope;
use dom::window::Window;
use servo_atoms::Atom;
@ -71,7 +70,7 @@ impl WebGLContextEvent {
event
}
pub fn Constructor(global: &GlobalScope,
pub fn Constructor(window: &Window,
type_: DOMString,
init: &WebGLContextEventInit) -> Fallible<Root<WebGLContextEvent>> {
let status_message = match init.statusMessage.as_ref() {
@ -83,7 +82,7 @@ impl WebGLContextEvent {
let cancelable = EventCancelable::from(init.parent.cancelable);
Ok(WebGLContextEvent::new(global.as_window(),
Ok(WebGLContextEvent::new(window,
Atom::from(type_),
bubbles,
cancelable,