Skip to content

Commit

Permalink
util: fix inspection of class instance prototypes
Browse files Browse the repository at this point in the history
To achieve this, some internal custom inspect functions had to be
changed. They relied upon the former behavior.

Signed-off-by: Ruben Bridgewater <[email protected]>

PR-URL: #33449
Fixes: #33419
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
BridgeAR authored and codebytere committed Jun 18, 2020
1 parent 134ed0e commit a90c9aa
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 72 deletions.
91 changes: 45 additions & 46 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,54 +514,53 @@ function makeTextDecoderJS() {
}

// Mix in some shared properties.
{
ObjectDefineProperties(
TextDecoder.prototype,
ObjectGetOwnPropertyDescriptors({
get encoding() {
validateDecoder(this);
return this[kEncoding];
},

get fatal() {
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
},

get ignoreBOM() {
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
CONVERTER_FLAGS_IGNORE_BOM;
},

[inspect](depth, opts) {
validateDecoder(this);
if (typeof depth === 'number' && depth < 0)
return this;
const ctor = getConstructorOf(this);
const obj = ObjectCreate({
constructor: ctor === null ? TextDecoder : ctor
});
obj.encoding = this.encoding;
obj.fatal = this.fatal;
obj.ignoreBOM = this.ignoreBOM;
if (opts.showHidden) {
obj[kFlags] = this[kFlags];
obj[kHandle] = this[kHandle];
}
// Lazy to avoid circular dependency
return require('internal/util/inspect').inspect(obj, opts);
ObjectDefineProperties(
TextDecoder.prototype,
ObjectGetOwnPropertyDescriptors({
get encoding() {
validateDecoder(this);
return this[kEncoding];
},

get fatal() {
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
},

get ignoreBOM() {
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
CONVERTER_FLAGS_IGNORE_BOM;
},

[inspect](depth, opts) {
validateDecoder(this);
if (typeof depth === 'number' && depth < 0)
return this;
const constructor = getConstructorOf(this) || TextDecoder;
const obj = ObjectCreate({ constructor });
obj.encoding = this.encoding;
obj.fatal = this.fatal;
obj.ignoreBOM = this.ignoreBOM;
if (opts.showHidden) {
obj[kFlags] = this[kFlags];
obj[kHandle] = this[kHandle];
}
}));
ObjectDefineProperties(TextDecoder.prototype, {
decode: { enumerable: true },
[inspect]: { enumerable: false },
[SymbolToStringTag]: {
configurable: true,
value: 'TextDecoder'
// Lazy to avoid circular dependency
const { inspect } = require('internal/util/inspect');
return `${constructor.name} ${inspect(obj)}`;
}
});
}
})
);

ObjectDefineProperties(TextDecoder.prototype, {
decode: { enumerable: true },
[inspect]: { enumerable: false },
[SymbolToStringTag]: {
configurable: true,
value: 'TextDecoder'
}
});

module.exports = {
getEncodingFromLabel,
Expand Down
9 changes: 3 additions & 6 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,8 @@ class URL {
if (typeof depth === 'number' && depth < 0)
return this;

const ctor = getConstructorOf(this);

const obj = ObjectCreate({
constructor: ctor === null ? URL : ctor
});
const constructor = getConstructorOf(this) || URL;
const obj = ObjectCreate({ constructor });

obj.href = this.href;
obj.origin = this.origin;
Expand All @@ -383,7 +380,7 @@ class URL {
obj[context] = this[context];
}

return inspect(obj, opts);
return `${constructor.name} ${inspect(obj, opts)}`;
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
descriptor.value.name !== '') {
descriptor.value.name !== '' &&
tmp instanceof descriptor.value) {
if (protoProps !== undefined &&
(firstProto !== obj ||
!builtInObjects.has(descriptor.value.name))) {
Expand Down
24 changes: 18 additions & 6 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const {
ArrayIsArray,
ObjectCreate,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
SafePromise,
Symbol,
WeakMap,
Expand Down Expand Up @@ -223,17 +225,27 @@ class Module {
}

[customInspectSymbol](depth, options) {
let ctor = getConstructorOf(this);
ctor = ctor === null ? Module : ctor;

if (this[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
if (typeof depth === 'number' && depth < 0)
return options.stylize(`[${ctor.name}]`, 'special');
return this;

const o = ObjectCreate({ constructor: ctor });
const constructor = getConstructorOf(this) || Module;
const o = ObjectCreate({ constructor });
o.status = this.status;
o.identifier = this.identifier;
o.context = this.context;
return require('internal/util/inspect').inspect(o, options);

ObjectSetPrototypeOf(o, ObjectGetPrototypeOf(this));
ObjectDefineProperty(o, Symbol.toStringTag, {
value: constructor.name,
configurable: true
});

// Lazy to avoid circular dependency
const { inspect } = require('internal/util/inspect');
return inspect(o, { ...options, customInspect: false });
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ assert.strictEqual(
' func: <ref *1> [Function: func] {\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
'}');
assert.strictEqual(
Expand All @@ -279,7 +279,7 @@ assert.strictEqual(
' a: <ref *1> [Function: a] {\n' +
' [length]: 0,\n' +
' [name]: \'a\',\n' +
' [prototype]: a { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
' },\n' +
' [length]: 1\n' +
Expand All @@ -294,7 +294,7 @@ assert.strictEqual(
' func: <ref *1> [Function: func] {\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
' }\n' +
'}');
Expand All @@ -306,15 +306,15 @@ assert.strictEqual(
' func: <ref *1> [Function: func] {\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
'} {\n' +
' foo: \'bar\',\n' +
' foobar: 1,\n' +
' func: <ref *1> [Function: func] {\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
'}');
assert.strictEqual(
Expand All @@ -325,7 +325,7 @@ assert.strictEqual(
' func: <ref *1> [Function: func] {\n' +
' [length]: 0,\n' +
' [name]: \'func\',\n' +
' [prototype]: func { [constructor]: [Circular *1] }\n' +
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
'} %o');

Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,37 @@ assert.strictEqual(
'{ \x1B[2mabc: \x1B[33mtrue\x1B[39m\x1B[22m, ' +
'\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }'
);

assert.strictEqual(
inspect(Object.getPrototypeOf(bar), { showHidden: true, getters: true }),
'<ref *1> Foo [Map] {\n' +
' [constructor]: [class Bar extends Foo] {\n' +
' [length]: 0,\n' +
' [prototype]: [Circular *1],\n' +
" [name]: 'Bar',\n" +
' [Symbol(Symbol.species)]: [Getter: <Inspection threw ' +
"(Symbol.prototype.toString requires that 'this' be a Symbol)>]\n" +
' },\n' +
" [xyz]: [Getter: 'YES!'],\n" +
' [Symbol(nodejs.util.inspect.custom)]: ' +
'[Function: [nodejs.util.inspect.custom]] {\n' +
' [length]: 0,\n' +
" [name]: '[nodejs.util.inspect.custom]'\n" +
' },\n' +
' [abc]: [Getter: true],\n' +
' [def]: [Getter/Setter: false]\n' +
' }'
);

assert.strictEqual(
inspect(Object.getPrototypeOf(bar)),
'Foo [Map] {}'
);

assert.strictEqual(
inspect(Object.getPrototypeOf(new Foo())),
'Map {}'
);
}

// Test changing util.inspect.colors colors and aliases.
Expand Down
9 changes: 6 additions & 3 deletions test/parallel/test-vm-module-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ const util = require('util');

assert.strictEqual(util.inspect(m, { depth: -1 }), '[SourceTextModule]');

assert.strictEqual(
m[util.inspect.custom].call(Object.create(null)),
'Module { status: undefined, identifier: undefined, context: undefined }',
assert.throws(
() => m[util.inspect.custom].call(Object.create(null)),
{
code: 'ERR_VM_MODULE_NOT_MODULE',
message: 'Provided module is not an instance of Module'
},
);
}

Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ if (common.hasIntl) {
assert.strictEqual(dec.encoding, 'utf-8');
assert.strictEqual(dec.fatal, false);
assert.strictEqual(dec.ignoreBOM, false);
assert.strictEqual(dec[Symbol.toStringTag], 'TextDecoder');
}

// Test TextDecoder, UTF-16le
Expand Down Expand Up @@ -125,10 +126,7 @@ if (common.hasIntl) {
' [Symbol(flags)]: 4,\n' +
' [Symbol(handle)]: StringDecoder {\n' +
" encoding: 'utf8',\n" +
' [Symbol(kNativeDecoder)]: <Buffer 00 00 00 00 00 00 01>,\n' +
' lastChar: [Getter],\n' +
' lastNeed: [Getter],\n' +
' lastTotal: [Getter]\n' +
' [Symbol(kNativeDecoder)]: <Buffer 00 00 00 00 00 00 01>\n' +
' }\n' +
'}'
);
Expand Down

0 comments on commit a90c9aa

Please sign in to comment.