Use DOMRefCell<T> in Element.

This commit is contained in:
Tetsuharu OHZEKI 2014-10-15 03:41:06 +09:00
parent 2d5d1e36ad
commit fb98384fa5
2 changed files with 13 additions and 18 deletions

View file

@ -35,6 +35,7 @@ use css::node_style::StyledNode;
use util::{LayoutDataAccess, LayoutDataWrapper, PrivateLayoutData, OpaqueNodeMethods}; use util::{LayoutDataAccess, LayoutDataWrapper, PrivateLayoutData, OpaqueNodeMethods};
use gfx::display_list::OpaqueNode; use gfx::display_list::OpaqueNode;
use script::dom::bindings::cell::{Ref, RefMut};
use script::dom::bindings::codegen::InheritTypes::{ElementCast, HTMLIFrameElementCast, HTMLImageElementCast}; use script::dom::bindings::codegen::InheritTypes::{ElementCast, HTMLIFrameElementCast, HTMLImageElementCast};
use script::dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, TextCast}; use script::dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, TextCast};
use script::dom::bindings::js::JS; use script::dom::bindings::js::JS;
@ -50,7 +51,6 @@ use script::dom::text::Text;
use script::layout_interface::LayoutChan; use script::layout_interface::LayoutChan;
use servo_msg::constellation_msg::{PipelineId, SubpageId}; use servo_msg::constellation_msg::{PipelineId, SubpageId};
use servo_util::str::{LengthOrPercentageOrAuto, is_whitespace}; use servo_util::str::{LengthOrPercentageOrAuto, is_whitespace};
use std::cell::{RefCell, Ref, RefMut};
use std::kinds::marker::ContravariantLifetime; use std::kinds::marker::ContravariantLifetime;
use std::mem; use std::mem;
use style::computed_values::{content, display, white_space}; use style::computed_values::{content, display, white_space};
@ -445,9 +445,7 @@ pub struct LayoutElement<'le> {
impl<'le> LayoutElement<'le> { impl<'le> LayoutElement<'le> {
pub fn style_attribute(&self) -> &'le Option<PropertyDeclarationBlock> { pub fn style_attribute(&self) -> &'le Option<PropertyDeclarationBlock> {
let style: &Option<PropertyDeclarationBlock> = unsafe { let style: &Option<PropertyDeclarationBlock> = unsafe {
let style: &RefCell<Option<PropertyDeclarationBlock>> = self.element.style_attribute(); &*self.element.style_attribute().borrow_for_layout()
// cast to the direct reference to T placed on the head of RefCell<T>
mem::transmute(style)
}; };
style style
} }

View file

@ -7,6 +7,7 @@
use dom::attr::{Attr, ReplacedAttr, FirstSetAttr, AttrHelpers, AttrHelpersForLayout}; use dom::attr::{Attr, ReplacedAttr, FirstSetAttr, AttrHelpers, AttrHelpersForLayout};
use dom::attr::{AttrValue, StringAttrValue, UIntAttrValue, AtomAttrValue}; use dom::attr::{AttrValue, StringAttrValue, UIntAttrValue, AtomAttrValue};
use dom::namednodemap::NamedNodeMap; use dom::namednodemap::NamedNodeMap;
use dom::bindings::cell::{DOMRefCell, Ref, RefMut};
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods; use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use dom::bindings::codegen::Bindings::ElementBinding; use dom::bindings::codegen::Bindings::ElementBinding;
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
@ -39,7 +40,6 @@ use servo_util::namespace;
use servo_util::str::{DOMString, LengthOrPercentageOrAuto}; use servo_util::str::{DOMString, LengthOrPercentageOrAuto};
use std::ascii::StrAsciiExt; use std::ascii::StrAsciiExt;
use std::cell::{Ref, RefMut, RefCell};
use std::default::Default; use std::default::Default;
use std::mem; use std::mem;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
@ -53,8 +53,8 @@ pub struct Element {
local_name: Atom, local_name: Atom,
namespace: Namespace, namespace: Namespace,
prefix: Option<DOMString>, prefix: Option<DOMString>,
attrs: RefCell<Vec<JS<Attr>>>, attrs: DOMRefCell<Vec<JS<Attr>>>,
style_attribute: RefCell<Option<style::PropertyDeclarationBlock>>, style_attribute: DOMRefCell<Option<style::PropertyDeclarationBlock>>,
attr_list: MutNullableJS<NamedNodeMap>, attr_list: MutNullableJS<NamedNodeMap>,
class_list: MutNullableJS<DOMTokenList>, class_list: MutNullableJS<DOMTokenList>,
} }
@ -160,10 +160,10 @@ impl Element {
local_name: Atom::from_slice(local_name.as_slice()), local_name: Atom::from_slice(local_name.as_slice()),
namespace: namespace, namespace: namespace,
prefix: prefix, prefix: prefix,
attrs: RefCell::new(vec!()), attrs: DOMRefCell::new(vec!()),
attr_list: Default::default(), attr_list: Default::default(),
class_list: Default::default(), class_list: Default::default(),
style_attribute: RefCell::new(None), style_attribute: DOMRefCell::new(None),
} }
} }
@ -203,7 +203,7 @@ impl Element {
} }
#[inline] #[inline]
pub fn style_attribute<'a>(&'a self) -> &'a RefCell<Option<style::PropertyDeclarationBlock>> { pub fn style_attribute<'a>(&'a self) -> &'a DOMRefCell<Option<style::PropertyDeclarationBlock>> {
&self.style_attribute &self.style_attribute
} }
} }
@ -226,8 +226,7 @@ impl RawLayoutElementHelpers for Element {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
unsafe fn get_attr_val_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom) unsafe fn get_attr_val_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom)
-> Option<&'a str> { -> Option<&'a str> {
// cast to point to T in RefCell<T> directly let attrs = self.attrs.borrow_for_layout();
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs);
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
*name == (*attr).local_name_atom_forever() && *name == (*attr).local_name_atom_forever() &&
@ -241,8 +240,7 @@ impl RawLayoutElementHelpers for Element {
#[inline] #[inline]
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
unsafe fn get_attr_vals_for_layout<'a>(&'a self, name: &Atom) -> Vec<&'a str> { unsafe fn get_attr_vals_for_layout<'a>(&'a self, name: &Atom) -> Vec<&'a str> {
// cast to point to T in RefCell<T> directly let attrs = self.attrs.borrow_for_layout();
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs);
(*attrs).iter().filter_map(|attr: &JS<Attr>| { (*attrs).iter().filter_map(|attr: &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
if *name == (*attr).local_name_atom_forever() { if *name == (*attr).local_name_atom_forever() {
@ -257,8 +255,7 @@ impl RawLayoutElementHelpers for Element {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom) unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom)
-> Option<Atom> { -> Option<Atom> {
// cast to point to T in RefCell<T> directly let attrs = self.attrs.borrow_for_layout();
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs);
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
*name == (*attr).local_name_atom_forever() && *name == (*attr).local_name_atom_forever() &&
@ -272,7 +269,7 @@ impl RawLayoutElementHelpers for Element {
#[inline] #[inline]
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
unsafe fn has_class_for_layout(&self, name: &Atom) -> bool { unsafe fn has_class_for_layout(&self, name: &Atom) -> bool {
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs); let attrs = self.attrs.borrow_for_layout();
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
(*attr).local_name_atom_forever() == atom!("class") (*attr).local_name_atom_forever() == atom!("class")
@ -287,7 +284,7 @@ impl RawLayoutElementHelpers for Element {
#[inline] #[inline]
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> { unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> {
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs); let attrs = self.attrs.borrow_for_layout();
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
(*attr).local_name_atom_forever() == atom!("class") (*attr).local_name_atom_forever() == atom!("class")