lib: enforce the use of Object from primordials

PR-URL: https://github.com/nodejs/node/pull/27146
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
This commit is contained in:
Michaël Zasso 2019-04-09 09:55:53 +02:00 committed by Daniel Bevenius
parent 9ad5106934
commit 908292cf1f
85 changed files with 167 additions and 64 deletions

View File

@ -10,6 +10,8 @@ rules:
message: "Use `const { JSON } = primordials;` instead of the global."
- name: Math
message: "Use `const { Math } = primordials;` instead of the global."
- name: Object
message: "Use `const { Object } = primordials;` instead of the global."
- name: Reflect
message: "Use `const { Reflect } = primordials;` instead of the global."
no-restricted-syntax:

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const net = require('net');
const EventEmitter = require('events');
const debug = require('internal/util/debuglog').debuglog('http');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const net = require('net');
const url = require('url');
const assert = require('internal/assert');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const Stream = require('stream');
function readStart(socket) {

View File

@ -21,7 +21,7 @@
'use strict';
const { ObjectPrototype } = primordials;
const { Object, ObjectPrototype } = primordials;
const assert = require('internal/assert');
const Stream = require('stream');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const net = require('net');
const assert = require('internal/assert');
const {

View File

@ -26,6 +26,8 @@
'use strict';
const { Object } = primordials;
module.exports = Duplex;
const Readable = require('_stream_readable');

View File

@ -25,6 +25,8 @@
'use strict';
const { Object } = primordials;
module.exports = PassThrough;
const Transform = require('_stream_transform');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
module.exports = Readable;
Readable.ReadableState = ReadableState;

View File

@ -63,6 +63,8 @@
'use strict';
const { Object } = primordials;
module.exports = Transform;
const {
ERR_METHOD_NOT_IMPLEMENTED,

View File

@ -25,6 +25,8 @@
'use strict';
const { Object } = primordials;
module.exports = Writable;
Writable.WritableState = WritableState;

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const { parseCertString } = require('internal/tls');
const { isArrayBufferView } = require('internal/util/types');
const tls = require('tls');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const {
assertCrypto,
deprecate

View File

@ -20,6 +20,8 @@
'use strict';
const { Object } = primordials;
const { Buffer } = require('buffer');
const { codes: {
ERR_AMBIGUOUS_ARGUMENT,

View File

@ -21,7 +21,7 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const {
byteLengthUtf8,

View File

@ -21,6 +21,8 @@
'use strict';
const { Object, ObjectPrototype } = primordials;
const {
promisify,
convertToValidSignal,
@ -515,8 +517,7 @@ function normalizeSpawnArguments(file, args, options) {
// process.env.NODE_V8_COVERAGE always propagates, making it possible to
// collect coverage for programs that spawn with white-listed environment.
if (process.env.NODE_V8_COVERAGE &&
!Object.prototype.hasOwnProperty.call(options.env || {},
'NODE_V8_COVERAGE')) {
!ObjectPrototype.hasOwnProperty(options.env || {}, 'NODE_V8_COVERAGE')) {
env.NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE;
}

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
// This module is deprecated in documentation only. Users should be directed
// towards using the specific constants exposed by the individual modules on
// which they are most relevant.

View File

@ -24,6 +24,8 @@
'use strict';
const { Object } = primordials;
const {
assertCrypto,
deprecate

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const errors = require('internal/errors');
const {
kStateSymbol,

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const cares = internalBinding('cares_wrap');
const { toASCII } = require('internal/idna');
const { isIP, isIPv4, isLegalPort } = require('internal/net');

View File

@ -26,7 +26,7 @@
// No new pull requests targeting this module will be accepted
// unless they address existing, critical bugs.
const { Reflect } = primordials;
const { Object, Reflect } = primordials;
const util = require('util');
const EventEmitter = require('events');

View File

@ -21,7 +21,7 @@
'use strict';
const { Math, Reflect } = primordials;
const { Math, Object, Reflect } = primordials;
var spliceOne;

View File

@ -24,7 +24,7 @@
'use strict';
const { Math, Reflect } = primordials;
const { Math, Object, Reflect } = primordials;
const { fs: constants } = internalBinding('constants');
const {

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const httpAgent = require('_http_agent');
const { ClientRequest } = require('_http_client');
const { methods } = require('_http_common');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
require('internal/util').assertCrypto();
const tls = require('tls');

View File

@ -1,6 +1,6 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const { inspect } = require('internal/util/inspect');
const { codes: {

View File

@ -1,6 +1,6 @@
'use strict';
const { FunctionPrototype, Reflect } = primordials;
const { FunctionPrototype, Object, Reflect } = primordials;
const {
ERR_ASYNC_TYPE,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const { getOptionValue } = require('internal/options');
const { Buffer } = require('buffer');

View File

@ -1,6 +1,6 @@
'use strict';
const { JSON } = primordials;
const { JSON, Object } = primordials;
const {
errnoException,

View File

@ -1,4 +1,7 @@
'use strict';
const { Object } = primordials;
const assert = require('internal/assert');
const path = require('path');
const EventEmitter = require('events');

View File

@ -1,4 +1,7 @@
'use strict';
const { Object } = primordials;
const assert = require('internal/assert');
const { fork } = require('child_process');
const path = require('path');
@ -8,7 +11,6 @@ const SharedHandle = require('internal/cluster/shared_handle');
const Worker = require('internal/cluster/worker');
const { internal, sendHelper } = require('internal/cluster/utils');
const { ERR_SOCKET_BAD_PORT } = require('internal/errors').codes;
const keys = Object.keys;
const cluster = new EventEmitter();
const intercom = new EventEmitter();
const SCHED_NONE = 1;
@ -141,7 +143,7 @@ function removeWorker(worker) {
assert(worker);
delete cluster.workers[worker.id];
if (keys(cluster.workers).length === 0) {
if (Object.keys(cluster.workers).length === 0) {
assert(handles.size === 0, 'Resource leak detected.');
intercom.emit('disconnect');
}
@ -219,7 +221,7 @@ function emitForkNT(worker) {
}
cluster.disconnect = function(cb) {
const workers = keys(cluster.workers);
const workers = Object.keys(cluster.workers);
if (workers.length === 0) {
process.nextTick(() => intercom.emit('disconnect'));

View File

@ -1,4 +1,7 @@
'use strict';
const { Object } = primordials;
const EventEmitter = require('events');
module.exports = Worker;

View File

@ -3,7 +3,7 @@
// The Console constructor is not actually used to construct the global
// console. It's exported for backwards compatibility.
const { ObjectPrototype, Reflect } = primordials;
const { Object, ObjectPrototype, Reflect } = primordials;
const { trace } = internalBinding('trace_events');
const {
@ -32,11 +32,6 @@ const kTraceBegin = 'b'.charCodeAt(0);
const kTraceEnd = 'e'.charCodeAt(0);
const kTraceInstant = 'n'.charCodeAt(0);
const {
keys: ObjectKeys,
values: ObjectValues,
} = Object;
const {
isArray: ArrayIsArray,
from: ArrayFrom,
@ -414,7 +409,7 @@ const consoleMethods = {
const depth = v !== null &&
typeof v === 'object' &&
!isArray(v) &&
ObjectKeys(v).length > 2 ? -1 : 0;
Object.keys(v).length > 2 ? -1 : 0;
const opt = {
depth,
maxArrayLength: 3,
@ -478,7 +473,7 @@ const consoleMethods = {
const map = {};
let hasPrimitives = false;
const valuesKeyArray = [];
const indexKeyArray = ObjectKeys(tabularData);
const indexKeyArray = Object.keys(tabularData);
for (; i < indexKeyArray.length; i++) {
const item = tabularData[indexKeyArray[i]];
@ -488,7 +483,7 @@ const consoleMethods = {
hasPrimitives = true;
valuesKeyArray[i] = _inspect(item);
} else {
const keys = properties || ObjectKeys(item);
const keys = properties || Object.keys(item);
for (const key of keys) {
if (map[key] === undefined)
map[key] = [];
@ -501,8 +496,8 @@ const consoleMethods = {
}
}
const keys = ObjectKeys(map);
const values = ObjectValues(map);
const keys = Object.keys(map);
const values = Object.values(map);
if (hasPrimitives) {
keys.push(valuesKey);
values.push(valuesKeyArray);

View File

@ -12,7 +12,7 @@
// Therefore, the console.Console.prototype is not
// in the global console prototype chain anymore.
const { Reflect } = primordials;
const { Object, Reflect } = primordials;
const {
Console,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
RSA_PKCS1_OAEP_PADDING,
RSA_PKCS1_PADDING

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const { Buffer } = require('buffer');
const {
ERR_CRYPTO_ECDH_INVALID_FORMAT,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
Hash: _Hash,
Hmac: _Hmac

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const {
generateKeyPairRSA,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
KeyObject: KeyObjectHandle,
kKeyTypeSecret,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
ERR_CRYPTO_SIGN_KEY_REQUIRED,
ERR_INVALID_ARG_TYPE,

View File

@ -1,4 +1,7 @@
'use strict';
const { Object } = primordials;
const {
bindDefaultResolver,
Resolver: CallbackResolver,

View File

@ -3,6 +3,8 @@
// An implementation of the WHATWG Encoding Standard
// https://encoding.spec.whatwg.org
const { Object } = primordials;
const {
ERR_ENCODING_INVALID_ENCODED_DATA,
ERR_ENCODING_NOT_SUPPORTED,

View File

@ -10,13 +10,14 @@
// value statically and permanently identifies the error. While the error
// message may change, the code should not.
const { Object } = primordials;
const kCode = Symbol('code');
const kInfo = Symbol('info');
const messages = new Map();
const codes = {};
const { kMaxLength } = internalBinding('buffer');
const { defineProperty } = Object;
let excludedStackFn;
@ -100,7 +101,7 @@ class SystemError extends Error {
}
set code(value) {
defineProperty(this, 'code', {
Object.defineProperty(this, 'code', {
configurable: true,
enumerable: true,
value,
@ -188,7 +189,7 @@ function makeNodeErrorWithCode(Base, key) {
}
set code(value) {
defineProperty(this, 'code', {
Object.defineProperty(this, 'code', {
configurable: true,
enumerable: true,
value,
@ -234,7 +235,7 @@ function addCodeToName(err, name, code) {
err.stack;
// Reset the name to the actual name.
if (name === 'SystemError') {
defineProperty(err, 'name', {
Object.defineProperty(err, 'name', {
value: name,
enumerable: false,
writable: true,

View File

@ -1,6 +1,6 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const {
FSReqCallback,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const { Writable } = require('stream');
const { closeSync, writeSync } = require('fs');

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const errors = require('internal/errors');
const {
kFsStatsFieldsNumber,

View File

@ -1,6 +1,6 @@
'use strict';
const { Reflect } = primordials;
const { Object, ObjectPrototype, Reflect } = primordials;
const assert = require('internal/assert');
const Stream = require('stream');
@ -539,7 +539,7 @@ class Http2ServerResponse extends Stream {
hasHeader(name) {
validateString(name, 'name');
name = name.trim().toLowerCase();
return Object.prototype.hasOwnProperty.call(this[kHeaders], name);
return ObjectPrototype.hasOwnProperty(this[kHeaders], name);
}
removeHeader(name) {

View File

@ -2,7 +2,7 @@
/* eslint-disable no-use-before-define */
const { Math, Reflect } = primordials;
const { Math, Object, Reflect } = primordials;
const {
assertCrypto,

View File

@ -1,6 +1,6 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const binding = internalBinding('http2');
const {

View File

@ -3,6 +3,8 @@
// In worker threads, execute the script sent through the
// message port.
const { Object } = primordials;
const {
patchProcessObject,
setupCoverageHooks,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const { validateString } = require('internal/validators');
const path = require('path');
const { pathToFileURL } = require('internal/url');

View File

@ -21,7 +21,7 @@
'use strict';
const { JSON, Reflect } = primordials;
const { JSON, Object, Reflect } = primordials;
const { NativeModule } = require('internal/bootstrap/loaders');
const { pathToFileURL } = require('internal/url');

View File

@ -1,11 +1,13 @@
'use strict';
const { ModuleWrap } = internalBinding('module_wrap');
const {
Object,
SafeSet,
SafePromise
} = primordials;
const { ModuleWrap } = internalBinding('module_wrap');
const { decorateErrorStack } = require('internal/util');
const assert = require('internal/assert');
const resolvedPromise = SafePromise.resolve();

View File

@ -1,5 +1,9 @@
'use strict';
// `per_context` scripts are executed before creating the primordials so we
// cannot use them here.
/* eslint-disable no-restricted-globals */
class ERR_INVALID_THIS extends TypeError {
constructor(type) {
super('Value of "this" must be of ' + type);

View File

@ -5,6 +5,7 @@
// thread and the worker threads.
const {
Object,
RegExpPrototype,
SetPrototype,
StringPrototype

View File

@ -1,6 +1,6 @@
'use strict';
const { JSON, Reflect } = primordials;
const { JSON, Object, Reflect } = primordials;
const {
ERR_MANIFEST_TDZ,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const REPL = require('repl');
const { kStandaloneREPL } = require('internal/repl/utils');

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const acorn = require('internal/deps/acorn/acorn/dist/acorn');
const walk = require('internal/deps/acorn/acorn-walk/dist/walk');

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const finished = require('internal/streams/end-of-stream');
const kLastResolve = Symbol('lastResolve');

View File

@ -3,6 +3,8 @@
// for the stream, one conventional and one non-conventional.
'use strict';
const { Object } = primordials;
const stream = require('stream');
const {

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const EE = require('events');
function Stream() {

View File

@ -72,7 +72,7 @@
// timers within (or creation of a new list). However, these operations combined
// have shown to be trivial in comparison to other timers architectures.
const { Math, Reflect } = primordials;
const { Math, Object, Reflect } = primordials;
const {
scheduleTimer,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
// Example:
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
function parseCertString(s) {

View File

@ -1,9 +1,10 @@
'use strict';
const { Object, SafeMap, SafeSet } = primordials;
const { trace } = internalBinding('trace_events');
const async_wrap = internalBinding('async_wrap');
const async_hooks = require('async_hooks');
const { SafeMap, SafeSet } = primordials;
// Use small letters such that chrome://tracing groups by the name.
// The behavior is not only useful but the same as the events emitted using

View File

@ -1,6 +1,6 @@
'use strict';
const { Reflect } = primordials;
const { Object, Reflect } = primordials;
const { inspect } = require('internal/util/inspect');
const {

View File

@ -1,6 +1,6 @@
'use strict';
const { Reflect } = primordials;
const { Object, Reflect } = primordials;
const {
ERR_INVALID_ARG_TYPE,
@ -33,10 +33,6 @@ function isError(e) {
return isNativeError(e) || e instanceof Error;
}
function objectToString(o) {
return Object.prototype.toString.call(o);
}
// Keep a list of deprecation codes that have been warned on so we only warn on
// each one once.
const codesWarned = {};
@ -402,7 +398,6 @@ module.exports = {
isInsideNodeModules,
join,
normalizeEncoding,
objectToString,
once,
promisify,
spliceOne,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
let session;
function sendInspectorCommand(cb, onError) {
const { hasInspector } = internalBinding('config');

View File

@ -1,6 +1,6 @@
'use strict';
const { uncurryThis } = primordials;
const { Object, uncurryThis } = primordials;
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);

View File

@ -1,5 +1,7 @@
'use strict';
const { Object, SafePromise } = primordials;
const { isModuleNamespaceObject } = require('util').types;
const { URL } = require('internal/url');
const { isContext } = internalBinding('contextify');
@ -17,7 +19,6 @@ const {
customInspectSymbol,
emitExperimentalWarning
} = require('internal/util');
const { SafePromise } = primordials;
const {
validateInt32,
validateUint32,

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const EventEmitter = require('events');
const assert = require('internal/assert');
const path = require('path');

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
handle_onclose: handleOnCloseSymbol,
oninit: onInitSymbol

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const EventEmitter = require('events');
const stream = require('stream');
const { inspect } = require('internal/util/inspect');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const { safeGetenv } = internalBinding('credentials');
const constants = internalBinding('constants').os;
const { deprecate } = require('internal/util');

View File

@ -1,5 +1,7 @@
'use strict';
const { Object } = primordials;
const {
ELDHistogram: _ELDHistogram,
PerformanceEntry,

View File

@ -23,6 +23,8 @@
'use strict';
const { Object } = primordials;
const { Buffer } = require('buffer');
const {
encodeStr,

View File

@ -27,7 +27,7 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const {
ERR_INVALID_CURSOR_POS,

View File

@ -42,7 +42,7 @@
'use strict';
const { Math } = primordials;
const { Math, Object, ObjectPrototype } = primordials;
const {
builtinLibs,
@ -116,13 +116,6 @@ try {
// Hack for repl require to work properly with node_modules folders
module.paths = CJSModule._nodeModulePaths(module.filename);
// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
// This is the default "writer" value, if none is passed in the REPL options,
// and it can be overridden by custom print functions, such as `probe` or
// `eyes.js`.
@ -1263,7 +1256,7 @@ function complete(line, callback) {
group.sort();
for (var j = group.length - 1; j >= 0; j--) {
c = group[j];
if (!hasOwnProperty(uniq, c)) {
if (!ObjectPrototype.hasOwnProperty(uniq, c)) {
completions.unshift(c);
uniq[c] = true;
}

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const { Buffer } = require('buffer');
const pipeline = require('internal/streams/pipeline');
const eos = require('internal/streams/end-of-stream');

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const { Buffer } = require('buffer');
const {
kIncompleteCharactersStart,

View File

@ -21,6 +21,8 @@
'use strict';
const { Object } = primordials;
const net = require('net');
const { TTY, isTTY } = internalBinding('tty_wrap');
const errors = require('internal/errors');

View File

@ -21,9 +21,10 @@
'use strict';
const { Object, SafeSet } = primordials;
const { toASCII } = require('internal/idna');
const { encodeStr, hexTable } = require('internal/querystring');
const { SafeSet } = primordials;
const {
ERR_INVALID_ARG_TYPE

View File

@ -21,7 +21,7 @@
'use strict';
const { ObjectPrototype, Reflect } = primordials;
const { Object, ObjectPrototype, Reflect } = primordials;
const {
codes: {

View File

@ -14,6 +14,8 @@
'use strict';
const { ObjectPrototype } = primordials;
const { Buffer } = require('buffer');
const { validateString } = require('internal/validators');
const {
@ -22,7 +24,6 @@ const {
} = internalBinding('serdes');
const assert = require('internal/assert');
const { copy } = internalBinding('buffer');
const { objectToString } = require('internal/util');
const { FastBuffer } = require('internal/buffer');
const { toPathIfFileURL } = require('internal/url');
const { validatePath } = require('internal/fs/utils');
@ -193,7 +194,7 @@ const arrayBufferViewTypeToIndex = new Map();
{
const dummy = new ArrayBuffer();
for (const [i, ctor] of arrayBufferViewTypes.entries()) {
const tag = objectToString(new ctor(dummy));
const tag = ObjectPrototype.toString(new ctor(dummy));
arrayBufferViewTypeToIndex.set(tag, i);
}
}
@ -212,7 +213,7 @@ class DefaultSerializer extends Serializer {
if (abView.constructor === Buffer) {
i = bufferConstructorIndex;
} else {
const tag = objectToString(abView);
const tag = ObjectPrototype.toString(abView);
i = arrayBufferViewTypeToIndex.get(tag);
if (i === undefined) {

View File

@ -21,7 +21,7 @@
'use strict';
const { Math } = primordials;
const { Math, Object } = primordials;
const {
codes: {

View File

@ -381,12 +381,14 @@ MaybeLocal<Value> StartExecution(Environment* env, const char* main_script_id) {
env->process_string(),
env->require_string(),
env->internal_binding_string(),
env->primordials_string(),
FIXED_ONE_BYTE_STRING(env->isolate(), "markBootstrapComplete")};
std::vector<Local<Value>> arguments = {
env->process_object(),
env->native_module_require(),
env->internal_binding_loader(),
env->primordials(),
env->NewFunctionTemplate(MarkBootstrapComplete)
->GetFunction(env->context())
.ToLocalChecked()};