From aa93260f1e415d9e00acbcf07879004f110947b2 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 6 Jan 2017 16:13:10 +0100 Subject: [PATCH 1/4] Change the order of code in callback.rs to make more sense. --- components/script/dom/bindings/callback.rs | 76 +++++++++++----------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/components/script/dom/bindings/callback.rs b/components/script/dom/bindings/callback.rs index efe1b8a5681..a9846477ac4 100644 --- a/components/script/dom/bindings/callback.rs +++ b/components/script/dom/bindings/callback.rs @@ -30,6 +30,32 @@ pub enum ExceptionHandling { Rethrow, } + +/// A common base class for representing IDL callback function and +/// callback interface types. +#[derive(JSTraceable)] +struct CallbackObject { + /// The underlying `JSObject`. + callback: Heap<*mut JSObject>, +} + +impl PartialEq for CallbackObject { + fn eq(&self, other: &CallbackObject) -> bool { + self.callback.get() == other.callback.get() + } +} + + +/// A trait to be implemented by concrete IDL callback function and +/// callback interface types. +pub trait CallbackContainer { + /// Create a new CallbackContainer object for the given `JSObject`. + fn new(callback: *mut JSObject) -> Rc; + /// Returns the underlying `JSObject`. + fn callback(&self) -> *mut JSObject; +} + + /// A common base class for representing IDL callback function types. #[derive(JSTraceable, PartialEq)] pub struct CallbackFunction { @@ -51,51 +77,20 @@ impl CallbackFunction { pub fn init(&mut self, callback: *mut JSObject) { self.object.callback.set(callback); } + + /// Returns the underlying `JSObject`. + pub fn callback(&self) -> *mut JSObject { + self.object.callback.get() + } } + /// A common base class for representing IDL callback interface types. #[derive(JSTraceable, PartialEq)] pub struct CallbackInterface { object: CallbackObject, } -/// A common base class for representing IDL callback function and -/// callback interface types. -#[derive(JSTraceable)] -struct CallbackObject { - /// The underlying `JSObject`. - callback: Heap<*mut JSObject>, -} - -impl PartialEq for CallbackObject { - fn eq(&self, other: &CallbackObject) -> bool { - self.callback.get() == other.callback.get() - } -} - -/// A trait to be implemented by concrete IDL callback function and -/// callback interface types. -pub trait CallbackContainer { - /// Create a new CallbackContainer object for the given `JSObject`. - fn new(callback: *mut JSObject) -> Rc; - /// Returns the underlying `JSObject`. - fn callback(&self) -> *mut JSObject; -} - -impl CallbackInterface { - /// Returns the underlying `JSObject`. - pub fn callback(&self) -> *mut JSObject { - self.object.callback.get() - } -} - -impl CallbackFunction { - /// Returns the underlying `JSObject`. - pub fn callback(&self) -> *mut JSObject { - self.object.callback.get() - } -} - impl CallbackInterface { /// Create a new CallbackInterface object for the given `JSObject`. pub fn new() -> CallbackInterface { @@ -112,6 +107,11 @@ impl CallbackInterface { self.object.callback.set(callback); } + /// Returns the underlying `JSObject`. + pub fn callback(&self) -> *mut JSObject { + self.object.callback.get() + } + /// Returns the property with the given `name`, if it is a callable object, /// or an error otherwise. pub fn get_callable_property(&self, cx: *mut JSContext, name: &str) -> Fallible { @@ -132,6 +132,7 @@ impl CallbackInterface { } } + /// Wraps the reflector for `p` into the compartment of `cx`. pub fn wrap_call_this_object(cx: *mut JSContext, p: &T, @@ -146,6 +147,7 @@ pub fn wrap_call_this_object(cx: *mut JSContext, } } + /// A class that performs whatever setup we need to safely make a call while /// this class is on the stack. After `new` returns, the call is safe to make. pub struct CallSetup { From 11c21d3b1d0f2f202d505c79cff20bf7c883d78e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 6 Jan 2017 16:23:57 +0100 Subject: [PATCH 2/4] Introduce CallbackObject::new(). --- components/script/dom/bindings/callback.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/components/script/dom/bindings/callback.rs b/components/script/dom/bindings/callback.rs index a9846477ac4..897cc3bd39f 100644 --- a/components/script/dom/bindings/callback.rs +++ b/components/script/dom/bindings/callback.rs @@ -33,12 +33,20 @@ pub enum ExceptionHandling { /// A common base class for representing IDL callback function and /// callback interface types. -#[derive(JSTraceable)] +#[derive(Default, JSTraceable)] struct CallbackObject { /// The underlying `JSObject`. callback: Heap<*mut JSObject>, } +impl CallbackObject { + fn new() -> CallbackObject { + CallbackObject { + callback: Heap::default(), + } + } +} + impl PartialEq for CallbackObject { fn eq(&self, other: &CallbackObject) -> bool { self.callback.get() == other.callback.get() @@ -66,9 +74,7 @@ impl CallbackFunction { /// Create a new `CallbackFunction` for this object. pub fn new() -> CallbackFunction { CallbackFunction { - object: CallbackObject { - callback: Heap::default(), - }, + object: CallbackObject::new(), } } @@ -95,9 +101,7 @@ impl CallbackInterface { /// Create a new CallbackInterface object for the given `JSObject`. pub fn new() -> CallbackInterface { CallbackInterface { - object: CallbackObject { - callback: Heap::default(), - }, + object: CallbackObject::new(), } } From b8554abaa4634a66f41a51d35985f24c39c392f0 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 6 Jan 2017 18:01:10 +0100 Subject: [PATCH 3/4] Expose CallbackObject more. This will make it easier to use new fields added to it. --- components/script/dom/bindings/callback.rs | 34 ++++++++++++------- .../dom/bindings/codegen/CodegenRust.py | 17 +++++----- components/script/dom/eventtarget.rs | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/components/script/dom/bindings/callback.rs b/components/script/dom/bindings/callback.rs index 897cc3bd39f..067b498a35d 100644 --- a/components/script/dom/bindings/callback.rs +++ b/components/script/dom/bindings/callback.rs @@ -34,7 +34,7 @@ pub enum ExceptionHandling { /// A common base class for representing IDL callback function and /// callback interface types. #[derive(Default, JSTraceable)] -struct CallbackObject { +pub struct CallbackObject { /// The underlying `JSObject`. callback: Heap<*mut JSObject>, } @@ -45,6 +45,10 @@ impl CallbackObject { callback: Heap::default(), } } + + pub fn get(&self) -> *mut JSObject { + self.callback.get() + } } impl PartialEq for CallbackObject { @@ -59,8 +63,12 @@ impl PartialEq for CallbackObject { pub trait CallbackContainer { /// Create a new CallbackContainer object for the given `JSObject`. fn new(callback: *mut JSObject) -> Rc; + /// Returns the underlying `CallbackObject`. + fn callback_holder(&self) -> &CallbackObject; /// Returns the underlying `JSObject`. - fn callback(&self) -> *mut JSObject; + fn callback(&self) -> *mut JSObject { + self.callback_holder().get() + } } @@ -78,16 +86,16 @@ impl CallbackFunction { } } + /// Returns the underlying `CallbackObject`. + pub fn callback_holder(&self) -> &CallbackObject { + &self.object + } + /// Initialize the callback function with a value. /// Should be called once this object is done moving. pub fn init(&mut self, callback: *mut JSObject) { self.object.callback.set(callback); } - - /// Returns the underlying `JSObject`. - pub fn callback(&self) -> *mut JSObject { - self.object.callback.get() - } } @@ -105,22 +113,22 @@ impl CallbackInterface { } } + /// Returns the underlying `CallbackObject`. + pub fn callback_holder(&self) -> &CallbackObject { + &self.object + } + /// Initialize the callback function with a value. /// Should be called once this object is done moving. pub fn init(&mut self, callback: *mut JSObject) { self.object.callback.set(callback); } - /// Returns the underlying `JSObject`. - pub fn callback(&self) -> *mut JSObject { - self.object.callback.get() - } - /// Returns the property with the given `name`, if it is a callable object, /// or an error otherwise. pub fn get_callable_property(&self, cx: *mut JSContext, name: &str) -> Fallible { rooted!(in(cx) let mut callable = UndefinedValue()); - rooted!(in(cx) let obj = self.callback()); + rooted!(in(cx) let obj = self.callback_holder().get()); unsafe { let c_name = CString::new(name).unwrap(); if !JS_GetProperty(cx, obj.handle(), c_name.as_ptr(), callable.handle_mut()) { diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 49098a27d54..ffd82a7f135 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -5565,6 +5565,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries 'dom::bindings::callback::CallbackContainer', 'dom::bindings::callback::CallbackInterface', 'dom::bindings::callback::CallbackFunction', + 'dom::bindings::callback::CallbackObject', 'dom::bindings::callback::ExceptionHandling', 'dom::bindings::callback::wrap_call_this_object', 'dom::bindings::conversions::ConversionBehavior', @@ -6339,8 +6340,8 @@ impl CallbackContainer for ${type} { ${type}::new(callback) } - fn callback(&self) -> *mut JSObject { - self.parent.callback() + fn callback_holder(&self) -> &CallbackObject { + self.parent.callback_holder() } } @@ -6619,11 +6620,11 @@ class CallCallback(CallbackMethod): return "aThisObj.get()" def getCallableDecl(self): - return "rooted!(in(cx) let callable = ObjectValue(self.parent.callback()));\n" + return "rooted!(in(cx) let callable = ObjectValue(self.callback()));\n" def getCallGuard(self): if self.callback._treatNonObjectAsNull: - return "!IsCallable(self.parent.callback()) || " + return "!IsCallable(self.callback()) || " return "" @@ -6638,11 +6639,11 @@ class CallbackOperationBase(CallbackMethod): def getThisObj(self): if not self.singleOperation: - return "self.parent.callback()" + return "self.callback()" # This relies on getCallableDecl declaring a boolean # isCallable in the case when we're a single-operation # interface. - return "if isCallable { aThisObj.get() } else { self.parent.callback() }" + return "if isCallable { aThisObj.get() } else { self.callback() }" def getCallableDecl(self): replacements = { @@ -6654,11 +6655,11 @@ class CallbackOperationBase(CallbackMethod): if not self.singleOperation: return 'rooted!(in(cx) let callable =\n' + getCallableFromProp + ');\n' return ( - 'let isCallable = IsCallable(self.parent.callback());\n' + 'let isCallable = IsCallable(self.callback());\n' 'rooted!(in(cx) let callable =\n' + CGIndenter( CGIfElseWrapper('isCallable', - CGGeneric('ObjectValue(self.parent.callback())'), + CGGeneric('ObjectValue(self.callback())'), CGGeneric(getCallableFromProp))).define() + ');\n') def getCallGuard(self): diff --git a/components/script/dom/eventtarget.rs b/components/script/dom/eventtarget.rs index 579b8822065..669c9110683 100644 --- a/components/script/dom/eventtarget.rs +++ b/components/script/dom/eventtarget.rs @@ -484,7 +484,7 @@ impl EventTarget { pub fn get_event_handler_common(&self, ty: &str) -> Option> { let listener = self.get_inline_event_listener(&Atom::from(ty)); - listener.map(|listener| CallbackContainer::new(listener.parent().callback())) + listener.map(|listener| CallbackContainer::new(listener.parent().callback_holder().get())) } pub fn has_handlers(&self) -> bool { From 63f0d3ecf5179a68bcc6cc398f90278b3125912a Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 9 Jan 2017 10:21:50 +0100 Subject: [PATCH 4/4] Privatize some items in EventTarget. --- components/script/dom/eventtarget.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/components/script/dom/eventtarget.rs b/components/script/dom/eventtarget.rs index 669c9110683..09cafb65bac 100644 --- a/components/script/dom/eventtarget.rs +++ b/components/script/dom/eventtarget.rs @@ -69,7 +69,7 @@ pub enum ListenerPhase { /// https://html.spec.whatwg.org/multipage/#internal-raw-uncompiled-handler #[derive(JSTraceable, Clone, PartialEq)] -pub struct InternalRawUncompiledHandler { +struct InternalRawUncompiledHandler { source: DOMString, url: ServoUrl, line: usize, @@ -77,7 +77,7 @@ pub struct InternalRawUncompiledHandler { /// A representation of an event handler, either compiled or uncompiled raw source, or null. #[derive(JSTraceable, PartialEq, Clone)] -pub enum InlineEventListener { +enum InlineEventListener { Uncompiled(InternalRawUncompiledHandler), Compiled(CommonEventHandler), Null, @@ -308,9 +308,9 @@ impl EventTarget { } /// https://html.spec.whatwg.org/multipage/#event-handler-attributes:event-handlers-11 - pub fn set_inline_event_listener(&self, - ty: Atom, - listener: Option) { + fn set_inline_event_listener(&self, + ty: Atom, + listener: Option) { let mut handlers = self.handlers.borrow_mut(); let entries = match handlers.entry(ty) { Occupied(entry) => entry.into_mut(), @@ -363,10 +363,10 @@ impl EventTarget { // https://html.spec.whatwg.org/multipage/#getting-the-current-value-of-the-event-handler #[allow(unsafe_code)] - pub fn get_compiled_event_handler(&self, - handler: InternalRawUncompiledHandler, - ty: &Atom) - -> Option { + fn get_compiled_event_handler(&self, + handler: InternalRawUncompiledHandler, + ty: &Atom) + -> Option { // Step 1.1 let element = self.downcast::(); let document = match element {