-
-
Notifications
You must be signed in to change notification settings - Fork 140
fix(runtime): EventTarget methods must live on the prototype so subclasses inherit them (#6301) #6311
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
Merged
Merged
fix(runtime): EventTarget methods must live on the prototype so subclasses inherit them (#6301) #6311
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Move this fallback before the keyless early return.
For class instances with
keys_array == NULL,get_field_by_name_object_tailreturns at Line [1521] before reaching this block. Since EventTarget methods have no prototype home, the keyless path will still returnundefinedfor inheritedaddEventListener,removeEventListener, anddispatchEvent.Invoke the same binding logic in the keyless branch after existing override/prototype checks, preserving subclass overrides.
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
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.
Verified empirically, and this one doesn't hold — the premise that a class instance can reach here with
keys_array == NULLis false. Not moving the fallback, but I've documented the invariant and pinned it with fixtures so the next reader doesn't have to re-derive it.Why a
class X extends EventTargetinstance never takes the keyless branchEvery class-instance allocator installs the shape-cached keys array unconditionally:
js_object_alloc_class_inline_keys—set_object_keys_array(ptr, keys_array)(object/alloc.rs:240)js_object_alloc_class_with_keys—set_object_keys_array(ptr, keys_arr)(object/alloc.rs:402)js_object_alloc_class_dynamic_parent— merges parent+own keys, else delegates to_with_keys(object/alloc.rs:436)and the array they install is never NULL for a fieldless class —
js_build_class_keys_arrayreturns a zero-length array:(
object/alloc.rs:284)keys_array == NULLis the state of a freshly bump-allocated object (js_object_alloc,js_object_alloc_fast) before its first by-name store — i.e.Object.create(proto)results and barejs_new_function_constructinstances. Aclassinstance is never in that state.Empirical check. I instrumented the tail with a temporary print at the
keys.is_null()test and at the keyless dead-end return, and ran every shape I could think of that might produce an empty-shaped event target:keys_arraytypeof x.dispatchEventclass Bus extends EventTarget {}(no fields, no ctor)functionfunctionB extends A extends EventTargetfunctionnew Ctor()through a variable (dynamicnew)functionReflect.construct(Bus, [])functionconst K = class extends EventTarget {}functionfunctionfunctiondeletedfunctionObject.freeze(new Bus())functionObject.create(Bus.prototype)functionThe
KEYLESS-DEAD-ENDprobe never fired once — for any of these, for any of the three method names.The only receiver that does reach the keyless branch is
Object.create(Bus.prototype), and it already resolves correctly inside that branch:resolve_proto_chain_field_with_receiver(the first thing the keyless branch tries, well before itsreturn undefined()) hops to the prototype object, which is itself a shaped, class-id-stamped receiver, and that hop reaches this fallback. I also checked it doesn't leak listeners onto the shared prototype — a listener added via anObject.created receiver does not fire on a siblingnew Bus(). (Node actually throwsCannot read properties of undefined (reading 'get')on that shape, since there's no internal slot — so we're strictly more permissive there, not less.)What I changed in response
Rather than move the block (which would put it ahead of the own-property and class-vtable lookups and break the subclass-override ordering this PR exists to get right), I:
event_target::event_target_value_read, so the placement is justified where the mechanism lives.test-files/test_gap_6301_event_target_subclass.ts— empty ctor, dynamicnew,Reflect.construct, class expression, function-local subclass, deleted-own-prop, frozen instance. All byte-identical tonode --experimental-strip-types, also underPERRY_GC_FORCE_EVACUATE=1 PERRY_GC_VERIFY_EVACUATION=1.Thanks for the push on this — the invariant was load-bearing and undocumented, which is exactly what made the finding plausible.
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.
🧩 Analysis chain
🏁 Script executed:
Length of output: 5176
Confirms the claim: every class-instance allocator path (
js_object_alloc_class_inline_keys,js_object_alloc_class_with_keys,js_object_alloc_class_dynamic_parent, and the two others found at lines 523/609/681/747) callsset_object_keys_arrayunconditionally, andjs_build_class_keys_arrayreturns an empty (non-null) array rather than null whenfield_count == 0. So the keyless branch is genuinely unreachable for class instances, matching the empirical results.@proggeramlugThanks for the thorough empirical verification — I checked the allocator call sites directly and confirm your analysis holds:js_object_alloc_class_inline_keys,js_object_alloc_class_with_keys, andjs_object_alloc_class_dynamic_parentall callset_object_keys_arrayunconditionally, andjs_build_class_keys_arrayreturns an empty (non-null)ArrayHeader— notNULL— whenfield_count == 0. Sokeys_array == NULLgenuinely can't occur for a class instance (fieldless or not), only for the pre-first-store bump-allocated shapes you identified.Withdrawing the original suggestion — the current placement after the
keys.is_null()early return is correct, and the ordering constraint (own-property/vtable-override resolution before the EventTarget fallback) is preserved. Appreciate the documentation and fixture coverage added for this invariant.ヽ(・∀・)ノ
🧬 Code Graph Analysis Results
Relevant snippets
crates/perry-runtime/src/object/field_get_set/get_field_by_name_tail.rskeys_arraynull fast-path and EventTarget value read)let keys = (*obj).keys_array;if keys.is_null() { ... }block that handles keyless receivers (including prototype-chain resolution and prototype-method lookup/vtable getter walk) and returnsJSValue::undefined()if nothing resolves.EventTarget“method surface read as VALUE” hook:if !key.is_null() { if let Some(bound) = crate::event_target::event_target_value_read(obj as *mut ObjectHeader, key_bytes) { ... } }EventTargetarm is after thekeys_array.is_null()early return so subclass overrides and own-property precedence remain correct.crates/perry-runtime/src/event_target.rscrates/perry-runtime/src/object/class_registry/construct.rslookup_prototype_method(...)(used by the keyless branch for JS-classic prototype method reads)crates/perry-runtime/src/object/class_registry/parent_static.rsget_parent_class_id(...)(used during class/vtable/prototype chain walking)crates/perry-runtime/src/object/class_registry/parent_static.rslookup_class_method_in_chain(...)(method lookup across class parent chain; relevant to the keyless branch’s “fallback to class method in chain” logic)