From 5e8dfd7e4021f6a37c9c0fee364eacf730189731 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 00:31:30 +0100 Subject: [PATCH 01/13] events: refactor to use more primordials --- lib/events.js | 60 +++++++++++++++++++++--------------- lib/internal/event_target.js | 10 +++--- lib/trace_events.js | 7 +++-- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/lib/events.js b/lib/events.js index dc08042578bc7a..8d749dba0df796 100644 --- a/lib/events.js +++ b/lib/events.js @@ -22,10 +22,19 @@ 'use strict'; const { + ArrayPrototypeForEach, + ArrayPrototypeIndexOf, + ArrayPrototypeJoin, ArrayPrototypePush, + ArrayPrototypeShift, + ArrayPrototypeSlice, + ArrayPrototypeSplice, + ArrayPrototypeUnshift, Boolean, Error, ErrorCaptureStackTrace, + FunctionPrototypeBind, + FunctionPrototypeCall, MathMin, NumberIsNaN, ObjectCreate, @@ -39,6 +48,7 @@ const { ReflectApply, ReflectOwnKeys, String, + StringPrototypeSplit, Symbol, SymbolFor, SymbolAsyncIterator @@ -81,7 +91,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 +183,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,11 +234,11 @@ function addCatch(that, promise, type, args) { const then = promise.then; if (typeof then === 'function') { - then.call(promise, undefined, function(err) { + ReflectApply(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); - }); + }]); } } catch (err) { that.emit('error', err); @@ -281,7 +291,7 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() { function identicalSequenceRange(a, b) { for (let i = 0; i < a.length - 3; i++) { // Find the first entry of b that matches the current entry of a. - const pos = b.indexOf(a[i]); + const pos = ArrayPrototypeIndexOf(b, a[i]); if (pos !== -1) { const rest = b.length - pos; if (rest > 3) { @@ -310,16 +320,18 @@ function enhanceStackTrace(err, own) { } catch {} const sep = `\nEmitted 'error' event${ctorInfo} at:\n`; - const errStack = err.stack.split('\n').slice(1); - const ownStack = own.stack.split('\n').slice(1); + const errStack = + ArrayPrototypeSlice(StringPrototypeSplit(err.stack, '\n'), 1); + const ownStack = + ArrayPrototypeSlice(StringPrototypeSplit(own.stack, '\n'), 1); const [ len, off ] = identicalSequenceRange(ownStack, errStack); if (len > 0) { - ownStack.splice(off + 1, len - 2, - ' [... lines matching original stack trace ...]'); + ArrayPrototypeSplice(ownStack, off + 1, len - 2, + ' [... lines matching original stack trace ...]'); } - return err.stack + sep + ownStack.join('\n'); + return err.stack + sep + ArrayPrototypeJoin(ownStack, '\n'); } EventEmitter.prototype.emit = function emit(type, ...args) { @@ -343,7 +355,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { const capture = {}; ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit); ObjectDefineProperty(er, kEnhanceStackBeforeInspector, { - value: enhanceStackTrace.bind(this, er, capture), + value: FunctionPrototypeBind(enhanceStackTrace, this, er, capture), configurable: true }); } catch {} @@ -437,9 +449,9 @@ function _addListener(target, type, listener, prepend) { prepend ? [listener, existing] : [existing, listener]; // If we've already got an array, just append. } else if (prepend) { - existing.unshift(listener); + ArrayPrototypeUnshift(existing, listener); } else { - existing.push(listener); + ArrayPrototypePush(existing, listener); } // Check for listener leak @@ -479,14 +491,14 @@ function onceWrapper() { this.target.removeListener(this.type, this.wrapFn); this.fired = true; if (arguments.length === 0) - return this.listener.call(this.target); - return this.listener.apply(this.target, arguments); + return FunctionPrototypeCall(this.listener, this.target); + return ReflectApply(this.listener, this.target, arguments); } } function _onceWrap(target, type, listener) { const state = { fired: false, wrapFn: undefined, target, type, listener }; - const wrapped = onceWrapper.bind(state); + const wrapped = FunctionPrototypeBind(onceWrapper, state); wrapped.listener = listener; state.wrapFn = wrapped; return wrapped; @@ -542,7 +554,7 @@ EventEmitter.prototype.removeListener = return this; if (position === 0) - list.shift(); + ArrayPrototypeShift(list); else { if (spliceOne === undefined) spliceOne = require('internal/util').spliceOne; @@ -636,7 +648,7 @@ EventEmitter.listenerCount = function(emitter, type) { if (typeof emitter.listenerCount === 'function') { return emitter.listenerCount(type); } - return listenerCount.call(emitter, type); + return FunctionPrototypeCall(listenerCount, emitter, type); }; EventEmitter.prototype.listenerCount = listenerCount; @@ -670,7 +682,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) { @@ -791,7 +803,7 @@ function on(emitter, event, options) { const iterator = ObjectSetPrototypeOf({ next() { // First, we consume all unread events - const value = unconsumedEvents.shift(); + const value = ArrayPrototypeShift(unconsumedEvents); if (value) { return PromiseResolve(createIterResult(value, false)); } @@ -813,7 +825,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 }); }); }, @@ -873,18 +885,18 @@ function on(emitter, event, options) { } function eventHandler(...args) { - const promise = unconsumedPromises.shift(); + const promise = ArrayPrototypeShift(unconsumedPromises); if (promise) { promise.resolve(createIterResult(args, false)); } else { - unconsumedEvents.push(args); + ArrayPrototypePush(unconsumedEvents, args); } } function errorHandler(err) { finished = true; - const toError = unconsumedPromises.shift(); + const toError = ArrayPrototypeShift(unconsumedPromises); if (toError) { toError.reject(err); diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index a0efe8c18e875a..d2a2c712a7d1c4 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -5,6 +5,8 @@ const { Boolean, Error, FunctionPrototypeCall, + FunctionPrototypeBind, + FunctionPrototypeCall, NumberIsInteger, ObjectAssign, ObjectDefineProperties, @@ -212,7 +214,7 @@ class Listener { this.callback = typeof listener === 'function' ? listener : - listener.handleEvent.bind(listener); + FunctionPrototypeBind(listener.handleEvent, listener); } same(listener, capture) { @@ -419,7 +421,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) { @@ -590,11 +592,11 @@ function isEventTarget(obj) { function addCatch(that, promise, event) { const then = promise.then; if (typeof then === 'function') { - then.call(promise, undefined, function(err) { + ReflectApply(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) { From 4355063bfb1529d1907ca27ed23a6dae40be5c91 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 17 Dec 2020 10:11:25 -0500 Subject: [PATCH 02/13] fixup! events: refactor to use more primordials Co-authored-by: Darshan Sen --- lib/events.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/events.js b/lib/events.js index 8d749dba0df796..5f678dd2b6e87a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -234,11 +234,11 @@ function addCatch(that, promise, type, args) { const then = promise.then; if (typeof then === 'function') { - ReflectApply(then, 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); - }]); + }); } } catch (err) { that.emit('error', err); From 69ff9a34b7b48fb4febf69c0b2862992cd8da349 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 17 Dec 2020 16:17:20 +0100 Subject: [PATCH 03/13] fixup! events: refactor to use more primordials --- lib/internal/event_target.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index d2a2c712a7d1c4..cfc138d6dbf7cf 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -4,7 +4,6 @@ const { ArrayFrom, Boolean, Error, - FunctionPrototypeCall, FunctionPrototypeBind, FunctionPrototypeCall, NumberIsInteger, @@ -592,11 +591,11 @@ function isEventTarget(obj) { function addCatch(that, promise, event) { const then = promise.then; if (typeof then === 'function') { - ReflectApply(then, 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); - }]); + }); } } From e3d0bb1496e6f03a5f7f5ed309627b644472de2d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 01:02:16 +0100 Subject: [PATCH 04/13] fixup! events: refactor to use more primordials --- lib/events.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/events.js b/lib/events.js index 5f678dd2b6e87a..78760a0c84fe8a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -486,22 +486,13 @@ EventEmitter.prototype.prependListener = return _addListener(this, type, listener, true); }; -function onceWrapper() { - if (!this.fired) { - this.target.removeListener(this.type, this.wrapFn); - this.fired = true; - if (arguments.length === 0) - return FunctionPrototypeCall(this.listener, this.target); - return ReflectApply(this.listener, this.target, arguments); - } -} - function _onceWrap(target, type, listener) { - const state = { fired: false, wrapFn: undefined, target, type, listener }; - const wrapped = FunctionPrototypeBind(onceWrapper, state); - wrapped.listener = listener; - state.wrapFn = wrapped; - return wrapped; + return function onceWrapper() { + target.removeListener(type, onceWrapper); + if (arguments.length === 0) + return FunctionPrototypeCall(listener, target); + return ReflectApply(listener, target, arguments); + }; } EventEmitter.prototype.once = function once(type, listener) { @@ -789,7 +780,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) { } function on(emitter, event, options) { - const { signal } = { ...options }; + const signal = options?.signal; validateAbortSignal(signal, 'options.signal'); if (signal && signal.aborted) { throw lazyDOMException('The operation was aborted', 'AbortError'); From 940bd127b597238d8753424e7aab70fb934ac7a2 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 02:25:41 +0100 Subject: [PATCH 05/13] fixup! events: refactor to use more primordials --- lib/events.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index 78760a0c84fe8a..f645b402a4fa2a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -33,8 +33,10 @@ const { Boolean, Error, ErrorCaptureStackTrace, + Function, FunctionPrototypeBind, FunctionPrototypeCall, + FunctionPrototypeToString, MathMin, NumberIsNaN, ObjectCreate, @@ -49,6 +51,7 @@ const { ReflectOwnKeys, String, StringPrototypeSplit, + StringPrototypeReplace, Symbol, SymbolFor, SymbolAsyncIterator @@ -90,9 +93,6 @@ const lazyDOMException = hideStackFrames((message, name) => { }); -function EventEmitter(opts) { - FunctionPrototypeCall(EventEmitter.init, this, opts); -} module.exports = EventEmitter; module.exports.once = once; module.exports.on = on; @@ -199,7 +199,9 @@ EventEmitter.setMaxListeners = } }; -EventEmitter.init = function(opts) { +let customInit; +function EventEmitter(opts) { + if (customInit) return FunctionPrototypeCall(customInit, this, opts); if (this._events === undefined || this._events === ObjectGetPrototypeOf(this)._events) { @@ -221,7 +223,16 @@ EventEmitter.init = function(opts) { // prototype lookup in a very sensitive hot path. this[kCapture] = EventEmitter.prototype[kCapture]; } -}; +} +ObjectDefineProperty(EventEmitter, 'init', { + enumerable: true, + get: () => + new Function(StringPrototypeReplace(FunctionPrototypeToString(EventEmitter), + /^\s*if\s*\(customInit\)/, '')), + set: (init) => { + customInit = init; + } +}); function addCatch(that, promise, type, args) { if (!that[kCapture]) { From 7a158a243d8ac30ca242c0f76796fb9ff56d3356 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 02:28:14 +0100 Subject: [PATCH 06/13] fixup! events: refactor to use more primordials This reverts commit e3d0bb1496e6f03a5f7f5ed309627b644472de2d. --- lib/events.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/events.js b/lib/events.js index f645b402a4fa2a..758afda27b6aac 100644 --- a/lib/events.js +++ b/lib/events.js @@ -497,13 +497,22 @@ EventEmitter.prototype.prependListener = return _addListener(this, type, listener, true); }; -function _onceWrap(target, type, listener) { - return function onceWrapper() { - target.removeListener(type, onceWrapper); +function onceWrapper() { + if (!this.fired) { + this.target.removeListener(this.type, this.wrapFn); + this.fired = true; if (arguments.length === 0) - return FunctionPrototypeCall(listener, target); - return ReflectApply(listener, target, arguments); - }; + return FunctionPrototypeCall(this.listener, this.target); + return ReflectApply(this.listener, this.target, arguments); + } +} + +function _onceWrap(target, type, listener) { + const state = { fired: false, wrapFn: undefined, target, type, listener }; + const wrapped = FunctionPrototypeBind(onceWrapper, state); + wrapped.listener = listener; + state.wrapFn = wrapped; + return wrapped; } EventEmitter.prototype.once = function once(type, listener) { @@ -791,7 +800,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) { } function on(emitter, event, options) { - const signal = options?.signal; + const { signal } = { ...options }; validateAbortSignal(signal, 'options.signal'); if (signal && signal.aborted) { throw lazyDOMException('The operation was aborted', 'AbortError'); From 9863b295d9746122aefcc921be11b48211f2a77b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 14:32:53 +0100 Subject: [PATCH 07/13] fixup! events: refactor to use more primordials This reverts commit 940bd127b597238d8753424e7aab70fb934ac7a2. --- lib/events.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/lib/events.js b/lib/events.js index 758afda27b6aac..5f678dd2b6e87a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -33,10 +33,8 @@ const { Boolean, Error, ErrorCaptureStackTrace, - Function, FunctionPrototypeBind, FunctionPrototypeCall, - FunctionPrototypeToString, MathMin, NumberIsNaN, ObjectCreate, @@ -51,7 +49,6 @@ const { ReflectOwnKeys, String, StringPrototypeSplit, - StringPrototypeReplace, Symbol, SymbolFor, SymbolAsyncIterator @@ -93,6 +90,9 @@ const lazyDOMException = hideStackFrames((message, name) => { }); +function EventEmitter(opts) { + FunctionPrototypeCall(EventEmitter.init, this, opts); +} module.exports = EventEmitter; module.exports.once = once; module.exports.on = on; @@ -199,9 +199,7 @@ EventEmitter.setMaxListeners = } }; -let customInit; -function EventEmitter(opts) { - if (customInit) return FunctionPrototypeCall(customInit, this, opts); +EventEmitter.init = function(opts) { if (this._events === undefined || this._events === ObjectGetPrototypeOf(this)._events) { @@ -223,16 +221,7 @@ function EventEmitter(opts) { // prototype lookup in a very sensitive hot path. this[kCapture] = EventEmitter.prototype[kCapture]; } -} -ObjectDefineProperty(EventEmitter, 'init', { - enumerable: true, - get: () => - new Function(StringPrototypeReplace(FunctionPrototypeToString(EventEmitter), - /^\s*if\s*\(customInit\)/, '')), - set: (init) => { - customInit = init; - } -}); +}; function addCatch(that, promise, type, args) { if (!that[kCapture]) { From df98f27de9f7463546d275b6c43b98ae554562cf Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 14:34:19 +0100 Subject: [PATCH 08/13] fixup! events: refactor to use more primordials --- lib/events.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/events.js b/lib/events.js index 5f678dd2b6e87a..241b68031c5b3b 100644 --- a/lib/events.js +++ b/lib/events.js @@ -554,7 +554,7 @@ EventEmitter.prototype.removeListener = return this; if (position === 0) - ArrayPrototypeShift(list); + list.shift(); else { if (spliceOne === undefined) spliceOne = require('internal/util').spliceOne; @@ -803,7 +803,7 @@ function on(emitter, event, options) { const iterator = ObjectSetPrototypeOf({ next() { // First, we consume all unread events - const value = ArrayPrototypeShift(unconsumedEvents); + const value = unconsumedEvents.shift(); if (value) { return PromiseResolve(createIterResult(value, false)); } @@ -885,7 +885,7 @@ function on(emitter, event, options) { } function eventHandler(...args) { - const promise = ArrayPrototypeShift(unconsumedPromises); + const promise = unconsumedPromises.shift(); if (promise) { promise.resolve(createIterResult(args, false)); } else { @@ -896,7 +896,7 @@ function on(emitter, event, options) { function errorHandler(err) { finished = true; - const toError = ArrayPrototypeShift(unconsumedPromises); + const toError = unconsumedPromises.shift(); if (toError) { toError.reject(err); From 4db1567377c97a180fe11ba294d718d67e865ca1 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 21 Dec 2020 21:14:27 +0100 Subject: [PATCH 09/13] fixup! events: refactor to use more primordials --- lib/events.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index 241b68031c5b3b..660b05d9f18762 100644 --- a/lib/events.js +++ b/lib/events.js @@ -26,10 +26,8 @@ const { ArrayPrototypeIndexOf, ArrayPrototypeJoin, ArrayPrototypePush, - ArrayPrototypeShift, ArrayPrototypeSlice, ArrayPrototypeSplice, - ArrayPrototypeUnshift, Boolean, Error, ErrorCaptureStackTrace, @@ -449,7 +447,7 @@ function _addListener(target, type, listener, prepend) { prepend ? [listener, existing] : [existing, listener]; // If we've already got an array, just append. } else if (prepend) { - ArrayPrototypeUnshift(existing, listener); + existing.unshift(listener); } else { ArrayPrototypePush(existing, listener); } @@ -491,8 +489,8 @@ function onceWrapper() { this.target.removeListener(this.type, this.wrapFn); this.fired = true; if (arguments.length === 0) - return FunctionPrototypeCall(this.listener, this.target); - return ReflectApply(this.listener, this.target, arguments); + return this.listener.call(this.target); + return this.listener.apply(this.target, arguments); } } From 19239d12c7f97350c9e900c24a785b794dc888e8 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 21 Dec 2020 21:18:04 +0100 Subject: [PATCH 10/13] fixup! events: refactor to use more primordials --- lib/events.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/events.js b/lib/events.js index 660b05d9f18762..c584bc1284be74 100644 --- a/lib/events.js +++ b/lib/events.js @@ -20,6 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; +// Don't skip const { ArrayPrototypeForEach, From 3d16ad82cec5316ea2aab66a65cd61c8734f8a4c Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 21 Dec 2020 21:18:22 +0100 Subject: [PATCH 11/13] fixup! events: refactor to use more primordials --- lib/events.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index c584bc1284be74..660b05d9f18762 100644 --- a/lib/events.js +++ b/lib/events.js @@ -20,7 +20,6 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -// Don't skip const { ArrayPrototypeForEach, From 8d5b6767bfac6b134d26b0da4c88cbf0855f5ef6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 22 Dec 2020 19:06:25 +0100 Subject: [PATCH 12/13] fixup! events: refactor to use more primordials --- lib/events.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/events.js b/lib/events.js index 660b05d9f18762..3b1999f23b05d0 100644 --- a/lib/events.js +++ b/lib/events.js @@ -289,7 +289,7 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() { function identicalSequenceRange(a, b) { for (let i = 0; i < a.length - 3; i++) { // Find the first entry of b that matches the current entry of a. - const pos = ArrayPrototypeIndexOf(b, a[i]); + const pos = b.indexOf(a[i]); if (pos !== -1) { const rest = b.length - pos; if (rest > 3) { @@ -318,18 +318,16 @@ function enhanceStackTrace(err, own) { } catch {} const sep = `\nEmitted 'error' event${ctorInfo} at:\n`; - const errStack = - ArrayPrototypeSlice(StringPrototypeSplit(err.stack, '\n'), 1); - const ownStack = - ArrayPrototypeSlice(StringPrototypeSplit(own.stack, '\n'), 1); + const errStack = err.stack.split('\n').slice(1); + const ownStack = own.stack.split('\n').slice(1); const [ len, off ] = identicalSequenceRange(ownStack, errStack); if (len > 0) { - ArrayPrototypeSplice(ownStack, off + 1, len - 2, - ' [... lines matching original stack trace ...]'); + ownStack.splice(off + 1, len - 2, + ' [... lines matching original stack trace ...]'); } - return err.stack + sep + ArrayPrototypeJoin(ownStack, '\n'); + return err.stack + sep + ownStack.join('\n'); } EventEmitter.prototype.emit = function emit(type, ...args) { @@ -353,7 +351,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { const capture = {}; ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit); ObjectDefineProperty(er, kEnhanceStackBeforeInspector, { - value: FunctionPrototypeBind(enhanceStackTrace, this, er, capture), + value: enhanceStackTrace.bind(this, er, capture), configurable: true }); } catch {} @@ -449,7 +447,7 @@ function _addListener(target, type, listener, prepend) { } else if (prepend) { existing.unshift(listener); } else { - ArrayPrototypePush(existing, listener); + existing.push(listener); } // Check for listener leak @@ -496,7 +494,7 @@ function onceWrapper() { function _onceWrap(target, type, listener) { const state = { fired: false, wrapFn: undefined, target, type, listener }; - const wrapped = FunctionPrototypeBind(onceWrapper, state); + const wrapped = onceWrapper.bind(state); wrapped.listener = listener; state.wrapFn = wrapped; return wrapped; @@ -646,7 +644,7 @@ EventEmitter.listenerCount = function(emitter, type) { if (typeof emitter.listenerCount === 'function') { return emitter.listenerCount(type); } - return FunctionPrototypeCall(listenerCount, emitter, type); + return listenerCount.call(emitter, type); }; EventEmitter.prototype.listenerCount = listenerCount; From 27d8c940461a00dc142484a11567ee08fd67498b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 22 Dec 2020 20:10:17 +0100 Subject: [PATCH 13/13] fixup! events: refactor to use more primordials --- lib/events.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index 3b1999f23b05d0..942e5e01eee044 100644 --- a/lib/events.js +++ b/lib/events.js @@ -23,15 +23,11 @@ const { ArrayPrototypeForEach, - ArrayPrototypeIndexOf, - ArrayPrototypeJoin, ArrayPrototypePush, ArrayPrototypeSlice, - ArrayPrototypeSplice, Boolean, Error, ErrorCaptureStackTrace, - FunctionPrototypeBind, FunctionPrototypeCall, MathMin, NumberIsNaN, @@ -46,7 +42,6 @@ const { ReflectApply, ReflectOwnKeys, String, - StringPrototypeSplit, Symbol, SymbolFor, SymbolAsyncIterator