-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
events: Handle a range of this values for dispatchEvent #33661
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ const { | |
Set, | ||
Symbol, | ||
NumberIsNaN, | ||
SymbolFor, | ||
SymbolToStringTag, | ||
} = primordials; | ||
|
||
|
@@ -16,13 +17,16 @@ const { | |
ERR_INVALID_ARG_TYPE, | ||
ERR_EVENT_RECURSION, | ||
ERR_OUT_OF_RANGE, | ||
ERR_MISSING_ARGS | ||
ERR_MISSING_ARGS, | ||
ERR_INVALID_THIS, | ||
} | ||
} = require('internal/errors'); | ||
|
||
const { customInspectSymbol } = require('internal/util'); | ||
const { inspect } = require('util'); | ||
|
||
const kIsEventTarget = SymbolFor('nodejs.event_target'); | ||
|
||
const kEvents = Symbol('kEvents'); | ||
const kStop = Symbol('kStop'); | ||
const kTarget = Symbol('kTarget'); | ||
|
@@ -185,6 +189,10 @@ class Listener { | |
} | ||
|
||
class EventTarget { | ||
// Used in checking whether an object is an EventTarget. This is a well-known | ||
// symbol as EventTarget may be used cross-realm. See discussion in #33661. | ||
static [kIsEventTarget] = true; | ||
|
||
[kEvents] = new Map(); | ||
#emitting = new Set(); | ||
|
||
|
@@ -257,6 +265,10 @@ class EventTarget { | |
throw new ERR_INVALID_ARG_TYPE('event', 'Event', event); | ||
} | ||
|
||
if (!isEventTarget(this)) { | ||
throw new ERR_INVALID_THIS('EventTarget'); | ||
} | ||
|
||
if (this.#emitting.has(event.type) || | ||
event[kTarget] !== null) { | ||
throw new ERR_EVENT_RECURSION(event.type); | ||
|
@@ -447,6 +459,15 @@ function validateEventListenerOptions(options) { | |
}; | ||
} | ||
|
||
// Test whether the argument is an event object. This is far from a fool-proof | ||
// test, for example this input will result in a false positive: | ||
// > isEventTarget({ constructor: EventTarget }) | ||
// It stands in its current implementation as a compromise. For the relevant | ||
// discussion, see #33661. | ||
function isEventTarget(obj) { | ||
return obj && obj.constructor && obj.constructor[kIsEventTarget]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear how fool-proof you want to be here, but I'll note that people can fool this brand check. For example, But anything you do will probably be a compromise. If something works cross-realm, then it can be fooled (and thus is not spec-compliant). If it doesn't work cross-realm, then it is not spec compliant either. So without C++ there will always be compromises. Maybe this version is a fine compromise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We should likely include this explanation in a comment here on |
||
} | ||
|
||
function addCatch(that, promise, event) { | ||
const then = promise.then; | ||
if (typeof then === 'function') { | ||
|
Uh oh!
There was an error while loading. Please reload this page.