From 7197052c0dd69eafb5331ba1c0d23f2afaa7f7eb Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 27 Apr 2015 17:50:50 +0200 Subject: [PATCH] Uniformise root() methods They now live in traits Rootable, OptionalOptionalRootable, OptionalRootable and ResultRootable. --- components/script/devtools.rs | 2 +- components/script/dom/activation.rs | 2 +- components/script/dom/attr.rs | 3 +- .../dom/bindings/codegen/CodegenRust.py | 6 +- components/script/dom/bindings/error.rs | 1 + components/script/dom/bindings/global.rs | 2 +- components/script/dom/bindings/js.rs | 90 +++++++------------ components/script/dom/bindings/utils.rs | 2 +- components/script/dom/browsercontext.rs | 4 +- .../script/dom/canvasrenderingcontext2d.rs | 2 +- components/script/dom/comment.rs | 2 +- components/script/dom/cssstyledeclaration.rs | 2 +- components/script/dom/customevent.rs | 2 +- .../script/dom/dedicatedworkerglobalscope.rs | 2 +- components/script/dom/document.rs | 4 +- components/script/dom/documentfragment.rs | 2 +- components/script/dom/domimplementation.rs | 3 +- components/script/dom/domparser.rs | 2 +- components/script/dom/domstringmap.rs | 2 +- components/script/dom/domtokenlist.rs | 2 +- components/script/dom/element.rs | 4 +- components/script/dom/errorevent.rs | 2 +- components/script/dom/event.rs | 2 +- components/script/dom/eventdispatcher.rs | 2 +- components/script/dom/htmlanchorelement.rs | 2 +- components/script/dom/htmlbodyelement.rs | 2 +- components/script/dom/htmlbuttonelement.rs | 2 +- components/script/dom/htmlcanvaselement.rs | 3 +- components/script/dom/htmlcollection.rs | 2 +- components/script/dom/htmldatalistelement.rs | 2 +- components/script/dom/htmlelement.rs | 2 +- components/script/dom/htmlfieldsetelement.rs | 2 +- components/script/dom/htmlformelement.rs | 2 +- components/script/dom/htmliframeelement.rs | 2 +- components/script/dom/htmlimageelement.rs | 2 +- components/script/dom/htmlinputelement.rs | 4 +- components/script/dom/htmllinkelement.rs | 2 +- components/script/dom/htmlobjectelement.rs | 2 +- components/script/dom/htmloptgroupelement.rs | 2 +- components/script/dom/htmloptionelement.rs | 2 +- components/script/dom/htmloutputelement.rs | 2 +- components/script/dom/htmlscriptelement.rs | 3 +- components/script/dom/htmlselectelement.rs | 2 +- components/script/dom/htmlstyleelement.rs | 2 +- components/script/dom/htmltableelement.rs | 2 +- components/script/dom/htmltextareaelement.rs | 3 +- components/script/dom/htmltitleelement.rs | 2 +- components/script/dom/keyboardevent.rs | 2 +- components/script/dom/location.rs | 2 +- components/script/dom/messageevent.rs | 2 +- components/script/dom/mouseevent.rs | 2 +- components/script/dom/namednodemap.rs | 2 +- components/script/dom/node.rs | 8 +- components/script/dom/nodelist.rs | 2 +- components/script/dom/performance.rs | 2 +- components/script/dom/progressevent.rs | 2 +- components/script/dom/range.rs | 2 +- components/script/dom/servohtmlparser.rs | 2 +- components/script/dom/storage.rs | 2 +- components/script/dom/storageevent.rs | 2 +- components/script/dom/text.rs | 3 +- components/script/dom/treewalker.rs | 3 +- components/script/dom/uievent.rs | 2 +- components/script/dom/urlsearchparams.rs | 2 +- components/script/dom/userscripts.rs | 2 +- components/script/dom/window.rs | 2 +- components/script/dom/worker.rs | 2 +- components/script/dom/xmlhttprequest.rs | 4 +- components/script/parse/html.rs | 4 +- components/script/script_task.rs | 5 +- components/script/webdriver_handlers.rs | 2 +- 71 files changed, 122 insertions(+), 139 deletions(-) diff --git a/components/script/devtools.rs b/components/script/devtools.rs index 271cc39ab35..cdc0537fa15 100644 --- a/components/script/devtools.rs +++ b/components/script/devtools.rs @@ -5,7 +5,7 @@ use devtools_traits::{EvaluateJSReply, NodeInfo, Modification, TimelineMarker, TimelineMarkerType}; use dom::bindings::conversions::FromJSValConvertible; use dom::bindings::conversions::StringificationBehavior; -use dom::bindings::js::{JSRef, Temporary, OptionalRootable}; +use dom::bindings::js::{JSRef, OptionalRootable, Rootable, Temporary}; use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast}; use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; use dom::bindings::codegen::Bindings::DOMRectBinding::{DOMRectMethods}; diff --git a/components/script/dom/activation.rs b/components/script/dom/activation.rs index 34bd440527c..047d3a5c4df 100644 --- a/components/script/dom/activation.rs +++ b/components/script/dom/activation.rs @@ -4,7 +4,7 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods; use dom::bindings::codegen::InheritTypes::{EventCast, EventTargetCast}; -use dom::bindings::js::{JSRef, Temporary, OptionalRootable}; +use dom::bindings::js::{JSRef, Temporary, OptionalRootable, Rootable}; use dom::element::{Element, ActivationElementHelpers}; use dom::event::{Event, EventHelpers, EventBubbles, EventCancelable}; use dom::eventtarget::EventTarget; diff --git a/components/script/dom/attr.rs b/components/script/dom/attr.rs index d9fba643985..5df12e5aa63 100644 --- a/components/script/dom/attr.rs +++ b/components/script/dom/attr.rs @@ -7,8 +7,7 @@ use dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods}; use dom::bindings::codegen::InheritTypes::NodeCast; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, JSRef, MutNullableHeap, Temporary}; -use dom::bindings::js::{OptionalRootable, OptionalRootedRootable}; -use dom::bindings::js::RootedReference; +use dom::bindings::js::{OptionalRootable, Rootable, RootedReference}; use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::element::{Element, AttributeHandlers}; use dom::node::Node; diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 6c0bef73c02..6b4acd64bd4 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -4733,8 +4733,8 @@ class CGBindingRoot(CGThing): 'dom::bindings::global::GlobalRef', 'dom::bindings::global::global_object_for_js_object', 'dom::bindings::js::{JS, JSRef, Root, RootedReference, Temporary, Unrooted}', - 'dom::bindings::js::{OptionalRootable, OptionalRootedRootable, ResultRootable}', - 'dom::bindings::js::{OptionalRootedReference, OptionalOptionalRootedRootable}', + 'dom::bindings::js::{OptionalOptionalRootable, OptionalRootable}', + 'dom::bindings::js::{OptionalRootedReference, ResultRootable, Rootable}', 'dom::bindings::utils::{create_dom_global, do_create_interface_objects}', 'dom::bindings::utils::ConstantSpec', 'dom::bindings::utils::{DOMClass}', @@ -5415,7 +5415,7 @@ class GlobalGenRoots(): descriptors = config.getDescriptors(register=True, isCallback=False) allprotos = [CGGeneric("#![allow(unused_imports)]\n"), CGGeneric("use dom::types::*;\n"), - CGGeneric("use dom::bindings::js::{JS, JSRef, LayoutJS, Temporary};\n"), + CGGeneric("use dom::bindings::js::{JS, JSRef, LayoutJS, Rootable, Temporary};\n"), CGGeneric("use dom::bindings::trace::JSTraceable;\n"), CGGeneric("use dom::bindings::utils::Reflectable;\n"), CGGeneric("use js::jsapi::JSTracer;\n\n"), diff --git a/components/script/dom/bindings/error.rs b/components/script/dom/bindings/error.rs index 181d02f0353..3fe23eaca2a 100644 --- a/components/script/dom/bindings/error.rs +++ b/components/script/dom/bindings/error.rs @@ -6,6 +6,7 @@ use dom::bindings::conversions::ToJSValConvertible; use dom::bindings::global::GlobalRef; +use dom::bindings::js::Rootable; use dom::domexception::{DOMException, DOMErrorName}; use util::str::DOMString; diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs index 41fb88adf56..8297c2244a9 100644 --- a/components/script/dom/bindings/global.rs +++ b/components/script/dom/bindings/global.rs @@ -8,7 +8,7 @@ //! code that works in workers as well as window scopes. use dom::bindings::conversions::native_from_reflector_jsmanaged; -use dom::bindings::js::{JS, JSRef, Root, Unrooted}; +use dom::bindings::js::{JS, JSRef, Rootable, Root, Unrooted}; use dom::bindings::utils::{Reflectable, Reflector}; use dom::workerglobalscope::{WorkerGlobalScope, WorkerGlobalScopeHelpers}; use dom::window::{self, WindowHelpers}; diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs index a6dc495fc04..e7f790fb1ab 100644 --- a/components/script/dom/bindings/js.rs +++ b/components/script/dom/bindings/js.rs @@ -31,17 +31,18 @@ //! the self value will not be collected for the duration of the method call. //! //! Both `Temporary` and `JS` do not allow access to their inner value -//! without explicitly creating a stack-based root via the `root` method. This -//! returns a `Root`, which causes the JS-owned value to be uncollectable -//! for the duration of the `Root` object's lifetime. A `JSRef` can be -//! obtained from a `Root` by calling the `r` method. These `JSRef` -//! values are not allowed to outlive their originating `Root`, to ensure -//! that all interactions with the enclosed value only occur when said value is -//! uncollectable, and will cause static lifetime errors if misused. +//! without explicitly creating a stack-based root via the `root` method +//! through the `Rootable` trait. This returns a `Root`, which causes the +//! JS-owned value to be uncollectable for the duration of the `Root` object's +//! lifetime. A `JSRef` can be obtained from a `Root` by calling the `r` +//! method. These `JSRef` values are not allowed to outlive their +//! originating `Root`, to ensure that all interactions with the enclosed +//! value only occur when said value is uncollectable, and will cause static +//! lifetime errors if misused. //! //! Other miscellaneous helper traits: //! -//! - `OptionalRootable` and `OptionalRootedRootable`: make rooting `Option` +//! - `OptionalRootable` and `OptionalOptionalRootable`: make rooting `Option` //! values easy via a `root` method //! - `ResultRootable`: make rooting successful `Result` values easy //! - `TemporaryPushable`: allows mutating vectors of `JS` with new elements @@ -108,9 +109,11 @@ impl Unrooted { pub unsafe fn unsafe_get(&self) -> *const T { *self.ptr } +} +impl Rootable for Unrooted { /// Create a stack-bounded root for this value. - pub fn root(self) -> Root { + fn root(&self) -> Root { STACK_ROOTS.with(|ref collection| { let RootCollectionPtr(collection) = collection.get().unwrap(); unsafe { @@ -172,21 +175,18 @@ impl Temporary { Temporary::new(JS::from_rooted(root)) } - /// Create a stack-bounded root for this value. - pub fn root(&self) -> Root { - STACK_ROOTS.with(|ref collection| { - let RootCollectionPtr(collection) = collection.get().unwrap(); - unsafe { - Root::new(&*collection, self.inner.ptr) - } - }) - } - unsafe fn inner(&self) -> JS { self.inner.clone() } } +impl Rootable for Temporary { + /// Create a stack-bounded root for this value. + fn root(&self) -> Root { + self.inner.root() + } +} + /// A traced reference to a DOM object. Must only be used as a field in other /// DOM objects. #[must_root] @@ -264,9 +264,9 @@ impl LayoutJS { } } -impl JS { +impl Rootable for JS { /// Root this JS-owned value to prevent its collection as garbage. - pub fn root(&self) -> Root { + fn root(&self) -> Root { STACK_ROOTS.with(|ref collection| { let RootCollectionPtr(collection) = collection.get().unwrap(); unsafe { @@ -475,12 +475,12 @@ impl Assignable for Temporary { /// Root a rootable `Option` type (used for `Option>`) pub trait OptionalRootable { /// Root the inner value, if it exists. - fn root(self) -> Option>; + fn root(&self) -> Option>; } -impl OptionalRootable for Option> { - fn root(self) -> Option> { - self.map(|inner| inner.root()) +impl> OptionalRootable for Option { + fn root(&self) -> Option> { + self.as_ref().map(|inner| inner.root()) } } @@ -496,61 +496,37 @@ impl<'a, T: Reflectable> OptionalUnrootable for Option> { } } -/// Root a rootable `Option` type (used for `Option>`) -pub trait OptionalRootedRootable { - /// Root the inner value, if it exists. - fn root(&self) -> Option>; -} - -impl OptionalRootedRootable for Option> { - fn root(&self) -> Option> { - self.as_ref().map(|inner| inner.root()) - } -} - -impl OptionalRootedRootable for Option> { - fn root(&self) -> Option> { - self.as_ref().map(|inner| inner.root()) - } -} - /// Root a rootable `Option