Skip to content

Call EventSource and MessagePort event handlers with the target as this - #8816

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-wpt-listener-this
Nov 25, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-21-wpt-listener-this

Conversation

@littledivy

Copy link
Copy Markdown
Member

The TypeScript EventSource and MessagePort set onopen, onmessage and onerror through EventTarget's C++ event handler attribute, whose trampoline calls the handler with the global object as this. addEventListener() listeners already get the target under set_event_target_this, and HTML calls an event handler with the event's currentTarget as this. Nine eventsource WPT files call this.close() from an onX handler, so they were expected to fail.

In this patch the event-target-internals seam passes C++ a trampoline that calls the handler with the target as this when set_event_target_this is on. The getter maps the trampoline back to the handler, so it still returns the value assigned. Without the flag nothing changes. Code that relied on this being the global object inside a function handler on these two classes now sees the target. We accept that without a new flag, because set_event_target_this already covers the this of event handlers. The new handlerReceiver case and the updated onmessageHandlerReceiver case fail without the change.

The nine WPT files now pass. request-cache-control.any.js was disabled for the same reason. Only its cross-origin pair stays disabled, because fetch() sends no Origin header and cors.py fails with a 500.

//src/wpt:eventsource@ and //src/wpt:eventsource@all-autogates each passed 100 of 100 runs with --runs_per_test=100. The eventsource, messagechannel, events, abort and websocket tests and the dom/events, dom/abort, websockets and webmessaging WPTs pass.

@littledivy
littledivy requested review from a team as code owners November 23, 2026 02:33

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 9cebbd78.

This review, of 0413d51d

✅ Approved — The change is small and correct, it only applies under set_event_target_this, and tests cover it. No findings.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only affects the this of event handlers. It touches no trust boundary, input handling, secrets or authorization.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 0413d51d

The TypeScript EventSource and MessagePort set their onopen, onmessage and
onerror handlers through EventTarget's C++ event handler attribute, whose
trampoline calls the handler with the global object as `this`.
addEventListener() listeners get the target under set_event_target_this,
and HTML calls an event handler with the event's currentTarget as `this`.
Nine eventsource WPT files call this.close() from an onX handler and were
expected to fail.

In this patch the event-target-internals seam passes C++ a trampoline that
calls the handler with the target as `this` when set_event_target_this is
on, and maps the trampoline back to the handler for the getter. Without the
flag nothing changes. Code that relied on `this` being the global object
inside a function-style handler on these two classes now sees the target;
we accept that without a new flag, since set_event_target_this already
covers the `this` of event handlers.

The nine WPT files pass now. request-cache-control.any.js was disabled for
the same reason; only its cross-origin pair stays disabled, because fetch()
sends no Origin header and cors.py fails with a 500.
@littledivy
littledivy force-pushed the littledivy/2026-11-21-wpt-listener-this branch from 0413d51 to 9cebbd7 Compare November 23, 2026 02:40

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 80f4bcb7.

This review, of 9cebbd78

✅ Approved — The change is small and correct, it only applies under set_event_target_this, and tests cover both flag states. No findings.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only affects the this of event handlers. It touches no trust boundary, input handling, secrets or authorization.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The trampoline and getter mapping are correct. Behaviour without the flag is unchanged, and the tests branch on setEventTargetThis, so they pass with the flag on or off.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 9cebbd78

@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.75%. Comparing base (0a09936) to head (80f4bcb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8816   +/-   ##
=======================================
  Coverage   67.75%   67.75%           
=======================================
  Files         846      846           
  Lines      214951   214951           
  Branches    32584    32584           
=======================================
  Hits       145633   145633           
  Misses      50077    50077           
  Partials    19241    19241           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — The change is small and correct, it only applies under set_event_target_this, and tests cover both flag states. No findings.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only affects the this of event handlers. It touches no trust boundary, input handling, secrets or authorization.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The trampoline and getter mapping are correct. Behaviour without the flag is unchanged, and the tests branch on setEventTargetThis, so they pass with the flag on or off.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 80f4bcb7 — same changes as 9cebbd78, so this is that commit's review (no new review was run; line numbers are as of 9cebbd78)

@littledivy
littledivy merged commit 3e96a76 into main Nov 25, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-wpt-listener-this branch November 25, 2026 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants