1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Flags: --harmony-templates --harmony-unicode
9 function fn() { return "result"; }
13 fn: function() { return "result"; }
16 (function testBasicExpressions() {
17 assertEquals("foo 5 bar", `foo ${num} bar`);
18 assertEquals("foo str bar", `foo ${str} bar`);
19 assertEquals("foo [object Object] bar", `foo ${obj} bar`);
20 assertEquals("foo result bar", `foo ${fn()} bar`);
21 assertEquals("foo 5 bar", `foo ${obj.num} bar`);
22 assertEquals("foo str bar", `foo ${obj.str} bar`);
23 assertEquals("foo result bar", `foo ${obj.fn()} bar`);
26 (function testExpressionsContainingTemplates() {
27 assertEquals("foo bar 5", `foo ${`bar ${num}`}`);
30 (function testMultilineTemplates() {
31 assertEquals("foo\n bar\n baz", `foo
35 assertEquals("foo\n bar\n baz", eval("`foo\r\n bar\r baz`"));
38 (function testLineContinuation() {
44 (function testTaggedTemplates() {
49 assertEquals(1, calls);
52 // assert tag is invoked in right context
56 assertEquals(obj, this);
61 assertEquals(1, calls);
64 // Simple templates only have a callSiteObj
67 assertEquals(1, arguments.length);
69 assertEquals(1, calls);
71 // Templates containing expressions have the values of evaluated expressions
73 (function(site, n, s, o, f, r) {
75 assertEquals(6, arguments.length);
76 assertEquals("number", typeof n);
77 assertEquals("string", typeof s);
78 assertEquals("object", typeof o);
79 assertEquals("function", typeof f);
80 assertEquals("result", r);
81 })`${num}${str}${obj}${fn}${fn()}`;
82 assertEquals(1, calls);
84 // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit
89 assertEquals(1, s.length);
90 assertEquals(1, s.raw.length);
91 assertEquals("", s[0]);
93 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest">
94 assertEquals("", s.raw[0]);
96 assertEquals(1, calls);
98 // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence.
102 assertEquals(2, s.length);
103 assertEquals(2, s.raw.length);
104 assertEquals("", s[0]);
105 assertEquals("", s.raw[0]);
107 assertEquals(1, calls);
109 // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence.
113 assertEquals(3, s.length);
114 assertEquals(3, s.raw.length);
115 assertEquals("", s[1]);
116 assertEquals("", s.raw[1]);
118 assertEquals(1, calls);
120 // The TV and TRV of TemplateTail :: }` is the empty code unit sequence.
124 assertEquals(2, s.length);
125 assertEquals(2, s.raw.length);
126 assertEquals("", s[1]);
127 assertEquals("", s.raw[1]);
129 assertEquals(1, calls);
131 // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of
132 // TemplateCharacters.
134 (function(s) { calls++; assertEquals("foo", s[0]); })`foo`;
135 assertEquals(1, calls);
137 // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of
138 // TemplateCharacters.
140 (function(s) { calls++; assertEquals("foo", s[0]); })`foo${1}`;
141 assertEquals(1, calls);
143 // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of
144 // TemplateCharacters.
146 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo${2}`;
147 assertEquals(1, calls);
149 // The TV of TemplateTail :: } TemplateCharacters ` is the TV of
150 // TemplateCharacters.
152 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo`;
153 assertEquals(1, calls);
155 // The TV of TemplateCharacters :: TemplateCharacter is the TV of
156 // TemplateCharacter.
158 (function(s) { calls++; assertEquals("f", s[0]); })`f`;
159 assertEquals(1, calls);
161 // The TV of TemplateCharacter :: $ is the code unit value 0x0024.
163 (function(s) { calls++; assertEquals("$", s[0]); })`$`;
164 assertEquals(1, calls);
166 // The TV of TemplateCharacter :: \ EscapeSequence is the CV of
169 (function(s) { calls++; assertEquals("안녕", s[0]); })`\uc548\uB155`;
170 (function(s) { calls++; assertEquals("\xff", s[0]); })`\xff`;
171 (function(s) { calls++; assertEquals("\n", s[0]); })`\n`;
172 assertEquals(3, calls);
174 // The TV of TemplateCharacter :: LineContinuation is the TV of
175 // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is
176 // the empty code unit sequence.
178 (function(s) { calls++; assertEquals("", s[0]); })`\
180 assertEquals(1, calls);
182 // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of
183 // TemplateCharacters.
185 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test`;
186 assertEquals(1, calls);
188 // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of
189 // TemplateCharacters.
191 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test${1}`;
192 assertEquals(1, calls);
194 // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of
195 // TemplateCharacters.
197 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test${2}`;
198 assertEquals(1, calls);
200 // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of
201 // TemplateCharacters.
203 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test`;
204 assertEquals(1, calls);
206 // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of
207 // TemplateCharacter.
209 (function(s) { calls++; assertEquals("f", s.raw[0]); })`f`;
210 assertEquals(1, calls);
212 // The TRV of TemplateCharacter :: $ is the code unit value 0x0024.
214 (function(s) { calls++; assertEquals("\u0024", s.raw[0]); })`$`;
215 assertEquals(1, calls);
217 // The TRV of EscapeSequence :: 0 is the code unit value 0x0030.
219 (function(s) { calls++; assertEquals("\u005C\u0030", s.raw[0]); })`\0`;
220 assertEquals(1, calls);
222 // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting
223 // of the code unit value 0x005C followed by the code units of TRV of
226 // The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the
227 // HexEscapeSequence.
229 (function(s) { calls++; assertEquals("\u005Cxff", s.raw[0]); })`\xff`;
230 assertEquals(1, calls);
232 // The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the
233 // UnicodeEscapeSequence.
235 (function(s) { calls++; assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`;
236 assertEquals(1, calls);
238 // The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of
239 // the SingleEscapeCharacter.
241 (function(s) { calls++; assertEquals("\u005C\u0027", s.raw[0]); })`\'`;
242 (function(s) { calls++; assertEquals("\u005C\u0022", s.raw[0]); })`\"`;
243 (function(s) { calls++; assertEquals("\u005C\u005C", s.raw[0]); })`\\`;
244 (function(s) { calls++; assertEquals("\u005Cb", s.raw[0]); })`\b`;
245 (function(s) { calls++; assertEquals("\u005Cf", s.raw[0]); })`\f`;
246 (function(s) { calls++; assertEquals("\u005Cn", s.raw[0]); })`\n`;
247 (function(s) { calls++; assertEquals("\u005Cr", s.raw[0]); })`\r`;
248 (function(s) { calls++; assertEquals("\u005Ct", s.raw[0]); })`\t`;
249 (function(s) { calls++; assertEquals("\u005Cv", s.raw[0]); })`\v`;
250 (function(s) { calls++; assertEquals("\u005C`", s.raw[0]); })`\``;
251 assertEquals(10, calls);
253 // The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the
254 // NonEscapeCharacter.
256 (function(s) { calls++; assertEquals("\u005Cz", s.raw[0]); })`\z`;
257 assertEquals(1, calls);
259 // The TRV of LineTerminatorSequence :: <LF> is the code unit value 0x000A.
260 // The TRV of LineTerminatorSequence :: <CR> is the code unit value 0x000A.
261 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence consisting of
262 // the code unit value 0x000A.
264 function testRawLineNormalization(cs) {
266 assertEquals(cs.raw[0], "\n\n\n");
267 assertEquals(cs.raw[1], "\n\n\n");
269 eval("testRawLineNormalization`\r\n\n\r${1}\r\n\n\r`");
270 assertEquals(1, calls);
272 // The TRV of LineContinuation :: \ LineTerminatorSequence is the sequence
273 // consisting of the code unit value 0x005C followed by the code units of TRV
274 // of LineTerminatorSequence.
276 function testRawLineContinuation(cs) {
278 assertEquals(cs.raw[0], "\u005C\n\u005C\n\u005C\n");
279 assertEquals(cs.raw[1], "\u005C\n\u005C\n\u005C\n");
281 eval("testRawLineContinuation`\\\r\n\\\n\\\r${1}\\\r\n\\\n\\\r`");
282 assertEquals(1, calls);
286 (function testCallSiteObj() {
290 assertTrue(cs.hasOwnProperty("raw"));
291 assertTrue(Object.isFrozen(cs));
292 assertTrue(Object.isFrozen(cs.raw));
293 var raw = Object.getOwnPropertyDescriptor(cs, "raw");
294 assertFalse(raw.writable);
295 assertFalse(raw.configurable);
296 assertFalse(raw.enumerable);
297 assertEquals(Array.prototype, Object.getPrototypeOf(cs.raw));
298 assertTrue(Array.isArray(cs.raw));
299 assertEquals(Array.prototype, Object.getPrototypeOf(cs));
300 assertTrue(Array.isArray(cs));
302 var cooked0 = Object.getOwnPropertyDescriptor(cs, "0");
303 assertFalse(cooked0.writable);
304 assertFalse(cooked0.configurable);
305 assertTrue(cooked0.enumerable);
307 var raw0 = Object.getOwnPropertyDescriptor(cs.raw, "0");
308 assertFalse(cooked0.writable);
309 assertFalse(cooked0.configurable);
310 assertTrue(cooked0.enumerable);
312 var length = Object.getOwnPropertyDescriptor(cs, "length");
313 assertFalse(length.writable);
314 assertFalse(length.configurable);
315 assertFalse(length.enumerable);
317 length = Object.getOwnPropertyDescriptor(cs.raw, "length");
318 assertFalse(length.writable);
319 assertFalse(length.configurable);
320 assertFalse(length.enumerable);
323 assertEquals(1, calls);
327 (function testUTF16ByteOrderMark() {
328 assertEquals("\uFEFFtest", `\uFEFFtest`);
329 assertEquals("\uFEFFtest", eval("`\uFEFFtest`"));
333 (function testStringRawAsTagFn() {
334 assertEquals("\\u0065\\`\\r\\r\\n\\ntestcheck",
335 String.raw`\u0065\`\r\r\n\n${"test"}check`);
336 assertEquals("\\\n\\\n\\\n", eval("String.raw`\\\r\\\r\n\\\n`"));
337 assertEquals("", String.raw``);
341 (function testCallSiteCaching() {
343 function tag(cs) { callSites.push(cs); }
350 assertEquals(2, callSites.length);
351 assertSame(callSites[0], callSites[1]);
353 eval("tag`head${a}tail`");
354 assertEquals(3, callSites.length);
355 assertSame(callSites[1], callSites[2]);
357 eval("tag`head${b}tail`");
358 assertEquals(4, callSites.length);
359 assertSame(callSites[2], callSites[3]);
361 (new Function("tag", "a", "b", "return tag`head${a}tail`;"))(tag, 1, 2);
362 assertEquals(5, callSites.length);
363 assertSame(callSites[3], callSites[4]);
365 (new Function("tag", "a", "b", "return tag`head${b}tail`;"))(tag, 1, 2);
366 assertEquals(6, callSites.length);
367 assertSame(callSites[4], callSites[5]);
373 assertEquals(2, callSites.length);
374 assertEquals(2, callSites[0].length);
375 assertEquals(1, callSites[1].length);
379 eval("tag`\\\r\n\\\n\\\r`");
380 eval("tag`\\\r\n\\\n\\\r`");
381 assertEquals(2, callSites.length);
382 assertSame(callSites[0], callSites[1]);
383 assertEquals("", callSites[0][0]);
384 assertEquals("\\\n\\\n\\\n", callSites[0].raw[0]);
390 assertEquals(2, callSites.length);
391 assertSame(callSites[0], callSites[1]);
392 assertEquals("안녕", callSites[0][0]);
393 assertEquals("\\uc548\\ub155", callSites[0].raw[0]);
399 assertEquals(2, callSites.length);
400 assertTrue(callSites[0] !== callSites[1]);
401 assertEquals("안녕", callSites[0][0]);
402 assertEquals("\\uc548\\ub155", callSites[0].raw[0]);
403 assertEquals("안녕", callSites[1][0]);
404 assertEquals("안녕", callSites[1].raw[0]);
406 // Extra-thorough UTF8 decoding test.
409 tag`Iñtërnâtiônàlizætiøn\u2603\uD83D\uDCA9`;
410 tag`Iñtërnâtiônàlizætiøn☃💩`;
412 assertEquals(2, callSites.length);
413 assertTrue(callSites[0] !== callSites[1]);
414 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[0][0]);
416 "Iñtërnâtiônàlizætiøn\\u2603\\uD83D\\uDCA9", callSites[0].raw[0]);
417 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1][0]);
418 assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1].raw[0]);
422 (function testExtendedArrayPrototype() {
423 Object.defineProperty(Array.prototype, 0, {
433 (function testRawLineNormalization() {
434 function raw0(callSiteObj) {
435 return callSiteObj.raw[0];
437 assertEquals(eval("raw0`\r`"), "\n");
438 assertEquals(eval("raw0`\r\n`"), "\n");
439 assertEquals(eval("raw0`\r\r\n`"), "\n\n");
440 assertEquals(eval("raw0`\r\n\r\n`"), "\n\n");
441 assertEquals(eval("raw0`\r\r\r\n`"), "\n\n\n");
445 (function testHarmonyUnicode() {
446 function raw0(callSiteObj) {
447 return callSiteObj.raw[0];
449 assertEquals(raw0`a\u{62}c`, "a\\u{62}c");
450 assertEquals(raw0`a\u{000062}c`, "a\\u{000062}c");
451 assertEquals(raw0`a\u{0}c`, "a\\u{0}c");
453 assertEquals(`a\u{62}c`, "abc");
454 assertEquals(`a\u{000062}c`, "abc");
458 (function testLiteralAfterRightBrace() {
459 // Regression test for https://code.google.com/p/v8/issues/detail?id=3734
463 function g() {}`def`;
476 (function testLegacyOctal() {
477 assertEquals('\u0000', `\0`);
478 assertEquals('\u0000a', `\0a`);
479 for (var i = 0; i < 8; i++) {
480 var code = "`\\0" + i + "`";
481 assertThrows(code, SyntaxError);
482 code = "(function(){})" + code;
483 assertThrows(code, SyntaxError);
486 assertEquals('\\0', String.raw`\0`);
490 (function testSyntaxErrorsNonEscapeCharacter() {
491 assertThrows("`\\x`", SyntaxError);
492 assertThrows("`\\u`", SyntaxError);
493 for (var i = 1; i < 8; i++) {
494 var code = "`\\" + i + "`";
495 assertThrows(code, SyntaxError);
496 code = "(function(){})" + code;
497 assertThrows(code, SyntaxError);
502 (function testValidNumericEscapes() {
503 assertEquals("8", `\8`);
504 assertEquals("9", `\9`);
505 assertEquals("\u00008", `\08`);
506 assertEquals("\u00009", `\09`);
510 (function testLegacyOctalEscapesInExpressions() {
511 // Allowed in sloppy expression
512 assertEquals("\x07", `${"\07"}`);
514 // Disallowed in template tail
515 assertThrows("`${\"\\07\"}\\07`", SyntaxError);
517 // Disallowed in strict expression
518 assertThrows("`${(function() { \"use strict\"; return \"\\07\"; })()}`",