templates.js 24.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var num = 5;
var str = "str";
function fn() { return "result"; }
var obj = {
  num: num,
  str: str,
  fn: function() { return "result"; }
};

(function testBasicExpressions() {
  assertEquals("foo 5 bar", `foo ${num} bar`);
  assertEquals("foo str bar", `foo ${str} bar`);
  assertEquals("foo [object Object] bar", `foo ${obj} bar`);
  assertEquals("foo result bar", `foo ${fn()} bar`);
  assertEquals("foo 5 bar", `foo ${obj.num} bar`);
  assertEquals("foo str bar", `foo ${obj.str} bar`);
  assertEquals("foo result bar", `foo ${obj.fn()} bar`);
})();

(function testExpressionsContainingTemplates() {
  assertEquals("foo bar 5", `foo ${`bar ${num}`}`);
})();

(function testMultilineTemplates() {
  assertEquals("foo\n    bar\n    baz", `foo
    bar
    baz`);

  assertEquals("foo\n  bar\n  baz", eval("`foo\r\n  bar\r  baz`"));
})();

(function testLineContinuation() {
  assertEquals("\n", `\

`);
})();

(function testTaggedTemplates() {
  var calls = 0;
  (function(s) {
    calls++;
  })`test`;
  assertEquals(1, calls);

  calls = 0;
  // assert tag is invoked in right context
  obj = {
    fn: function() {
      calls++;
      assertEquals(obj, this);
    }
  };

  obj.fn`test`;
  assertEquals(1, calls);

  calls = 0;
  // Simple templates only have a callSiteObj
  (function(s) {
    calls++;
    assertEquals(1, arguments.length);
  })`test`;
  assertEquals(1, calls);

  // Templates containing expressions have the values of evaluated expressions
  calls = 0;
  (function(site, n, s, o, f, r) {
    calls++;
    assertEquals(6, arguments.length);
    assertEquals("number", typeof n);
    assertEquals("string", typeof s);
    assertEquals("object", typeof o);
    assertEquals("function", typeof f);
    assertEquals("result", r);
  })`${num}${str}${obj}${fn}${fn()}`;
  assertEquals(1, calls);

  // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit
  // sequence.
  calls = 0;
  (function(s) {
    calls++;
    assertEquals(1, s.length);
    assertEquals(1, s.raw.length);
    assertEquals("", s[0]);

    // Failure: expected <""> found <"foo  barfoo  barfoo foo foo foo testtest">
    assertEquals("", s.raw[0]);
  })``;
  assertEquals(1, calls);

  // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence.
  calls = 0;
  (function(s) {
    calls++;
    assertEquals(2, s.length);
    assertEquals(2, s.raw.length);
    assertEquals("", s[0]);
    assertEquals("", s.raw[0]);
  })`${1}`;
  assertEquals(1, calls);

  // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence.
  calls = 0;
  (function(s) {
    calls++;
    assertEquals(3, s.length);
    assertEquals(3, s.raw.length);
    assertEquals("", s[1]);
    assertEquals("", s.raw[1]);
  })`${1}${2}`;
  assertEquals(1, calls);

  // The TV and TRV of TemplateTail :: }` is the empty code unit sequence.
  calls = 0;
  (function(s) {
    calls++;
    assertEquals(2, s.length);
    assertEquals(2, s.raw.length);
    assertEquals("", s[1]);
    assertEquals("", s.raw[1]);
  })`${1}`;
  assertEquals(1, calls);

  // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("foo", s[0]); })`foo`;
  assertEquals(1, calls);

  // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("foo", s[0]); })`foo${1}`;
  assertEquals(1, calls);

  // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo${2}`;
  assertEquals(1, calls);

  // The TV of TemplateTail :: } TemplateCharacters ` is the TV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo`;
  assertEquals(1, calls);

  // The TV of TemplateCharacters :: TemplateCharacter is the TV of
  // TemplateCharacter.
  calls = 0;
  (function(s) { calls++; assertEquals("f", s[0]); })`f`;
  assertEquals(1, calls);

  // The TV of TemplateCharacter :: $ is the code unit value 0x0024.
  calls = 0;
  (function(s) { calls++; assertEquals("$", s[0]); })`$`;
  assertEquals(1, calls);

  // The TV of TemplateCharacter :: \ EscapeSequence is the CV of
  // EscapeSequence.
  calls = 0;
  (function(s) { calls++; assertEquals("안녕", s[0]); })`\uc548\uB155`;
  (function(s) { calls++; assertEquals("\xff", s[0]); })`\xff`;
  (function(s) { calls++; assertEquals("\n", s[0]); })`\n`;
  assertEquals(3, calls);

  // The TV of TemplateCharacter :: LineContinuation is the TV of
  // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is
  // the empty code unit sequence.
  calls = 0;
  (function(s) { calls++; assertEquals("", s[0]); })`\
`;
  assertEquals(1, calls);

  // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("test", s.raw[0]); })`test`;
  assertEquals(1, calls);

  // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("test", s.raw[0]); })`test${1}`;
  assertEquals(1, calls);

  // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test${2}`;
  assertEquals(1, calls);

  // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of
  // TemplateCharacters.
  calls = 0;
  (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test`;
  assertEquals(1, calls);

  // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of
  // TemplateCharacter.
  calls = 0;
  (function(s) { calls++; assertEquals("f", s.raw[0]); })`f`;
  assertEquals(1, calls);

  // The TRV of TemplateCharacter :: $ is the code unit value 0x0024.
  calls = 0;
  (function(s) { calls++; assertEquals("\u0024", s.raw[0]); })`$`;
  assertEquals(1, calls);

  // The TRV of EscapeSequence :: 0 is the code unit value 0x0030.
  calls = 0;
  (function(s) { calls++; assertEquals("\u005C\u0030", s.raw[0]); })`\0`;
  assertEquals(1, calls);

  // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting
  // of the code unit value 0x005C followed by the code units of TRV of
  // EscapeSequence.

  //   The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the
  //   HexEscapeSequence.
  calls = 0;
  (function(s) { calls++; assertEquals("\u005Cxff", s.raw[0]); })`\xff`;
  assertEquals(1, calls);

  //   The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the
  //   UnicodeEscapeSequence.
  calls = 0;
  (function(s) { calls++; assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`;
  assertEquals(1, calls);

  //   The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of
  //   the SingleEscapeCharacter.
  calls = 0;
  (function(s) { calls++; assertEquals("\u005C\u0027", s.raw[0]); })`\'`;
  (function(s) { calls++; assertEquals("\u005C\u0022", s.raw[0]); })`\"`;
  (function(s) { calls++; assertEquals("\u005C\u005C", s.raw[0]); })`\\`;
  (function(s) { calls++; assertEquals("\u005Cb", s.raw[0]); })`\b`;
  (function(s) { calls++; assertEquals("\u005Cf", s.raw[0]); })`\f`;
  (function(s) { calls++; assertEquals("\u005Cn", s.raw[0]); })`\n`;
  (function(s) { calls++; assertEquals("\u005Cr", s.raw[0]); })`\r`;
  (function(s) { calls++; assertEquals("\u005Ct", s.raw[0]); })`\t`;
  (function(s) { calls++; assertEquals("\u005Cv", s.raw[0]); })`\v`;
  (function(s) { calls++; assertEquals("\u005C`", s.raw[0]); })`\``;
  assertEquals(10, calls);

  //   The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the
  //   NonEscapeCharacter.
  calls = 0;
254
  (function(s) { calls++; assertEquals("\u005Cz", s.raw[0]); })`\z`;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  assertEquals(1, calls);

  // The TRV of LineTerminatorSequence :: <LF> is the code unit value 0x000A.
  // The TRV of LineTerminatorSequence :: <CR> is the code unit value 0x000A.
  // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence consisting of
  // the code unit value 0x000A.
  calls = 0;
  function testRawLineNormalization(cs) {
    calls++;
    assertEquals(cs.raw[0], "\n\n\n");
    assertEquals(cs.raw[1], "\n\n\n");
  }
  eval("testRawLineNormalization`\r\n\n\r${1}\r\n\n\r`");
  assertEquals(1, calls);

  // The TRV of LineContinuation :: \ LineTerminatorSequence is the sequence
  // consisting of the code unit value 0x005C followed by the code units of TRV
  // of LineTerminatorSequence.
  calls = 0;
  function testRawLineContinuation(cs) {
    calls++;
    assertEquals(cs.raw[0], "\u005C\n\u005C\n\u005C\n");
    assertEquals(cs.raw[1], "\u005C\n\u005C\n\u005C\n");
  }
  eval("testRawLineContinuation`\\\r\n\\\n\\\r${1}\\\r\n\\\n\\\r`");
  assertEquals(1, calls);
})();


(function testCallSiteObj() {
  var calls = 0;
  function tag(cs) {
    calls++;
    assertTrue(cs.hasOwnProperty("raw"));
    assertTrue(Object.isFrozen(cs));
    assertTrue(Object.isFrozen(cs.raw));
    var raw = Object.getOwnPropertyDescriptor(cs, "raw");
    assertFalse(raw.writable);
    assertFalse(raw.configurable);
    assertFalse(raw.enumerable);
    assertEquals(Array.prototype, Object.getPrototypeOf(cs.raw));
    assertTrue(Array.isArray(cs.raw));
    assertEquals(Array.prototype, Object.getPrototypeOf(cs));
    assertTrue(Array.isArray(cs));

    var cooked0 = Object.getOwnPropertyDescriptor(cs, "0");
    assertFalse(cooked0.writable);
    assertFalse(cooked0.configurable);
    assertTrue(cooked0.enumerable);

    var raw0 = Object.getOwnPropertyDescriptor(cs.raw, "0");
mike's avatar
mike committed
306 307 308
    assertFalse(raw0.writable);
    assertFalse(raw0.configurable);
    assertTrue(raw0.enumerable);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

    var length = Object.getOwnPropertyDescriptor(cs, "length");
    assertFalse(length.writable);
    assertFalse(length.configurable);
    assertFalse(length.enumerable);

    length = Object.getOwnPropertyDescriptor(cs.raw, "length");
    assertFalse(length.writable);
    assertFalse(length.configurable);
    assertFalse(length.enumerable);
  }
  tag`${1}`;
  assertEquals(1, calls);
})();


(function testUTF16ByteOrderMark() {
  assertEquals("\uFEFFtest", `\uFEFFtest`);
  assertEquals("\uFEFFtest", eval("`\uFEFFtest`"));
})();


331 332 333 334 335 336 337 338
(function testStringRawAsTagFn() {
  assertEquals("\\u0065\\`\\r\\r\\n\\ntestcheck",
               String.raw`\u0065\`\r\r\n\n${"test"}check`);
  assertEquals("\\\n\\\n\\\n", eval("String.raw`\\\r\\\r\n\\\n`"));
  assertEquals("", String.raw``);
})();


339 340 341 342 343 344
(function testCallSiteCaching() {
  var callSites = [];
  function tag(cs) { callSites.push(cs); }
  var a = 1;
  var b = 2;

345 346 347 348 349
  // Call-sites are cached by ParseNode. Same tag call in a loop
  // means same template object
  for (var i = 0; i < 2; ++i) {
    tag`head${i == 0 ? a : b}tail`;
  }
350 351 352
  assertEquals(2, callSites.length);
  assertSame(callSites[0], callSites[1]);

353 354 355 356 357
  // Tag calls within eval() never have the same ParseNode as the same tag
  // call from a different eval() invocation.
  for (var i = 0; i < 2; ++i) {
    eval("tag`head${i == 0 ? a : b}tail`");
  }
358
  assertEquals(4, callSites.length);
359 360
  assertTrue(callSites[1] !== callSites[2]);
  assertTrue(callSites[2] !== callSites[3]);
361 362 363

  (new Function("tag", "a", "b", "return tag`head${a}tail`;"))(tag, 1, 2);
  assertEquals(5, callSites.length);
364
  assertTrue(callSites[3] !== callSites[4]);
365 366 367

  (new Function("tag", "a", "b", "return tag`head${b}tail`;"))(tag, 1, 2);
  assertEquals(6, callSites.length);
368
  assertTrue(callSites[4] !== callSites[5]);
369 370 371 372 373 374 375 376 377 378 379

  callSites = [];

  tag`foo${a}bar`;
  tag`foo\${.}bar`;
  assertEquals(2, callSites.length);
  assertEquals(2, callSites[0].length);
  assertEquals(1, callSites[1].length);

  callSites = [];

380 381 382
  for (var i = 0; i < 2; ++i) {
    eval("tag`\\\r\n\\\n\\\r`");
  }
383
  assertEquals(2, callSites.length);
384
  assertTrue(callSites[0] !== callSites[1]);
385 386 387 388 389
  assertEquals("", callSites[0][0]);
  assertEquals("\\\n\\\n\\\n", callSites[0].raw[0]);

  callSites = [];

390 391 392
  for (var i = 0; i < 2; ++i) {
    tag`\uc548\ub155`;
  }
393 394 395 396 397 398 399 400 401 402 403 404 405 406
  assertEquals(2, callSites.length);
  assertSame(callSites[0], callSites[1]);
  assertEquals("안녕", callSites[0][0]);
  assertEquals("\\uc548\\ub155", callSites[0].raw[0]);

  callSites = [];

  tag`\uc548\ub155`;
  tag`안녕`;
  assertEquals(2, callSites.length);
  assertTrue(callSites[0] !== callSites[1]);
  assertEquals("안녕", callSites[0][0]);
  assertEquals("\\uc548\\ub155", callSites[0].raw[0]);
  assertEquals("안녕", callSites[1][0]);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  assertEquals("안녕", callSites[1].raw[0]);

  // Extra-thorough UTF8 decoding test.
  callSites = [];

  tag`Iñtërnâtiônàlizætiøn\u2603\uD83D\uDCA9`;
  tag`Iñtërnâtiônàlizætiøn☃💩`;

  assertEquals(2, callSites.length);
  assertTrue(callSites[0] !== callSites[1]);
  assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[0][0]);
  assertEquals(
      "Iñtërnâtiônàlizætiøn\\u2603\\uD83D\\uDCA9", callSites[0].raw[0]);
  assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1][0]);
  assertEquals("Iñtërnâtiônàlizætiøn☃💩", callSites[1].raw[0]);
422 423 424
})();


425 426 427 428
(function testExtendedArrayPrototype() {
  Object.defineProperty(Array.prototype, 0, {
    set: function() {
      assertUnreachable();
429 430
    },
    configurable: true
431 432 433
  });
  function tag(){}
  tag`a${1}b`;
434
  delete Array.prototype[0];
435
})();
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460


(function testRawLineNormalization() {
  function raw0(callSiteObj) {
    return callSiteObj.raw[0];
  }
  assertEquals(eval("raw0`\r`"), "\n");
  assertEquals(eval("raw0`\r\n`"), "\n");
  assertEquals(eval("raw0`\r\r\n`"), "\n\n");
  assertEquals(eval("raw0`\r\n\r\n`"), "\n\n");
  assertEquals(eval("raw0`\r\r\r\n`"), "\n\n\n");
})();


(function testHarmonyUnicode() {
  function raw0(callSiteObj) {
    return callSiteObj.raw[0];
  }
  assertEquals(raw0`a\u{62}c`, "a\\u{62}c");
  assertEquals(raw0`a\u{000062}c`, "a\\u{000062}c");
  assertEquals(raw0`a\u{0}c`, "a\\u{0}c");

  assertEquals(`a\u{62}c`, "abc");
  assertEquals(`a\u{000062}c`, "abc");
})();
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478


(function testLiteralAfterRightBrace() {
  // Regression test for https://code.google.com/p/v8/issues/detail?id=3734
  function f() {}
  `abc`;

  function g() {}`def`;

  {
    // block
  }
  `ghi`;

  {
    // block
  }`jkl`;
})();
479 480 481 482 483


(function testLegacyOctal() {
  assertEquals('\u0000', `\0`);
  assertEquals('\u0000a', `\0a`);
484
  for (var i = 0; i < 10; i++) {
485 486
    var code = "`\\0" + i + "`";
    assertThrows(code, SyntaxError);
487
    // Not an error if tagged.
488
    code = "(function(){})" + code;
489
    assertDoesNotThrow(code, SyntaxError);
490 491 492 493 494 495 496 497 498
  }

  assertEquals('\\0', String.raw`\0`);
})();


(function testSyntaxErrorsNonEscapeCharacter() {
  assertThrows("`\\x`", SyntaxError);
  assertThrows("`\\u`", SyntaxError);
499
  for (var i = 1; i < 8; i++) {
500 501
    var code = "`\\" + i + "`";
    assertThrows(code, SyntaxError);
502
    // Not an error if tagged.
503
    code = "(function(){})" + code;
504
    assertDoesNotThrow(code, SyntaxError);
505 506
  }
})();
507 508 509 510 511 512


