Skip to content

Commit

Permalink
Move EventTargetTypeId/NodeTypeId to DOMClass
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwu authored and nox committed Sep 11, 2015
1 parent 8d7ba12 commit 941f7dc
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 74 deletions.
3 changes: 3 additions & 0 deletions components/layout/construct.rs
Expand Up @@ -1430,6 +1430,8 @@ impl<'a> PostorderNodeMutTraversal for FlowConstructor<'a> {
Some(NodeTypeId::Document) => {
(display::T::none, float::T::none, position::T::static_)
}
Some(NodeTypeId::Node) => unreachable!(),
Some(NodeTypeId::CharacterData(CharacterDataTypeId::CharacterData)) => unreachable!(),
};

debug!("building flow for node: {:?} {:?} {:?} {:?}", display, float, positioning, node.type_id());
Expand Down Expand Up @@ -1585,6 +1587,7 @@ impl<'ln> NodeUtils for ThreadSafeLayoutNode<'ln> {
Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
HTMLElementTypeId::HTMLObjectElement))) => self.has_object_data(),
Some(NodeTypeId::Element(_)) => false,
Some(NodeTypeId::Node) => unreachable!(),
}
}

Expand Down
22 changes: 21 additions & 1 deletion components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -1722,6 +1722,25 @@ def build(namespaces, child, public=False):
return CGNamespace(namespaces[0], inner, public=public)


def EventTargetEnum(desc):
protochain = desc.prototypeChain
if protochain[0] != "EventTarget":
return "None"

inner = ""
name = desc.interface.identifier.name
if desc.interface.getUserData("hasConcreteDescendant", False):
inner = "(::dom::%s::%sTypeId::%s)" % (name.lower(), name, name)
prev_proto = ""
for proto in reversed(protochain):
if prev_proto != "":
inner = "(::dom::%s::%sTypeId::%s%s)" % (proto.lower(), proto, prev_proto, inner)
prev_proto = proto
if inner == "":
return "None"
return "Some%s" % inner


def DOMClass(descriptor):
protoList = ['PrototypeList::ID::' + proto for proto in descriptor.prototypeChain]
# Pad out the list to the right length with ID::Count so we
Expand All @@ -1734,7 +1753,8 @@ def DOMClass(descriptor):
DOMClass {
interface_chain: [ %s ],
native_hooks: &sNativePropertyHooks,
}""" % prototypeChainString
type_id: %s,
}""" % (prototypeChainString, EventTargetEnum(descriptor))


class CGDOMJSClass(CGThing):
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/bindings/codegen/Configuration.py
Expand Up @@ -223,8 +223,9 @@ def addIndexedOrNamedOperation(operation, m):
if m.isDeleter():
addIndexedOrNamedOperation('Deleter', m)

iface.setUserData('hasConcreteDescendant', True)
iface = iface.parent
if iface:
iface.setUserData('hasConcreteDescendant', True)

if self.proxy:
iface = self.interface
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/bindings/conversions.rs
Expand Up @@ -645,20 +645,20 @@ pub unsafe fn native_from_reflector<T>(obj: *mut JSObject) -> *const T {
}

