From 4fdcbae583e6d4203e430911fc8f1e2dece53a2c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 00:31:30 +0100 Subject: [PATCH] events: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36304 Reviewed-By: Rich Trott --- lib/events.js | 15 +++++++++------ lib/internal/event_target.js | 8 +++++--- lib/trace_events.js | 7 ++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/events.js b/lib/events.js index c80e6ae2eed3ac..9e60a1946ad7b1 100644 --- a/lib/events.js +++ b/lib/events.js @@ -22,10 +22,13 @@ 'use strict'; const { + ArrayPrototypeForEach, ArrayPrototypePush, + ArrayPrototypeSlice, Boolean, Error, ErrorCaptureStackTrace, + FunctionPrototypeCall, MathMin, NumberIsNaN, ObjectCreate, @@ -81,7 +84,7 @@ const lazyDOMException = hideStackFrames((message, name) => { function EventEmitter(opts) { - EventEmitter.init.call(this, opts); + FunctionPrototypeCall(EventEmitter.init, this, opts); } module.exports = EventEmitter; module.exports.once = once; @@ -173,7 +176,7 @@ EventEmitter.setMaxListeners = isEventTarget = require('internal/event_target').isEventTarget; // Performance for forEach is now comparable with regular for-loop - eventTargets.forEach((target) => { + ArrayPrototypeForEach(eventTargets, (target) => { if (isEventTarget(target)) { target[kMaxEventTargetListeners] = n; target[kMaxEventTargetListenersWarned] = false; @@ -224,7 +227,7 @@ function addCatch(that, promise, type, args) { const then = promise.then; if (typeof then === 'function') { - then.call(promise, undefined, function(err) { + FunctionPrototypeCall(then, promise, undefined, function(err) { // The callback is called with nextTick to avoid a follow-up // rejection from this promise. process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args); @@ -670,7 +673,7 @@ function arrayClone(arr) { case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]]; case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]]; } - return arr.slice(); + return ArrayPrototypeSlice(arr); } function unwrapListeners(arr) { @@ -839,7 +842,7 @@ function on(emitter, event, options) { // Wait until an event happens return new Promise(function(resolve, reject) { - unconsumedPromises.push({ resolve, reject }); + ArrayPrototypePush(unconsumedPromises, { resolve, reject }); }); }, @@ -903,7 +906,7 @@ function on(emitter, event, options) { if (promise) { promise.resolve(createIterResult(args, false)); } else { - unconsumedEvents.push(args); + ArrayPrototypePush(unconsumedEvents, args); } } diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 1f6ae1c17c8317..b51bf836c810f4 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -4,6 +4,8 @@ const { ArrayFrom, Boolean, Error, + FunctionPrototypeBind, + FunctionPrototypeCall, NumberIsInteger, ObjectAssign, ObjectDefineProperties, @@ -208,7 +210,7 @@ class Listener { this.callback = typeof listener === 'function' ? listener : - listener.handleEvent.bind(listener); + FunctionPrototypeBind(listener.handleEvent, listener); } same(listener, capture) { @@ -396,7 +398,7 @@ class EventTarget { } else { arg = createEvent(); } - const result = handler.callback.call(this, arg); + const result = FunctionPrototypeCall(handler.callback, this, arg); if (result !== undefined && result !== null) addCatch(this, result, createEvent()); } catch (err) { @@ -566,7 +568,7 @@ function isEventTarget(obj) { function addCatch(that, promise, event) { const then = promise.then; if (typeof then === 'function') { - then.call(promise, undefined, function(err) { + FunctionPrototypeCall(then, promise, undefined, function(err) { // The callback is called with nextTick to avoid a follow-up // rejection from this promise. process.nextTick(emitUnhandledRejectionOrErr, that, err, event); diff --git a/lib/trace_events.js b/lib/trace_events.js index 35f776ad53c310..85101e7930d977 100644 --- a/lib/trace_events.js +++ b/lib/trace_events.js @@ -2,7 +2,8 @@ const { ArrayIsArray, - Set, + ArrayPrototypeJoin, + SafeSet, Symbol, } = primordials; @@ -27,7 +28,7 @@ const { CategorySet, getEnabledCategories } = internalBinding('trace_events'); const { customInspectSymbol } = require('internal/util'); const { format } = require('internal/util/inspect'); -const enabledTracingObjects = new Set(); +const enabledTracingObjects = new SafeSet(); class Tracing { constructor(categories) { @@ -63,7 +64,7 @@ class Tracing { } get categories() { - return this[kCategories].join(','); + return ArrayPrototypeJoin(this[kCategories], ','); } [customInspectSymbol](depth, opts) {