(function testValidNumericEscapes() {
  assertEquals("8", `\8`);
  assertEquals("9", `\9`);
})();
513 514 515 516 517 518 519 520 521 522 523 524 525


(function testLegacyOctalEscapesInExpressions() {
  // Allowed in sloppy expression
  assertEquals("\x07", `${"\07"}`);

  // Disallowed in template tail
  assertThrows("`${\"\\07\"}\\07`", SyntaxError);

  // Disallowed in strict expression
  assertThrows("`${(function() { \"use strict\"; return \"\\07\"; })()}`",
               SyntaxError);
})();
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591


var global = this;
(function testCallNew() {
  "use strict";
  var called = false;
  var calledWith;
  global.log = function(x) { called = true; calledWith = x; }

  assertInstanceof(new Function`log("test")`, Object);
  assertTrue(called);
  assertSame("test", calledWith);
  delete global.log;
})();


(function testCallNew2() {
  "use strict";
  var log = [];
  function tag(x) {
    log.push(x);
    if (!(this instanceof tag)) {
      return tag;
    }
    this.x = x === void 0 ? null : x;
    return this;
  }
  // No arguments passed to constructor
  var instance = new tag`x``y``z`;
  assertInstanceof(instance, tag);
  assertSame(tag.prototype, Object.getPrototypeOf(instance));
  assertEquals({ x: null }, instance);
  assertEquals([["x"], ["y"], ["z"], undefined], log);

  // Arguments passed to constructor
  log.length = 0;
  instance = new tag`x2` `y2` `z2` (`test`);
  assertInstanceof(instance, tag);
  assertSame(tag.prototype, Object.getPrototypeOf(instance));
  assertEquals({ x: "test" }, instance);
  assertEquals([["x2"], ["y2"], ["z2"], "test"], log);
})();


(function testCallResultOfTagFn() {
  "use strict";
  var i = 0;
  var raw = [];
  function tag(cs) {
    var args = Array.prototype.slice.call(arguments);
    var text = String.raw.apply(null, args);
    if (i++ < 2) {
      raw.push("tag;" + text);
      return tag;
    }

    raw.push("raw;" + text);
    return text;
  }
  assertEquals("test3", tag`test1``test2``test3`);
  assertEquals([
    "tag;test1",
    "tag;test2",
    "raw;test3"
  ], raw);
})();
592 593


594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
(function testReturnValueAsTagFn() {
  "use strict";
  var i = 0;
  function makeTag() {
    return function tag(cs) {
      var args = Array.prototype.slice.call(arguments, 1);
      var rcs = [];
      rcs.raw = cs.map(function(s) {
        return '!' + s + '!';
      });
      args.unshift(rcs);
      return String.raw.apply(null, args);
    }
  }
  assertEquals('!hi!', makeTag()`hi`);
  assertEquals('!test!0!test!', makeTag()`test${0}test`);
  assertEquals('!!', makeTag()``);
});


614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
(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`);
})();


(function testToStringSubstitutionsOrder() {
  var subs = [];
  var log = [];
  function getter(name, value) {
    return {
      get: function() {
        log.push("get" + name);
        return value;
      },
      set: function(v) {
        log.push("set" + name);
      }
    };
  }
  Object.defineProperties(subs, {
    0: getter(0, "a"),
    1: getter(1, "b"),
    2: getter(2, "c")
  });

  assertEquals("-a-b-c-", `-${subs[0]}-${subs[1]}-${subs[2]}-`);
  assertArrayEquals(["get0", "get1", "get2"], log);
})();


(function testTaggedToStringSubstitutionsOrder() {
  var subs = [];
  var log = [];
  var tagged = [];
  function getter(name, value) {
    return {
      get: function() {
        log.push("get" + name);
        return value;
      },
      set: function(v) {
        log.push("set" + name);
      }
    };
  }
  Object.defineProperties(subs, {
    0: getter(0, 1),
    1: getter(1, 2),
    2: getter(2, 3)
  });

  function tag(cs) {
    var n_substitutions = arguments.length - 1;
    var n_cooked = cs.length;
    var e = cs[0];
    var i = 0;
    assertEquals(n_cooked, n_substitutions + 1);
    while (i < n_substitutions) {
      var sub = arguments[i++ + 1];
      var tail = cs[i];
      tagged.push(sub);
      e = e.concat(sub, tail);
    }
    return e;
  }

  assertEquals("-1-2-3-", tag`-${subs[0]}-${subs[1]}-${subs[2]}-`);
  assertArrayEquals(["get0", "get1", "get2"], log);
  assertArrayEquals([1, 2, 3], tagged);

  tagged.length = 0;
  log.length = 0;
  assertEquals("-1-", tag`-${subs[0]}-`);
  assertArrayEquals(["get0"], log);
  assertArrayEquals([1], tagged);
})();
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723


// Since the first argument to the tag function is always an array,
// eval calls will always just return that array.
(function testEvalTagStrict() {
  "use strict";
  var f = (x) => eval`a${x}b`;
  var result = f();
  assertEquals(["a", "b"], result);
  assertSame(result, f());
})();


(function testEvalTagSloppy() {
  var f = (x) => eval`a${x}b`;
  var result = f();
  assertEquals(["a", "b"], result);
  assertSame(result, f());
})();
724 725 726 727

(function testTaggedTemplateInvalidAssignmentTargetStrict() {
  "use strict";
  function f() {}
728 729 730 731 732
  assertThrows(() => Function("++f`foo`"), SyntaxError);
  assertThrows(() => Function("f`foo`++"), SyntaxError);
  assertThrows(() => Function("--f`foo`"), SyntaxError);
  assertThrows(() => Function("f`foo`--"), SyntaxError);
  assertThrows(() => Function("f`foo` = 1"), SyntaxError);
733 734 735 736
})();

(function testTaggedTemplateInvalidAssignmentTargetSloppy() {
  function f() {}
737 738 739 740 741
  assertThrows(() => Function("++f`foo`"), SyntaxError);
  assertThrows(() => Function("f`foo`++"), SyntaxError);
  assertThrows(() => Function("--f`foo`"), SyntaxError);
  assertThrows(() => Function("f`foo`--"), SyntaxError);
  assertThrows(() => Function("f`foo` = 1"), SyntaxError);
742
})();
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

// Disable eval caching if a tagged template occurs in a nested function
var v = 0;
var templates = [];
function tag(callSite) { templates.push(callSite); }
for (v = 0; v < 6; v += 2) {
  eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${v}world` })()");
  assertSame(templates[v], templates[v + 1]);
}
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);

