Template Literals Incorrect #1065

Closed
jacobp100 opened this Issue Mar 23, 2015 · 15 comments

Projects

None yet

3 participants

@jacobp100

Say I have the object,

let x = {
    toString() { return 'String' },
    valueOf() { return 4; }
};
`${x}`;

Expected result:

'String'

Actual result:

'4'

We should be calling String(x) for all the variables.

@kittens
Member
kittens commented Mar 23, 2015

Do you have a spec citation?

@kittens
Member
kittens commented Mar 23, 2015

Also your example has a ton of syntax errors.

@kittens
Member
kittens commented Mar 23, 2015

GetTemplateObject isn't the relevant section, ToString is just being called on index.

12.2.8.5 3 appears to be where the expression is turned into a string. Looks like the current (apparently incorrect) behaviour is the same as the current V8 and Traceur so bugs should potentially be reported on both of those.

Closing this as a wontfix for the time being until I collect more information to evaluate the practicality and perf impact of wrapping all expressions in a String.

@kittens kittens closed this Mar 23, 2015
@kittens kittens added the wontfix label Mar 23, 2015
@jacobp100

Why is there a perf impact? x: ${x} should map to 'x: ' + String(x). In terms of perf, what you're doing currently is the equivalent of String(x.valueOf()).

@kittens
Member
kittens commented Mar 23, 2015

Yep, and that's the performance impact I alluded to.

screen shot 2015-03-23 at 9 31 59 pm

@jacobp100

http://jsperf.com/tostring-vs-appending-empty-string

Alright. There is a perf impact. However, this is clearly not spec compliant. To what level should optimisations allow spec deviations?

@kittens
Member
kittens commented Mar 23, 2015

To what level should optimisations allow spec deviations?

Almost never but seeing as all other implementations follow this behaviour I think it's ok for the timebeing.

@jacobp100

Firefox is correct.

@kittens
Member
kittens commented Mar 23, 2015

Reopening, need to evaluate and investigate this more.

@AluisioASG
Contributor

Wouldn't this be the case for a spec transformer?

@kittens
Member
kittens commented Mar 23, 2015

@AluisioASG Nope. The spec transformer category is very loosley defined and is just a catch-all for things that don't really belong in another category but are related to spec compliancy.

@kittens kittens reopened this Mar 23, 2015
@jacobp100

https://code.google.com/p/v8/issues/detail?id=3980 for the V8 issue, although V8 is probably not a good metric to use for JS correctness.

@kittens
Member
kittens commented Mar 23, 2015

@jacobp100 Didn't say it was. Just wanted to verify if it was present in the latest and not in the version of iojs I'm using.

@kittens
Member
kittens commented Mar 23, 2015

Relevant Traceur issue: google/traceur-compiler#1836

@kittens kittens closed this Mar 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment