From 7022609dcc8b33bf38262a72853204fca7a1de59 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 19 Mar 2019 18:15:44 +0800 Subject: [PATCH] events: load internal/errors eagerly Since `internal/errors` is loaded by many builtin modules and is currently the first module loaded during bootstrap, it is fine to load it eagerly. We just need to make sure that `internal/errors` itself load other modules lazily. PR-URL: https://github.com/nodejs/node/pull/26771 Reviewed-By: Yongsheng Zhang Reviewed-By: Ruben Bridgewater --- lib/events.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/events.js b/lib/events.js index 1d253fdd33cea7..8a676773b6507a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -23,6 +23,12 @@ var spliceOne; +const { + ERR_INVALID_ARG_TYPE, + ERR_OUT_OF_RANGE, + ERR_UNHANDLED_ERROR +} = require('internal/errors').codes; + function EventEmitter() { EventEmitter.init.call(this); } @@ -42,17 +48,9 @@ EventEmitter.prototype._maxListeners = undefined; // added to it. This is a useful default which helps finding memory leaks. var defaultMaxListeners = 10; -var errors; -function lazyErrors() { - if (errors === undefined) - errors = require('internal/errors').codes; - return errors; -} - function checkListener(listener) { if (typeof listener !== 'function') { - const errors = lazyErrors(); - throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); + throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener); } } @@ -63,10 +61,9 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', { }, set: function(arg) { if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) { - const errors = lazyErrors(); - throw new errors.ERR_OUT_OF_RANGE('defaultMaxListeners', - 'a non-negative number', - arg); + throw new ERR_OUT_OF_RANGE('defaultMaxListeners', + 'a non-negative number', + arg); } defaultMaxListeners = arg; } @@ -87,8 +84,7 @@ EventEmitter.init = function() { // that to be increased. Set to zero for unlimited. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) { - const errors = lazyErrors(); - throw new errors.ERR_OUT_OF_RANGE('n', 'a non-negative number', n); + throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n); } this._maxListeners = n; return this; @@ -183,8 +179,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { } // At least give some kind of context to the user - const errors = lazyErrors(); - const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr); + const err = new ERR_UNHANDLED_ERROR(stringifiedEr); err.context = er; throw err; // Unhandled 'error' event }