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

console.log("%s", { [Symbol.toPrimitive]: () => "hello" }) shows the object, not "hello" #50909

Closed
jcbhmr opened this issue Nov 25, 2023 · 3 comments · Fixed by #50992
Closed
Labels
confirmed-bug Issues with confirmed bugs. console Issues and PRs related to the console subsystem.

Comments

@jcbhmr
Copy link
Contributor

jcbhmr commented Nov 25, 2023

Version

v21.2.0

Platform

Linux PIG-2016 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

inspect

What steps will reproduce the bug?

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
undefined
>

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

always

What is the expected behavior? Why is that the expected behavior?

deno does:

jcbhmr@PIG-2016:~$ deno
Deno 1.38.3
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
hello
undefined
>

chrome browser does:

< let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
> {Symbol(Symbol.toPrimitive): ƒ}
< console.log("%s", o)
VM5744:1 hello
> undefined

bun does:

jcbhmr@PIG-2016:~$ bun repl
Welcome to Bun v1.0.14
Type ".help" for more information.
[!] Please note that the REPL implementation is still experimental!
    Don't consider it to be representative of the stability or behavior of Bun overall.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
hello
undefined
>

firefox browser does:

>> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
<- Object { Symbol("Symbol.toPrimitive"): Symbol.toPrimitive() }

>> console.log("%s", o)
hello                                          debugger eval code:1:9
<- undefined

makes sense; it has a stringifier so it should act just like it would if it had a toString() which Node.js does recognize:

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, toString: () => "hello" }
[Object: null prototype] { toString: [Function: toString] }
> console.log("%s", o)
hello
undefined
>

What do you see instead?

jcbhmr@PIG-2016:~$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let o; o = { __proto__: null, [Symbol.toPrimitive]: () => "hello" }
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
> console.log("%s", o)
[Object: null prototype] {
  [Symbol(Symbol.toPrimitive)]: [Function: [Symbol.toPrimitive]]
}
undefined
>

Additional information

No response

@himself65
Copy link
Member

According to the spec, this is a bug in node.js

Spec:

@himself65 himself65 added confirmed-bug Issues with confirmed bugs. console Issues and PRs related to the console subsystem. labels Nov 25, 2023
@MrJithil
Copy link
Member

Looking into it.

@Ch3nYuY
Copy link

Ch3nYuY commented Dec 1, 2023

I believe this is because console.log("%s", o) calls inspect to obtain the object.

case 115: { // 's'
const tempArg = args[++a];
if (typeof tempArg === 'number') {
tempStr = formatNumberNoColor(tempArg, inspectOptions);
} else if (typeof tempArg === 'bigint') {
tempStr = formatBigIntNoColor(tempArg, inspectOptions);
} else if (typeof tempArg !== 'object' ||
tempArg === null ||
!hasBuiltInToString(tempArg)) {
tempStr = String(tempArg);
} else {
tempStr = inspect(tempArg, {
...inspectOptions,
compact: 3,
colors: false,
depth: 0,
});
}
break;
}

Ch3nYuY pushed a commit to Ch3nYuY/node that referenced this issue Mar 18, 2024
Ensure console.log("%s", obj) correctly invokes obj[Symbol.toPrimitive]
for string conversion, fixing unexpected object display issue.

Fixes: nodejs#50909
Ch3nYuY pushed a commit to Ch3nYuY/node that referenced this issue Mar 18, 2024
Ensure console.log("%s", obj) correctly invokes obj[Symbol.toPrimitive]
for string conversion, fixing unexpected object display issue.

Fixes: nodejs#50909
aduh95 pushed a commit to Ch3nYuY/node that referenced this issue May 11, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

Fixes: nodejs#50909
aduh95 pushed a commit that referenced this issue May 12, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: #50992
Fixes: #50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
targos pushed a commit that referenced this issue May 12, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: #50992
Fixes: #50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: #50992
Fixes: #50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: #50992
Fixes: #50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
marco-ippolito pushed a commit that referenced this issue Jun 17, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: #50992
Fixes: #50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
EliphazBouye pushed a commit to EliphazBouye/node that referenced this issue Jun 20, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: nodejs#50992
Fixes: nodejs#50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
bmeck pushed a commit to bmeck/node that referenced this issue Jun 22, 2024
This commit ensures `console.log("%s", obj)` correctly invokes
`obj[Symbol.toPrimitive]` for string conversion, fixing unexpected
object display issue.

PR-URL: nodejs#50992
Fixes: nodejs#50909
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. console Issues and PRs related to the console subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants