-
Notifications
You must be signed in to change notification settings - Fork 81
fix(engine): run derived class field initializers after super() #992
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
Draft
simonyang08
wants to merge
3
commits into
trynova:main
Choose a base branch
from
simonyang08:codex/nova-948-class-field-init-derived
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f867078
fix(engine): run derived class field initializers after super() (#948)
ppt5928-dot e23eafd
refactor(engine): reuse ExecutableHeapData.class_initializer_bytecode…
ppt5928-dot 311718a
fix(engine): reserve class_initializer_bytecodes slot 0 before compil…
ppt5928-dot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Regression test for https://github.com/trynova/nova/issues/948 | ||
| // "class field initializers are broken in subclasses" | ||
| // | ||
| // A user-written derived class constructor used to throw | ||
| // `ReferenceError: Uninitialized this binding` because the instance field | ||
| // initializer prelude was emitted at the start of the constructor body, | ||
| // before `super()` had bound `this`. The fix defers the field initializer | ||
| // to a separate executable that runs after `super()` returns. | ||
|
|
||
| class A {} | ||
| class B extends A { | ||
| b = 2 | ||
| constructor() { super() } | ||
| } | ||
|
|
||
| const b = new B() | ||
| if (b.b !== 2) { | ||
| throw new Error('expected b.b === 2, got ' + b.b) | ||
| } | ||
|
|
||
| // Field visible to the constructor body after super() returns. | ||
| class C extends A { | ||
| c = 3 | ||
| constructor() { | ||
| super() | ||
| if (this.c !== 3) { | ||
| throw new Error('expected this.c === 3 inside constructor') | ||
| } | ||
| } | ||
| } | ||
| new C() | ||
|
|
||
| // Multiple instance fields. | ||
| class E extends A { | ||
| e1 = 1 | ||
| e2 = 2 | ||
| constructor() { super() } | ||
| } | ||
| const e = new E() | ||
| if (e.e1 !== 1 || e.e2 !== 2) { | ||
| throw new Error('expected e.e1 === 1 and e.e2 === 2') | ||
| } | ||
|
|
||
| // Grand-child still inherits fields from both levels. | ||
| class I extends B { | ||
| i = 'i-field' | ||
| constructor() { super() } | ||
| } | ||
| const i = new I() | ||
| if (i.b !== 2 || i.i !== 'i-field') { | ||
| throw new Error('expected i.b === 2 and i.i === "i-field"') | ||
| } | ||
|
|
||
| // Base class with fields must remain unchanged (no regression). | ||
| class G { | ||
| g = 42 | ||
| constructor() {} | ||
| } | ||
| if (new G().g !== 42) { | ||
| throw new Error('expected new G().g === 42') | ||
| } | ||
| // Nested classes inside a derived constructor must not displace the | ||
| // deferred field-initializer entry: the compiler reserves slot 0 of the | ||
| // body executable's class_initializer_bytecodes for it before compiling | ||
| // the body, and EvaluateSuper reads slot 0. | ||
|
|
||
| // Variant 1: nested base class with fields and an implicit constructor | ||
| // (its default-constructor initializer entry is appended after ours). | ||
| class P extends A { | ||
| p = 'p-field' | ||
| constructor() { | ||
| class InnerWithDefault { | ||
| inner = 7 | ||
| } | ||
| if (new InnerWithDefault().inner !== 7) { | ||
| throw new Error('expected inner === 7') | ||
| } | ||
| super() | ||
| } | ||
| } | ||
| if (new P().p !== 'p-field') { | ||
| throw new Error('expected p === "p-field"') | ||
| } | ||
|
|
||
| // Variant 2: nested derived class with its own user constructor and | ||
| // fields (its deferred initializer lives at slot 0 of its own body | ||
| // executable, one level down). | ||
| class Q extends A { | ||
| q = 'q-field' | ||
| constructor() { | ||
| const Nested = class extends A { | ||
| nested = 'nested-field' | ||
| constructor() { super() } | ||
| } | ||
| const inner = new Nested() | ||
| if (inner.nested !== 'nested-field') { | ||
| throw new Error('expected nested === "nested-field"') | ||
| } | ||
| super() | ||
| if (this.q !== 'q-field') { | ||
| throw new Error('expected this.q === "q-field" inside constructor') | ||
| } | ||
| } | ||
| } | ||
| const q = new Q() | ||
| if (q.q !== 'q-field') { | ||
| throw new Error('expected q.q === "q-field"') | ||
| } |
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.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I'd honestly prefer to build the initializers as part of the constructor bytecode directly. That would probably need one or two new VM bytecode instructions but that's kinda cheap still.
The harder part is the refactoring of moving the initialization bytecode compilation out of ClassDefinitionEvaluation and into the
super()instruction.