summaryrefslogtreecommitdiff
path: root/lib/internal/util/inspect.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-04-14 21:37:03 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2019-05-02 23:22:38 +0200
commitd0667e814e8be53d329a9c7f4849996c192395c9 (patch)
treeb9bd25e8aac5995f464f732c98e27912979a6258 /lib/internal/util/inspect.js
parent57fd70fc7d3918a6bd4c714fe479488391a563f6 (diff)
downloadandroid-node-v8-d0667e814e8be53d329a9c7f4849996c192395c9.tar.gz
android-node-v8-d0667e814e8be53d329a9c7f4849996c192395c9.tar.bz2
android-node-v8-d0667e814e8be53d329a9c7f4849996c192395c9.zip
util: improve function inspection
This commit contains the following changes: 1) Add null prototype support for functions. 2) Safely detect async and generator functions. 3) Mark anonymous functions as such instead of just leaving out the name. PR-URL: https://github.com/nodejs/node/pull/27227 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/internal/util/inspect.js')
-rw-r--r--lib/internal/util/inspect.js37
1 files changed, 30 insertions, 7 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index fccb46085d..8ffa303268 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -49,6 +49,8 @@ const {
} = require('internal/errors');
const {
+ isAsyncFunction,
+ isGeneratorFunction,
isAnyArrayBuffer,
isArrayBuffer,
isArgumentsObject,
@@ -642,14 +644,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
return `${braces[0]}}`;
}
} else if (typeof value === 'function') {
- const type = constructor || tag || 'Function';
- let name = `${type}`;
- if (value.name && typeof value.name === 'string') {
- name += `: ${value.name}`;
- }
+ base = getFunctionBase(value, constructor, tag);
if (keys.length === 0)
- return ctx.stylize(`[${name}]`, 'special');
- base = `[${name}]`;
+ return ctx.stylize(base, 'special');
} else if (isRegExp(value)) {
// Make RegExps say that they are RegExps
base = RegExpPrototype.toString(
@@ -834,6 +831,32 @@ function getBoxedBase(value, ctx, keys, constructor, tag) {
return ctx.stylize(base, type.toLowerCase());
}
+function getFunctionBase(value, constructor, tag) {
+ let type = 'Function';
+ if (isAsyncFunction(value)) {
+ type = 'AsyncFunction';
+ } else if (isGeneratorFunction(value)) {
+ type = 'GeneratorFunction';
+ }
+ let base = `[${type}`;
+ if (constructor === null) {
+ base += ' (null prototype)';
+ }
+ if (value.name === '') {
+ base += ' (anonymous)';
+ } else {
+ base += `: ${value.name}`;
+ }
+ base += ']';
+ if (constructor !== type && constructor !== null) {
+ base += ` ${constructor}`;
+ }
+ if (tag !== '' && constructor !== tag) {
+ base += ` [${tag}]`;
+ }
+ return base;
+}
+
function formatError(err, constructor, tag, ctx) {
// TODO(BridgeAR): Always show the error code if present.
let stack = err.stack || ErrorPrototype.toString(err);