Skip to content

Commit

Permalink
lib: use static Number properties from primordials
Browse files Browse the repository at this point in the history
PR-URL: #30686
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
targos committed Dec 1, 2019
1 parent 892bde6 commit 5a9340d
Show file tree
Hide file tree
Showing 27 changed files with 84 additions and 47 deletions.
3 changes: 2 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
ArrayIsArray,
Boolean,
NumberIsFinite,
ObjectAssign,
ObjectKeys,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -210,7 +211,7 @@ function ClientRequest(input, options, cb) {
// but only if the Agent will actually reuse the connection!
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
// there's never a case where this socket will actually be reused
if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
if (!this.agent.keepAlive && !NumberIsFinite(this.agent.maxSockets)) {
this._last = true;
this.shouldKeepAlive = false;
} else {
Expand Down
6 changes: 4 additions & 2 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

const {
ArrayIsArray,
NumberIsInteger,
NumberIsNaN,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -406,7 +408,7 @@ function howMuchToRead(n, state) {
return 0;
if (state.objectMode)
return 1;
if (Number.isNaN(n)) {
if (NumberIsNaN(n)) {
// Only flow one buffer at a time
if (state.flowing && state.length)
return state.buffer.first().length;
Expand All @@ -425,7 +427,7 @@ Readable.prototype.read = function(n) {
// in this scenario, so we are doing it manually.
if (n === undefined) {
n = NaN;
} else if (!Number.isInteger(n)) {
} else if (!NumberIsInteger(n)) {
n = parseInt(n, 10);
}
const state = this._readableState;
Expand Down
3 changes: 2 additions & 1 deletion lib/async_hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
NumberIsSafeInteger,
ReflectApply,
} = primordials;

Expand Down Expand Up @@ -147,7 +148,7 @@ class AsyncResource {

// Unlike emitInitScript, AsyncResource doesn't supports null as the
// triggerAsyncId.
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
if (!NumberIsSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
}

Expand Down
15 changes: 9 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const {
MathFloor,
MathMin,
MathTrunc,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
Expand Down Expand Up @@ -175,9 +178,9 @@ function showFlaggedDeprecation() {

function toInteger(n, defaultVal) {
n = +n;
if (!Number.isNaN(n) &&
n >= Number.MIN_SAFE_INTEGER &&
n <= Number.MAX_SAFE_INTEGER) {
if (!NumberIsNaN(n) &&
n >= NumberMIN_SAFE_INTEGER &&
n <= NumberMAX_SAFE_INTEGER) {
return ((n % 1) === 0 ? n : MathFloor(n));
}
return defaultVal;
Expand Down Expand Up @@ -442,7 +445,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
byteOffset = 0;
} else {
byteOffset = +byteOffset;
if (Number.isNaN(byteOffset))
if (NumberIsNaN(byteOffset))
byteOffset = 0;
}

Expand Down Expand Up @@ -890,7 +893,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
// Coerce to Number. Values like null and [] become 0.
byteOffset = +byteOffset;
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
if (Number.isNaN(byteOffset)) {
if (NumberIsNaN(byteOffset)) {
byteOffset = dir ? 0 : buffer.length;
}
dir = !!dir; // Cast to bool.
Expand Down Expand Up @@ -1063,7 +1066,7 @@ function adjustOffset(offset, length) {
if (offset < length) {
return offset;
}
return Number.isNaN(offset) ? 0 : length;
return NumberIsNaN(offset) ? 0 : length;
}

Buffer.prototype.slice = function slice(start, end) {
Expand Down
3 changes: 2 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const {
ArrayIsArray,
NumberIsInteger,
ObjectAssign,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Expand Down Expand Up @@ -651,7 +652,7 @@ function execSync(command, options) {


function validateTimeout(timeout) {
if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) {
if (timeout != null && !(NumberIsInteger(timeout) && timeout >= 0)) {
throw new ERR_OUT_OF_RANGE('timeout', 'an unsigned integer', timeout);
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
Array,
MathMin,
NumberIsNaN,
ObjectCreate,
ObjectDefineProperty,
ObjectGetPrototypeOf,
Expand Down Expand Up @@ -79,7 +80,7 @@ ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
return defaultMaxListeners;
},
set: function(arg) {
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
Expand All @@ -102,7 +103,7 @@ EventEmitter.init = function() {
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
}
this._maxListeners = n;
Expand Down
5 changes: 3 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

const {
MathMax,
NumberIsSafeInteger,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
Expand Down Expand Up @@ -472,7 +473,7 @@ function read(fd, buffer, offset, length, position, callback) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (!Number.isSafeInteger(position))
if (!NumberIsSafeInteger(position))
position = -1;

function wrapper(err, bytesRead) {
Expand Down Expand Up @@ -512,7 +513,7 @@ function readSync(fd, buffer, offset, length, position) {

validateOffsetLengthRead(offset, length, buffer.byteLength);

if (!Number.isSafeInteger(position))
if (!NumberIsSafeInteger(position))
position = -1;

const ctx = {};
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
FunctionPrototypeBind,
NumberIsSafeInteger,
ObjectDefineProperty,
} = primordials;

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

if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
if (!NumberIsSafeInteger(asyncId) || asyncId < -1) {
fatalError(new ERR_INVALID_ASYNC_ID(type, asyncId));
}
}
Expand Down Expand Up @@ -299,7 +300,7 @@ function clearDefaultTriggerAsyncId() {
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
if (triggerAsyncId === undefined)
return block(...args);
// CHECK(Number.isSafeInteger(triggerAsyncId))
// CHECK(NumberIsSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
MathMin,
NumberIsNaN,
} = primordials;

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

const maxLength = MathMin(length, kMaxPossibleLength);
if (Number.isNaN(offset) || offset > maxLength || offset < 0) {
if (NumberIsNaN(offset) || offset > maxLength || offset < 0) {
throw new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${maxLength}`, offset);
}

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

if (Number.isNaN(size) || size > kMaxPossibleLength || size < 0) {
if (NumberIsNaN(size) || size > kMaxPossibleLength || size < 0) {
throw new ERR_OUT_OF_RANGE('size',
`>= 0 && <= ${kMaxPossibleLength}`, size);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const {
ArrayIsArray,
MathAbs,
NumberIsInteger,
ObjectDefineProperty,
ObjectKeys,
} = primordials;
Expand Down Expand Up @@ -1108,7 +1109,7 @@ E('ERR_OUT_OF_RANGE',
let msg = replaceDefaultBoolean ? str :
`The value of "${str}" is out of range.`;
let received;
if (Number.isInteger(input) && MathAbs(input) > 2 ** 32) {
if (NumberIsInteger(input) && MathAbs(input) > 2 ** 32) {
received = addNumericalSeparator(String(input));
} else if (typeof input === 'bigint') {
received = String(input);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
MathMax,
MathMin,
NumberIsSafeInteger,
} = primordials;

const {
Expand Down Expand Up @@ -235,7 +236,7 @@ async function read(handle, buffer, offset, length, position) {

validateOffsetLengthRead(offset, length, buffer.length);

if (!Number.isSafeInteger(position))
if (!NumberIsSafeInteger(position))
position = -1;

const bytesRead = (await binding.read(handle.fd, buffer, offset, length,
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const {
Array,
MathMin,
NumberIsInteger,
NumberIsSafeInteger,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -41,9 +43,9 @@ function allocNewPool(poolSize) {

// Check the `this.start` and `this.end` of stream.
function checkPosition(pos, name) {
if (!Number.isSafeInteger(pos)) {
if (!NumberIsSafeInteger(pos)) {
validateNumber(pos, name);
if (!Number.isInteger(pos))
if (!NumberIsInteger(pos))
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
DateNow,
NumberIsFinite,
ObjectSetPrototypeOf,
ReflectOwnKeys,
} = primordials;
Expand Down Expand Up @@ -486,7 +487,7 @@ function toUnixTimestamp(time, name = 'time') {
if (typeof time === 'string' && +time == time) {
return +time;
}
if (Number.isFinite(time)) {
if (NumberIsFinite(time)) {
if (time < 0) {
return DateNow() / 1000;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const {
ArrayIsArray,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
Expand Down Expand Up @@ -102,7 +103,7 @@ function wrapProcessMethods(binding) {
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER.
function previousValueIsValid(num) {
return typeof num === 'number' &&
num <= Number.MAX_SAFE_INTEGER &&
num <= NumberMAX_SAFE_INTEGER &&
num >= 0;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/internal/readline/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
Boolean,
NumberIsInteger,
} = primordials;

// Regex used for ansi escape code splitting
Expand Down Expand Up @@ -38,7 +39,7 @@ if (internalBinding('config').hasIntl) {
const icu = internalBinding('icu');
getStringWidth = function getStringWidth(str, options) {
options = options || {};
if (Number.isInteger(str)) {
if (NumberIsInteger(str)) {
// Provide information about the character with code point 'str'.
return icu.getStringWidth(
str,
Expand Down Expand Up @@ -76,7 +77,7 @@ if (internalBinding('config').hasIntl) {
* Returns the number of columns required to display the given string.
*/
getStringWidth = function getStringWidth(str) {
if (Number.isInteger(str))
if (NumberIsInteger(str))
return isFullWidthCodePoint(str) ? 2 : 1;

let width = 0;
Expand Down Expand Up @@ -107,7 +108,7 @@ if (internalBinding('config').hasIntl) {
isFullWidthCodePoint = function isFullWidthCodePoint(code) {
// Code points are derived from:
// http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
return Number.isInteger(code) && code >= 0x1100 && (
return NumberIsInteger(code) && code >= 0x1100 && (
code <= 0x115f || // Hangul Jamo
code === 0x2329 || // LEFT-POINTING ANGLE BRACKET
code === 0x232a || // RIGHT-POINTING ANGLE BRACKET
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
NumberIsNaN,
ObjectCreate,
} = primordials;

Expand Down Expand Up @@ -39,7 +40,7 @@ function createRepl(env, opts, cb) {
}

const historySize = Number(env.NODE_REPL_HISTORY_SIZE);
if (!Number.isNaN(historySize) && historySize > 0) {
if (!NumberIsNaN(historySize) && historySize > 0) {
opts.historySize = historySize;
} else {
opts.historySize = 1000;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/streams/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
MathFloor,
NumberIsInteger,
} = primordials;

const { ERR_INVALID_OPT_VALUE } = require('internal/errors').codes;
Expand All @@ -18,7 +19,7 @@ function getDefaultHighWaterMark(objectMode) {
function getHighWaterMark(state, options, duplexKey, isDuplex) {
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
if (hwm != null) {
if (!Number.isInteger(hwm) || hwm < 0) {
if (!NumberIsInteger(hwm) || hwm < 0) {
const name = isDuplex ? duplexKey : 'highWaterMark';
throw new ERR_INVALID_OPT_VALUE(name, hwm);
}
Expand Down
Loading

0 comments on commit 5a9340d

Please sign in to comment.