summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-05-01 22:21:00 +0200
committerMichaël Zasso <targos@protonmail.com>2019-05-06 13:01:00 +0200
commit95498df1cfc887e277c18cd1fce7de9c93a5569e (patch)
tree40eff14d2098cbe8895f0c2a311ad6ae47c32810
parent7b5bd93ced0967ed0d385e96926735bd09c0a817 (diff)
downloadnode-new-95498df1cfc887e277c18cd1fce7de9c93a5569e.tar.gz
util: inspect constructor closer
This adds an extra check to `util.inspect` to closer inspect object constructors in case there's not much other information about the constructor. PR-URL: https://github.com/nodejs/node/pull/27522 Backport-PR-URL: https://github.com/nodejs/node/pull/27570 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
-rw-r--r--lib/internal/util/inspect.js4
-rw-r--r--src/node_util.cc11
-rw-r--r--test/parallel/test-util-inspect.js10
3 files changed, 19 insertions, 6 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index 7f189b5b74..90be075357 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -28,6 +28,7 @@ const {
kPending,
kRejected,
previewEntries,
+ getConstructorName: internalGetConstructorName,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
@@ -349,6 +350,7 @@ function getEmptyFormatArray() {
function getConstructorName(obj, ctx) {
let firstProto;
+ const tmp = obj;
while (obj) {
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined &&
@@ -367,7 +369,7 @@ function getConstructorName(obj, ctx) {
return null;
}
- return `<${inspect(firstProto, {
+ return `${internalGetConstructorName(tmp)} <${inspect(firstProto, {
...ctx,
customInspect: false
})}>`;
diff --git a/src/node_util.cc b/src/node_util.cc
index 180c58c416..0c498e2838 100644
--- a/src/node_util.cc
+++ b/src/node_util.cc
@@ -58,6 +58,16 @@ static void GetOwnNonIndexProperties(
args.GetReturnValue().Set(properties);
}
+static void GetConstructorName(
+ const FunctionCallbackInfo<Value>& args) {
+ CHECK(args[0]->IsObject());
+
+ Local<Object> object = args[0].As<Object>();
+ Local<String> name = object->GetConstructorName();
+
+ args.GetReturnValue().Set(name);
+}
+
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
// Return undefined if it's not a Promise.
if (!args[0]->IsPromise())
@@ -262,6 +272,7 @@ void Initialize(Local<Object> target,
env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
GetOwnNonIndexProperties);
+ env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
Local<Object> constants = Object::New(env->isolate());
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js
index f1d5680d65..4ecb699978 100644
--- a/test/parallel/test-util-inspect.js
+++ b/test/parallel/test-util-inspect.js
@@ -2003,11 +2003,11 @@ assert.strictEqual(
Object.setPrototypeOf(obj, value);
assert.strictEqual(
util.inspect(obj),
- '<[Function (null prototype)]> { a: true }'
+ 'Object <[Function (null prototype)]> { a: true }'
);
assert.strictEqual(
util.inspect(obj, { colors: true }),
- '<\u001b[36m[Function (null prototype)]\u001b[39m> ' +
+ 'Object <\u001b[36m[Function (null prototype)]\u001b[39m> ' +
'{ a: \u001b[33mtrue\u001b[39m }'
);
@@ -2017,14 +2017,14 @@ assert.strictEqual(
Object.setPrototypeOf(obj, value);
assert.strictEqual(
util.inspect(obj),
- '<[Array: null prototype] []> { a: true }'
+ 'Object <[Array: null prototype] []> { a: true }'
);
function StorageObject() {}
StorageObject.prototype = Object.create(null);
assert.strictEqual(
util.inspect(new StorageObject()),
- '<[Object: null prototype] {}> {}'
+ 'StorageObject <[Object: null prototype] {}> {}'
);
obj = [1, 2, 3];
@@ -2034,7 +2034,7 @@ assert.strictEqual(
Object.setPrototypeOf(obj, Object.create(null));
assert.strictEqual(
inspect(obj),
- "<[Object: null prototype] {}> { '0': 1, '1': 2, '2': 3 }"
+ "Array <[Object: null prototype] {}> { '0': 1, '1': 2, '2': 3 }"
);
}