Skip to content

Commit

Permalink
Make layout methods accessing rare data be safe
Browse files Browse the repository at this point in the history
They don't do anything fancy so there is no additional unsafety calling them
compared to using LayoutDom in the first place, the usual story of all
those changes.
  • Loading branch information
nox committed Mar 31, 2020
1 parent 414d477 commit 9c8540a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
18 changes: 7 additions & 11 deletions components/layout_thread/dom_wrapper.rs
Expand Up @@ -649,21 +649,17 @@ impl<'le> TElement for ServoLayoutElement<'le> {

/// The shadow root this element is a host of.
fn shadow_root(&self) -> Option<ServoShadowRoot<'le>> {
unsafe {
self.element
.get_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}
self.element
.get_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}

/// The shadow root which roots the subtree this element is contained in.
fn containing_shadow(&self) -> Option<ServoShadowRoot<'le>> {
unsafe {
self.element
.upcast()
.containing_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}
self.element
.upcast()
.containing_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}

fn local_name(&self) -> &LocalName {
Expand Down
18 changes: 7 additions & 11 deletions components/layout_thread_2020/dom_wrapper.rs
Expand Up @@ -656,21 +656,17 @@ impl<'le> TElement for ServoLayoutElement<'le> {

/// The shadow root this element is a host of.
fn shadow_root(&self) -> Option<ServoShadowRoot<'le>> {
unsafe {
self.element
.get_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}
self.element
.get_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}

/// The shadow root which roots the subtree this element is contained in.
fn containing_shadow(&self) -> Option<ServoShadowRoot<'le>> {
unsafe {
self.element
.upcast()
.containing_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}
self.element
.upcast()
.containing_shadow_root_for_layout()
.map(ServoShadowRoot::from_layout_js)
}

fn local_name(&self) -> &LocalName {
Expand Down
21 changes: 11 additions & 10 deletions components/script/dom/element.rs
Expand Up @@ -583,8 +583,7 @@ pub trait LayoutElementHelpers<'dom> {
fn insert_selector_flags(self, flags: ElementSelectorFlags);
fn has_selector_flags(self, flags: ElementSelectorFlags) -> bool;
/// The shadow root this element is a host of.
#[allow(unsafe_code)]
unsafe fn get_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>>;
fn get_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>>;
fn get_attr_for_layout(
self,
namespace: &Namespace,
Expand Down Expand Up @@ -1038,14 +1037,16 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {

#[inline]
#[allow(unsafe_code)]
unsafe fn get_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>> {
self.unsafe_get()
.rare_data
.borrow_for_layout()
.as_ref()?
.shadow_root
.as_ref()
.map(|sr| sr.to_layout())
fn get_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>> {
unsafe {
self.unsafe_get()
.rare_data
.borrow_for_layout()
.as_ref()?
.shadow_root
.as_ref()
.map(|sr| sr.to_layout())
}
}

#[inline]
Expand Down
20 changes: 11 additions & 9 deletions components/script/dom/node.rs
Expand Up @@ -1314,7 +1314,7 @@ pub trait LayoutNodeHelpers<'dom> {
unsafe fn next_sibling_ref(self) -> Option<LayoutDom<'dom, Node>>;

unsafe fn owner_doc_for_layout(self) -> LayoutDom<'dom, Document>;
unsafe fn containing_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>>;
fn containing_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>>;

unsafe fn is_element_for_layout(self) -> bool;
unsafe fn get_flag(self, flag: NodeFlags) -> bool;
Expand Down Expand Up @@ -1399,14 +1399,16 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {

#[inline]
#[allow(unsafe_code)]
unsafe fn containing_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>> {
self.unsafe_get()
.rare_data
.borrow_for_layout()
.as_ref()?
.containing_shadow_root
.as_ref()
.map(|sr| sr.to_layout())
fn containing_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>> {
unsafe {
self.unsafe_get()
.rare_data
.borrow_for_layout()
.as_ref()?
.containing_shadow_root
.as_ref()
.map(|sr| sr.to_layout())
}
}

#[inline]
Expand Down

0 comments on commit 9c8540a

Please sign in to comment.