From 21c0a0e6e4a8a2de5b3e27ece84efe09235a0e8f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 20 Dec 2018 17:35:40 +0100 Subject: [PATCH 1/4] bootstrap: make sure all type checks exist early Combine all type checks on the internal types binding during bootstrapping. This makes sure all of those checks exist early on and not only after loading the util module. --- lib/internal/bootstrap/node.js | 3 ++- lib/internal/util/inspect.js | 14 +++++++------- lib/util.js | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 33d79689d0993f..9ee74eb159ae52 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -187,13 +187,14 @@ function startup() { NativeModule.require('internal/process/esm_loader').setup(); } + const types = internalBinding('types'); + Object.assign(types, NativeModule.require('internal/util/types')); const { deprecate } = NativeModule.require('internal/util'); { // Install legacy getters on the `util` binding for typechecking. // TODO(addaleax): Turn into a full runtime deprecation. const pendingDeprecation = getOptionValue('--pending-deprecation'); const utilBinding = internalBinding('util'); - const types = internalBinding('types'); for (const name of [ 'isArrayBuffer', 'isArrayBufferView', 'isAsyncFunction', 'isDataView', 'isDate', 'isExternal', 'isMap', 'isMapIterator', diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 256a4a8b06904b..671e4bfec3e191 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -27,8 +27,6 @@ const { isStackOverflowError } = require('internal/errors'); -const types = internalBinding('types'); -Object.assign(types, require('internal/util/types')); const { isAnyArrayBuffer, isArrayBuffer, @@ -38,6 +36,8 @@ const { isExternal, isMap, isMapIterator, + isModuleNamespaceObject, + isNativeError, isPromise, isSet, isSetIterator, @@ -61,7 +61,7 @@ const { isFloat64Array, isBigInt64Array, isBigUint64Array -} = types; +} = internalBinding('types'); const ReflectApply = Reflect.apply; @@ -385,9 +385,9 @@ function getKeys(value, showHidden) { try { keys = Object.keys(value); } catch (err) { - if (types.isNativeError(err) && + if (isNativeError(err) && err.name === 'ReferenceError' && - types.isModuleNamespaceObject(value)) { + isModuleNamespaceObject(value)) { keys = Object.getOwnPropertyNames(value); } else { throw err; @@ -690,7 +690,7 @@ function formatRaw(ctx, value, recurseTimes) { } else if (isWeakMap(value)) { braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`; formatter = ctx.showHidden ? formatWeakMap : formatWeakCollection; - } else if (types.isModuleNamespaceObject(value)) { + } else if (isModuleNamespaceObject(value)) { braces[0] = `[${tag}] {`; formatter = formatNamespaceObject; skip = true; @@ -877,7 +877,7 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) { output[i] = formatProperty(ctx, value, recurseTimes, keys[i], kObjectType); } catch (err) { - if (!(types.isNativeError(err) && err.name === 'ReferenceError')) { + if (!(isNativeError(err) && err.name === 'ReferenceError')) { throw err; } // Use the existing functionality. This makes sure the indentation and diff --git a/lib/util.js b/lib/util.js index f48d93d124738a..a47fbe8d15319f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -33,7 +33,6 @@ const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; const types = internalBinding('types'); -Object.assign(types, require('internal/util/types')); const { isRegExp, isDate, From 8c06fd3115c1a1b0bcffdc17d071485788160e1f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 20 Dec 2018 17:36:57 +0100 Subject: [PATCH 2/4] console: use spread notation instead of Object.assign --- lib/internal/console/constructor.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index e10eef74c243b7..84339310f0b731 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -434,11 +434,15 @@ Console.prototype.table = function(tabularData, properties) { const final = (k, v) => this.log(cliTable(k, v)); const inspect = (v) => { - const opt = { depth: 0, maxArrayLength: 3 }; - if (v !== null && typeof v === 'object' && - !isArray(v) && ObjectKeys(v).length > 2) - opt.depth = -1; - Object.assign(opt, this[kGetInspectOptions](this._stdout)); + const depth = v !== null && + typeof v === 'object' && + !isArray(v) && + ObjectKeys(v).length > 2 ? -1 : 0; + const opt = { + depth, + maxArrayLength: 3, + ...this[kGetInspectOptions](this._stdout) + }; return util.inspect(v, opt); }; const getIndexArray = (length) => ArrayFrom({ length }, (_, i) => inspect(i)); From 8f562133e13842c6ee2c2c01b0297066cfbbc7ac Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 23 Dec 2018 23:09:31 +0100 Subject: [PATCH 3/4] fixup --- lib/internal/bootstrap/node.js | 6 ++++-- lib/util.js | 8 ++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 9ee74eb159ae52..d5f5c05a1aa54e 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -31,6 +31,10 @@ const { internalBinding, NativeModule } = loaderExports; const exceptionHandlerState = { captureFn: null }; function startup() { + // Do this early so all type checks are accessible from everywhere. + const types = internalBinding('types'); + Object.assign(types, NativeModule.require('internal/util/types')); + setupTraceCategoryState(); setupProcessObject(); @@ -187,8 +191,6 @@ function startup() { NativeModule.require('internal/process/esm_loader').setup(); } - const types = internalBinding('types'); - Object.assign(types, NativeModule.require('internal/util/types')); const { deprecate } = NativeModule.require('internal/util'); { // Install legacy getters on the `util` binding for typechecking. diff --git a/lib/util.js b/lib/util.js index a47fbe8d15319f..f606487392d329 100644 --- a/lib/util.js +++ b/lib/util.js @@ -33,10 +33,6 @@ const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; const types = internalBinding('types'); -const { - isRegExp, - isDate, -} = types; const { deprecate, @@ -431,9 +427,9 @@ module.exports = exports = { isString, isSymbol, isUndefined, - isRegExp, + isRegExp: types.isRegExp, isObject, - isDate, + isDate: types.isDate, isError(e) { return objectToString(e) === '[object Error]' || e instanceof Error; }, From 394bfb9074401fd664ab9f9ddb34cbf34be97312 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 24 Dec 2018 01:12:46 +0100 Subject: [PATCH 4/4] fixup: refactor internal types Now all type checks should be required loading 'internal/util/types'. --- lib/buffer.js | 2 +- lib/internal/bootstrap/node.js | 5 +---- lib/internal/encoding.js | 7 +++---- lib/internal/util.js | 2 +- lib/internal/util/comparisons.js | 4 ++-- lib/internal/util/inspect.js | 2 +- lib/internal/util/types.js | 1 + lib/util.js | 3 +-- 8 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 019969acd1ad97..743a068dc252a7 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -37,7 +37,6 @@ const { kMaxLength, kStringMaxLength } = internalBinding('buffer'); -const { isAnyArrayBuffer } = internalBinding('types'); const { customInspectSymbol, isInsideNodeModules, @@ -45,6 +44,7 @@ const { kIsEncodingSymbol } = require('internal/util'); const { + isAnyArrayBuffer, isArrayBufferView, isUint8Array } = require('internal/util/types'); diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index d5f5c05a1aa54e..6dfa6c55e7fc2b 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -31,10 +31,6 @@ const { internalBinding, NativeModule } = loaderExports; const exceptionHandlerState = { captureFn: null }; function startup() { - // Do this early so all type checks are accessible from everywhere. - const types = internalBinding('types'); - Object.assign(types, NativeModule.require('internal/util/types')); - setupTraceCategoryState(); setupProcessObject(); @@ -197,6 +193,7 @@ function startup() { // TODO(addaleax): Turn into a full runtime deprecation. const pendingDeprecation = getOptionValue('--pending-deprecation'); const utilBinding = internalBinding('util'); + const types = NativeModule.require('internal/util/types'); for (const name of [ 'isArrayBuffer', 'isArrayBufferView', 'isAsyncFunction', 'isDataView', 'isDate', 'isExternal', 'isMap', 'isMapIterator', diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index ad056fd0bd997f..0084c19b904a82 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -21,11 +21,10 @@ const { customInspectSymbol: inspect } = require('internal/util'); -const { isArrayBufferView } = require('internal/util/types'); - const { - isArrayBuffer -} = internalBinding('types'); + isArrayBuffer, + isArrayBufferView +} = require('internal/util/types'); const { encodeUtf8String diff --git a/lib/internal/util.js b/lib/internal/util.js index 7af06351c91406..35239a7655bc81 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -14,7 +14,7 @@ const { } = internalBinding('util'); const { isNativeError -} = internalBinding('types'); +} = require('internal/util/types'); const { errmap } = internalBinding('uv'); diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 1d959f843bcbd3..d0726089063852 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -1,9 +1,9 @@ 'use strict'; const { compare } = internalBinding('buffer'); -const { isArrayBufferView } = require('internal/util/types'); const { isAnyArrayBuffer, + isArrayBufferView, isDate, isMap, isRegExp, @@ -15,7 +15,7 @@ const { isBooleanObject, isBigIntObject, isSymbolObject -} = internalBinding('types'); +} = require('internal/util/types'); const { getOwnNonIndexProperties, propertyFilter: { diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 671e4bfec3e191..54e3a1d3ab39c3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -61,7 +61,7 @@ const { isFloat64Array, isBigInt64Array, isBigUint64Array -} = internalBinding('types'); +} = require('internal/util/types'); const ReflectApply = Reflect.apply; diff --git a/lib/internal/util/types.js b/lib/internal/util/types.js index 182625a9716c72..865818c661b78d 100644 --- a/lib/internal/util/types.js +++ b/lib/internal/util/types.js @@ -70,6 +70,7 @@ function isBigUint64Array(value) { } module.exports = { + ...internalBinding('types'), isArrayBufferView, isTypedArray, isUint8Array, diff --git a/lib/util.js b/lib/util.js index f606487392d329..1eaf958c3939ba 100644 --- a/lib/util.js +++ b/lib/util.js @@ -31,8 +31,7 @@ const { const { validateNumber } = require('internal/validators'); const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; - -const types = internalBinding('types'); +const types = require('internal/util/types'); const { deprecate,