Skip to content

Commit 95498df

Browse files
BridgeARtargos
authored andcommitted
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: #27522 Backport-PR-URL: #27570 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent 7b5bd93 commit 95498df

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

lib/internal/util/inspect.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
kPending,
2929
kRejected,
3030
previewEntries,
31+
getConstructorName: internalGetConstructorName,
3132
propertyFilter: {
3233
ALL_PROPERTIES,
3334
ONLY_ENUMERABLE
@@ -349,6 +350,7 @@ function getEmptyFormatArray() {
349350

350351
function getConstructorName(obj, ctx) {
351352
let firstProto;
353+
const tmp = obj;
352354
while (obj) {
353355
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
354356
if (descriptor !== undefined &&
@@ -367,7 +369,7 @@ function getConstructorName(obj, ctx) {
367369
return null;
368370
}
369371

370-
return `<${inspect(firstProto, {
372+
return `${internalGetConstructorName(tmp)} <${inspect(firstProto, {
371373
...ctx,
372374
customInspect: false
373375
})}>`;

src/node_util.cc

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ static void GetOwnNonIndexProperties(
5858
args.GetReturnValue().Set(properties);
5959
}
6060

61+
static void GetConstructorName(
62+
const FunctionCallbackInfo<Value>& args) {
63+
CHECK(args[0]->IsObject());
64+
65+
Local<Object> object = args[0].As<Object>();
66+
Local<String> name = object->GetConstructorName();
67+
68+
args.GetReturnValue().Set(name);
69+
}
70+
6171
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
6272
// Return undefined if it's not a Promise.
6373
if (!args[0]->IsPromise())
@@ -262,6 +272,7 @@ void Initialize(Local<Object> target,
262272
env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
263273
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
264274
GetOwnNonIndexProperties);
275+
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
265276

266277
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
267278
Local<Object> constants = Object::New(env->isolate());

test/parallel/test-util-inspect.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2003,11 +2003,11 @@ assert.strictEqual(
20032003
Object.setPrototypeOf(obj, value);
20042004
assert.strictEqual(
20052005
util.inspect(obj),
2006-
'<[Function (null prototype)]> { a: true }'
2006+
'Object <[Function (null prototype)]> { a: true }'
20072007
);
20082008
assert.strictEqual(
20092009
util.inspect(obj, { colors: true }),
2010-
'<\u001b[36m[Function (null prototype)]\u001b[39m> ' +
2010+
'Object <\u001b[36m[Function (null prototype)]\u001b[39m> ' +
20112011
'{ a: \u001b[33mtrue\u001b[39m }'
20122012
);
20132013

@@ -2017,14 +2017,14 @@ assert.strictEqual(
20172017
Object.setPrototypeOf(obj, value);
20182018
assert.strictEqual(
20192019
util.inspect(obj),
2020-
'<[Array: null prototype] []> { a: true }'
2020+
'Object <[Array: null prototype] []> { a: true }'
20212021
);
20222022

20232023
function StorageObject() {}
20242024
StorageObject.prototype = Object.create(null);
20252025
assert.strictEqual(
20262026
util.inspect(new StorageObject()),
2027-
'<[Object: null prototype] {}> {}'
2027+
'StorageObject <[Object: null prototype] {}> {}'
20282028
);
20292029

20302030
obj = [1, 2, 3];
@@ -2034,7 +2034,7 @@ assert.strictEqual(
20342034
Object.setPrototypeOf(obj, Object.create(null));
20352035
assert.strictEqual(
20362036
inspect(obj),
2037-
"<[Object: null prototype] {}> { '0': 1, '1': 2, '2': 3 }"
2037+
"Array <[Object: null prototype] {}> { '0': 1, '1': 2, '2': 3 }"
20382038
);
20392039
}
20402040

0 commit comments

Comments
 (0)