Give a lifetime parameter to LayoutDom

This commit is contained in:
Anthony Ramine 2020-03-27 17:37:56 +01:00
parent 60ca98b753
commit dba6a635e5
26 changed files with 145 additions and 235 deletions

View file

@ -70,7 +70,6 @@ use servo_url::ServoUrl;
use std::fmt;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::atomic::Ordering;
use std::sync::Arc as StdArc;
@ -100,12 +99,9 @@ pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) {
}
#[derive(Clone, Copy)]
pub struct ServoLayoutNode<'a> {
pub struct ServoLayoutNode<'dom> {
/// The wrapped node.
node: LayoutDom<Node>,
/// Being chained to a PhantomData prevents `LayoutNode`s from escaping.
chain: PhantomData<&'a ()>,
node: LayoutDom<'dom, Node>,
}
impl<'ln> Debug for ServoLayoutNode<'ln> {
@ -130,25 +126,14 @@ impl<'a> PartialEq for ServoLayoutNode<'a> {
}
impl<'ln> ServoLayoutNode<'ln> {
fn from_layout_js(n: LayoutDom<Node>) -> ServoLayoutNode<'ln> {
ServoLayoutNode {
node: n,
chain: PhantomData,
}
fn from_layout_js(n: LayoutDom<'ln, Node>) -> Self {
ServoLayoutNode { node: n }
}
pub unsafe fn new(address: &TrustedNodeAddress) -> Self {
ServoLayoutNode::from_layout_js(LayoutDom::from_trusted_node_address(*address))
}
/// Creates a new layout node with the same lifetime as this layout node.
pub unsafe fn new_with_this_lifetime(&self, node: &LayoutDom<Node>) -> ServoLayoutNode<'ln> {
ServoLayoutNode {
node: *node,
chain: self.chain,
}
}
fn script_type_id(&self) -> NodeTypeId {
unsafe { self.node.type_id_for_layout() }
}
@ -166,12 +151,9 @@ impl<'ln> NodeInfo for ServoLayoutNode<'ln> {
}
#[derive(Clone, Copy, PartialEq)]
pub struct ServoShadowRoot<'a> {
pub struct ServoShadowRoot<'dom> {
/// The wrapped shadow root.
shadow_root: LayoutDom<ShadowRoot>,
/// Being chained to a PhantomData prevents `ShadowRoot`s from escaping.
chain: PhantomData<&'a ()>,
shadow_root: LayoutDom<'dom, ShadowRoot>,
}
impl<'lr> Debug for ServoShadowRoot<'lr> {
@ -205,11 +187,8 @@ impl<'lr> TShadowRoot for ServoShadowRoot<'lr> {
}
impl<'lr> ServoShadowRoot<'lr> {
fn from_layout_js(shadow_root: LayoutDom<ShadowRoot>) -> ServoShadowRoot<'lr> {
ServoShadowRoot {
shadow_root,
chain: PhantomData,
}
fn from_layout_js(shadow_root: LayoutDom<'lr, ShadowRoot>) -> Self {
ServoShadowRoot { shadow_root }
}
pub unsafe fn flush_stylesheets(
@ -232,40 +211,24 @@ impl<'ln> TNode for ServoLayoutNode<'ln> {
unsafe {
self.node
.composed_parent_node_ref()
.map(|node| self.new_with_this_lifetime(&node))
.map(Self::from_layout_js)
}
}
fn first_child(&self) -> Option<Self> {
unsafe {
self.node
.first_child_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
unsafe { self.node.first_child_ref().map(Self::from_layout_js) }
}
fn last_child(&self) -> Option<Self> {
unsafe {
self.node
.last_child_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
unsafe { self.node.last_child_ref().map(Self::from_layout_js) }
}
fn prev_sibling(&self) -> Option<Self> {
unsafe {
self.node
.prev_sibling_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
unsafe { self.node.prev_sibling_ref().map(Self::from_layout_js) }
}
fn next_sibling(&self) -> Option<Self> {
unsafe {
self.node
.next_sibling_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
unsafe { self.node.next_sibling_ref().map(Self::from_layout_js) }
}
fn owner_doc(&self) -> Self::ConcreteDocument {
@ -311,7 +274,7 @@ impl<'ln> LayoutNode<'ln> for ServoLayoutNode<'ln> {
type ConcreteThreadSafeLayoutNode = ServoThreadSafeLayoutNode<'ln>;
fn to_threadsafe(&self) -> Self::ConcreteThreadSafeLayoutNode {
ServoThreadSafeLayoutNode::new(self)
ServoThreadSafeLayoutNode::new(*self)
}
fn type_id(&self) -> LayoutNodeType {
@ -368,16 +331,15 @@ impl<'le> GetLayoutData<'le> for ServoThreadSafeLayoutElement<'le> {
impl<'ln> ServoLayoutNode<'ln> {
/// Returns the interior of this node as a `LayoutDom`. This is highly unsafe for layout to
/// call and as such is marked `unsafe`.
pub unsafe fn get_jsmanaged(&self) -> &LayoutDom<Node> {
&self.node
pub unsafe fn get_jsmanaged(&self) -> LayoutDom<'ln, Node> {
self.node
}
}
// A wrapper around documents that ensures ayout can only ever access safe properties.
#[derive(Clone, Copy)]
pub struct ServoLayoutDocument<'ld> {
document: LayoutDom<Document>,
chain: PhantomData<&'ld ()>,
pub struct ServoLayoutDocument<'dom> {
document: LayoutDom<'dom, Document>,
}
impl<'ld> TDocument for ServoLayoutDocument<'ld> {
@ -446,19 +408,15 @@ impl<'ld> ServoLayoutDocument<'ld> {
}
}
pub fn from_layout_js(doc: LayoutDom<Document>) -> ServoLayoutDocument<'ld> {
ServoLayoutDocument {
document: doc,
chain: PhantomData,
}
pub fn from_layout_js(doc: LayoutDom<'ld, Document>) -> Self {
ServoLayoutDocument { document: doc }
}
}
/// A wrapper around elements that ensures layout can only ever access safe properties.
#[derive(Clone, Copy)]
pub struct ServoLayoutElement<'le> {
element: LayoutDom<Element>,
chain: PhantomData<&'le ()>,
pub struct ServoLayoutElement<'dom> {
element: LayoutDom<'dom, Element>,
}
impl<'le> fmt::Debug for ServoLayoutElement<'le> {
@ -739,11 +697,8 @@ impl<'le> Hash for ServoLayoutElement<'le> {
impl<'le> Eq for ServoLayoutElement<'le> {}
impl<'le> ServoLayoutElement<'le> {
fn from_layout_js(el: LayoutDom<Element>) -> ServoLayoutElement<'le> {
ServoLayoutElement {
element: el,
chain: PhantomData,
}
fn from_layout_js(el: LayoutDom<'le, Element>) -> Self {
ServoLayoutElement { element: el }
}
#[inline]
@ -789,7 +744,7 @@ impl<'le> ServoLayoutElement<'le> {
}
}
fn as_element<'le>(node: LayoutDom<Node>) -> Option<ServoLayoutElement<'le>> {
fn as_element<'dom>(node: LayoutDom<'dom, Node>) -> Option<ServoLayoutElement<'dom>> {
node.downcast().map(ServoLayoutElement::from_layout_js)
}
@ -1044,29 +999,20 @@ impl<'ln> DangerousThreadSafeLayoutNode<'ln> for ServoThreadSafeLayoutNode<'ln>
unsafe fn dangerous_first_child(&self) -> Option<Self> {
self.get_jsmanaged()
.first_child_ref()
.map(|node| self.new_with_this_lifetime(&node))
.map(ServoLayoutNode::from_layout_js)
.map(Self::new)
}
unsafe fn dangerous_next_sibling(&self) -> Option<Self> {
self.get_jsmanaged()
.next_sibling_ref()
.map(|node| self.new_with_this_lifetime(&node))
.map(ServoLayoutNode::from_layout_js)
.map(Self::new)
}
}
impl<'ln> ServoThreadSafeLayoutNode<'ln> {
/// Creates a new layout node with the same lifetime as this layout node.
pub unsafe fn new_with_this_lifetime(
&self,
node: &LayoutDom<Node>,
) -> ServoThreadSafeLayoutNode<'ln> {
ServoThreadSafeLayoutNode {
node: self.node.new_with_this_lifetime(node),
pseudo: PseudoElementType::Normal,
}
}
/// Creates a new `ServoThreadSafeLayoutNode` from the given `ServoLayoutNode`.
pub fn new<'a>(node: &ServoLayoutNode<'a>) -> ServoThreadSafeLayoutNode<'a> {
pub fn new(node: ServoLayoutNode<'ln>) -> Self {
ServoThreadSafeLayoutNode {
node: node.clone(),
pseudo: PseudoElementType::Normal,
@ -1075,7 +1021,7 @@ impl<'ln> ServoThreadSafeLayoutNode<'ln> {
/// Returns the interior of this node as a `LayoutDom`. This is highly unsafe for layout to
/// call and as such is marked `unsafe`.
unsafe fn get_jsmanaged(&self) -> &LayoutDom<Node> {
unsafe fn get_jsmanaged(&self) -> LayoutDom<'ln, Node> {
self.node.get_jsmanaged()
}
}