diff options
author | Michaël Zasso <targos@protonmail.com> | 2019-04-05 11:11:26 +0200 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2019-04-08 11:23:09 +0200 |
commit | 112cc7c27551254aa2b17098fb774867f05ed0d9 (patch) | |
tree | 0f48ef3ac9e8949f0bf49af38fb6dc7c6e2f64af /lib/internal/util/inspect.js | |
parent | 969bd1eb7b56fda3573ad3d41745a491f2b06dde (diff) | |
download | android-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.tar.gz android-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.tar.bz2 android-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.zip |
lib: use safe methods from primordials
This changes the primordials to expose built-in prototypes with their
methods already uncurried.
The uncurryThis function is therefore moved to the primordials.
All uses of uncurryThis on built-ins are changed to import the relevant
prototypes from primordials.
All uses of Function.call.bind are also changed to use primordials.
PR-URL: https://github.com/nodejs/node/pull/27096
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'lib/internal/util/inspect.js')
-rw-r--r-- | lib/internal/util/inspect.js | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 8131166417..e5a8b2ecb6 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1,6 +1,25 @@ 'use strict'; -const { JSON, Math } = primordials; +const { + BigIntPrototype, + BooleanPrototype, + DatePrototype, + ErrorPrototype, + JSON, + MapPrototype, + Math, + NumberPrototype, + Object, + ObjectPrototype: { + hasOwnProperty, + propertyIsEnumerable + }, + RegExpPrototype, + SetPrototype, + StringPrototype, + SymbolPrototype, + uncurryThis +} = primordials; const { getOwnNonIndexProperties, @@ -19,8 +38,7 @@ const { customInspectSymbol, isError, join, - removeColors, - uncurryThis + removeColors } = require('internal/util'); const { @@ -68,25 +86,6 @@ const { const assert = require('internal/assert'); -// Avoid monkey-patched built-ins. -const { Object } = primordials; - -const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable); -const regExpToString = uncurryThis(RegExp.prototype.toString); -const dateToISOString = uncurryThis(Date.prototype.toISOString); -const dateToString = uncurryThis(Date.prototype.toString); -const errorToString = uncurryThis(Error.prototype.toString); - -const bigIntValueOf = uncurryThis(BigInt.prototype.valueOf); -const booleanValueOf = uncurryThis(Boolean.prototype.valueOf); -const numberValueOf = uncurryThis(Number.prototype.valueOf); -const symbolValueOf = uncurryThis(Symbol.prototype.valueOf); -const stringValueOf = uncurryThis(String.prototype.valueOf); - -const setValues = uncurryThis(Set.prototype.values); -const mapEntries = uncurryThis(Map.prototype.entries); -const dateGetTime = uncurryThis(Date.prototype.getTime); -const hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty); let hexSlice; const inspectDefaultOptions = Object.seal({ @@ -475,10 +474,10 @@ function noPrototypeIterator(ctx, value, recurseTimes) { let newVal; if (isSet(value)) { const clazz = clazzWithNullPrototype(Set, 'Set'); - newVal = new clazz(setValues(value)); + newVal = new clazz(SetPrototype.values(value)); } else if (isMap(value)) { const clazz = clazzWithNullPrototype(Map, 'Map'); - newVal = new clazz(mapEntries(value)); + newVal = new clazz(MapPrototype.entries(value)); } else if (Array.isArray(value)) { const clazz = clazzWithNullPrototype(Array, 'Array'); newVal = new clazz(value.length); @@ -650,7 +649,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { base = `[${name}]`; } else if (isRegExp(value)) { // Make RegExps say that they are RegExps - base = regExpToString(constructor !== null ? value : new RegExp(value)); + base = RegExpPrototype.toString( + constructor !== null ? value : new RegExp(value) + ); const prefix = getPrefix(constructor, tag, 'RegExp'); if (prefix !== 'RegExp ') base = `${prefix}${base}`; @@ -658,9 +659,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { return ctx.stylize(base, 'regexp'); } else if (isDate(value)) { // Make dates with properties first say the date - base = Number.isNaN(dateGetTime(value)) ? - dateToString(value) : - dateToISOString(value); + base = Number.isNaN(DatePrototype.getTime(value)) ? + DatePrototype.toString(value) : + DatePrototype.toISOString(value); const prefix = getPrefix(constructor, tag, 'Date'); if (prefix !== 'Date ') base = `${prefix}${base}`; @@ -705,23 +706,25 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { } else if (isBoxedPrimitive(value)) { let type; if (isNumberObject(value)) { - base = `[Number: ${getBoxedValue(numberValueOf(value))}]`; + base = `[Number: ${getBoxedValue(NumberPrototype.valueOf(value))}]`; type = 'number'; } else if (isStringObject(value)) { - base = `[String: ${getBoxedValue(stringValueOf(value), ctx)}]`; + base = `[String: ${ + getBoxedValue(StringPrototype.valueOf(value), ctx) + }]`; type = 'string'; // For boxed Strings, we have to remove the 0-n indexed entries, // since they just noisy up the output and are redundant // Make boxed primitive Strings look like such keys = keys.slice(value.length); } else if (isBooleanObject(value)) { - base = `[Boolean: ${getBoxedValue(booleanValueOf(value))}]`; + base = `[Boolean: ${getBoxedValue(BooleanPrototype.valueOf(value))}]`; type = 'boolean'; } else if (isBigIntObject(value)) { - base = `[BigInt: ${getBoxedValue(bigIntValueOf(value))}]`; + base = `[BigInt: ${getBoxedValue(BigIntPrototype.valueOf(value))}]`; type = 'bigint'; } else { - base = `[Symbol: ${getBoxedValue(symbolValueOf(value))}]`; + base = `[Symbol: ${getBoxedValue(SymbolPrototype.valueOf(value))}]`; type = 'symbol'; } if (keys.length === 0) { @@ -832,7 +835,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { function formatError(err, constructor, tag, ctx) { // TODO(BridgeAR): Always show the error code if present. - let stack = err.stack || errorToString(err); + let stack = err.stack || ErrorPrototype.toString(err); // A stack trace may contain arbitrary data. Only manipulate the output // for "regular errors" (errors that "look normal") for now. |