Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) (anonymous)]> { a: true }'
'Object <[Function (null prototype) (anonymous)]> { a: true }'
);
assert.strictEqual(
util.inspect(obj, { colors: true }),
'<\u001b[36m[Function (null prototype) (anonymous)]\u001b[39m> ' +
'Object <\u001b[36m[Function (null prototype) (anonymous)]\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