aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2018-06-29 10:34:09 -0700
committerManish Goregaokar <manishsmail@gmail.com>2018-07-03 09:39:29 -0700
commitad198993b127ef87f129add8336f8bb7dfd30e4d (patch)
treedb1756c2241abbd068d81e4b07a4d481a55b5c73 /components/script/dom/bindings
parente2fca1b228ff20d54b67e0d1fa502b694b5c290e (diff)
downloadservo-ad198993b127ef87f129add8336f8bb7dfd30e4d.tar.gz
servo-ad198993b127ef87f129add8336f8bb7dfd30e4d.zip
Assert that DOM structs have the correct first field
DOM structs embed their parent type as their first field. This introduces a `.parent()` method to the DOM struct that returns its first field, and codegens a type assert that ensures that `.parent()` returns the parent struct. This generates: On `#[dom_struct]`: ```rust impl HasParent for Type { type Parent = ParentType; fn as_parent(&self) -> ParentType { &self.first_field } } ``` In the codegen files: ```rust impl Type { fn __assert_parent_type(&self) { let _: &ParentType = self.as_parent(); } } ````
Diffstat (limited to 'components/script/dom/bindings')
-rw-r--r--components/script/dom/bindings/codegen/CodegenRust.py52
-rw-r--r--components/script/dom/bindings/inheritance.rs5
2 files changed, 56 insertions, 1 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py
index 01b52d916ae..ae76667707e 100644
--- a/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/components/script/dom/bindings/codegen/CodegenRust.py
@@ -2116,6 +2116,10 @@ class CGDOMJSClass(CGThing):
self.descriptor = descriptor
def define(self):
+ parentName = self.descriptor.getParentName()
+ if not parentName:
+ parentName = "::dom::bindings::reflector::Reflector"
+
args = {
"domClass": DOMClass(self.descriptor),
"enumerateHook": "None",
@@ -2161,7 +2165,51 @@ static Class: DOMJSClass = DOMJSClass {
reserved: [0 as *mut _; 3],
},
dom_class: %(domClass)s
-};""" % args
+};
+""" % args
+
+
+class CGAssertInheritance(CGThing):
+ """
+ Generate a type assertion for inheritance
+ """
+ def __init__(self, descriptor):
+ CGThing.__init__(self)
+ self.descriptor = descriptor
+
+ def define(self):
+ parent = self.descriptor.interface.parent
+ parentName = ""
+ if parent:
+ parentName = parent.identifier.name
+ else:
+ parentName = "::dom::bindings::reflector::Reflector"
+
+ selfName = self.descriptor.interface.identifier.name
+
+ if selfName == "PaintRenderingContext2D":
+ # PaintRenderingContext2D embeds a CanvasRenderingContext2D
+ # instead of a Reflector as an optimization,
+ # but this is fine since CanvasRenderingContext2D
+ # also has a reflector
+ #
+ # FIXME *RenderingContext2D should use Inline
+ parentName = "::dom::canvasrenderingcontext2d::CanvasRenderingContext2D"
+ args = {
+ "parentName": parentName,
+ "selfName": selfName,
+ }
+
+ return """\
+impl %(selfName)s {
+ fn __assert_parent_type(&self) {
+ use dom::bindings::inheritance::HasParent;
+ // If this type assertion fails, make sure the first field of your
+ // DOM struct is of the correct type -- it must be the parent class.
+ let _: &%(parentName)s = self.as_parent();
+ }
+}
+""" % args
def str_to_const_array(s):
@@ -6011,6 +6059,8 @@ class CGDescriptor(CGThing):
pass
else:
cgThings.append(CGDOMJSClass(descriptor))
+ if not descriptor.interface.isIteratorInterface():
+ cgThings.append(CGAssertInheritance(descriptor))
pass
if descriptor.isGlobal():
diff --git a/components/script/dom/bindings/inheritance.rs b/components/script/dom/bindings/inheritance.rs
index d97843bf42a..c017a806b6e 100644
--- a/components/script/dom/bindings/inheritance.rs
+++ b/components/script/dom/bindings/inheritance.rs
@@ -41,3 +41,8 @@ pub trait Castable: IDLInterface + DomObject + Sized {
}
}
}
+
+pub trait HasParent {
+ type Parent;
+ fn as_parent(&self) -> &Self::Parent;
+}