Skip to content

Commit

Permalink
util: inspect constructor closer
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
BridgeAR authored and targos committed May 6, 2019
1 parent 7b5bd93 commit 95498df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
kPending,
kRejected,
previewEntries,
getConstructorName: internalGetConstructorName,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
Expand Down Expand Up @@ -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 &&
Expand All @@ -367,7 +369,7 @@ function getConstructorName(obj, ctx) {
return null;
}

return `<${inspect(firstProto, {
return `${internalGetConstructorName(tmp)} <${inspect(firstProto, {
...ctx,
customInspect: false
})}>`;
Expand Down
11 changes: 11 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }'
);

Expand All @@ -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];
Expand All @@ -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 }"
);
}

Expand Down

0 comments on commit 95498df

Please sign in to comment.