Skip to content

Commit 5de272c

Browse files
committed
lib: use static Number properties from primordials
PR-URL: #30686 Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 2e81795 commit 5de272c

27 files changed

+84
-47
lines changed

lib/_http_client.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {
2525
ArrayIsArray,
2626
Boolean,
27+
NumberIsFinite,
2728
ObjectAssign,
2829
ObjectKeys,
2930
ObjectSetPrototypeOf,
@@ -211,7 +212,7 @@ function ClientRequest(input, options, cb) {
211212
// but only if the Agent will actually reuse the connection!
212213
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
213214
// there's never a case where this socket will actually be reused
214-
if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
215+
if (!this.agent.keepAlive && !NumberIsFinite(this.agent.maxSockets)) {
215216
this._last = true;
216217
this.shouldKeepAlive = false;
217218
} else {

lib/_stream_readable.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
const {
2525
ArrayIsArray,
26+
NumberIsInteger,
27+
NumberIsNaN,
2628
ObjectDefineProperty,
2729
ObjectSetPrototypeOf,
2830
} = primordials;
@@ -389,7 +391,7 @@ function howMuchToRead(n, state) {
389391
return 0;
390392
if (state.objectMode)
391393
return 1;
392-
if (Number.isNaN(n)) {
394+
if (NumberIsNaN(n)) {
393395
// Only flow one buffer at a time
394396
if (state.flowing && state.length)
395397
return state.buffer.first().length;
@@ -408,7 +410,7 @@ Readable.prototype.read = function(n) {
408410
// in this scenario, so we are doing it manually.
409411
if (n === undefined) {
410412
n = NaN;
411-
} else if (!Number.isInteger(n)) {
413+
} else if (!NumberIsInteger(n)) {
412414
n = parseInt(n, 10);
413415
}
414416
const state = this._readableState;

lib/async_hooks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
NumberIsSafeInteger,
45
ReflectApply,
56
} = primordials;
67

@@ -147,7 +148,7 @@ class AsyncResource {
147148

148149
// Unlike emitInitScript, AsyncResource doesn't supports null as the
149150
// triggerAsyncId.
150-
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
151+
if (!NumberIsSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
151152
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
152153
}
153154

lib/buffer.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const {
2727
MathFloor,
2828
MathMin,
2929
MathTrunc,
30+
NumberIsNaN,
31+
NumberMAX_SAFE_INTEGER,
32+
NumberMIN_SAFE_INTEGER,
3033
ObjectCreate,
3134
ObjectDefineProperties,
3235
ObjectDefineProperty,
@@ -175,9 +178,9 @@ function showFlaggedDeprecation() {
175178

176179
function toInteger(n, defaultVal) {
177180
n = +n;
178-
if (!Number.isNaN(n) &&
179-
n >= Number.MIN_SAFE_INTEGER &&
180-
n <= Number.MAX_SAFE_INTEGER) {
181+
if (!NumberIsNaN(n) &&
182+
n >= NumberMIN_SAFE_INTEGER &&
183+
n <= NumberMAX_SAFE_INTEGER) {
181184
return ((n % 1) === 0 ? n : MathFloor(n));
182185
}
183186
return defaultVal;
@@ -442,7 +445,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
442445
byteOffset = 0;
443446
} else {
444447
byteOffset = +byteOffset;
445-
if (Number.isNaN(byteOffset))
448+
if (NumberIsNaN(byteOffset))
446449
byteOffset = 0;
447450
}
448451

@@ -890,7 +893,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
890893
// Coerce to Number. Values like null and [] become 0.
891894
byteOffset = +byteOffset;
892895
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
893-
if (Number.isNaN(byteOffset)) {
896+
if (NumberIsNaN(byteOffset)) {
894897
byteOffset = dir ? 0 : buffer.length;
895898
}
896899
dir = !!dir; // Cast to bool.
@@ -1063,7 +1066,7 @@ function adjustOffset(offset, length) {
10631066
if (offset < length) {
10641067
return offset;
10651068
}
1066-
return Number.isNaN(offset) ? 0 : length;
1069+
return NumberIsNaN(offset) ? 0 : length;
10671070
}
10681071

10691072
Buffer.prototype.slice = function slice(start, end) {

lib/child_process.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
ArrayIsArray,
26+
NumberIsInteger,
2627
ObjectAssign,
2728
ObjectDefineProperty,
2829
ObjectPrototypeHasOwnProperty,
@@ -670,7 +671,7 @@ function execSync(command, options) {
670671

671672

672673
function validateTimeout(timeout) {
673-
if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) {
674+
if (timeout != null && !(NumberIsInteger(timeout) && timeout >= 0)) {
674675
throw new ERR_OUT_OF_RANGE('timeout', 'an unsigned integer', timeout);
675676
}
676677
}

lib/events.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {
2525
Array,
2626
MathMin,
27+
NumberIsNaN,
2728
ObjectCreate,
2829
ObjectDefineProperty,
2930
ObjectGetPrototypeOf,
@@ -79,7 +80,7 @@ ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
7980
return defaultMaxListeners;
8081
},
8182
set: function(arg) {
82-
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
83+
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
8384
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
8485
'a non-negative number',
8586
arg);
@@ -102,7 +103,7 @@ EventEmitter.init = function() {
102103
// Obviously not all Emitters should be limited to 10. This function allows
103104
// that to be increased. Set to zero for unlimited.
104105
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
105-
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
106+
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
106107
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
107108
}
108109
this._maxListeners = n;

lib/fs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
const {
2828
MathMax,
29+
NumberIsSafeInteger,
2930
ObjectCreate,
3031
ObjectDefineProperties,
3132
ObjectDefineProperty,
@@ -476,7 +477,7 @@ function read(fd, buffer, offset, length, position, callback) {
476477

477478
validateOffsetLengthRead(offset, length, buffer.byteLength);
478479

479-
if (!Number.isSafeInteger(position))
480+
if (!NumberIsSafeInteger(position))
480481
position = -1;
481482

482483
function wrapper(err, bytesRead) {
@@ -511,7 +512,7 @@ function readSync(fd, buffer, offset, length, position) {
511512

512513
validateOffsetLengthRead(offset, length, buffer.byteLength);
513514

514-
if (!Number.isSafeInteger(position))
515+
if (!NumberIsSafeInteger(position))
515516
position = -1;
516517

517518
const ctx = {};

lib/internal/async_hooks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
FunctionPrototypeBind,
5+
NumberIsSafeInteger,
56
ObjectDefineProperty,
67
} = primordials;
78

@@ -118,7 +119,7 @@ function validateAsyncId(asyncId, type) {
118119
// Skip validation when async_hooks is disabled
119120
if (async_hook_fields[kCheck] <= 0) return;
120121

121-
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
122+
if (!NumberIsSafeInteger(asyncId) || asyncId < -1) {
122123
fatalError(new ERR_INVALID_ASYNC_ID(type, asyncId));
123124
}
124125
}
@@ -299,7 +300,7 @@ function clearDefaultTriggerAsyncId() {
299300
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
300301
if (triggerAsyncId === undefined)
301302
return block(...args);
302-
// CHECK(Number.isSafeInteger(triggerAsyncId))
303+
// CHECK(NumberIsSafeInteger(triggerAsyncId))
303304
// CHECK(triggerAsyncId > 0)
304305
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
305306
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;

lib/internal/crypto/random.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
MathMin,
5+
NumberIsNaN,
56
} = primordials;
67

78
const { AsyncWrap, Providers } = internalBinding('async_wrap');
@@ -23,7 +24,7 @@ function assertOffset(offset, elementSize, length) {
2324
offset *= elementSize;
2425

2526
const maxLength = MathMin(length, kMaxPossibleLength);
26-
if (Number.isNaN(offset) || offset > maxLength || offset < 0) {
27+
if (NumberIsNaN(offset) || offset > maxLength || offset < 0) {
2728
throw new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${maxLength}`, offset);
2829
}
2930

@@ -34,7 +35,7 @@ function assertSize(size, elementSize, offset, length) {
3435
validateNumber(size, 'size');
3536
size *= elementSize;
3637

37-
if (Number.isNaN(size) || size > kMaxPossibleLength || size < 0) {
38+
if (NumberIsNaN(size) || size > kMaxPossibleLength || size < 0) {
3839
throw new ERR_OUT_OF_RANGE('size',
3940
`>= 0 && <= ${kMaxPossibleLength}`, size);
4041
}

lib/internal/errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
const {
1414
ArrayIsArray,
1515
MathAbs,
16+
NumberIsInteger,
1617
ObjectDefineProperty,
1718
ObjectKeys,
1819
} = primordials;
@@ -1113,7 +1114,7 @@ E('ERR_OUT_OF_RANGE',
11131114
let msg = replaceDefaultBoolean ? str :
11141115
`The value of "${str}" is out of range.`;
11151116
let received;
1116-
if (Number.isInteger(input) && MathAbs(input) > 2 ** 32) {
1117+
if (NumberIsInteger(input) && MathAbs(input) > 2 ** 32) {
11171118
received = addNumericalSeparator(String(input));
11181119
} else if (typeof input === 'bigint') {
11191120
received = String(input);

lib/internal/fs/promises.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
MathMax,
55
MathMin,
6+
NumberIsSafeInteger,
67
} = primordials;
78

89
const {
@@ -230,7 +231,7 @@ async function read(handle, buffer, offset, length, position) {
230231

231232
validateOffsetLengthRead(offset, length, buffer.length);
232233

233-
if (!Number.isSafeInteger(position))
234+
if (!NumberIsSafeInteger(position))
234235
position = -1;
235236

236237
const bytesRead = (await binding.read(handle.fd, buffer, offset, length,

lib/internal/fs/streams.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const {
44
Array,
55
MathMin,
6+
NumberIsInteger,
7+
NumberIsSafeInteger,
68
ObjectDefineProperty,
79
ObjectSetPrototypeOf,
810
} = primordials;
@@ -40,9 +42,9 @@ function allocNewPool(poolSize) {
4042

4143
// Check the `this.start` and `this.end` of stream.
4244
function checkPosition(pos, name) {
43-
if (!Number.isSafeInteger(pos)) {
45+
if (!NumberIsSafeInteger(pos)) {
4446
validateNumber(pos, name);
45-
if (!Number.isInteger(pos))
47+
if (!NumberIsInteger(pos))
4648
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
4749
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
4850
}

lib/internal/fs/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayIsArray,
55
DateNow,
6+
NumberIsFinite,
67
ObjectSetPrototypeOf,
78
ReflectOwnKeys,
89
} = primordials;
@@ -486,7 +487,7 @@ function toUnixTimestamp(time, name = 'time') {
486487
if (typeof time === 'string' && +time == time) {
487488
return +time;
488489
}
489-
if (Number.isFinite(time)) {
490+
if (NumberIsFinite(time)) {
490491
if (time < 0) {
491492
return DateNow() / 1000;
492493
}

lib/internal/process/per_thread.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const {
88
ArrayIsArray,
9+
NumberMAX_SAFE_INTEGER,
910
ObjectDefineProperties,
1011
ObjectDefineProperty,
1112
ObjectFreeze,
@@ -102,7 +103,7 @@ function wrapProcessMethods(binding) {
102103
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER.
103104
function previousValueIsValid(num) {
104105
return typeof num === 'number' &&
105-
num <= Number.MAX_SAFE_INTEGER &&
106+
num <= NumberMAX_SAFE_INTEGER &&
106107
num >= 0;
107108
}
108109

lib/internal/readline/utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
Boolean,
5+
NumberIsInteger,
56
} = primordials;
67

78
// Regex used for ansi escape code splitting
@@ -38,7 +39,7 @@ if (internalBinding('config').hasIntl) {
3839
const icu = internalBinding('icu');
3940
getStringWidth = function getStringWidth(str, options) {
4041
options = options || {};
41-
if (Number.isInteger(str)) {
42+
if (NumberIsInteger(str)) {
4243
// Provide information about the character with code point 'str'.
4344
return icu.getStringWidth(
4445
str,
@@ -76,7 +77,7 @@ if (internalBinding('config').hasIntl) {
7677
* Returns the number of columns required to display the given string.
7778
*/
7879
getStringWidth = function getStringWidth(str) {
79-
if (Number.isInteger(str))
80+
if (NumberIsInteger(str))
8081
return isFullWidthCodePoint(str) ? 2 : 1;
8182

8283
let width = 0;
@@ -107,7 +108,7 @@ if (internalBinding('config').hasIntl) {
107108
isFullWidthCodePoint = function isFullWidthCodePoint(code) {
108109
// Code points are derived from:
109110
// http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
110-
return Number.isInteger(code) && code >= 0x1100 && (
111+
return NumberIsInteger(code) && code >= 0x1100 && (
111112
code <= 0x115f || // Hangul Jamo
112113
code === 0x2329 || // LEFT-POINTING ANGLE BRACKET
113114
code === 0x232a || // RIGHT-POINTING ANGLE BRACKET

lib/internal/repl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
NumberIsNaN,
45
ObjectCreate,
56
} = primordials;
67

@@ -39,7 +40,7 @@ function createRepl(env, opts, cb) {
3940
}
4041

4142
const historySize = Number(env.NODE_REPL_HISTORY_SIZE);
42-
if (!Number.isNaN(historySize) && historySize > 0) {
43+
if (!NumberIsNaN(historySize) && historySize > 0) {
4344
opts.historySize = historySize;
4445
} else {
4546
opts.historySize = 1000;

lib/internal/streams/state.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
MathFloor,
5+
NumberIsInteger,
56
} = primordials;
67

78
const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes;
@@ -18,7 +19,7 @@ function getDefaultHighWaterMark(objectMode) {
1819
function getHighWaterMark(state, options, duplexKey, isDuplex) {
1920
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
2021
if (hwm != null) {
21-
if (!Number.isInteger(hwm) || hwm < 0) {
22+
if (!NumberIsInteger(hwm) || hwm < 0) {
2223
const name = isDuplex ? duplexKey : 'highWaterMark';
2324
throw new ERR_INVALID_OPT_VALUE(name, hwm);
2425
}

0 commit comments

Comments
 (0)