util: inspect all prototypes
It is currently difficult to distinguish multiple objects from each other because the prototype is not properly inspected. From now on all prototypes will be inspected, even if we do not fully know how they will look like / what their shape really is. PR-URL: https://github.com/nodejs/node/pull/24974 Fixes: https://github.com/nodejs/node/issues/24917 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
5f4fa0756b
commit
02b66b5b86
@ -322,7 +322,7 @@ function getEmptyFormatArray() {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConstructorName(obj) {
|
function getConstructorName(obj, ctx) {
|
||||||
let firstProto;
|
let firstProto;
|
||||||
while (obj) {
|
while (obj) {
|
||||||
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
|
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
|
||||||
@ -341,10 +341,11 @@ function getConstructorName(obj) {
|
|||||||
if (firstProto === null) {
|
if (firstProto === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// TODO(BridgeAR): Improve prototype inspection.
|
|
||||||
// We could use inspect on the prototype itself to improve the output.
|
|
||||||
|
|
||||||
return '';
|
return `<${inspect(firstProto, {
|
||||||
|
...ctx,
|
||||||
|
customInspect: false
|
||||||
|
})}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPrefix(constructor, tag, fallback) {
|
function getPrefix(constructor, tag, fallback) {
|
||||||
@ -503,7 +504,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.stop !== undefined) {
|
if (ctx.stop !== undefined) {
|
||||||
const name = getConstructorName(value) || value[Symbol.toStringTag];
|
const name = getConstructorName(value, ctx) || value[Symbol.toStringTag];
|
||||||
return ctx.stylize(`[${name || 'Object'}]`, 'special');
|
return ctx.stylize(`[${name || 'Object'}]`, 'special');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -547,7 +548,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
function formatRaw(ctx, value, recurseTimes) {
|
function formatRaw(ctx, value, recurseTimes) {
|
||||||
let keys;
|
let keys;
|
||||||
|
|
||||||
const constructor = getConstructorName(value);
|
const constructor = getConstructorName(value, ctx);
|
||||||
let tag = value[Symbol.toStringTag];
|
let tag = value[Symbol.toStringTag];
|
||||||
if (typeof tag !== 'string')
|
if (typeof tag !== 'string')
|
||||||
tag = '';
|
tag = '';
|
||||||
|
@ -1738,19 +1738,34 @@ assert.strictEqual(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manipulate the prototype to one that we can not handle.
|
// Manipulate the prototype in weird ways.
|
||||||
{
|
{
|
||||||
let obj = { a: true };
|
let obj = { a: true };
|
||||||
let value = (function() { return function() {}; })();
|
let value = (function() { return function() {}; })();
|
||||||
Object.setPrototypeOf(value, null);
|
Object.setPrototypeOf(value, null);
|
||||||
Object.setPrototypeOf(obj, value);
|
Object.setPrototypeOf(obj, value);
|
||||||
assert.strictEqual(util.inspect(obj), '{ a: true }');
|
assert.strictEqual(util.inspect(obj), '<[Function]> { a: true }');
|
||||||
|
assert.strictEqual(
|
||||||
|
util.inspect(obj, { colors: true }),
|
||||||
|
'<\u001b[36m[Function]\u001b[39m> { a: \u001b[33mtrue\u001b[39m }'
|
||||||
|
);
|
||||||
|
|
||||||
obj = { a: true };
|
obj = { a: true };
|
||||||
value = [];
|
value = [];
|
||||||
Object.setPrototypeOf(value, null);
|
Object.setPrototypeOf(value, null);
|
||||||
Object.setPrototypeOf(obj, value);
|
Object.setPrototypeOf(obj, value);
|
||||||
assert.strictEqual(util.inspect(obj), '{ a: true }');
|
assert.strictEqual(
|
||||||
|
util.inspect(obj),
|
||||||
|
'<[Array: null prototype] []> { a: true }'
|
||||||
|
);
|
||||||
|
|
||||||
|
function StorageObject() {}
|
||||||
|
StorageObject.prototype = Object.create(null);
|
||||||
|
assert.strictEqual(
|
||||||
|
util.inspect(new StorageObject()),
|
||||||
|
'<[Object: null prototype] {}> {}'
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the fallback always works.
|
// Check that the fallback always works.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user