/// Get the `DOMClass` from `obj`, or `Err(())` if `obj` is not a DOM object.
unsafe fn get_dom_class(obj: *mut JSObject) -> Result<DOMClass, ()> {
pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()> {
use dom::bindings::utils::DOMJSClass;
use js::glue::GetProxyHandlerExtra;

let clasp = JS_GetClass(obj);
if is_dom_class(&*clasp) {
debug!("plain old dom object");
let domjsclass: *const DOMJSClass = clasp as *const DOMJSClass;
return Ok((*domjsclass).dom_class);
return Ok(&(&*domjsclass).dom_class);
}
if is_dom_proxy(obj) {
debug!("proxy dom object");
let dom_class: *const DOMClass = GetProxyHandlerExtra(obj) as *const DOMClass;
return Ok(*dom_class);
return Ok(&*dom_class);
}
debug!("not a dom object");
Err(())
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/bindings/utils.rs
Expand Up @@ -15,6 +15,7 @@ use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::trace::trace_object;
use dom::browsercontext;
use dom::eventtarget::EventTargetTypeId;
use dom::window;
use util::mem::HeapSizeOf;
use util::str::DOMString;
Expand Down Expand Up @@ -157,6 +158,9 @@ pub struct DOMClass {
/// derivedness.
pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],

/// The EventTarget type, if this is derived from an EventTarget.
pub type_id: Option<EventTargetTypeId>,

/// The NativePropertyHooks for the interface associated with this class.
pub native_hooks: &'static NativePropertyHooks,
}
Expand Down
4 changes: 3 additions & 1 deletion components/script/dom/characterdata.rs
Expand Up @@ -158,8 +158,10 @@ impl CharacterDataMethods for CharacterData {
}

/// The different types of CharacterData.
#[derive(JSTraceable, Copy, Clone, PartialEq, Debug, HeapSizeOf)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CharacterDataTypeId {
CharacterData,

Comment,
Text,
ProcessingInstruction,
Expand Down
5 changes: 2 additions & 3 deletions components/script/dom/dedicatedworkerglobalscope.rs
Expand Up @@ -163,8 +163,7 @@ impl DedicatedWorkerGlobalScope {
-> DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(
WorkerGlobalScopeTypeId::DedicatedGlobalScope, init, worker_url,
runtime, from_devtools_receiver),
init, worker_url, runtime, from_devtools_receiver),
id: id,
receiver: receiver,
own_sender: own_sender,
Expand Down Expand Up @@ -364,7 +363,7 @@ impl DedicatedWorkerGlobalScopeMethods for DedicatedWorkerGlobalScope {
impl DedicatedWorkerGlobalScopeDerived for EventTarget {
fn is_dedicatedworkerglobalscope(&self) -> bool {
match *self.type_id() {
EventTargetTypeId::WorkerGlobalScope(WorkerGlobalScopeTypeId::DedicatedGlobalScope) => true,
EventTargetTypeId::WorkerGlobalScope(WorkerGlobalScopeTypeId::DedicatedWorkerGlobalScope) => true,
_ => false
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/element.rs
Expand Up @@ -116,7 +116,7 @@ impl PartialEq for Element {
}
}

#[derive(JSTraceable, Copy, Clone, PartialEq, Debug, HeapSizeOf)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ElementTypeId {
HTMLElement(HTMLElementTypeId),
Element,
Expand Down
17 changes: 10 additions & 7 deletions components/script/dom/eventtarget.rs
Expand Up @@ -7,6 +7,7 @@ use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
use dom::bindings::codegen::Bindings::EventTargetBinding::EventTargetMethods;
use dom::bindings::conversions::get_dom_class;
use dom::bindings::error::Error::InvalidState;
use dom::bindings::error::{Fallible, report_pending_exception};
use dom::bindings::utils::{Reflectable, Reflector};
Expand Down Expand Up @@ -45,9 +46,10 @@ pub enum ListenerPhase {
Bubbling,
}

#[derive(JSTraceable, Copy, Clone)]
#[derive(HeapSizeOf)]
#[derive(Copy, Clone)]
pub enum EventTargetTypeId {
EventTarget,

Node(NodeTypeId),
WebSocket,
Window,
Expand Down Expand Up @@ -132,15 +134,13 @@ pub struct EventListenerEntry {
#[dom_struct]
pub struct EventTarget {
reflector_: Reflector,
type_id: EventTargetTypeId,
handlers: DOMRefCell<HashMap<DOMString, Vec<EventListenerEntry>, DefaultState<FnvHasher>>>,
}

impl EventTarget {
pub fn new_inherited(type_id: EventTargetTypeId) -> EventTarget {
pub fn new_inherited() -> EventTarget {
EventTarget {
reflector_: Reflector::new(),
type_id: type_id,
handlers: DOMRefCell::new(Default::default()),
}
}
Expand All @@ -159,9 +159,12 @@ impl EventTarget {
})
}

#[inline]
#[allow(unsafe_code)]
pub fn type_id(&self) -> &EventTargetTypeId {
&self.type_id
let domclass = unsafe {
get_dom_class(self.reflector_.get_jsobject().get()).unwrap()
};
domclass.type_id.as_ref().unwrap()
}

pub fn dispatch_event_with_target(&self,
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/filereader.rs
Expand Up @@ -15,7 +15,7 @@ use dom::bindings::utils::{reflect_dom_object, Reflectable};
use dom::blob::Blob;
use dom::domexception::{DOMException, DOMErrorName};
use dom::event::{EventCancelable, EventBubbles};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::eventtarget::EventTarget;
use dom::progressevent::ProgressEvent;
use encoding::all::UTF_8;
use encoding::label::encoding_from_whatwg_label;
Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct FileReader {
impl FileReader {
pub fn new_inherited(global: GlobalRef) -> FileReader {
FileReader {
eventtarget: EventTarget::new_inherited(EventTargetTypeId::FileReader),//?
eventtarget: EventTarget::new_inherited(),//?
global: GlobalField::from_rooted(&global),
ready_state: Cell::new(FileReaderReadyState::Empty),
error: MutNullableHeap::new(None),
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmlelement.rs
Expand Up @@ -344,7 +344,7 @@ impl VirtualMethods for HTMLElement {
}
}

#[derive(JSTraceable, Copy, Clone, Debug, HeapSizeOf)]
#[derive(Copy, Clone, Debug)]
pub enum HTMLElementTypeId {
HTMLElement,

Expand Down
4 changes: 3 additions & 1 deletion components/script/dom/htmlmediaelement.rs
Expand Up @@ -41,8 +41,10 @@ impl HTMLMediaElement {
}
}

#[derive(JSTraceable, Copy, Clone, Debug, HeapSizeOf)]
#[derive(Copy, Clone, Debug)]
pub enum HTMLMediaElementTypeId {
HTMLMediaElement = -1,

HTMLAudioElement = 0,
HTMLVideoElement = 1,
}
Expand Down
4 changes: 3 additions & 1 deletion components/script/dom/htmltablecellelement.rs
Expand Up @@ -22,8 +22,10 @@ use std::cmp::max;

const DEFAULT_COLSPAN: u32 = 1;

#[derive(JSTraceable, Copy, Clone, Debug, HeapSizeOf)]
#[derive(Copy, Clone, Debug)]
pub enum HTMLTableCellElementTypeId {
HTMLTableCellElement = -1,

HTMLTableDataCellElement = 0,
HTMLTableHeaderCellElement = 1,
}
Expand Down

0 comments on commit 941f7dc

Please sign in to comment.