Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.inspect fails to log getters which access this #36045

Closed
szmarczak opened this issue Nov 8, 2020 · 2 comments · Fixed by #36052
Closed

util.inspect fails to log getters which access this #36045

szmarczak opened this issue Nov 8, 2020 · 2 comments · Fixed by #36052
Assignees
Labels
util Issues and PRs related to the built-in util module.

Comments

@szmarczak
Copy link
Member

  • Version: v15.0.1
  • Platform: Linux solus 5.6.19-159.current #1 SMP PREEMPT Fri Oct 16 17:49:06 UTC 2020 x86_64 GNU/Linux
  • Subsystem: util

What steps will reproduce the bug?

const util = require('util');

class X {
    constructor() {
		this._y = 123;
	}

	get y() {
		return this._y;
	}
}

console.log(util.inspect(new X(), {
	colors: true,
    getters: true,
    showHidden: true
}));

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

X { _y: 123, [y]: [Getter: 123] }

What do you see instead?

X { _y: 123, [y]: [Getter: undefined] }

Additional information

May be related with #35956

@targos
Copy link
Member

targos commented Nov 9, 2020

@nodejs/util

@targos targos added the util Issues and PRs related to the built-in util module. label Nov 9, 2020
@BridgeAR BridgeAR self-assigned this Nov 9, 2020
@BridgeAR
Copy link
Member

BridgeAR commented Nov 9, 2020

diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index e6787760fe..9343b0772f 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -628,7 +628,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
         continue;
       }
       const value = formatProperty(
-        ctx, obj, recurseTimes, key, kObjectType, desc);
+        ctx, obj, recurseTimes, key, kObjectType, desc, main);
       if (ctx.colors) {
         // Faint!
         output.push(`\u001b[2m${value}\u001b[22m`);
@@ -1678,7 +1678,7 @@ function formatPromise(ctx, value, recurseTimes) {
   return output;
 }
 
-function formatProperty(ctx, value, recurseTimes, key, type, desc) {
+function formatProperty(ctx, value, recurseTimes, key, type, desc, original = value) {
   let name, str;
   let extra = ' ';
   desc = desc || ObjectGetOwnPropertyDescriptor(value, key) ||
@@ -1699,7 +1699,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc) {
           (ctx.getters === 'get' && desc.set === undefined) ||
           (ctx.getters === 'set' && desc.set !== undefined))) {
       try {
-        const tmp = value[key];
+        const tmp = desc.get.call(original);
         ctx.indentationLvl += 2;
         if (tmp === null) {
           str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;

This would be a functional fix. I did not yet check if this causes a performance regression due to mismatching the arguments count frequently.

@Trott Trott closed this as completed in 929c51f Nov 12, 2020
codebytere pushed a commit that referenced this issue Nov 22, 2020
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]>
BethGriggs pushed a commit that referenced this issue Dec 9, 2020
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]>
BethGriggs pushed a commit that referenced this issue Dec 10, 2020
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]>
BethGriggs pushed a commit that referenced this issue Dec 15, 2020
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants