Skip to content

Commit 4140f49

Browse files
RaisinTencodebytere
authored andcommitted
util: fix to inspect getters that access this
Fixes: #36045 Co-authored-by: Antoine du Hamel <[email protected]> PR-URL: #36052 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 63494e4 commit 4140f49

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/internal/util/inspect.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
ObjectPrototypePropertyIsEnumerable,
4747
ObjectSeal,
4848
ObjectSetPrototypeOf,
49+
ReflectApply,
4950
RegExp,
5051
RegExpPrototypeToString,
5152
Set,
@@ -627,7 +628,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
627628
continue;
628629
}
629630
const value = formatProperty(
630-
ctx, obj, recurseTimes, key, kObjectType, desc);
631+
ctx, obj, recurseTimes, key, kObjectType, desc, main);
631632
if (ctx.colors) {
632633
// Faint!
633634
output.push(`\u001b[2m${value}\u001b[22m`);
@@ -1677,7 +1678,8 @@ function formatPromise(ctx, value, recurseTimes) {
16771678
return output;
16781679
}
16791680

1680-
function formatProperty(ctx, value, recurseTimes, key, type, desc) {
1681+
function formatProperty(ctx, value, recurseTimes, key, type, desc,
1682+
original = value) {
16811683
let name, str;
16821684
let extra = ' ';
16831685
desc = desc || ObjectGetOwnPropertyDescriptor(value, key) ||
@@ -1698,7 +1700,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) {
16981700
(ctx.getters === 'get' && desc.set === undefined) ||
16991701
(ctx.getters === 'set' && desc.set !== undefined))) {
17001702
try {
1701-
const tmp = value[key];
1703+
const tmp = ReflectApply(desc.get, original, []);
17021704
ctx.indentationLvl += 2;
17031705
if (tmp === null) {
17041706
str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
// This test ensures that util.inspect logs getters
6+
// which access this.
7+
8+
const assert = require('assert');
9+
10+
const util = require('util');
11+
12+
class X {
13+
constructor() {
14+
this._y = 123;
15+
}
16+
17+
get y() {
18+
return this._y;
19+
}
20+
}
21+
22+
const result = util.inspect(new X(), {
23+
getters: true,
24+
showHidden: true
25+
});
26+
27+
assert.strictEqual(
28+
result,
29+
'X { _y: 123, [y]: [Getter: 123] }'
30+
);

0 commit comments

Comments
 (0)