function makeSource1(id) {
  return `function f() {
    for (var i = 0; i < 2; ++i) tag\`Hello${id}world\`;
  }
  f();`;
}
templates = [];
for (v = 0; v < 6; v += 2) {
  eval(makeSource1(v));
  assertSame(templates[v], templates[v + 1]);
}
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);

templates = [];
eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })()");
eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()");
eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()");
assertSame(templates[0], templates[1]);
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertSame(templates[2], templates[3]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);
assertSame(templates[4],templates[5]);

templates = [];
eval(makeSource1(1));
eval(makeSource1(2));
eval(makeSource1(3));
assertSame(templates[0], templates[1]);
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertSame(templates[2], templates[3]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);
assertSame(templates[4],templates[5]);

// Disable eval caching if a tagged template occurs in an even deeper nested function
var v = 0;
templates = [];
for (v = 0; v < 6; v += 2) {
    eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${v}world` })() })()");
    if (!v) continue;
    assertNotSame(templates[v], templates[v - 1]);
}
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);

function makeSource2(id) {
  return `function f() {
    function innerF() {
      for (var i = 0; i < 2; ++i) tag\`Hello${id}world\`;
    }
    return innerF();
  }
  f();`;
}
templates = [];
for (v = 0; v < 6; v += 2) {
  eval(makeSource2(v));
  assertSame(templates[v], templates[v + 1]);
}
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);

templates = [];
eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })() })()");
eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()");
eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()");
assertSame(templates[0], templates[1]);
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertSame(templates[2], templates[3]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);
assertSame(templates[4], templates[5]);

templates = [];
eval(makeSource2(1));
eval(makeSource2(2));
eval(makeSource2(3));
assertSame(templates[0], templates[1]);
assertNotSame(templates[0], templates[2]);
assertNotSame(templates[0], templates[4]);
assertNotSame(templates[1], templates[3]);
assertNotSame(templates[1], templates[5]);
assertSame(templates[2], templates[3]);
assertNotSame(templates[2], templates[4]);
assertNotSame(templates[3], templates[5]);
assertSame(templates[4], templates[5]);