Skip to content

Commit

Permalink
servo: Merge #1864 - Split TCast::to into TCast::to_unchecked and TCa…
Browse files Browse the repository at this point in the history
…st::to (from saneyuki:split_cast_to); r=jdm

fix #1836

Source-Repo: https://github.com/servo/servo
Source-Revision: 831712206865e2c0516009b16e2ac60f754dd1a8
  • Loading branch information
tetsuharuohzeki committed Mar 20, 2014
1 parent 83328e8 commit cb69916
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 64 deletions.
2 changes: 1 addition & 1 deletion servo/src/components/main/layout/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ impl<'ln> NodeUtils for ThreadSafeLayoutNode<'ln> {
match self.type_id() {
TextNodeTypeId => {
unsafe {
let text: JS<Text> = TextCast::to(self.get_jsmanaged());
let text: JS<Text> = TextCast::to(self.get_jsmanaged()).unwrap();
if !is_whitespace(text.get().characterdata.data) {
return false
}
Expand Down
11 changes: 9 additions & 2 deletions servo/src/components/script/dom/bindings/codegen/CodegenRust.py
Original file line number Diff line number Diff line change
Expand Up @@ -5793,9 +5793,16 @@ def InheritTypes(config):
unsafe { derived.clone().transmute() }
}
fn to<T: ${toBound}>(base: &JS<T>) -> JS<Self> {
fn to<T: ${toBound}>(base: &JS<T>) -> Option<JS<Self>> {
match base.get().${checkFn}() {
true => unsafe { Some(base.clone().transmute()) },
false => None
}
}
unsafe fn to_unchecked<T: ${toBound}>(base: &JS<T>) -> JS<Self> {
assert!(base.get().${checkFn}());
unsafe { base.clone().transmute() }
base.clone().transmute()
}
}
''').substitute({'checkFn': 'is_' + name.lower(),
Expand Down
12 changes: 6 additions & 6 deletions servo/src/components/script/dom/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Document {
// http://dom.spec.whatwg.org/#dom-document-doctype
pub fn GetDoctype(&self) -> Option<JS<DocumentType>> {
self.node.children().find(|child| child.is_doctype())
.map(|node| DocumentTypeCast::to(&node))
.map(|node| DocumentTypeCast::to(&node).unwrap())
}

// http://dom.spec.whatwg.org/#dom-document-documentelement
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Document {
.map(|title_elem| {
for child in title_elem.children() {
if child.is_text() {
let text: JS<Text> = TextCast::to(&child);
let text: JS<Text> = TextCast::to(&child).unwrap();
title.push_str(text.get().characterdata.data.as_slice());
}
}
Expand Down Expand Up @@ -362,7 +362,7 @@ impl Document {
fn get_html_element(&self) -> Option<JS<HTMLHtmlElement>> {
self.GetDocumentElement().filtered(|root| {
root.get().node.type_id == ElementNodeTypeId(HTMLHtmlElementTypeId)
}).map(|elem| HTMLHtmlElementCast::to(&elem))
}).map(|elem| HTMLHtmlElementCast::to(&elem).unwrap())
}

// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-head
Expand All @@ -371,7 +371,7 @@ impl Document {
let node: JS<Node> = NodeCast::from(&root);
node.children().find(|child| {
child.type_id() == ElementNodeTypeId(HTMLHeadElementTypeId)
}).map(|node| HTMLHeadElementCast::to(&node))
}).map(|node| HTMLHeadElementCast::to(&node).unwrap())
})
}

Expand All @@ -385,7 +385,7 @@ impl Document {
ElementNodeTypeId(HTMLFrameSetElementTypeId) => true,
_ => false
}
}).map(|node| HTMLElementCast::to(&node))
}).map(|node| HTMLElementCast::to(&node).unwrap())
})
}

Expand Down Expand Up @@ -434,7 +434,7 @@ impl Document {
return false;
}

let element: JS<Element> = ElementCast::to(node);
let element: JS<Element> = ElementCast::to(node).unwrap();
element.get().get_attribute(Null, "name").map_or(false, |attr| {
attr.get().value_ref() == name
})
Expand Down
10 changes: 5 additions & 5 deletions servo/src/components/script/dom/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ impl Element {
// This hardcoding is awful.
match abstract_self.get().node.type_id {
ElementNodeTypeId(HTMLImageElementTypeId) => {
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self);
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
ElementNodeTypeId(HTMLIFrameElementTypeId) => {
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self);
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
ElementNodeTypeId(HTMLObjectElementTypeId) => {
let mut elem: JS<HTMLObjectElement> = HTMLObjectElementCast::to(abstract_self);
let mut elem: JS<HTMLObjectElement> = HTMLObjectElementCast::to(abstract_self).unwrap();
elem.get_mut().AfterSetAttr(local_name.clone(), value.clone());
}
_ => ()
Expand Down Expand Up @@ -341,11 +341,11 @@ impl Element {
// This hardcoding is awful.
match abstract_self.get().node.type_id {
ElementNodeTypeId(HTMLImageElementTypeId) => {
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self);
let mut elem: JS<HTMLImageElement> = HTMLImageElementCast::to(abstract_self).unwrap();
elem.get_mut().BeforeRemoveAttr(local_name.clone());
}
ElementNodeTypeId(HTMLIFrameElementTypeId) => {
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self);
let mut elem: JS<HTMLIFrameElement> = HTMLIFrameElementCast::to(abstract_self).unwrap();
elem.get_mut().BeforeRemoveAttr(local_name.clone());
}
_ => ()
Expand Down
2 changes: 1 addition & 1 deletion servo/src/components/script/dom/eventdispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn dispatch_event(target: &JS<EventTarget>,

//TODO: no chain if not participating in a tree
if target.get().is_node() {
let target_node: JS<Node> = NodeCast::to(target);
let target_node: JS<Node> = NodeCast::to(target).unwrap();
for ancestor in target_node.ancestors() {
let ancestor_target: JS<EventTarget> = EventTargetCast::from(&ancestor);
chain.push(ancestor_target);
Expand Down
2 changes: 1 addition & 1 deletion servo/src/components/script/dom/htmlcollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl HTMLCollection {
let mut elements = ~[];
for child in root.traverse_preorder() {
if child.is_element() {
let elem: JS<Element> = ElementCast::to(&child);
let elem: JS<Element> = ElementCast::to(&child).unwrap();
if predicate(&elem) {
elements.push(elem);
}
Expand Down
14 changes: 7 additions & 7 deletions servo/src/components/script/dom/htmlserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ pub fn serialize(iterator: &mut NodeIterator) -> ~str {
html.push_str(
match node.type_id() {
ElementNodeTypeId(..) => {
let elem: JS<Element> = ElementCast::to(&node);
let elem: JS<Element> = ElementCast::to(&node).unwrap();
serialize_elem(&elem, &mut open_elements)
}
CommentNodeTypeId => {
let comment: JS<Comment> = CommentCast::to(&node);
let comment: JS<Comment> = CommentCast::to(&node).unwrap();
serialize_comment(&comment)
}
TextNodeTypeId => {
let text: JS<Text> = TextCast::to(&node);
let text: JS<Text> = TextCast::to(&node).unwrap();
serialize_text(&text)
}
DoctypeNodeTypeId => {
let doctype: JS<DocumentType> = DocumentTypeCast::to(&node);
let doctype: JS<DocumentType> = DocumentTypeCast::to(&node).unwrap();
serialize_doctype(&doctype)
}
ProcessingInstructionNodeTypeId => {
let processing_instruction: JS<ProcessingInstruction> = ProcessingInstructionCast::to(&node);
let processing_instruction: JS<ProcessingInstruction> = ProcessingInstructionCast::to(&node).unwrap();
serialize_processing_instruction(&processing_instruction)
}
DocumentFragmentNodeTypeId => {
Expand All @@ -71,7 +71,7 @@ fn serialize_comment(comment: &JS<Comment>) -> ~str {
fn serialize_text(text: &JS<Text>) -> ~str {
match text.get().characterdata.node.parent_node {
Some(ref parent) if parent.is_element() => {
let elem: JS<Element> = ElementCast::to(parent);
let elem: JS<Element> = ElementCast::to(parent).unwrap();
match elem.get().tag_name.as_slice() {
"style" | "script" | "xmp" | "iframe" |
"noembed" | "noframes" | "plaintext" |
Expand Down Expand Up @@ -103,7 +103,7 @@ fn serialize_elem(elem: &JS<Element>, open_elements: &mut ~[~str]) -> ~str {
"pre" | "listing" | "textarea" if elem.get().namespace == namespace::HTML => {
match elem.get().node.first_child {
Some(ref child) if child.is_text() => {
let text: JS<CharacterData> = CharacterDataCast::to(child);
let text: JS<CharacterData> = CharacterDataCast::to(child).unwrap();
if text.get().data.len() > 0 && text.get().data[0] == 0x0A as u8 {
rv.push_str("\x0A");
}
Expand Down
Loading

0 comments on commit cb69916

Please sign in to comment.