Skip to content

Commit

Permalink
lib: enforce use of Array from primordials
Browse files Browse the repository at this point in the history
PR-URL: #30635
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
targos authored and BethGriggs committed Feb 6, 2020
1 parent e2ae4c1 commit 9260844
Show file tree
Hide file tree
Showing 48 changed files with 140 additions and 69 deletions.
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ rules:
- groups: [[ "&&", "||" ]]
no-restricted-globals:
- error
- name: Array
message: "Use `const { Array } = primordials;` instead of the global."
- name: JSON
message: "Use `const { JSON } = primordials;` instead of the global."
- name: Math
Expand Down
3 changes: 2 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
ObjectAssign,
ObjectKeys,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -226,7 +227,7 @@ function ClientRequest(input, options, cb) {
}
}

const headersArray = Array.isArray(options.headers);
const headersArray = ArrayIsArray(options.headers);
if (!headersArray) {
if (options.headers) {
const keys = ObjectKeys(options.headers);
Expand Down
7 changes: 4 additions & 3 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
Expand Down Expand Up @@ -350,7 +351,7 @@ function _storeHeader(firstLine, headers) {
const entry = headers[key];
processHeader(this, state, entry[0], entry[1], false);
}
} else if (Array.isArray(headers)) {
} else if (ArrayIsArray(headers)) {
for (const entry of headers) {
processHeader(this, state, entry[0], entry[1], true);
}
Expand Down Expand Up @@ -444,7 +445,7 @@ function _storeHeader(firstLine, headers) {
function processHeader(self, state, key, value, validate) {
if (validate)
validateHeaderName(key);
if (Array.isArray(value)) {
if (ArrayIsArray(value)) {
if (value.length < 2 || !isCookieField(key)) {
for (var i = 0; i < value.length; i++)
storeHeader(self, state, key, value[i], validate);
Expand Down Expand Up @@ -686,7 +687,7 @@ function connectionCorkNT(conn) {
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
this._trailer = '';
const keys = ObjectKeys(headers);
const isArray = Array.isArray(headers);
const isArray = ArrayIsArray(headers);
var field, value;
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
Expand Down
3 changes: 2 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -70,7 +71,7 @@ function prependListener(emitter, event, fn) {
// the prependListener() method. The goal is to eventually remove this hack.
if (!emitter._events || !emitter._events[event])
emitter.on(event, fn);
else if (Array.isArray(emitter._events[event]))
else if (ArrayIsArray(emitter._events[event]))
emitter._events[event].unshift(fn);
else
emitter._events[event] = [fn, emitter._events[event]];
Expand Down
1 change: 1 addition & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'use strict';

const {
Array,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down
11 changes: 6 additions & 5 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
ObjectCreate,
} = primordials;

Expand Down Expand Up @@ -105,7 +106,7 @@ exports.createSecureContext = function createSecureContext(options) {
// Add CA before the cert to be able to load cert's issuer in C++ code.
const { ca } = options;
if (ca) {
if (Array.isArray(ca)) {
if (ArrayIsArray(ca)) {
for (i = 0; i < ca.length; ++i) {
val = ca[i];
validateKeyOrCertOption('ca', val);
Expand All @@ -121,7 +122,7 @@ exports.createSecureContext = function createSecureContext(options) {

const { cert } = options;
if (cert) {
if (Array.isArray(cert)) {
if (ArrayIsArray(cert)) {
for (i = 0; i < cert.length; ++i) {
val = cert[i];
validateKeyOrCertOption('cert', val);
Expand All @@ -140,7 +141,7 @@ exports.createSecureContext = function createSecureContext(options) {
const key = options.key;
const passphrase = options.passphrase;
if (key) {
if (Array.isArray(key)) {
if (ArrayIsArray(key)) {
for (i = 0; i < key.length; ++i) {
val = key[i];
// eslint-disable-next-line eqeqeq
Expand Down Expand Up @@ -240,7 +241,7 @@ exports.createSecureContext = function createSecureContext(options) {
}

if (options.crl) {
if (Array.isArray(options.crl)) {
if (ArrayIsArray(options.crl)) {
for (i = 0; i < options.crl.length; i++) {
c.context.addCRL(options.crl[i]);
}
Expand All @@ -257,7 +258,7 @@ exports.createSecureContext = function createSecureContext(options) {
if (!toBuf)
toBuf = require('internal/crypto/util').toBuf;

if (Array.isArray(options.pfx)) {
if (ArrayIsArray(options.pfx)) {
for (i = 0; i < options.pfx.length; i++) {
const pfx = options.pfx[i];
const raw = pfx.buf ? pfx.buf : pfx;
Expand Down
6 changes: 4 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'use strict';

const {
Array,
ArrayIsArray,
MathFloor,
MathMin,
MathTrunc,
Expand Down Expand Up @@ -483,7 +485,7 @@ function fromObject(obj) {
return fromArrayLike(obj);
}

if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
if (obj.type === 'Buffer' && ArrayIsArray(obj.data)) {
return fromArrayLike(obj.data);
}
}
Expand Down Expand Up @@ -518,7 +520,7 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
let i;
if (!Array.isArray(list)) {
if (!ArrayIsArray(list)) {
throw new ERR_INVALID_ARG_TYPE('list', 'Array', list);
}

Expand Down
9 changes: 5 additions & 4 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
ObjectAssign,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Expand Down Expand Up @@ -63,7 +64,7 @@ function fork(modulePath /* , args, options */) {
let options = {};
let args = [];
let pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
args = arguments[pos++];
}

Expand Down Expand Up @@ -96,7 +97,7 @@ function fork(modulePath /* , args, options */) {

if (typeof options.stdio === 'string') {
options.stdio = stdioStringToArray(options.stdio, 'ipc');
} else if (!Array.isArray(options.stdio)) {
} else if (!ArrayIsArray(options.stdio)) {
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
// and stderr from the parent if silent isn't set.
options.stdio = stdioStringToArray(
Expand Down Expand Up @@ -186,7 +187,7 @@ function execFile(file /* , args, options, callback */) {

// Parse the optional positional parameters.
let pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
args = arguments[pos++];
} else if (pos < arguments.length && arguments[pos] == null) {
pos++;
Expand Down Expand Up @@ -404,7 +405,7 @@ function normalizeSpawnArguments(file, args, options) {
if (file.length === 0)
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');

if (Array.isArray(args)) {
if (ArrayIsArray(args)) {
args = args.slice(0);
} else if (args == null) {
args = [];
Expand Down
4 changes: 3 additions & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'use strict';

const {
Array,
ArrayIsArray,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -592,7 +594,7 @@ Socket.prototype.send = function(buffer,
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
}

if (!Array.isArray(buffer)) {
if (!ArrayIsArray(buffer)) {
if (typeof buffer === 'string') {
list = [ Buffer.from(buffer) ];
} else if (!isUint8Array(buffer)) {
Expand Down
1 change: 1 addition & 0 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// unless they address existing, critical bugs.

const {
Array,
ObjectDefineProperty,
ReflectApply,
} = primordials;
Expand Down
1 change: 1 addition & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
Array,
MathMin,
ObjectCreate,
ObjectDefineProperty,
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -357,7 +358,7 @@ ChildProcess.prototype.spawn = function(options) {
// Let child process know about opened IPC channel
if (options.envPairs === undefined)
options.envPairs = [];
else if (!Array.isArray(options.envPairs)) {
else if (!ArrayIsArray(options.envPairs)) {
throw new ERR_INVALID_ARG_TYPE('options.envPairs',
'Array',
options.envPairs);
Expand All @@ -370,7 +371,7 @@ ChildProcess.prototype.spawn = function(options) {
validateString(options.file, 'options.file');
this.spawnfile = options.file;

if (Array.isArray(options.args))
if (ArrayIsArray(options.args))
this.spawnargs = options.args;
else if (options.args === undefined)
this.spawnargs = [];
Expand Down Expand Up @@ -606,7 +607,7 @@ function setupChannel(target, channel, serializationMode) {
}
}

assert(Array.isArray(target._handleQueue));
assert(ArrayIsArray(target._handleQueue));
const queue = target._handleQueue;
target._handleQueue = null;

Expand Down Expand Up @@ -905,7 +906,7 @@ function getValidStdio(stdio, sync) {
// Replace shortcut with an array
if (typeof stdio === 'string') {
stdio = stdioStringToArray(stdio);
} else if (!Array.isArray(stdio)) {
} else if (!ArrayIsArray(stdio)) {
throw new ERR_INVALID_OPT_VALUE('stdio', inspect(stdio));
}

Expand Down
7 changes: 2 additions & 5 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// console. It's exported for backwards compatibility.

const {
ArrayFrom,
ArrayIsArray,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectKeys,
Expand Down Expand Up @@ -39,11 +41,6 @@ const kTraceBegin = 'b'.charCodeAt(0);
const kTraceEnd = 'e'.charCodeAt(0);
const kTraceInstant = 'n'.charCodeAt(0);

const {
isArray: ArrayIsArray,
from: ArrayFrom,
} = Array;

// Lazy loaded for startup performance.
let cliTable;

Expand Down
7 changes: 6 additions & 1 deletion lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
'use strict';

const {
ArrayIsArray,
} = primordials;

const errors = require('internal/errors');
const { isIP } = require('internal/net');
const {
Expand Down Expand Up @@ -38,7 +43,7 @@ class Resolver {
}

setServers(servers) {
if (!Array.isArray(servers)) {
if (!ArrayIsArray(servers)) {
throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// message may change, the code should not.

const {
ArrayIsArray,
MathAbs,
ObjectDefineProperty,
ObjectKeys,
Expand Down Expand Up @@ -611,7 +612,7 @@ function isStackOverflowError(err) {

function oneOf(expected, thing) {
assert(typeof thing === 'string', '`thing` has to be of type string');
if (Array.isArray(expected)) {
if (ArrayIsArray(expected)) {
const len = expected.length;
assert(len > 0,
'At least one expected value needs to be specified');
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/fixed_queue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

const {
Array,
} = primordials;

// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
const kSize = 2048;
const kMask = kSize - 1;
Expand Down
1 change: 1 addition & 0 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
Array,
MathMin,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
ObjectSetPrototypeOf,
ReflectOwnKeys,
} = primordials;
Expand Down Expand Up @@ -542,7 +543,7 @@ const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
});

const validateBufferArray = hideStackFrames((buffers, propName = 'buffers') => {
if (!Array.isArray(buffers))
if (!ArrayIsArray(buffers))
throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers);

for (let i = 0; i < buffers.length; i++) {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
ObjectAssign,
ObjectCreate,
ObjectKeys,
Expand Down Expand Up @@ -617,7 +618,7 @@ class Http2ServerResponse extends Stream {
headers = statusMessage;

let i;
if (Array.isArray(headers)) {
if (ArrayIsArray(headers)) {
for (i = 0; i < headers.length; i++) {
const header = headers[i];
this[kSetHeader](header[0], header[1]);
Expand Down
Loading

0 comments on commit 9260844

Please sign in to comment.