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: logging symbols should convert using String(symbol) #927

Closed
domenic opened this issue Feb 23, 2015 · 3 comments
Closed

util: logging symbols should convert using String(symbol) #927

domenic opened this issue Feb 23, 2015 · 3 comments
Labels
util Issues and PRs related to the built-in util module.

Comments

@domenic
Copy link
Contributor

domenic commented Feb 23, 2015

var symbol = Symbol("My symbol");

console.log(symbol); // fine, logs Symbol(My symbol)
console.log("foo", symbol); // throws

Output:

$ iojs test.js
Symbol(My symbol)
util.js:34
      str += ' ' + x;
                 ^
TypeError: Cannot convert a Symbol value to a string
    at Console.exports.format (util.js:34:18)
    at Console.log (console.js:36:34)
    at Object.<anonymous> (c:\Users\Domenic\Dropbox\GitHub\whatwg\streams\reference-implementation\test.js:4:9)
    at Module._compile (module.js:444:26)
    at Object.Module._extensions..js (module.js:462:10)
    at Module.load (module.js:339:32)
    at Function.Module._load (module.js:294:12)
    at Function.Module.runMain (module.js:485:10)
    at startup (node.js:111:16)
    at node.js:799:3

Note that the correct way to stringify a symbol is with the explicit conversion, String(symbol), instead of implicitly with the + sign.

I can get to this after work if nobody else does first.

@vkurchatkin
Copy link
Contributor

Is it the correct behaviour though? I thought it was v8 bug

@vkurchatkin vkurchatkin added the util Issues and PRs related to the built-in util module. label Feb 23, 2015
@domenic
Copy link
Contributor Author

domenic commented Feb 23, 2015

Yeah, this is correct per spec and several TC39 meetings. It is dangerous to allow symbols to be implicitly converted to strings (and in some cases you just flat out can't do it, e.g. obj[prop] usually does an implicit string conversion). However explicit conversion is fine.

cjihrig added a commit that referenced this issue Feb 24, 2015
Currently, if util.format() is called with a string as its first
argument, and a Symbol as one of the subsequent arguments, an
exception is thrown due to an attempted implicit string conversion.
This commit causes Symbols to be explicitly converted.

Fixes: #927
PR-URL: #931
Reviewed-By: Domenic Denicola <[email protected]>
@cjihrig
Copy link
Contributor

cjihrig commented Feb 24, 2015

Closed by ed3b057

@cjihrig cjihrig closed this as completed Feb 24, 2015
petkaantonov pushed a commit to petkaantonov/io.js that referenced this issue Feb 25, 2015
Currently, if util.format() is called with a string as its first
argument, and a Symbol as one of the subsequent arguments, an
exception is thrown due to an attempted implicit string conversion.
This commit causes Symbols to be explicitly converted.

Fixes: nodejs#927
PR-URL: nodejs#931
Reviewed-By: Domenic Denicola <[email protected]>
jasonkarns added a commit to testdouble/testdouble.js that referenced this issue Nov 17, 2017
Apparently (nodejs/node#927 (comment)),
Symbols cannot be implicitly coerced to string.

Template stringification leverages implicit coercion, so we must
stringify propKey before sending it to the template.

Options for explicitly stringifying:

- `String(propKey)`
- `String.prototype.toString(propKey)`

Differences:

```
> String(undefined)
'undefined'
> String.prototype.toString(undefined)
''
```

In this case, `propKey` represents the key for the stubbed function. If
the key is `undefined`, it would be implicitly coerced to `"undefined"`
as the property key. So `String()` is the explicit converstion we want.
jasonkarns added a commit to testdouble/testdouble.js that referenced this issue Nov 18, 2017
Apparently (nodejs/node#927 (comment)),
Symbols cannot be implicitly coerced to string.

Template stringification leverages implicit coercion, so we must
stringify propKey before sending it to the template.

Options for explicitly stringifying:

- `String(propKey)`
- `String.prototype.toString(propKey)`

Differences:

```
> String(undefined)
'undefined'
> String.prototype.toString(undefined)
''
```

In this case, `propKey` represents the key for the stubbed function. If
the key is `undefined`, it would be implicitly coerced to `"undefined"`
as the property key. So `String()` is the explicit converstion we want.

Four instances of this issue, each where tdFunction is invoked with a
dynamic name.
jasonkarns added a commit to testdouble/testdouble.js that referenced this issue Nov 18, 2017
Apparently (nodejs/node#927 (comment)),
Symbols cannot be implicitly coerced to string.

Template stringification leverages implicit coercion, so we must
stringify propKey before sending it to the template.

Options for explicitly stringifying:

- `String(propKey)`
- `String.prototype.toString(propKey)`

Differences:

```
> String(undefined)
'undefined'
> String.prototype.toString(undefined)
''
```

In this case, `propKey` represents the key for the stubbed function. If
the key is `undefined`, it would be implicitly coerced to `"undefined"`
as the property key. So `String()` is the explicit converstion we want.

Four instances of this issue, each where tdFunction is invoked with a
dynamic name.
jasonkarns added a commit to testdouble/testdouble.js that referenced this issue Jan 17, 2018
Apparently (nodejs/node#927 (comment)),
Symbols cannot be implicitly coerced to string.

Template stringification leverages implicit coercion, so we must
stringify propKey before sending it to the template.

Options for explicitly stringifying:

- `String(propKey)`
- `String.prototype.toString(propKey)`

Differences:

```
> String(undefined)
'undefined'
> String.prototype.toString(undefined)
''
```

In this case, `propKey` represents the key for the stubbed function. If
the key is `undefined`, it would be implicitly coerced to `"undefined"`
as the property key. So `String()` is the explicit converstion we want.

Four instances of this issue, each where tdFunction is invoked with a
dynamic name.
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

No branches or pull requests

3 participants