New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assert that DOM structs have the correct first field #21105
Merged
+109
−9
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Loading status checks…
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();
}
}
````- Loading branch information
commit ad198993b127ef87f129add8336f8bb7dfd30e4d
Some generated files are not rendered by default. Learn more.
Oops, something went wrong.
| @@ -2116,6 +2116,10 @@ def __init__(self, descriptor): | ||
| 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 @@ def define(self): | ||
| 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) { | ||
Manishearth
Author
Member
|
||
| 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 @@ def reexportedName(name): | ||
| pass | ||
| else: | ||
| cgThings.append(CGDOMJSClass(descriptor)) | ||
| if not descriptor.interface.isIteratorInterface(): | ||
| cgThings.append(CGAssertInheritance(descriptor)) | ||
| pass | ||
|
|
||
| if descriptor.isGlobal(): | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
I would rather encode the relation with a trait specifying from which interface it is extended.