From 7c95cfc1640670ce5e3ca08f75554c8d32fa4174 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 Backport-PR-URL: https://github.com/nodejs/node/pull/38386 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 8507935d198bc7..9762280426e544 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -47,6 +47,8 @@ Object.defineProperties(AbortSignal.prototype, { aborted: { enumerable: true } }); +defineEventHandler(AbortSignal.prototype, 'abort'); + function abortSignal(signal) { if (signal[kAborted]) return; signal[kAborted] = true; @@ -64,7 +66,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 30ee46909e6274..fef80c65d97ba3 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -4,16 +4,15 @@ const { ArrayFrom, Boolean, Error, - Map, NumberIsInteger, Object, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, + SafeMap, String, Symbol, SymbolFor, SymbolToStringTag, - SafeWeakMap, SafeWeakSet, } = primordials; @@ -35,6 +34,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 = Symbol.for('nodejs.internal.kHybridDispatch'); const kCreateEvent = Symbol('kCreateEvent'); @@ -218,7 +218,7 @@ class Listener { } function initEventTarget(self) { - self[kEvents] = new Map(); + self[kEvents] = new SafeMap(); } class EventTarget { @@ -577,27 +577,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 b2fed9f06ff35c..406c8e77fd3375 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -99,8 +99,6 @@ ObjectDefineProperty( // This is called from inside the `MessagePort` constructor. function oninit() { initNodeEventTarget(this); - defineEventHandler(this, 'message'); - defineEventHandler(this, 'messageerror'); setupPortReferencing(this, this, 'message'); }