From 940bd127b597238d8753424e7aab70fb934ac7a2 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 18 Dec 2020 02:25:41 +0100 Subject: [PATCH] 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]) {