Reviewers: arv,
Message:
PTAL --- quick fix to ensure template substitutions are ToString()'d as
spec'd
(https://people.mozilla.org/~jorendorff/es6-draft.html#sec-template-literals-runtime-semantics-evaluation)
Description:
[es6] call ToString() on template substitutions
BUG=v8:3980
[email protected]
LOG=N
Please review this at https://codereview.chromium.org/1027183002/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+43, -11 lines):
M src/ast-value-factory.h
M src/parser.cc
M test/mjsunit/es6/templates.js
Index: src/ast-value-factory.h
diff --git a/src/ast-value-factory.h b/src/ast-value-factory.h
index
43fa2d0e2ee7bdd543b8b710d669842c60edc676..b34a55b2a69a55d91a99b6c3cd44b38608cda232
100644
--- a/src/ast-value-factory.h
+++ b/src/ast-value-factory.h
@@ -261,6 +261,7 @@ class AstValue : public ZoneObject {
F(prototype, "prototype")
\
F(this, "this")
\
F(throw_iterator_result_not_an_object, "ThrowIteratorResultNotAnObject")
\
+ F(to_string, "ToString")
\
F(use_asm, "use asm")
\
F(use_strong, "use strong")
\
F(use_strict, "use strict")
\
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index
be8c3f30de25af5f19fe5285a8411083486f9390..a6063475e1fc534970d2e3530a476d8d1b43e2a2
100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -5485,18 +5485,29 @@ Expression*
Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start,
expr = cooked_strings->at(0);
} else {
int i;
- Expression* cooked_str = cooked_strings->at(0);
- expr = factory()->NewBinaryOperation(
- Token::ADD, cooked_str, expressions->at(0),
cooked_str->position());
- for (i = 1; i < expressions->length(); ++i) {
- cooked_str = cooked_strings->at(i);
- expr = factory()->NewBinaryOperation(
- Token::ADD, expr, factory()->NewBinaryOperation(
- Token::ADD, cooked_str,
expressions->at(i),
- cooked_str->position()),
- cooked_str->position());
+ for (i = 0; i < expressions->length(); ++i) {
+ Expression* cooked_str = cooked_strings->at(i);
+ Expression* sub = expressions->at(i);
+
+ // Let middle be ToString(sub).
+ ZoneList<Expression*>* args =
+ new (zone()) ZoneList<Expression*>(1, zone());
+ args->Add(sub, zone());
+ Expression* middle = factory()->NewCallRuntime(
+ ast_value_factory()->to_string_string(), NULL, args,
+ sub->position());
+
+ Expression* tail = factory()->NewBinaryOperation(
+ Token::ADD, cooked_str, middle, cooked_str->position());
+
+ if (expr) {
+ expr = factory()->NewBinaryOperation(
+ Token::ADD, expr, tail, cooked_str->position());
+ } else {
+ expr = tail;
+ }
}
- cooked_str = cooked_strings->at(i);
+ Expression* cooked_str = cooked_strings->at(i);
expr = factory()->NewBinaryOperation(Token::ADD, expr, cooked_str,
cooked_str->position());
}
Index: test/mjsunit/es6/templates.js
diff --git a/test/mjsunit/es6/templates.js b/test/mjsunit/es6/templates.js
index
a2b77b13c531560a71a2e584711cdba6923e3818..b2d5788dd2db2459ac399e186d2cca610f51a86b
100644
--- a/test/mjsunit/es6/templates.js
+++ b/test/mjsunit/es6/templates.js
@@ -586,3 +586,23 @@ var global = this;
"raw;test3"
], raw);
})();
+
+
+(function testToStringSubstitutions() {
+ var a = {
+ toString: function() { return "a"; },
+ valueOf: function() { return "-a-"; }
+ };
+ var b = {
+ toString: function() { return "b"; },
+ valueOf: function() { return "-b-"; }
+ };
+ assertEquals("a", `${a}`);
+ assertEquals("ab", `${a}${b}`);
+ assertEquals("-a--b-", `${a + b}`);
+ assertEquals("-a-", `${a + ""}`);
+ assertEquals("1a", `1${a}`);
+ assertEquals("1a2", `1${a}2`);
+ assertEquals("1a2b", `1${a}2${b}`);
+ assertEquals("1a2b3", `1${a}2${b}3`);
+})();
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.