events: refactor to use `validateNumber`

Need to use validateNumber for checking `TypeError`

PR-URL: https://github.com/nodejs/node/pull/45770
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
Deokjin Kim 2023-01-22 15:50:37 +09:00 committed by GitHub
parent a08a1feb3c
commit cc18fd9608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 20 deletions

View File

@ -33,7 +33,6 @@ const {
ErrorCaptureStackTrace, ErrorCaptureStackTrace,
FunctionPrototypeBind, FunctionPrototypeBind,
FunctionPrototypeCall, FunctionPrototypeCall,
NumberIsNaN,
NumberMAX_SAFE_INTEGER, NumberMAX_SAFE_INTEGER,
ObjectDefineProperty, ObjectDefineProperty,
ObjectDefineProperties, ObjectDefineProperties,
@ -69,7 +68,6 @@ const {
codes: { codes: {
ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS, ERR_INVALID_THIS,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR ERR_UNHANDLED_ERROR
}, },
genericNodeError, genericNodeError,
@ -80,6 +78,7 @@ const {
validateAbortSignal, validateAbortSignal,
validateBoolean, validateBoolean,
validateFunction, validateFunction,
validateNumber,
validateString, validateString,
} = require('internal/validators'); } = require('internal/validators');
@ -278,11 +277,7 @@ ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
return defaultMaxListeners; return defaultMaxListeners;
}, },
set: function(arg) { set: function(arg) {
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) { validateNumber(arg, 'defaultMaxListeners', 0);
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
}
defaultMaxListeners = arg; defaultMaxListeners = arg;
} }
}); });
@ -312,8 +307,7 @@ ObjectDefineProperties(EventEmitter, {
*/ */
EventEmitter.setMaxListeners = EventEmitter.setMaxListeners =
function(n = defaultMaxListeners, ...eventTargets) { function(n = defaultMaxListeners, ...eventTargets) {
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) validateNumber(n, 'setMaxListeners', 0);
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
if (eventTargets.length === 0) { if (eventTargets.length === 0) {
defaultMaxListeners = n; defaultMaxListeners = n;
} else { } else {
@ -409,9 +403,7 @@ function emitUnhandledRejectionOrErr(ee, err, type, args) {
* @returns {EventEmitter} * @returns {EventEmitter}
*/ */
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { validateNumber(n, 'setMaxListeners', 0);
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
}
this._maxListeners = n; this._maxListeners = n;
return this; return this;
}; };

View File

@ -23,7 +23,6 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const events = require('events'); const events = require('events');
const { inspect } = require('util');
const e = new events.EventEmitter(); const e = new events.EventEmitter();
e.on('maxListeners', common.mustCall()); e.on('maxListeners', common.mustCall());
@ -31,16 +30,15 @@ e.on('maxListeners', common.mustCall());
// Should not corrupt the 'maxListeners' queue. // Should not corrupt the 'maxListeners' queue.
e.setMaxListeners(42); e.setMaxListeners(42);
const throwsObjs = [NaN, -1, 'and even this']; const rangeErrorObjs = [NaN, -1];
const typeErrorObj = 'and even this';
for (const obj of throwsObjs) { for (const obj of rangeErrorObjs) {
assert.throws( assert.throws(
() => e.setMaxListeners(obj), () => e.setMaxListeners(obj),
{ {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_OUT_OF_RANGE',
name: 'RangeError', name: 'RangeError',
message: 'The value of "n" is out of range. ' +
`It must be a non-negative number. Received ${inspect(obj)}`,
} }
); );
@ -49,22 +47,40 @@ for (const obj of throwsObjs) {
{ {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_OUT_OF_RANGE',
name: 'RangeError', name: 'RangeError',
message: 'The value of "defaultMaxListeners" is out of range. ' +
`It must be a non-negative number. Received ${inspect(obj)}`,
} }
); );
} }
assert.throws(
() => e.setMaxListeners(typeErrorObj),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
assert.throws(
() => events.defaultMaxListeners = typeErrorObj,
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
e.emit('maxListeners'); e.emit('maxListeners');
{ {
const { EventEmitter, defaultMaxListeners } = events; const { EventEmitter, defaultMaxListeners } = events;
for (const obj of throwsObjs) { for (const obj of rangeErrorObjs) {
assert.throws(() => EventEmitter.setMaxListeners(obj), { assert.throws(() => EventEmitter.setMaxListeners(obj), {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_OUT_OF_RANGE',
}); });
} }
assert.throws(() => EventEmitter.setMaxListeners(typeErrorObj), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws( assert.throws(
() => EventEmitter.setMaxListeners(defaultMaxListeners, 'INVALID_EMITTER'), () => EventEmitter.setMaxListeners(defaultMaxListeners, 'INVALID_EMITTER'),
{ code: 'ERR_INVALID_ARG_TYPE' } { code: 'ERR_INVALID_ARG_TYPE' }