88 Set,
99 Symbol,
1010 NumberIsNaN,
11+ SymbolFor,
1112 SymbolToStringTag,
1213} = primordials ;
1314
@@ -16,13 +17,16 @@ const {
1617 ERR_INVALID_ARG_TYPE ,
1718 ERR_EVENT_RECURSION ,
1819 ERR_OUT_OF_RANGE ,
19- ERR_MISSING_ARGS
20+ ERR_MISSING_ARGS ,
21+ ERR_INVALID_THIS ,
2022 }
2123} = require ( 'internal/errors' ) ;
2224
2325const { customInspectSymbol } = require ( 'internal/util' ) ;
2426const { inspect } = require ( 'util' ) ;
2527
28+ const kIsEventTarget = SymbolFor ( 'nodejs.event_target' ) ;
29+
2630const kEvents = Symbol ( 'kEvents' ) ;
2731const kStop = Symbol ( 'kStop' ) ;
2832const kTarget = Symbol ( 'kTarget' ) ;
@@ -185,6 +189,10 @@ class Listener {
185189}
186190
187191class EventTarget {
192+ // Used in checking whether an object is an EventTarget. This is a well-known
193+ // symbol as EventTarget may be used cross-realm. See discussion in #33661.
194+ static [ kIsEventTarget ] = true ;
195+
188196 [ kEvents ] = new Map ( ) ;
189197 #emitting = new Set ( ) ;
190198
@@ -257,6 +265,10 @@ class EventTarget {
257265 throw new ERR_INVALID_ARG_TYPE ( 'event' , 'Event' , event ) ;
258266 }
259267
268+ if ( ! isEventTarget ( this ) ) {
269+ throw new ERR_INVALID_THIS ( 'EventTarget' ) ;
270+ }
271+
260272 if ( this . #emitting. has ( event . type ) ||
261273 event [ kTarget ] !== null ) {
262274 throw new ERR_EVENT_RECURSION ( event . type ) ;
@@ -447,6 +459,15 @@ function validateEventListenerOptions(options) {
447459 } ;
448460}
449461
462+ // Test whether the argument is an event object. This is far from a fool-proof
463+ // test, for example this input will result in a false positive:
464+ // > isEventTarget({ constructor: EventTarget })
465+ // It stands in its current implementation as a compromise. For the relevant
466+ // discussion, see #33661.
467+ function isEventTarget ( obj ) {
468+ return obj && obj . constructor && obj . constructor [ kIsEventTarget ] ;
469+ }
470+
450471function addCatch ( that , promise , event ) {
451472 const then = promise . then ;
452473 if ( typeof then === 'function' ) {
0 commit comments