Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalise RootedVec::<JS<T>>::r as [JS<T>]::r #11171

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -125,7 +125,7 @@ pub fn handle_get_layout(context: &BrowsingContext,

let window = context.active_window();
let elem = node.downcast::<Element>().expect("should be getting layout of element");
let computed_style = window.r().GetComputedStyle(elem, None);
let computed_style = window.GetComputedStyle(elem, None);

reply.send(ComputedNodeLayout {
display: String::from(computed_style.Display()),
@@ -61,11 +61,11 @@ pub fn synthetic_click_activation(element: &Element,
// https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event
let win = window_from_node(element);
let target = element.upcast::<EventTarget>();
let mouse = MouseEvent::new(win.r(),
let mouse = MouseEvent::new(&win,
DOMString::from("click"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
Some(win.r()),
Some(&win),
1,
0,
0,
@@ -6,6 +6,7 @@

use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::global_root_from_object;
use dom::bindings::js::RootedReference;
use dom::bindings::reflector::Reflectable;
use js::jsapi::GetGlobalForObjectCrossCompartment;
use js::jsapi::JSAutoCompartment;
@@ -5569,7 +5569,6 @@ def __init__(self, config, prefix, webIDLFile):
'dom::bindings::interface::{ConstantSpec, NonNullJSNative}',
'dom::bindings::interface::ConstantVal::{IntVal, UintVal}',
'dom::bindings::js::{JS, Root, RootedReference}',
'dom::bindings::js::{OptionalRootedReference}',
'dom::bindings::reflector::{Reflectable}',
'dom::bindings::utils::{DOMClass, DOMJSClass, Prefable}',
'dom::bindings::utils::{DOM_PROTO_UNFORGEABLE_HOLDER_SLOT, JSCLASS_DOM_GLOBAL}',
@@ -10,7 +10,7 @@
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::conversions::root_from_object;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::window::{self, ScriptHelpers};
use dom::workerglobalscope::WorkerGlobalScope;
@@ -265,10 +265,9 @@ impl<'a> GlobalRef<'a> {
}
}

impl GlobalRoot {
/// Obtain a safe reference to the global object that cannot outlive the
/// lifetime of this root.
pub fn r(&self) -> GlobalRef {
impl<'root> RootedReference<'root> for GlobalRoot {
type Ref = GlobalRef<'root>;
fn r(&'root self) -> GlobalRef<'root> {
match *self {
GlobalRoot::Window(ref window) => GlobalRef::Window(window.r()),
GlobalRoot::Worker(ref worker) => GlobalRef::Worker(worker.r()),
@@ -94,6 +94,13 @@ impl<T: Reflectable> JS<T> {
}
}

impl<'root, T: Reflectable + 'root> RootedReference<'root> for JS<T> {
type Ref = &'root T;
fn r(&'root self) -> &'root T {
&self
}
}

impl<T: Reflectable> Deref for JS<T> {
type Target = T;

@@ -441,36 +448,25 @@ impl<T: Reflectable> LayoutJS<T> {
}
}

/// Get an `Option<&T>` out of an `Option<Root<T>>`
pub trait RootedReference<T> {
/// Obtain a safe optional reference to the wrapped JS owned-value that
/// cannot outlive the lifetime of this root.
fn r(&self) -> Option<&T>;
/// Get a reference out of a rooted value.
pub trait RootedReference<'root> {
/// The type of the reference.
type Ref: 'root;
/// Obtain a reference out of the rooted value.
fn r(&'root self) -> Self::Ref;
}

impl<T: Reflectable> RootedReference<T> for Option<Root<T>> {
fn r(&self) -> Option<&T> {
self.as_ref().map(|root| root.r())
impl<'root, T: JSTraceable + Reflectable + 'root> RootedReference<'root> for [JS<T>] {
type Ref = &'root [&'root T];
fn r(&'root self) -> &'root [&'root T] {
unsafe { mem::transmute(self) }
}
}

/// Get an `Option<&T> out of an `Option<JS<T>>`
impl<T: Reflectable> RootedReference<T> for Option<JS<T>> {
fn r(&self) -> Option<&T> {
self.as_ref().map(|inner| &**inner)
}
}

/// Get an `Option<Option<&T>>` out of an `Option<Option<Root<T>>>`
pub trait OptionalRootedReference<T> {
/// Obtain a safe optional optional reference to the wrapped JS owned-value
/// that cannot outlive the lifetime of this root.
fn r(&self) -> Option<Option<&T>>;
}

impl<T: Reflectable> OptionalRootedReference<T> for Option<Option<Root<T>>> {
fn r(&self) -> Option<Option<&T>> {
self.as_ref().map(|inner| inner.r())
impl<'root, T: RootedReference<'root> + 'root> RootedReference<'root> for Option<T> {
type Ref = Option<T::Ref>;
fn r(&'root self) -> Option<T::Ref> {
self.as_ref().map(RootedReference::r)
}
}

@@ -595,11 +591,12 @@ impl<T: Reflectable> Root<T> {
pub fn from_ref(unrooted: &T) -> Root<T> {
Root::new(unsafe { NonZero::new(&*unrooted) })
}
}

/// Obtain a safe reference to the wrapped JS owned-value that cannot
/// outlive the lifetime of this root.
pub fn r(&self) -> &T {
&**self
impl<'root, T: Reflectable + 'root> RootedReference<'root> for Root<T> {
type Ref = &'root T;
fn r(&'root self) -> &'root T {
&self
}
}

@@ -23,7 +23,7 @@
//! is removed.

use core::nonzero::NonZero;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::bindings::trace::trace_reflector;
use js::jsapi::JSTracer;
@@ -75,7 +75,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::{BuildHasher, Hash};
use std::intrinsics::return_address;
use std::iter::{FromIterator, IntoIterator};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::Arc;
@@ -509,13 +508,6 @@ impl<T: JSTraceable> RootedVec<T> {
}
}

impl<T: JSTraceable + Reflectable> RootedVec<JS<T>> {
/// Obtain a safe slice of references that can't outlive that RootedVec.
pub fn r(&self) -> &[&T] {
unsafe { mem::transmute(&self.v[..]) }
}
}

impl<T: JSTraceable> Drop for RootedVec<T> {
fn drop(&mut self) {
unsafe {
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use dom::bindings::codegen::UnionTypes::BlobOrString;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use encoding::all::UTF_8;
use encoding::types::{EncoderTrap, Encoding};
@@ -9,7 +9,7 @@ use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothScanFilter, Bl
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothdevice::BluetoothDevice;
@@ -5,7 +5,7 @@
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
use dom::bindings::js::{JS, MutHeap, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
@@ -12,7 +12,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::{Network, Type};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
@@ -13,7 +13,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::{Type, Network};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
@@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::Bluetoot
use dom::bindings::error::Error::Type;
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice;
@@ -22,7 +22,7 @@ use dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
use dom::bindings::error::{Error, Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, Root};
use dom::bindings::js::{JS, LayoutJS, Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle};
@@ -33,6 +33,6 @@ impl Comment {

pub fn Constructor(global: GlobalRef, data: DOMString) -> Fallible<Root<Comment>> {
let document = global.as_window().Document();
Ok(Comment::new(data, document.r()))
Ok(Comment::new(data, &document))
}
}
@@ -7,7 +7,7 @@ use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::ConsoleBinding;
use dom::bindings::codegen::Bindings::ConsoleBinding::ConsoleMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use std::collections::HashMap;
use time::{Timespec, get_time};
@@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::error::ErrorResult;
use dom::bindings::global::{GlobalRef, global_root_from_context};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{Root, RootCollection};
use dom::bindings::js::{Root, RootCollection, RootedReference};
use dom::bindings::refcounted::LiveDOMReferences;
use dom::bindings::reflector::Reflectable;
use dom::bindings::structuredclone::StructuredCloneData;
@@ -23,8 +23,7 @@ use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::RootedReference;
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root};
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
@@ -41,7 +41,7 @@ impl DocumentFragment {
pub fn Constructor(global: GlobalRef) -> Fallible<Root<DocumentFragment>> {
let document = global.as_window().Document();

Ok(DocumentFragment::new(document.r()))
Ok(DocumentFragment::new(&document))
}
}

@@ -134,7 +134,7 @@ impl DOMImplementationMethods for DOMImplementation {
{
// Step 3.
let doc_node = doc.upcast::<Node>();
let doc_type = DocumentType::new(DOMString::from("html"), None, None, doc.r());
let doc_type = DocumentType::new(DOMString::from("html"), None, None, &doc);
doc_node.AppendChild(doc_type.upcast()).unwrap();
}

@@ -143,14 +143,14 @@ impl DOMImplementationMethods for DOMImplementation {
let doc_node = doc.upcast::<Node>();
let doc_html = Root::upcast::<Node>(HTMLHtmlElement::new(atom!("html"),
None,
doc.r()));
&doc));
doc_node.AppendChild(&doc_html).expect("Appending failed");

{
// Step 5.
let doc_head = Root::upcast::<Node>(HTMLHeadElement::new(atom!("head"),
None,
doc.r()));
&doc));
doc_html.AppendChild(&doc_head).unwrap();

// Step 6.
@@ -161,18 +161,18 @@ impl DOMImplementationMethods for DOMImplementation {
let doc_title =
Root::upcast::<Node>(HTMLTitleElement::new(atom!("title"),
None,
doc.r()));
&doc));
doc_head.AppendChild(&doc_title).unwrap();

// Step 6.2.
let title_text = Text::new(title_str, doc.r());
let title_text = Text::new(title_str, &doc);
doc_title.AppendChild(title_text.upcast()).unwrap();
}
}
}

// Step 7.
let doc_body = HTMLBodyElement::new(atom!("body"), None, doc.r());
let doc_body = HTMLBodyElement::new(atom!("body"), None, &doc);
doc_html.AppendChild(doc_body.upcast()).unwrap();
}

@@ -54,7 +54,6 @@ impl DOMParserMethods for DOMParser {
let content_type =
DOMString::from(DOMParserBinding::SupportedTypeValues::strings[ty as usize]);
let doc = self.window.Document();
let doc = doc.r();
let loader = DocumentLoader::new(&*doc.loader());
match ty {
Text_html => {
@@ -66,7 +65,7 @@ impl DOMParserMethods for DOMParser {
None,
DocumentSource::FromParser,
loader);
parse_html(document.r(), s, url, ParseContext::Owner(None));
parse_html(&document, s, url, ParseContext::Owner(None));
document.set_ready_state(DocumentReadyState::Complete);
Ok(document)
}
@@ -80,7 +79,7 @@ impl DOMParserMethods for DOMParser {
None,
DocumentSource::NotFromParser,
loader);
parse_xml(document.r(), s, url, xml::ParseContext::Owner(None));
parse_xml(&document, s, url, xml::ParseContext::Owner(None));
Ok(document)
}
}
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::DOMQuadBinding::{DOMQuadInit, DOMQuadMetho
use dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::{DOMRectInit};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{Root, JS};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::dompoint::DOMPoint;
use dom::domrect::DOMRect;
@@ -29,7 +29,7 @@ impl DOMStringMap {
pub fn new(element: &HTMLElement) -> Root<DOMStringMap> {
let window = window_from_node(element);
reflect_dom_object(box DOMStringMap::new_inherited(element),
GlobalRef::Window(window.r()),
GlobalRef::Window(&window),
DOMStringMapBinding::Wrap)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.