From ff59fcdf7bd381f225dcbbc94c5fd1caa86f3818 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 2 Nov 2020 22:59:54 +0200 Subject: [PATCH] events: define abort on prototype PR-URL: https://github.com/nodejs/node/pull/35931 Reviewed-By: Anna Henningsen Reviewed-By: Antoine du Hamel Reviewed-By: Daijiro Wachi Reviewed-By: Rich Trott --- lib/internal/abort_controller.js | 3 ++- lib/internal/event_target.js | 19 ++++++++----------- lib/internal/worker/io.js | 2 -- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 592e46e5320ce9..b387e5f4e2cea0 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -48,6 +48,8 @@ ObjectDefineProperties(AbortSignal.prototype, { aborted: { enumerable: true } }); +defineEventHandler(AbortSignal.prototype, 'abort'); + function abortSignal(signal) { if (signal[kAborted]) return; signal[kAborted] = true; @@ -65,7 +67,6 @@ class AbortController { constructor() { this[kSignal] = new AbortSignal(); emitExperimentalWarning('AbortController'); - defineEventHandler(this[kSignal], 'abort'); } get signal() { return this[kSignal]; } diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 4841306c3348bd..e476603b9cec14 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -4,17 +4,16 @@ const { ArrayFrom, Boolean, Error, - Map, NumberIsInteger, ObjectAssign, ObjectDefineProperties, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, + SafeMap, String, Symbol, SymbolFor, SymbolToStringTag, - SafeWeakMap, SafeWeakSet, } = primordials; @@ -36,6 +35,7 @@ const kIsEventTarget = SymbolFor('nodejs.event_target'); const kEvents = Symbol('kEvents'); const kStop = Symbol('kStop'); const kTarget = Symbol('kTarget'); +const kHandlers = Symbol('khandlers'); const kHybridDispatch = SymbolFor('nodejs.internal.kHybridDispatch'); const kCreateEvent = Symbol('kCreateEvent'); @@ -219,7 +219,7 @@ class Listener { } function initEventTarget(self) { - self[kEvents] = new Map(); + self[kEvents] = new SafeMap(); } class EventTarget { @@ -578,27 +578,24 @@ function emitUnhandledRejectionOrErr(that, err, event) { process.emit('error', err, event); } -// A map of emitter -> map of name -> handler -const eventHandlerValueMap = new SafeWeakMap(); - function defineEventHandler(emitter, name) { // 8.1.5.1 Event handlers - basically `on[eventName]` attributes ObjectDefineProperty(emitter, `on${name}`, { get() { - return eventHandlerValueMap.get(this)?.get(name); + return this[kHandlers]?.get(name); }, set(value) { - const oldValue = eventHandlerValueMap.get(this)?.get(name); + const oldValue = this[kHandlers]?.get(name); if (oldValue) { this.removeEventListener(name, oldValue); } if (typeof value === 'function') { this.addEventListener(name, value); } - if (!eventHandlerValueMap.has(this)) { - eventHandlerValueMap.set(this, new Map()); + if (!this[kHandlers]) { + this[kHandlers] = new SafeMap(); } - eventHandlerValueMap.get(this).set(name, value); + this[kHandlers].set(name, value); }, configurable: true, enumerable: true diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 6f4cc325714ecf..73958dbc2d78f5 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -146,8 +146,6 @@ ObjectDefineProperty( // This is called from inside the `MessagePort` constructor. function oninit() { initNodeEventTarget(this); - defineEventHandler(this, 'message'); - defineEventHandler(this, 'messageerror'); setupPortReferencing(this, this, 'message'); }