test-bytecode-generator.cc 86.9 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6
#include <fstream>

7
#include "src/init/v8.h"
8

9
#include "src/interpreter/bytecode-array-iterator.h"
10 11
#include "src/interpreter/bytecode-generator.h"
#include "src/interpreter/interpreter.h"
12
#include "src/objects/objects-inl.h"
13
#include "test/cctest/cctest.h"
14
#include "test/cctest/interpreter/bytecode-expectations-printer.h"
15
#include "test/cctest/test-feedback-vector.h"
16 17 18 19 20

namespace v8 {
namespace internal {
namespace interpreter {

21 22
#define XSTR(A) #A
#define STR(A) XSTR(A)
23

24
#define UNIQUE_VAR() "var a" STR(__COUNTER__) " = 0;\n"
25

26 27
#define LOAD_UNIQUE_PROPERTY() "  b.name" STR(__COUNTER__) ";\n"

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#define REPEAT_2(...) __VA_ARGS__ __VA_ARGS__
#define REPEAT_4(...) REPEAT_2(__VA_ARGS__) REPEAT_2(__VA_ARGS__)
#define REPEAT_8(...) REPEAT_4(__VA_ARGS__) REPEAT_4(__VA_ARGS__)
#define REPEAT_16(...) REPEAT_8(__VA_ARGS__) REPEAT_8(__VA_ARGS__)
#define REPEAT_32(...) REPEAT_16(__VA_ARGS__) REPEAT_16(__VA_ARGS__)
#define REPEAT_64(...) REPEAT_32(__VA_ARGS__) REPEAT_32(__VA_ARGS__)
#define REPEAT_128(...) REPEAT_64(__VA_ARGS__) REPEAT_64(__VA_ARGS__)
#define REPEAT_256(...) REPEAT_128(__VA_ARGS__) REPEAT_128(__VA_ARGS__)

#define REPEAT_127(...)  \
  REPEAT_64(__VA_ARGS__) \
  REPEAT_32(__VA_ARGS__) \
  REPEAT_16(__VA_ARGS__) \
  REPEAT_8(__VA_ARGS__)  \
  REPEAT_4(__VA_ARGS__)  \
  REPEAT_2(__VA_ARGS__)  \
  __VA_ARGS__
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define REPEAT_249(...)   \
  REPEAT_127(__VA_ARGS__) \
  REPEAT_64(__VA_ARGS__)  \
  REPEAT_32(__VA_ARGS__)  \
  REPEAT_16(__VA_ARGS__)  \
  REPEAT_8(__VA_ARGS__)   \
  REPEAT_2(__VA_ARGS__)

#define REPEAT_2_UNIQUE_VARS() UNIQUE_VAR() UNIQUE_VAR()
#define REPEAT_4_UNIQUE_VARS() REPEAT_2_UNIQUE_VARS() REPEAT_2_UNIQUE_VARS()
#define REPEAT_8_UNIQUE_VARS() REPEAT_4_UNIQUE_VARS() REPEAT_4_UNIQUE_VARS()
#define REPEAT_16_UNIQUE_VARS() REPEAT_8_UNIQUE_VARS() REPEAT_8_UNIQUE_VARS()
#define REPEAT_32_UNIQUE_VARS() REPEAT_16_UNIQUE_VARS() REPEAT_16_UNIQUE_VARS()
#define REPEAT_64_UNIQUE_VARS() REPEAT_32_UNIQUE_VARS() REPEAT_32_UNIQUE_VARS()
#define REPEAT_128_UNIQUE_VARS() REPEAT_64_UNIQUE_VARS() REPEAT_64_UNIQUE_VARS()

62
#define REPEAT_252_UNIQUE_VARS() \
63 64 65 66 67
  REPEAT_128_UNIQUE_VARS()       \
  REPEAT_64_UNIQUE_VARS()        \
  REPEAT_32_UNIQUE_VARS()        \
  REPEAT_16_UNIQUE_VARS()        \
  REPEAT_8_UNIQUE_VARS()         \
68
  REPEAT_4_UNIQUE_VARS()
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#define REPEAT_2_LOAD_UNIQUE_PROPERTY() \
  LOAD_UNIQUE_PROPERTY() LOAD_UNIQUE_PROPERTY()
#define REPEAT_4_LOAD_UNIQUE_PROPERTY() \
  REPEAT_2_LOAD_UNIQUE_PROPERTY() REPEAT_2_LOAD_UNIQUE_PROPERTY()
#define REPEAT_8_LOAD_UNIQUE_PROPERTY() \
  REPEAT_4_LOAD_UNIQUE_PROPERTY() REPEAT_4_LOAD_UNIQUE_PROPERTY()
#define REPEAT_16_LOAD_UNIQUE_PROPERTY() \
  REPEAT_8_LOAD_UNIQUE_PROPERTY() REPEAT_8_LOAD_UNIQUE_PROPERTY()
#define REPEAT_32_LOAD_UNIQUE_PROPERTY() \
  REPEAT_16_LOAD_UNIQUE_PROPERTY() REPEAT_16_LOAD_UNIQUE_PROPERTY()
#define REPEAT_64_LOAD_UNIQUE_PROPERTY() \
  REPEAT_32_LOAD_UNIQUE_PROPERTY() REPEAT_32_LOAD_UNIQUE_PROPERTY()
#define REPEAT_128_LOAD_UNIQUE_PROPERTY() \
  REPEAT_64_LOAD_UNIQUE_PROPERTY() REPEAT_64_LOAD_UNIQUE_PROPERTY()

85 86 87 88 89 90
static const char* kGoldenFileDirectory =
    "test/cctest/interpreter/bytecode_expectations/";

class InitializedIgnitionHandleScope : public InitializedHandleScope {
 public:
  InitializedIgnitionHandleScope() {
91
    i::FLAG_always_opt = false;
92
    i::FLAG_allow_natives_syntax = true;
93
    i::FLAG_enable_lazy_source_positions = false;
94
  }
95
};
96

97
void SkipGoldenFileHeader(std::istream* stream) {
98 99
  std::string line;
  int separators_seen = 0;
100
  while (std::getline(*stream, line)) {
101 102
    if (line == "---") separators_seen += 1;
    if (separators_seen == 2) return;
103
  }
104
}
105

106 107 108
std::string LoadGolden(const std::string& golden_filename) {
  std::ifstream expected_file((kGoldenFileDirectory + golden_filename).c_str());
  CHECK(expected_file.is_open());
109
  SkipGoldenFileHeader(&expected_file);
110 111 112 113 114
  std::ostringstream expected_stream;
  // Restore the first separator, which was consumed by SkipGoldenFileHeader
  expected_stream << "---\n" << expected_file.rdbuf();
  return expected_stream.str();
}
115

116 117 118 119 120 121 122 123 124 125 126
template <size_t N>
std::string BuildActual(const BytecodeExpectationsPrinter& printer,
                        const char* (&snippet_list)[N],
                        const char* prologue = nullptr,
                        const char* epilogue = nullptr) {
  std::ostringstream actual_stream;
  for (const char* snippet : snippet_list) {
    std::string source_code;
    if (prologue) source_code += prologue;
    source_code += snippet;
    if (epilogue) source_code += epilogue;
127
    printer.PrintExpectation(&actual_stream, source_code);
128
  }
129 130
  return actual_stream.str();
}
131

132
// inplace left trim
133 134 135 136
static inline void ltrim(std::string* str) {
  str->erase(str->begin(),
             std::find_if(str->begin(), str->end(),
                          [](unsigned char ch) { return !std::isspace(ch); }));
137 138 139
}

// inplace right trim
140 141 142 143 144
static inline void rtrim(std::string* str) {
  str->erase(std::find_if(str->rbegin(), str->rend(),
                          [](unsigned char ch) { return !std::isspace(ch); })
                 .base(),
             str->end());
145 146
}

147
static inline std::string trim(std::string* str) {
148 149
  ltrim(str);
  rtrim(str);
150
  return *str;
151 152
}

153 154 155 156 157 158 159
bool CompareTexts(const std::string& generated, const std::string& expected) {
  std::istringstream generated_stream(generated);
  std::istringstream expected_stream(expected);
  std::string generated_line;
  std::string expected_line;
  // Line number does not include golden file header.
  int line_number = 0;
160
  bool strings_match = true;
161 162 163 164 165 166

  do {
    std::getline(generated_stream, generated_line);
    std::getline(expected_stream, expected_line);

    if (!generated_stream.good() && !expected_stream.good()) {
167
      return strings_match;
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    }

    if (!generated_stream.good()) {
      std::cerr << "Expected has extra lines after line " << line_number
                << "\n";
      std::cerr << "  Expected: '" << expected_line << "'\n";
      return false;
    } else if (!expected_stream.good()) {
      std::cerr << "Generated has extra lines after line " << line_number
                << "\n";
      std::cerr << "  Generated: '" << generated_line << "'\n";
      return false;
    }

182
    if (trim(&generated_line) != trim(&expected_line)) {
183 184 185
      std::cerr << "Inputs differ at line " << line_number << "\n";
      std::cerr << "  Generated: '" << generated_line << "'\n";
      std::cerr << "  Expected:  '" << expected_line << "'\n";
186
      strings_match = false;
187 188 189 190 191
    }
    line_number++;
  } while (true);
}

192 193
TEST(PrimitiveReturnStatements) {
  InitializedIgnitionHandleScope scope;
194
  BytecodeExpectationsPrinter printer(CcTest::isolate());
195 196
  const char* snippets[] = {
      "",
197

198
      "return;\n",
199

200
      "return null;\n",
201

202
      "return true;\n",
203

204
      "return false;\n",
205

206
      "return 0;\n",
207

208
      "return +1;\n",
209

210
      "return -1;\n",
211

212
      "return +127;\n",
213

214
      "return -128;\n",
215 216

      "return 2.0;\n",
217 218
  };

219 220
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrimitiveReturnStatements.golden")));
221 222
}

223 224
TEST(PrimitiveExpressions) {
  InitializedIgnitionHandleScope scope;
225
  BytecodeExpectationsPrinter printer(CcTest::isolate());
226
  const char* snippets[] = {
227
      "var x = 0; return x;\n",
228

229
      "var x = 0; return x + 3;\n",
230

231 232
      "var x = 0; return 3 + x;\n",

233
      "var x = 0; return x - 3;\n",
234

235 236
      "var x = 0; return 3 - x;\n",

237
      "var x = 4; return x * 3;\n",
238

239 240
      "var x = 4; return 3 * x;\n",

241
      "var x = 4; return x / 3;\n",
242

243 244
      "var x = 4; return 3 / x;\n",

245
      "var x = 4; return x % 3;\n",
246

247 248
      "var x = 4; return 3 % x;\n",

249
      "var x = 1; return x | 2;\n",
250

251 252
      "var x = 1; return 2 | x;\n",

253
      "var x = 1; return x ^ 2;\n",
254

255 256
      "var x = 1; return 2 ^ x;\n",

257
      "var x = 1; return x & 2;\n",
258

259 260
      "var x = 1; return 2 & x;\n",

261
      "var x = 10; return x << 3;\n",
262

263 264
      "var x = 10; return 3 << x;\n",

265
      "var x = 10; return x >> 3;\n",
266

267 268
      "var x = 10; return 3 >> x;\n",

269
      "var x = 10; return x >>> 3;\n",
270

271 272
      "var x = 10; return 3 >>> x;\n",

273
      "var x = 0; return (x, 3);\n",
274 275
  };

276 277
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrimitiveExpressions.golden")));
278 279
}

280 281
TEST(LogicalExpressions) {
  InitializedIgnitionHandleScope scope;
282
  BytecodeExpectationsPrinter printer(CcTest::isolate());
283
  const char* snippets[] = {
284
      "var x = 0; return x || 3;\n",
285

286
      "var x = 0; return (x == 1) || 3;\n",
287

288
      "var x = 0; return x && 3;\n",
289

290
      "var x = 0; return (x == 0) && 3;\n",
291

292
      "var x = 0; return x || (1, 2, 3);\n",
293

294
      "var a = 2, b = 3, c = 4; return a || (a, b, a, b, c = 5, 3);\n",
295

296 297
      "var x = 1; var a = 2, b = 3; return x || ("  //
      REPEAT_32("\n  a = 1, b = 2, ")               //
298
      "3);\n",
299

300 301
      "var x = 0; var a = 2, b = 3; return x && ("  //
      REPEAT_32("\n  a = 1, b = 2, ")               //
302
      "3);\n",
303 304 305

      "var x = 1; var a = 2, b = 3; return (x > 3) || ("  //
      REPEAT_32("\n  a = 1, b = 2, ")                     //
306
      "3);\n",
307 308 309

      "var x = 0; var a = 2, b = 3; return (x < 5) && ("  //
      REPEAT_32("\n  a = 1, b = 2, ")                     //
310
      "3);\n",
311

312
      "return 0 && 3;\n",
313

314
      "return 1 || 3;\n",
315

316
      "var x = 1; return x && 3 || 0, 1;\n",
317 318
  };

319 320
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("LogicalExpressions.golden")));
321 322
}

323 324
TEST(Parameters) {
  InitializedIgnitionHandleScope scope;
325
  BytecodeExpectationsPrinter printer(CcTest::isolate());
326 327 328 329 330
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f() { return this; }",
331

332 333 334 335 336 337 338 339 340 341 342
      "function f(arg1) { return arg1; }",

      "function f(arg1) { return this; }",

      "function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return arg4; }",

      "function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return this; }",

      "function f(arg1) { arg1 = 1; }",

      "function f(arg1, arg2, arg3, arg4) { arg2 = 1; }",
343
  };
344

345 346
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("Parameters.golden")));
347
}
348

349 350
TEST(IntegerConstants) {
  InitializedIgnitionHandleScope scope;
351
  BytecodeExpectationsPrinter printer(CcTest::isolate());
352
  const char* snippets[] = {
353
      "return 12345678;\n",
354

355
      "var a = 1234; return 5678;\n",
356

357
      "var a = 1234; return 1234;\n",
358
  };
359

360 361
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("IntegerConstants.golden")));
362
}
363

364 365
TEST(HeapNumberConstants) {
  InitializedIgnitionHandleScope scope;
366
  BytecodeExpectationsPrinter printer(CcTest::isolate());
367
  const char* snippets[] = {
368
      "return 1.2;\n",
369

370
      "var a = 1.2; return 2.6;\n",
371

372
      "var a = 3.14; return 3.14;\n",
373
  };
374

375 376
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("HeapNumberConstants.golden")));
377 378
}

379 380
TEST(StringConstants) {
  InitializedIgnitionHandleScope scope;
381
  BytecodeExpectationsPrinter printer(CcTest::isolate());
382
  const char* snippets[] = {
383
      "return \"This is a string\";\n",
384

385
      "var a = \"First string\"; return \"Second string\";\n",
386

387
      "var a = \"Same string\"; return \"Same string\";\n",
388
  };
389

390 391
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StringConstants.golden")));
392 393
}

394 395
TEST(PropertyLoads) {
  InitializedIgnitionHandleScope scope;
396
  BytecodeExpectationsPrinter printer(CcTest::isolate());
397 398
  printer.set_wrap(false);
  printer.set_test_function_name("f");
399

400 401
  const char* snippets[] = {
    "function f(a) { return a.name; }\n"
402
    "f({name : \"test\"});\n",
403 404

    "function f(a) { return a[\"key\"]; }\n"
405
    "f({key : \"test\"});\n",
406 407

    "function f(a) { return a[100]; }\n"
408
    "f({100 : \"test\"});\n",
409 410

    "function f(a, b) { return a[b]; }\n"
411
    "f({arg : \"test\"}, \"arg\");\n",
412 413 414 415 416

    "function f(a) { var b = a.name; return a[-124]; }\n"
    "f({\"-124\" : \"test\", name : 123 })",

    "function f(a) {\n"
417 418
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
419 420 421 422 423 424 425 426 427 428 429
    "  return a.name;\n"
    "}\n"
    "f({name : \"test\"})\n",

    "function f(a, b) {\n"
    "  var c;\n"
    "  c = a[b];\n"
    REPEAT_127("  c = a[b];\n")
    "  return a[b];\n"
    "}\n"
    "f({name : \"test\"}, \"name\")\n",
430 431
  };

432 433
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PropertyLoads.golden")));
434 435
}

436 437 438 439 440
TEST(PropertyLoadStoreOneShot) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_top_level(true);
441 442
  printer.set_oneshot_opt(true);

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  const char* snippets[] = {
      R"(
      l = {
        'a': 1,
        'b': 2
      };

      v = l['a'] + l['b'];
      l['b'] = 7;
      l['a'] = l['b'];
      )",

      R"(
      l = {
        'a': 1.1,
        'b': 2.2
      };
      for (i = 0; i < 5; ++i) {
        l['a'] = l['a'] + l['b'];
        l['b'] = l['a'] + l['b'];
      }
      )",

      R"(
      l = {
        'a': 1.1,
        'b': 2.2
      };
      while (s > 0) {
        l['a']  = l['a'] - l['b'];
        l['b']  = l['b'] - l['a'];
      }
      )",

      R"(
      l = {
        'a': 1.1,
        'b': 2.2
      };
      s = 10;
      do {
        l['a'] = l['b'] - l['a'];
      } while (s < 10);
      )",

      R"(
      l = {
        'c': 1.1,
        'd': 2.2
      };
      if (l['c'] < 3) {
        l['c'] = 3;
      } else {
        l['d'] = 3;
      }
      )",
499 500 501 502 503 504 505 506

      R"(
      a = [1.1, [2.2, 4.5]];
      )",

      R"(
      b = [];
      )",
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 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
  };
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PropertyLoadStoreOneShot.golden")));
}

TEST(PropertyLoadStoreWithoutOneShot) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_top_level(true);

  const char* snippets[] = {
      R"(
      l = {
        'aa': 1.1,
        'bb': 2.2
      };

      v = l['aa'] + l['bb'];
      l['bb'] = 7;
      l['aa'] = l['bb'];
      )",

      R"(
      l = {
        'cc': 3.1,
        'dd': 4.2
      };
      if (l['cc'] < 3) {
        l['cc'] = 3;
      } else {
        l['dd'] = 3;
      }
      )",
  };
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PropertyLoadStoreWithoutOneShot.golden")));
}

TEST(IIFEWithOneshotOpt) {
  InitializedIgnitionHandleScope scope;
  v8::Isolate* isolate = CcTest::isolate();
  BytecodeExpectationsPrinter printer(isolate);
  printer.set_wrap(false);
  printer.set_top_level(true);
  printer.set_print_callee(true);
553
  printer.set_oneshot_opt(true);
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

  const char* snippets[] = {
      // No feedback vectors for top-level loads/store named property in an IIFE
      R"(
      (function() {
        l = {};
        l.aa = 2;
        l.bb = l.aa;
        return arguments.callee;
      })();
    )",
      // Normal load/store within loops of an IIFE
      R"(
      (function() {
        l = {};
        for (i = 0; i < 5; ++i) {
          l.aa = 2;
          l.bb = l.aa;
        }
        return arguments.callee;
      })();
    )",

      R"(
      (function() {
        l = {};
        c = 4;
        while(c > 4) {
          l.aa = 2;
          l.bb = l.aa;
          c--;
        }
        return arguments.callee;
      })();
    )",

      R"(
      (function() {
        l = {};
        c = 4;
        do {
          l.aa = 2;
          l.bb = l.aa;
          c--;
        } while(c > 4)
        return arguments.callee;
      })();
    )",
      // No feedback vectors for loads/stores in conditionals
      R"(
      (function() {
        l = {
          'aa': 3.3,
          'bb': 4.4
        };
        if (l.aa < 3) {
          l.aa = 3;
        } else {
          l.aa = l.bb;
        }
        return arguments.callee;
      })();
    )",
617 618 619 620 621 622 623 624 625 626 627 628 629 630

      R"(
      (function() {
        a = [0, [1, 1,2,], 3];
        return arguments.callee;
      })();
    )",

      R"(
      (function() {
        a = [];
        return arguments.callee;
      })();
    )",
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
      // CallNoFeedback instead of CallProperty
      R"(
      this.f0 = function() {};
      this.f1 = function(a) {};
      this.f2 = function(a, b) {};
      this.f3 = function(a, b, c) {};
      this.f4 = function(a, b, c, d) {};
      this.f5 = function(a, b, c, d, e) {};
      (function() {
        this.f0();
        this.f1(1);
        this.f2(1, 2);
        this.f3(1, 2, 3);
        this.f4(1, 2, 3, 4);
        this.f5(1, 2, 3, 4, 5);
        return arguments.callee;
      })();
    )",
      // CallNoFeedback instead of CallUndefinedReceiver
      R"(
      function f0() {}
      function f1(a) {}
      function f2(a, b) {}
      function f3(a, b, c) {}
      function f4(a, b, c, d) {}
      function f5(a, b, c, d, e) {}
      (function() {
        f0();
        f1(1);
        f2(1, 2);
        f3(1, 2, 3);
        f4(1, 2, 3, 4);
        f5(1, 2, 3, 4, 5);
        return arguments.callee;
      })();
    )",
667
      // TODO(rmcilroy): Make this function produce one-shot code.
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
      R"(
      var t = 0;
      function f2() {};
      if (t == 0) {
        (function(){
          l = {};
          l.a = 3;
          l.b = 4;
          f2();
          return arguments.callee;
        })();
      }
    )",
      // No one-shot opt for IIFE`s within a function
      R"(
        function f2() {};
        function f() {
          return (function(){
            l = {};
            l.a = 3;
            l.b = 4;
            f2();
            return arguments.callee;
          })();
        }
        f();
    )",
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
      R"(
        var f = function(l) {  l.a = 3; return l; };
        f({});
        f;
    )",
      // No one-shot opt for top-level functions enclosed in parentheses
      R"(
        var f = (function(l) {  l.a = 3; return l; });
        f;
    )",
      R"(
        var f = (function foo(l) {  l.a = 3; return l; });
        f;
    )",
      R"(
        var f = function foo(l) {  l.a = 3; return l; };
        f({});
        f;
    )",
      R"(
        l = {};
        var f = (function foo(l) {  l.a = 3; return arguments.callee; })(l);
        f;
    )",
      R"(
        var f = (function foo(l) {  l.a = 3; return arguments.callee; })({});
        f;
    )",
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
  };
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("IIFEWithOneshotOpt.golden")));
}

TEST(IIFEWithoutOneshotOpt) {
  InitializedIgnitionHandleScope scope;
  v8::Isolate* isolate = CcTest::isolate();
  BytecodeExpectationsPrinter printer(isolate);
  printer.set_wrap(false);
  printer.set_top_level(true);
  printer.set_print_callee(true);

  const char* snippets[] = {
      R"(
      (function() {
        l = {};
        l.a = 2;
        l.b = l.a;
        return arguments.callee;
      })();
    )",
      R"(
      (function() {
        l = {
          'a': 4.3,
          'b': 3.4
        };
        if (l.a < 3) {
          l.a = 3;
        } else {
          l.a = l.b;
        }
        return arguments.callee;
      })();
    )",
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
      R"(
      this.f0 = function() {};
      this.f1 = function(a) {};
      this.f2 = function(a, b) {};
      this.f3 = function(a, b, c) {};
      this.f4 = function(a, b, c, d) {};
      this.f5 = function(a, b, c, d, e) {};
      (function() {
        this.f0();
        this.f1(1);
        this.f2(1, 2);
        this.f3(1, 2, 3);
        this.f4(1, 2, 3, 4);
        this.f5(1, 2, 3, 4, 5);
        return arguments.callee;
      })();
    )",
      R"(
      function f0() {}
      function f1(a) {}
      function f2(a, b) {}
      function f3(a, b, c) {}
      function f4(a, b, c, d) {}
      function f5(a, b, c, d, e) {}
      (function() {
        f0();
        f1(1);
        f2(1, 2);
        f3(1, 2, 3);
        f4(1, 2, 3, 4);
        f5(1, 2, 3, 4, 5);
        return arguments.callee;
      })();
    )",
793 794 795 796 797
  };
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("IIFEWithoutOneshotOpt.golden")));
}

798 799
TEST(PropertyStores) {
  InitializedIgnitionHandleScope scope;
800
  BytecodeExpectationsPrinter printer(CcTest::isolate());
801 802
  printer.set_wrap(false);
  printer.set_test_function_name("f");
803

804 805 806
  const char* snippets[] = {
    "function f(a) { a.name = \"val\"; }\n"
    "f({name : \"test\"})",
807

808 809
    "function f(a) { a[\"key\"] = \"val\"; }\n"
    "f({key : \"test\"})",
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    "function f(a) { a[100] = \"val\"; }\n"
    "f({100 : \"test\"})",

    "function f(a, b) { a[b] = \"val\"; }\n"
    "f({arg : \"test\"}, \"arg\")",

    "function f(a) { a.name = a[-124]; }\n"
    "f({\"-124\" : \"test\", name : 123 })",

    "function f(a) { \"use strict\"; a.name = \"val\"; }\n"
    "f({name : \"test\"})",

    "function f(a, b) { \"use strict\"; a[b] = \"val\"; }\n"
    "f({arg : \"test\"}, \"arg\")",

    "function f(a) {\n"
    "  a.name = 1;\n"
828 829
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
830 831 832 833 834 835 836
    "  a.name = 2;\n"
    "}\n"
    "f({name : \"test\"})\n",

    "function f(a) {\n"
    " 'use strict';\n"
    "  a.name = 1;\n"
837 838
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    "  a.name = 2;\n"
    "}\n"
    "f({name : \"test\"})\n",

    "function f(a, b) {\n"
    "  a[b] = 1;\n"
    REPEAT_127("  a[b] = 1;\n")
    "  a[b] = 2;\n"
    "}\n"
    "f({name : \"test\"})\n",

    "function f(a, b) {\n"
    "  'use strict';\n"
    "  a[b] = 1;\n"
    REPEAT_127("  a[b] = 1;\n")
    "  a[b] = 2;\n"
    "}\n"
    "f({name : \"test\"})\n",
857
  };
858

859 860
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PropertyStores.golden")));
861 862
}

863
#define FUNC_ARG "new (function Obj() { this.func = function() { return; }})()"
864

865 866
TEST(PropertyCall) {
  InitializedIgnitionHandleScope scope;
867
  BytecodeExpectationsPrinter printer(CcTest::isolate());
868 869 870 871 872 873 874 875 876 877 878 879 880 881
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f(a) { return a.func(); }\n"
      "f(" FUNC_ARG ")",

      "function f(a, b, c) { return a.func(b, c); }\n"
      "f(" FUNC_ARG ", 1, 2)",

      "function f(a, b) { return a.func(b + b, b); }\n"
      "f(" FUNC_ARG ", 1)",

      "function f(a) {\n"
882 883 884 885
      "  var b = {};\n"
      REPEAT_128_LOAD_UNIQUE_PROPERTY()
      "  a.func;\n"              //
      "  return a.func(); }\n"
886
      "f(" FUNC_ARG ")",
887 888 889

      "function f(a) { return a.func(1).func(2).func(3); }\n"
      "f(new (function Obj() { this.func = function(a) { return this; }})())",
890 891
  };

892 893
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PropertyCall.golden")));
894 895
}

896 897
TEST(LoadGlobal) {
  InitializedIgnitionHandleScope scope;
898
  BytecodeExpectationsPrinter printer(CcTest::isolate());
899 900
  printer.set_wrap(false);
  printer.set_test_function_name("f");
901

902 903 904 905 906 907 908 909 910 911 912 913 914 915
  const char* snippets[] = {
    "var a = 1;\n"
    "function f() { return a; }\n"
    "f()",

    "function t() { }\n"
    "function f() { return t; }\n"
    "f()",

    "a = 1;\n"
    "function f() { return a; }\n"
    "f()",

    "a = 1;\n"
916 917 918
    "function f(c) {\n"
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
919 920
    "  return a;\n"
    "}\n"
921
    "f({name: 1});\n",
922 923
  };

924 925
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("LoadGlobal.golden")));
926 927
}

928 929
TEST(StoreGlobal) {
  InitializedIgnitionHandleScope scope;
930
  BytecodeExpectationsPrinter printer(CcTest::isolate());
931 932
  printer.set_wrap(false);
  printer.set_test_function_name("f");
933

934 935 936
  const char* snippets[] = {
    "var a = 1;\n"
    "function f() { a = 2; }\n"
937
    "f();\n",
938 939

    "var a = \"test\"; function f(b) { a = b; }\n"
940
    "f(\"global\");\n",
941 942 943

    "'use strict'; var a = 1;\n"
    "function f() { a = 2; }\n"
944
    "f();\n",
945 946 947

    "a = 1;\n"
    "function f() { a = 2; }\n"
948
    "f();\n",
949 950

    "a = 1;\n"
951 952 953
    "function f(c) {\n"
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
954 955
    "  a = 2;\n"
    "}\n"
956
    "f({name: 1});\n",
957 958

    "a = 1;\n"
959
    "function f(c) {\n"
960
    "  'use strict';\n"
961 962
    "  var b = {};\n"
    REPEAT_128_LOAD_UNIQUE_PROPERTY()
963 964
    "  a = 2;\n"
    "}\n"
965
    "f({name: 1});\n",
966 967
  };

968 969
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StoreGlobal.golden")));
970 971
}

972 973
TEST(CallGlobal) {
  InitializedIgnitionHandleScope scope;
974
  BytecodeExpectationsPrinter printer(CcTest::isolate());
975 976 977 978 979 980
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function t() { }\n"
      "function f() { return t(); }\n"
981
      "f();\n",
982

983 984
      "function t(a, b, c) { }\n"
      "function f() { return t(1, 2, 3); }\n"
985
      "f();\n",
986 987
  };

988 989
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CallGlobal.golden")));
990 991
}

992 993
TEST(CallRuntime) {
  InitializedIgnitionHandleScope scope;
994
  BytecodeExpectationsPrinter printer(CcTest::isolate());
995 996
  printer.set_wrap(false);
  printer.set_test_function_name("f");
997

998 999
  const char* snippets[] = {
      "function f() { %TheHole() }\n"
1000
      "f();\n",
1001 1002

      "function f(a) { return %IsArray(a) }\n"
1003
      "f(undefined);\n",
1004 1005

      "function f() { return %Add(1, 2) }\n"
1006
      "f();\n",
1007
  };
1008

1009 1010
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CallRuntime.golden")));
1011 1012
}

1013 1014
TEST(IfConditions) {
  InitializedIgnitionHandleScope scope;
1015
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
    "function f() {\n"
    "  if (0) {\n"
    "    return 1;\n"
    "  } else {\n"
    "    return -1;\n"
    "  }\n"
    "};\n"
1027
    "f();\n",
1028 1029 1030 1031 1032 1033 1034 1035

    "function f() {\n"
    "  if ('lucky') {\n"
    "    return 1;\n"
    "  } else {\n"
    "    return -1;\n"
    "  }\n"
    "};\n"
1036
    "f();\n",
1037 1038 1039 1040 1041 1042 1043 1044

    "function f() {\n"
    "  if (false) {\n"
    "    return 1;\n"
    "  } else {\n"
    "    return -1;\n"
    "  }\n"
    "};\n"
1045
    "f();\n",
1046 1047 1048 1049 1050 1051

    "function f() {\n"
    "  if (false) {\n"
    "    return 1;\n"
    "  }\n"
    "};\n"
1052
    "f();\n",
1053 1054 1055 1056 1057 1058 1059 1060 1061

    "function f() {\n"
    "  var a = 1;\n"
    "  if (a) {\n"
    "    a += 1;\n"
    "  } else {\n"
    "    return 2;\n"
    "  }\n"
    "};\n"
1062
    "f();\n",
1063 1064 1065 1066 1067 1068 1069 1070

    "function f(a) {\n"
    "  if (a <= 0) {\n"
    "    return 200;\n"
    "  } else {\n"
    "    return -200;\n"
    "  }\n"
    "};\n"
1071
    "f(99);\n",
1072 1073

    "function f(a, b) { if (a in b) { return 200; } }"
1074
    "f('prop', { prop: 'yes'});\n",
1075 1076 1077

    "function f(z) { var a = 0; var b = 0; if (a === 0.01) {\n"
    REPEAT_64("  b = a; a = b;\n")
1078
    " return 200; } else { return -200; } } f(0.001);\n",
1079 1080 1081 1082 1083 1084 1085

    "function f() {\n"
    "  var a = 0; var b = 0;\n"
    "  if (a) {\n"
    REPEAT_64("  b = a; a = b;\n")
    "  return 200; } else { return -200; }\n"
    "};\n"
1086
    "f();\n",
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

    "function f(a, b) {\n"
    "  if (a == b) { return 1; }\n"
    "  if (a === b) { return 1; }\n"
    "  if (a < b) { return 1; }\n"
    "  if (a > b) { return 1; }\n"
    "  if (a <= b) { return 1; }\n"
    "  if (a >= b) { return 1; }\n"
    "  if (a in b) { return 1; }\n"
    "  if (a instanceof b) { return 1; }\n"
    "  return 0;\n"
    "}\n"
1099
    "f(1, 1);\n",
1100 1101 1102 1103 1104 1105 1106 1107 1108

    "function f() {\n"
    "  var a = 0;\n"
    "  if (a) {\n"
    "    return 20;\n"
    "  } else {\n"
    "    return -20;\n"
    "  }\n"
    "};\n"
1109
    "f();\n",
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

    "function f(a, b) {\n"
    "  if (a == b || a < 0) {\n"
    "    return 1;\n"
    "  } else if (a > 0 && b > 0) {\n"
    "    return 0;\n"
    "  } else {\n"
    "    return -1;\n"
    "  }\n"
    "};\n"
    "f(-1, 1);\n",
1121 1122
  };

1123 1124
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("IfConditions.golden")));
1125
}
1126

1127
TEST(DeclareGlobals) {
1128
  InitializedIgnitionHandleScope scope;
1129
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1130 1131 1132
  printer.set_wrap(false);
  printer.set_test_function_name("f");
  printer.set_top_level(true);
1133

1134
  const char* snippets[] = {
1135
      "var a = 1;\n",
1136

1137
      "function f() {}\n",
1138

1139
      "var a = 1;\n"
1140
      "a=2;\n",
1141 1142

      "function f() {}\n"
1143
      "f();\n",
1144 1145
  };

1146 1147
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("DeclareGlobals.golden")));
1148 1149
}

1150 1151
TEST(BreakableBlocks) {
  InitializedIgnitionHandleScope scope;
1152
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1153 1154 1155 1156 1157 1158 1159 1160

  const char* snippets[] = {
      "var x = 0;\n"
      "label: {\n"
      "  x = x + 1;\n"
      "  break label;\n"
      "  x = x + 1;\n"
      "}\n"
1161
      "return x;\n",
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

      "var sum = 0;\n"
      "outer: {\n"
      "  for (var x = 0; x < 10; ++x) {\n"
      "    for (var y = 0; y < 3; ++y) {\n"
      "      ++sum;\n"
      "      if (x + y == 12) { break outer; }\n"
      "    }\n"
      "  }\n"
      "}\n"
1172
      "return sum;\n",
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

      "outer: {\n"
      "  let y = 10;\n"
      "  function f() { return y; }\n"
      "  break outer;\n"
      "}\n",

      "let x = 1;\n"
      "outer: {\n"
      "  inner: {\n"
      "   let y = 2;\n"
      "    function f() { return x + y; }\n"
      "    if (y) break outer;\n"
      "    y = 3;\n"
      "  }\n"
      "}\n"
1189
      "x = 4;\n",
1190 1191
  };

1192 1193
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("BreakableBlocks.golden")));
1194
}
1195

1196
TEST(BasicLoops) {
1197
  InitializedIgnitionHandleScope scope;
1198
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1199 1200 1201
  const char* snippets[] = {
      "var x = 0;\n"
      "while (false) { x = 99; break; continue; }\n"
1202
      "return x;\n",
1203 1204 1205 1206 1207

      "var x = 0;\n"
      "while (false) {\n"
      "  x = x + 1;\n"
      "};\n"
1208
      "return x;\n",
1209 1210 1211 1212 1213 1214 1215 1216 1217

      "var x = 0;\n"
      "var y = 1;\n"
      "while (x < 10) {\n"
      "  y = y * 12;\n"
      "  x = x + 1;\n"
      "  if (x == 3) continue;\n"
      "  if (x == 4) break;\n"
      "}\n"
1218
      "return y;\n",
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

      "var i = 0;\n"
      "while (true) {\n"
      "  if (i < 0) continue;\n"
      "  if (i == 3) break;\n"
      "  if (i == 4) break;\n"
      "  if (i == 10) continue;\n"
      "  if (i == 5) break;\n"
      "  i = i + 1;\n"
      "}\n"
1229
      "return i;\n",
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

      "var i = 0;\n"
      "while (true) {\n"
      "  while (i < 3) {\n"
      "    if (i == 2) break;\n"
      "    i = i + 1;\n"
      "  }\n"
      "  i = i + 1;\n"
      "  break;\n"
      "}\n"
1240
      "return i;\n",
1241 1242 1243 1244 1245 1246 1247

      "var x = 10;\n"
      "var y = 1;\n"
      "while (x) {\n"
      "  y = y * 12;\n"
      "  x = x - 1;\n"
      "}\n"
1248
      "return y;\n",
1249 1250 1251 1252 1253 1254 1255 1256

      "var x = 0; var y = 1;\n"
      "do {\n"
      "  y = y * 10;\n"
      "  if (x == 5) break;\n"
      "  if (x == 6) continue;\n"
      "  x = x + 1;\n"
      "} while (x < 10);\n"
1257
      "return y;\n",
1258 1259 1260 1261 1262 1263 1264

      "var x = 10;\n"
      "var y = 1;\n"
      "do {\n"
      "  y = y * 12;\n"
      "  x = x - 1;\n"
      "} while (x);\n"
1265
      "return y;\n",
1266 1267 1268 1269 1270 1271 1272 1273

      "var x = 0; var y = 1;\n"
      "do {\n"
      "  y = y * 10;\n"
      "  if (x == 5) break;\n"
      "  x = x + 1;\n"
      "  if (x == 6) continue;\n"
      "} while (false);\n"
1274
      "return y;\n",
1275 1276 1277 1278 1279 1280 1281 1282

      "var x = 0; var y = 1;\n"
      "do {\n"
      "  y = y * 10;\n"
      "  if (x == 5) break;\n"
      "  x = x + 1;\n"
      "  if (x == 6) continue;\n"
      "} while (true);\n"
1283
      "return y;\n",
1284 1285 1286 1287 1288 1289

      "var x = 0;\n"
      "for (;;) {\n"
      "  if (x == 1) break;\n"
      "  if (x == 2) continue;\n"
      "  x = x + 1;\n"
1290
      "}\n",
1291 1292 1293 1294 1295

      "for (var x = 0;;) {\n"
      "  if (x == 1) break;\n"
      "  if (x == 2) continue;\n"
      "  x = x + 1;\n"
1296
      "}\n",
1297 1298 1299 1300 1301

      "var x = 0;\n"
      "for (;; x = x + 1) {\n"
      "  if (x == 1) break;\n"
      "  if (x == 2) continue;\n"
1302
      "}\n",
1303 1304 1305 1306

      "for (var x = 0;; x = x + 1) {\n"
      "  if (x == 1) break;\n"
      "  if (x == 2) continue;\n"
1307
      "}\n",
1308 1309 1310 1311 1312

      "var u = 0;\n"
      "for (var i = 0; i < 100; i = i + 1) {\n"
      "  u = u + 1;\n"
      "  continue;\n"
1313
      "}\n",
1314 1315 1316 1317 1318

      "var y = 1;\n"
      "for (var x = 10; x; --x) {\n"
      "  y = y * 12;\n"
      "}\n"
1319
      "return y;\n",
1320 1321 1322 1323 1324

      "var x = 0;\n"
      "for (var i = 0; false; i++) {\n"
      "  x = x + 1;\n"
      "};\n"
1325
      "return x;\n",
1326 1327 1328 1329 1330 1331

      "var x = 0;\n"
      "for (var i = 0; true; ++i) {\n"
      "  x = x + 1;\n"
      "  if (x == 20) break;\n"
      "};\n"
1332
      "return x;\n",
1333 1334 1335 1336 1337 1338 1339 1340 1341

      "var a = 0;\n"
      "while (a) {\n"
      "  { \n"
      "   let z = 1;\n"
      "   function f() { z = 2; }\n"
      "   if (z) continue;\n"
      "   z++;\n"
      "  }\n"
1342
      "}\n",
1343 1344
  };

1345 1346
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("BasicLoops.golden")));
1347 1348
}

1349
TEST(UnaryOperators) {
1350
  InitializedIgnitionHandleScope scope;
1351
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1352 1353 1354 1355 1356
  const char* snippets[] = {
      "var x = 0;\n"
      "while (x != 10) {\n"
      "  x = x + 10;\n"
      "}\n"
1357
      "return x;\n",
1358

1359 1360 1361 1362
      "var x = false;\n"
      "do {\n"
      "  x = !x;\n"
      "} while(x == false);\n"
1363
      "return x;\n",
1364

1365
      "var x = 101;\n"
1366
      "return void(x * 3);\n",
1367

1368 1369
      "var x = 1234;\n"
      "var y = void (x * x - 1);\n"
1370
      "return y;\n",
1371 1372

      "var x = 13;\n"
1373
      "return ~x;\n",
1374 1375

      "var x = 13;\n"
1376
      "return +x;\n",
1377 1378

      "var x = 13;\n"
1379
      "return -x;\n",
1380 1381
  };

1382 1383
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("UnaryOperators.golden")));
1384 1385
}

1386 1387
TEST(Typeof) {
  InitializedIgnitionHandleScope scope;
1388
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1389 1390 1391 1392 1393 1394 1395 1396
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f() {\n"
      " var x = 13;\n"
      " return typeof(x);\n"
      "};",
1397

1398 1399 1400 1401
      "var x = 13;\n"
      "function f() {\n"
      " return typeof(x);\n"
      "};",
1402 1403
  };

1404 1405
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("Typeof.golden")));
1406 1407
}

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
TEST(CompareTypeOf) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "return typeof(1) === 'number';\n",

      "return 'string' === typeof('foo');\n",

      "return typeof(true) == 'boolean';\n",

      "return 'string' === typeof(undefined);\n",

      "return 'unknown' === typeof(undefined);\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CompareTypeOf.golden")));
}

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
TEST(CompareNil) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "var a = 1;\n"
      "return a === null;\n",

      "var a = undefined;\n"
      "return undefined === a;\n",

      "var a = undefined;\n"
      "return undefined !== a;\n",

      "var a = 2;\n"
      "return a != null;\n",

      "var a = undefined;\n"
      "return undefined == a;\n",

      "var a = undefined;\n"
      "return undefined === a ? 1 : 2;\n",

      "var a = 0;\n"
      "return null == a ? 1 : 2;\n",

      "var a = 0;\n"
      "return undefined !== a ? 1 : 2;\n",

      "var a = 0;\n"
      "return a === null ? 1 : 2;\n",

      "var a = 0;\n"
      "if (a === null) {\n"
      "  return 1;\n"
      "} else {\n"
      "  return 2;\n"
      "}\n",

      "var a = 0;\n"
      "if (a != undefined) {\n"
      "  return 1;\n"
      "}\n",

      "var a = undefined;\n"
      "var b = 0;\n"
      "while (a !== undefined) {\n"
      "  b++;\n"
      "}\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CompareNil.golden")));
}

1483 1484
TEST(Delete) {
  InitializedIgnitionHandleScope scope;
1485
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1486

1487
  const char* snippets[] = {
1488
      "var a = {x:13, y:14}; return delete a.x;\n",
1489

1490
      "'use strict'; var a = {x:13, y:14}; return delete a.x;\n",
1491

1492
      "var a = {1:13, 2:14}; return delete a[2];\n",
1493

1494
      "var a = 10; return delete a;\n",
1495 1496 1497 1498

      "'use strict';\n"
      "var a = {1:10};\n"
      "(function f1() {return a;});\n"
1499
      "return delete a[1];\n",
1500

1501
      "return delete 'test';\n",
1502 1503

      "return delete this;\n",
1504
  };
1505

1506 1507
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("Delete.golden")));
1508 1509
}

1510 1511
TEST(GlobalDelete) {
  InitializedIgnitionHandleScope scope;
1512
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1513 1514 1515 1516 1517 1518 1519 1520
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "var a = {x:13, y:14};\n"
      "function f() {\n"
      "  return delete a.x;\n"
      "};\n"
1521
      "f();\n",
1522 1523 1524 1525 1526 1527

      "a = {1:13, 2:14};\n"
      "function f() {\n"
      "  'use strict';\n"
      "  return delete a[1];\n"
      "};\n"
1528
      "f();\n",
1529 1530 1531 1532 1533

      "var a = {x:13, y:14};\n"
      "function f() {\n"
      "  return delete a;\n"
      "};\n"
1534
      "f();\n",
1535 1536 1537 1538 1539

      "b = 30;\n"
      "function f() {\n"
      "  return delete b;\n"
      "};\n"
1540
      "f();\n",
1541 1542
  };

1543 1544
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("GlobalDelete.golden")));
1545
}
1546

1547
TEST(FunctionLiterals) {
1548
  InitializedIgnitionHandleScope scope;
1549
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1550

1551
  const char* snippets[] = {
1552
      "return function(){ }\n",
1553

1554
      "return (function(){ })()\n",
1555

1556
      "return (function(x){ return x; })(1)\n",
1557 1558
  };

1559 1560
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("FunctionLiterals.golden")));
1561 1562
}

1563 1564
TEST(RegExpLiterals) {
  InitializedIgnitionHandleScope scope;
1565
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1566

1567
  const char* snippets[] = {
1568
      "return /ab+d/;\n",
1569

1570
      "return /(\\w+)\\s(\\w+)/i;\n",
1571

1572
      "return /ab+d/.exec('abdd');\n",
1573 1574
  };

1575 1576
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("RegExpLiterals.golden")));
1577 1578
}

1579 1580
TEST(ArrayLiterals) {
  InitializedIgnitionHandleScope scope;
1581
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1582

1583
  const char* snippets[] = {
1584
      "return [ 1, 2 ];\n",
1585

1586
      "var a = 1; return [ a, a + 1 ];\n",
1587

1588
      "return [ [ 1, 2 ], [ 3 ] ];\n",
1589

1590
      "var a = 1; return [ [ a, 2 ], [ a + 2 ] ];\n",
1591 1592 1593 1594 1595 1596

      "var a = [ 1, 2 ]; return [ ...a ];\n",

      "var a = [ 1, 2 ]; return [ 0, ...a ];\n",

      "var a = [ 1, 2 ]; return [ ...a, 3 ];\n",
1597 1598
  };

1599 1600
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ArrayLiterals.golden")));
1601 1602
}

1603 1604
TEST(ObjectLiterals) {
  InitializedIgnitionHandleScope scope;
1605
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1606

1607
  const char* snippets[] = {
1608
      "return { };\n",
1609

1610
      "return { name: 'string', val: 9.2 };\n",
1611

1612
      "var a = 1; return { name: 'string', val: a };\n",
1613

1614
      "var a = 1; return { val: a, val: a + 1 };\n",
1615

1616
      "return { func: function() { } };\n",
1617

1618
      "return { func(a) { return a; } };\n",
1619

1620
      "return { get a() { return 2; } };\n",
1621

1622
      "return { get a() { return this.x; }, set a(val) { this.x = val } };\n",
1623

1624
      "return { set b(val) { this.y = val } };\n",
1625

1626
      "var a = 1; return { 1: a };\n",
1627

1628
      "return { __proto__: null };\n",
1629

1630
      "var a = 'test'; return { [a]: 1 };\n",
1631

1632
      "var a = 'test'; return { val: a, [a]: 1 };\n",
1633

1634
      "var a = 'test'; return { [a]: 1, __proto__: {} };\n",
1635

1636
      "var n = 'name'; return { [n]: 'val', get a() { }, set a(b) {} };\n",
1637 1638
  };

1639 1640
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ObjectLiterals.golden")));
1641 1642
}

1643 1644
TEST(TopLevelObjectLiterals) {
  InitializedIgnitionHandleScope scope;
1645
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1646 1647 1648
  printer.set_wrap(false);
  printer.set_test_function_name("f");
  printer.set_top_level(true);
1649

1650
  const char* snippets[] = {
1651
      "var a = { func: function() { } };\n",
1652 1653
  };

1654 1655
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("TopLevelObjectLiterals.golden")));
1656 1657
}

1658 1659
TEST(TryCatch) {
  InitializedIgnitionHandleScope scope;
1660
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1661

1662
  const char* snippets[] = {
1663
      "try { return 1; } catch(e) { return 2; }\n",
1664 1665 1666

      "var a;\n"
      "try { a = 1 } catch(e1) {};\n"
1667
      "try { a = 2 } catch(e2) { a = 3 }\n",
1668 1669
  };

1670 1671
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("TryCatch.golden")));
1672 1673
}

1674 1675
TEST(TryFinally) {
  InitializedIgnitionHandleScope scope;
1676
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1677 1678
  const char* snippets[] = {
      "var a = 1;\n"
1679
      "try { a = 2; } finally { a = 3; }\n",
1680 1681

      "var a = 1;\n"
1682
      "try { a = 2; } catch(e) { a = 20 } finally { a = 3; }\n",
1683

1684 1685
      "var a; try {\n"
      "  try { a = 1 } catch(e) { a = 2 }\n"
1686
      "} catch(e) { a = 20 } finally { a = 3; }\n",
1687 1688
  };

1689 1690
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("TryFinally.golden")));
1691 1692
}

1693 1694
TEST(Throw) {
  InitializedIgnitionHandleScope scope;
1695
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1696
  const char* snippets[] = {
1697
      "throw 1;\n",
1698

1699
      "throw 'Error';\n",
1700

1701
      "var a = 1; if (a) { throw 'Error'; };\n",
1702 1703
  };

1704 1705
  CHECK(
      CompareTexts(BuildActual(printer, snippets), LoadGolden("Throw.golden")));
1706 1707
}

1708 1709
TEST(CallNew) {
  InitializedIgnitionHandleScope scope;
1710
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1711 1712
  printer.set_wrap(false);
  printer.set_test_function_name("f");
1713

1714 1715 1716
  const char* snippets[] = {
      "function bar() { this.value = 0; }\n"
      "function f() { return new bar(); }\n"
1717
      "f();\n",
1718

1719 1720
      "function bar(x) { this.value = 18; this.x = x;}\n"
      "function f() { return new bar(3); }\n"
1721
      "f();\n",
1722

1723 1724 1725 1726 1727 1728 1729
      "function bar(w, x, y, z) {\n"
      "  this.value = 18;\n"
      "  this.x = x;\n"
      "  this.y = y;\n"
      "  this.z = z;\n"
      "}\n"
      "function f() { return new bar(3, 4, 5); }\n"
1730
      "f();\n",
1731
  };
1732

1733 1734
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CallNew.golden")));
1735
}
1736

1737
TEST(ContextVariables) {
1738
  // The wide check below relies on MIN_CONTEXT_SLOTS + 3 + 250 == 256, if this
1739 1740
  // ever changes, the REPEAT_XXX should be changed to output the correct number
  // of unique variables to trigger the wide slot load / store.
1741
  STATIC_ASSERT(Context::MIN_CONTEXT_EXTENDED_SLOTS + 3 + 250 == 256);
1742

1743
  InitializedIgnitionHandleScope scope;
1744
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1745
  const char* snippets[] = {
1746
    "var a; return function() { a = 1; };\n",
1747

1748
    "var a = 1; return function() { a = 2; };\n",
1749

1750
    "var a = 1; var b = 2; return function() { a = 2; b = 3 };\n",
1751

1752
    "var a; (function() { a = 2; })(); return a;\n",
1753 1754 1755

    "'use strict';\n"
    "let a = 1;\n"
1756
    "{ let b = 2; return function() { a + b; }; }\n",
1757 1758

    "'use strict';\n"
1759
    REPEAT_252_UNIQUE_VARS()
1760 1761
    "eval();\n"
    "var b = 100;\n"
1762
    "return b\n",
1763 1764
  };

1765 1766
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ContextVariables.golden")));
1767 1768
}

1769 1770
TEST(ContextParameters) {
  InitializedIgnitionHandleScope scope;
1771
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1772 1773
  printer.set_wrap(false);
  printer.set_test_function_name("f");
1774

1775 1776 1777 1778 1779 1780 1781 1782
  const char* snippets[] = {
      "function f(arg1) { return function() { arg1 = 2; }; }",

      "function f(arg1) { var a = function() { arg1 = 2; }; return arg1; }",

      "function f(a1, a2, a3, a4) { return function() { a1 = a3; }; }",

      "function f() { var self = this; return function() { self = 2; }; }",
1783 1784
  };

1785 1786
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("ContextParameters.golden")));
1787 1788
}

1789 1790
TEST(OuterContextVariables) {
  InitializedIgnitionHandleScope scope;
1791
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function Outer() {\n"
      "  var outerVar = 1;\n"
      "  function Inner(innerArg) {\n"
      "    this.innerFunc = function() { return outerVar * innerArg; }\n"
      "  }\n"
      "  this.getInnerFunc = function() { return new Inner(1).innerFunc; }\n"
      "}\n"
      "var f = new Outer().getInnerFunc();",

      "function Outer() {\n"
      "  var outerVar = 1;\n"
      "  function Inner(innerArg) {\n"
      "    this.innerFunc = function() { outerVar = innerArg; }\n"
      "  }\n"
      "  this.getInnerFunc = function() { return new Inner(1).innerFunc; }\n"
      "}\n"
      "var f = new Outer().getInnerFunc();",
  };

1815 1816
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("OuterContextVariables.golden")));
1817
}
1818

1819
TEST(CountOperators) {
1820
  InitializedIgnitionHandleScope scope;
1821
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1822
  const char* snippets[] = {
1823
      "var a = 1; return ++a;\n",
1824

1825
      "var a = 1; return a++;\n",
1826

1827
      "var a = 1; return --a;\n",
1828

1829
      "var a = 1; return a--;\n",
1830

1831
      "var a = { val: 1 }; return a.val++;\n",
1832

1833
      "var a = { val: 1 }; return --a.val;\n",
1834

1835
      "var name = 'var'; var a = { val: 1 }; return a[name]--;\n",
1836

1837
      "var name = 'var'; var a = { val: 1 }; return ++a[name];\n",
1838

1839
      "var a = 1; var b = function() { return a }; return ++a;\n",
1840

1841
      "var a = 1; var b = function() { return a }; return a--;\n",
1842

1843
      "var idx = 1; var a = [1, 2]; return a[idx++] = 2;\n",
1844 1845
  };

1846 1847
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CountOperators.golden")));
1848 1849 1850
}

TEST(GlobalCountOperators) {
1851
  InitializedIgnitionHandleScope scope;
1852
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1853 1854 1855 1856 1857 1858
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "var global = 1;\n"
      "function f() { return ++global; }\n"
1859
      "f();\n",
1860 1861 1862

      "var global = 1;\n"
      "function f() { return global--; }\n"
1863
      "f();\n",
1864 1865 1866

      "unallocated = 1;\n"
      "function f() { 'use strict'; return --unallocated; }\n"
1867
      "f();\n",
1868 1869 1870

      "unallocated = 1;\n"
      "function f() { return unallocated++; }\n"
1871
      "f();\n",
1872 1873
  };

1874 1875
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("GlobalCountOperators.golden")));
1876 1877 1878
}

TEST(CompoundExpressions) {
1879
  InitializedIgnitionHandleScope scope;
1880
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1881
  const char* snippets[] = {
1882
      "var a = 1; a += 2;\n",
1883

1884
      "var a = 1; a /= 2;\n",
1885

1886
      "var a = { val: 2 }; a.name *= 2;\n",
1887

1888
      "var a = { 1: 2 }; a[1] ^= 2;\n",
1889

1890
      "var a = 1; (function f() { return a; }); a |= 24;\n",
1891 1892
  };

1893 1894
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CompoundExpressions.golden")));
1895 1896 1897
}

TEST(GlobalCompoundExpressions) {
1898
  InitializedIgnitionHandleScope scope;
1899
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1900 1901 1902 1903 1904 1905
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "var global = 1;\n"
      "function f() { return global &= 1; }\n"
1906
      "f();\n",
1907 1908 1909

      "unallocated = 1;\n"
      "function f() { return unallocated += 1; }\n"
1910
      "f();\n",
1911 1912
  };

1913 1914
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("GlobalCompoundExpressions.golden")));
1915 1916
}

1917
TEST(CreateArguments) {
1918
  InitializedIgnitionHandleScope scope;
1919
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f() { return arguments; }",

      "function f() { return arguments[0]; }",

      "function f() { 'use strict'; return arguments; }",

      "function f(a) { return arguments[0]; }",

      "function f(a, b, c) { return arguments; }",

      "function f(a, b, c) { 'use strict'; return arguments; }",
1935 1936
  };

1937 1938
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("CreateArguments.golden")));
1939 1940
}

1941
TEST(CreateRestParameter) {
1942
  InitializedIgnitionHandleScope scope;
1943
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f(...restArgs) { return restArgs; }",

      "function f(a, ...restArgs) { return restArgs; }",

      "function f(a, ...restArgs) { return restArgs[0]; }",

      "function f(a, ...restArgs) { return restArgs[0] + arguments[0]; }",
1955 1956
  };

1957 1958
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("CreateRestParameter.golden")));
1959
}
1960

1961
TEST(ForIn) {
1962
  InitializedIgnitionHandleScope scope;
1963
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1964
  const char* snippets[] = {
1965
      "for (var p in null) {}\n",
1966

1967
      "for (var p in undefined) {}\n",
1968

1969
      "for (var p in undefined) {}\n",
1970

1971
      "var x = 'potatoes';\n"
1972
      "for (var p in x) { return p; }\n",
1973 1974

      "var x = 0;\n"
1975
      "for (var p in [1,2,3]) { x += p; }\n",
1976 1977 1978 1979 1980

      "var x = { 'a': 1, 'b': 2 };\n"
      "for (x['a'] in [10, 20, 30]) {\n"
      "  if (x['a'] == 10) continue;\n"
      "  if (x['a'] == 20) break;\n"
1981
      "}\n",
1982 1983

      "var x = [ 10, 11, 12 ] ;\n"
1984
      "for (x[0] in [1,2,3]) { return x[3]; }\n",
1985 1986
  };

1987 1988
  CHECK(
      CompareTexts(BuildActual(printer, snippets), LoadGolden("ForIn.golden")));
1989 1990
}

1991 1992
TEST(ForOf) {
  InitializedIgnitionHandleScope scope;
1993
  BytecodeExpectationsPrinter printer(CcTest::isolate());
1994
  const char* snippets[] = {
1995
      "for (var p of [0, 1, 2]) {}\n",
1996

1997
      "var x = 'potatoes';\n"
1998
      "for (var p of x) { return p; }\n",
1999 2000 2001 2002

      "for (var x of [10, 20, 30]) {\n"
      "  if (x == 10) continue;\n"
      "  if (x == 20) break;\n"
2003
      "}\n",
2004 2005

      "var x = { 'a': 1, 'b': 2 };\n"
2006
      "for (x['a'] of [1,2,3]) { return x['a']; }\n",
2007 2008
  };

2009 2010
  CHECK(
      CompareTexts(BuildActual(printer, snippets), LoadGolden("ForOf.golden")));
2011 2012
}

2013 2014
TEST(Conditional) {
  InitializedIgnitionHandleScope scope;
2015
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2016
  const char* snippets[] = {
2017
      "return 1 ? 2 : 3;\n",
2018

2019
      "return 1 ? 2 ? 3 : 4 : 5;\n",
2020 2021 2022 2023 2024

      "return 0 < 1 ? 2 : 3;\n",

      "var x = 0;\n"
      "return x ? 2 : 3;\n",
2025 2026
  };

2027 2028
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("Conditional.golden")));
2029 2030
}

2031 2032
TEST(Switch) {
  InitializedIgnitionHandleScope scope;
2033
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2034 2035 2036 2037 2038
  const char* snippets[] = {
    "var a = 1;\n"
    "switch(a) {\n"
    " case 1: return 2;\n"
    " case 2: return 3;\n"
2039
    "}\n",
2040 2041 2042 2043 2044

    "var a = 1;\n"
    "switch(a) {\n"
    " case 1: a = 2; break;\n"
    " case 2: a = 3; break;\n"
2045
    "}\n",
2046 2047 2048 2049 2050

    "var a = 1;\n"
    "switch(a) {\n"
    " case 1: a = 2; // fall-through\n"
    " case 2: a = 3; break;\n"
2051
    "}\n",
2052 2053 2054 2055 2056 2057

    "var a = 1;\n"
    "switch(a) {\n"
    " case 2: break;\n"
    " case 3: break;\n"
    " default: a = 1; break;\n"
2058
    "}\n",
2059 2060 2061 2062 2063 2064

    "var a = 1;\n"
    "switch(typeof(a)) {\n"
    " case 2: a = 1; break;\n"
    " case 3: a = 2; break;\n"
    " default: a = 3; break;\n"
2065
    "}\n",
2066 2067 2068 2069 2070

    "var a = 1;\n"
    "switch(a) {\n"
    " case typeof(a): a = 1; break;\n"
    " default: a = 2; break;\n"
2071
    "}\n",
2072 2073 2074 2075 2076 2077 2078 2079 2080

    "var a = 1;\n"
    "switch(a) {\n"
    " case 1:\n"
    REPEAT_64("  a = 2;\n")
    "  break;\n"
    " case 2:\n"
    "  a = 3;\n"
    "  break;\n"
2081
    "}\n",
2082 2083 2084 2085 2086 2087 2088 2089 2090

    "var a = 1;\n"
    "switch(a) {\n"
    " case 1: \n"
    "   switch(a + 1) {\n"
    "      case 2 : a = 1; break;\n"
    "      default : a = 2; break;\n"
    "   }  // fall-through\n"
    " case 2: a = 3;\n"
2091
    "}\n",
2092 2093
  };

2094 2095
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("Switch.golden")));
2096
}
2097 2098

TEST(BasicBlockToBoolean) {
2099
  InitializedIgnitionHandleScope scope;
2100
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2101
  const char* snippets[] = {
2102
      "var a = 1; if (a || a < 0) { return 1; }\n",
2103

2104
      "var a = 1; if (a && a < 0) { return 1; }\n",
2105

2106
      "var a = 1; a = (a || a < 0) ? 2 : 3;\n",
2107 2108
  };

2109 2110
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("BasicBlockToBoolean.golden")));
2111 2112 2113
}

TEST(DeadCodeRemoval) {
2114
  InitializedIgnitionHandleScope scope;
2115
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2116
  const char* snippets[] = {
2117
      "return; var a = 1; a();\n",
2118

2119
      "if (false) { return; }; var a = 1;\n",
2120

2121
      "if (true) { return 1; } else { return 2; };\n",
2122

2123
      "var a = 1; if (a) { return 1; }; return 2;\n",
2124 2125
  };

2126 2127
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("DeadCodeRemoval.golden")));
2128 2129
}

2130
TEST(ThisFunction) {
2131
  InitializedIgnitionHandleScope scope;
2132
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2133 2134 2135 2136 2137 2138 2139 2140 2141
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "var f;\n"
      "f = function f() {};",

      "var f;\n"
      "f = function f() { return f; };",
2142 2143
  };

2144 2145
  CHECK(CompareTexts(BuildActual(printer, snippets, "", "\nf();"),
                     LoadGolden("ThisFunction.golden")));
2146 2147
}

2148
TEST(NewTarget) {
2149
  InitializedIgnitionHandleScope scope;
2150
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2151

2152
  const char* snippets[] = {
2153
      "return new.target;\n",
2154

2155
      "new.target;\n",
2156 2157
  };

2158 2159
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("NewTarget.golden")));
2160 2161
}

2162 2163
TEST(RemoveRedundantLdar) {
  InitializedIgnitionHandleScope scope;
2164
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2165 2166 2167 2168 2169 2170
  const char* snippets[] = {
      "var ld_a = 1;\n"          // This test is to check Ldar does not
      "while(true) {\n"          // get removed if the preceding Star is
      "  ld_a = ld_a + ld_a;\n"  // in a different basicblock.
      "  if (ld_a > 10) break;\n"
      "}\n"
2171
      "return ld_a;\n",
2172 2173 2174 2175 2176 2177

      "var ld_a = 1;\n"
      "do {\n"
      "  ld_a = ld_a + ld_a;\n"
      "  if (ld_a > 10) continue;\n"
      "} while(false);\n"
2178
      "return ld_a;\n",
2179

2180 2181
      "var ld_a = 1;\n"
      "  ld_a = ld_a + ld_a;\n"
2182
      "  return ld_a;\n",
2183
  };
2184

2185 2186
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("RemoveRedundantLdar.golden")));
2187 2188
}

2189 2190 2191 2192 2193 2194 2195 2196
TEST(GenerateTestUndetectable) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  const char* snippets[] = {
      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a == null) { b = 20;}\n"
      "return b;\n",
2197

2198 2199 2200 2201
      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a == undefined) { b = 20;}\n"
      "return b;\n",
2202

2203 2204 2205 2206
      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a != null) { b = 20;}\n"
      "return b;\n",
2207

2208 2209 2210
      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a != undefined) { b = 20;}\n"
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
      "return b;\n",

      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a === null) { b = 20;}\n"
      "return b;\n",

      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a === undefined) { b = 20;}\n"
      "return b;\n",

      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a !== null) { b = 20;}\n"
      "return b;\n",

      "var obj_a = {val:1};\n"
      "var b = 10;\n"
      "if (obj_a !== undefined) { b = 20;}\n"
2231 2232 2233 2234 2235 2236
      "return b;\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("GenerateTestUndetectable.golden")));
}

2237 2238
TEST(AssignmentsInBinaryExpression) {
  InitializedIgnitionHandleScope scope;
2239
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2240 2241
  const char* snippets[] = {
      "var x = 0, y = 1;\n"
2242
      "return (x = 2, y = 3, x = 4, y = 5);\n",
2243

2244 2245
      "var x = 55;\n"
      "var y = (x = 100);\n"
2246
      "return y;\n",
2247 2248 2249

      "var x = 55;\n"
      "x = x + (x = 100) + (x = 101);\n"
2250
      "return x;\n",
2251 2252 2253 2254

      "var x = 55;\n"
      "x = (x = 56) - x + (x = 57);\n"
      "x++;\n"
2255
      "return x;\n",
2256 2257 2258

      "var x = 55;\n"
      "var y = x + (x = 1) + (x = 2) + (x = 3);\n"
2259
      "return y;\n",
2260 2261 2262

      "var x = 55;\n"
      "var x = x + (x = 1) + (x = 2) + (x = 3);\n"
2263
      "return x;\n",
2264 2265 2266 2267 2268 2269 2270

      "var x = 10, y = 20;\n"
      "return x + (x = 1) + (x + 1) * (y = 2) + (y = 3) + (x = 4) + (y = 5) + "
      "y;\n",

      "var x = 17;\n"
      "return 1 + x + (x++) + (++x);\n",
2271 2272
  };

2273 2274
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("AssignmentsInBinaryExpression.golden")));
2275 2276
}

2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
TEST(DestructuringAssignment) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  const char* snippets[] = {
      "var x, a = [0,1,2,3];\n"
      "[x] = a;\n",

      "var x, y, a = [0,1,2,3];\n"
      "[,x,...y] = a;\n",

      "var x={}, y, a = [0];\n"
      "[x.foo,y=4] = a;\n",

      "var x, a = {x:1};\n"
      "({x} = a);\n",

      "var x={}, a = {y:1};\n"
      "({y:x.foo} = a);\n",

      "var x, a = {y:1, w:2, v:3};\n"
      "({x=0,...y} = a);\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("DestructuringAssignment.golden")));
}

2304 2305
TEST(Eval) {
  InitializedIgnitionHandleScope scope;
2306
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2307
  const char* snippets[] = {
2308
      "return eval('1;');\n",
2309 2310
  };

2311 2312
  CHECK(
      CompareTexts(BuildActual(printer, snippets), LoadGolden("Eval.golden")));
2313
}
2314 2315

TEST(LookupSlot) {
2316
  InitializedIgnitionHandleScope scope;
2317
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2318
  printer.set_test_function_name("f");
2319

2320
  // clang-format off
2321
  const char* snippets[] = {
2322
      "eval('var x = 10;'); return x;\n",
2323

2324
      "eval('var x = 10;'); return typeof x;\n",
2325

2326
      "x = 20; return eval('');\n",
2327 2328 2329 2330 2331 2332

      "var x = 20;\n"
      "f = function(){\n"
      "  eval('var x = 10');\n"
      "  return x;\n"
      "}\n"
2333 2334 2335 2336 2337 2338 2339
      "f();\n",

      "x = 20;\n"
      "f = function(){\n"
      "  eval('var x = 10');\n"
      "  return x;\n"
      "}\n"
2340
      "f();\n"
2341
  };
2342
  // clang-format on
2343

2344 2345
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("LookupSlot.golden")));
2346 2347 2348
}

TEST(CallLookupSlot) {
2349
  InitializedIgnitionHandleScope scope;
2350
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2351
  const char* snippets[] = {
2352
      "g = function(){}; eval(''); return g();\n",
2353 2354
  };

2355 2356
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CallLookupSlot.golden")));
2357 2358
}

2359 2360
// TODO(mythria): tests for variable/function declaration in lookup slots.

2361
TEST(LookupSlotInEval) {
2362
  InitializedIgnitionHandleScope scope;
2363
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "return x;",

      "x = 10;",

      "'use strict'; x = 10;",

      "return typeof x;",
2375
  };
2376

2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
  std::string actual = BuildActual(printer, snippets,
                                   "var f;\n"
                                   "var x = 1;\n"
                                   "function f1() {\n"
                                   "  eval(\"function t() { ",

                                   " }; f = t; f();\");\n"
                                   "}\n"
                                   "f1();");

2387
  CHECK(CompareTexts(actual, LoadGolden("LookupSlotInEval.golden")));
2388
}
2389

2390
TEST(DeleteLookupSlotInEval) {
2391
  InitializedIgnitionHandleScope scope;
2392
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2393 2394 2395 2396 2397 2398 2399 2400 2401
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "delete x;",

      "return delete y;",

      "return delete z;",
2402
  };
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415

  std::string actual = BuildActual(printer, snippets,
                                   "var f;\n"
                                   "var x = 1;\n"
                                   "z = 10;\n"
                                   "function f1() {\n"
                                   "  var y;\n"
                                   "  eval(\"function t() { ",

                                   " }; f = t; f();\");\n"
                                   "}\n"
                                   "f1();");

2416
  CHECK(CompareTexts(actual, LoadGolden("DeleteLookupSlotInEval.golden")));
2417 2418
}

2419 2420 2421 2422
TEST(WideRegisters) {
  // Prepare prologue that creates frame for lots of registers.
  std::ostringstream os;
  for (size_t i = 0; i < 157; ++i) {
2423
    os << "var x" << i << " = 0;\n";
2424 2425 2426
  }
  std::string prologue(os.str());

2427
  InitializedIgnitionHandleScope scope;
2428
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2429 2430
  const char* snippets[] = {
      "x0 = x127;\n"
2431
      "return x0;\n",
2432

2433
      "x127 = x126;\n"
2434
      "return x127;\n",
2435

2436
      "if (x2 > 3) { return x129; }\n"
2437
      "return x128;\n",
2438 2439 2440 2441

      "var x0 = 0;\n"
      "if (x129 == 3) { var x129 = x0; }\n"
      "if (x2 > 3) { return x0; }\n"
2442
      "return x129;\n",
2443 2444 2445 2446 2447 2448

      "var x0 = 0;\n"
      "var x1 = 0;\n"
      "for (x128 = 0; x128 < 64; x128++) {"
      "  x1 += x128;"
      "}"
2449
      "return x128;\n",
2450 2451 2452 2453 2454 2455

      "var x0 = 1234;\n"
      "var x1 = 0;\n"
      "for (x128 in x0) {"
      "  x1 += x128;"
      "}"
2456
      "return x1;\n",
2457 2458 2459 2460

      "x0 = %Add(x64, x63);\n"
      "x1 = %Add(x27, x143);\n"
      "%TheHole();\n"
2461
      "return x1;\n",
2462 2463
  };

2464 2465
  CHECK(CompareTexts(BuildActual(printer, snippets, prologue.c_str()),
                     LoadGolden("WideRegisters.golden")));
2466 2467
}

2468
TEST(ConstVariable) {
2469
  InitializedIgnitionHandleScope scope;
2470
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2471
  const char* snippets[] = {
2472
      "const x = 10;\n",
2473

2474
      "const x = 10; return x;\n",
2475

2476
      "const x = ( x = 20);\n",
2477

2478
      "const x = 10; x = 20;\n",
2479 2480
  };

2481 2482
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ConstVariable.golden")));
2483 2484 2485
}

TEST(LetVariable) {
2486
  InitializedIgnitionHandleScope scope;
2487
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2488
  const char* snippets[] = {
2489
      "let x = 10;\n",
2490

2491
      "let x = 10; return x;\n",
2492

2493
      "let x = (x = 20);\n",
2494

2495
      "let x = 10; x = 20;\n",
2496 2497
  };

2498 2499
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("LetVariable.golden")));
2500 2501 2502 2503 2504
}

TEST(ConstVariableContextSlot) {
  // TODO(mythria): Add tests for initialization of this via super calls.
  // TODO(mythria): Add tests that walk the context chain.
2505
  InitializedIgnitionHandleScope scope;
2506
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2507
  const char* snippets[] = {
2508
      "const x = 10; function f1() {return x;}\n",
2509

2510
      "const x = 10; function f1() {return x;} return x;\n",
2511

2512
      "const x = (x = 20); function f1() {return x;}\n",
2513

2514
      "const x = 10; x = 20; function f1() {return x;}\n",
2515 2516
  };

2517 2518
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ConstVariableContextSlot.golden")));
2519 2520 2521
}

TEST(LetVariableContextSlot) {
2522
  InitializedIgnitionHandleScope scope;
2523
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2524
  const char* snippets[] = {
2525
      "let x = 10; function f1() {return x;}\n",
2526

2527
      "let x = 10; function f1() {return x;} return x;\n",
2528

2529
      "let x = (x = 20); function f1() {return x;}\n",
2530

2531
      "let x = 10; x = 20; function f1() {return x;}\n",
2532 2533
  };

2534 2535
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("LetVariableContextSlot.golden")));
2536 2537
}

2538
TEST(WithStatement) {
2539
  InitializedIgnitionHandleScope scope;
2540
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2541
  const char* snippets[] = {
2542
      "with ({x:42}) { return x; }\n",
2543 2544
  };

2545 2546
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("WithStatement.golden")));
2547 2548
}

2549
TEST(DoDebugger) {
2550
  InitializedIgnitionHandleScope scope;
2551
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2552
  const char* snippets[] = {
2553
      "debugger;\n",
2554 2555
  };

2556 2557
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("DoDebugger.golden")));
2558 2559
}

2560
TEST(ClassDeclarations) {
2561
  InitializedIgnitionHandleScope scope;
2562
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2563 2564 2565 2566
  const char* snippets[] = {
      "class Person {\n"
      "  constructor(name) { this.name = name; }\n"
      "  speak() { console.log(this.name + ' is speaking.'); }\n"
2567
      "}\n",
2568 2569 2570 2571

      "class person {\n"
      "  constructor(name) { this.name = name; }\n"
      "  speak() { console.log(this.name + ' is speaking.'); }\n"
2572
      "}\n",
2573 2574 2575 2576 2577 2578

      "var n0 = 'a';\n"
      "var n1 = 'b';\n"
      "class N {\n"
      "  [n0]() { return n0; }\n"
      "  static [n1]() { return n1; }\n"
2579
      "}\n",
2580 2581 2582 2583

      "var count = 0;\n"
      "class C { constructor() { count++; }}\n"
      "return new C();\n",
2584 2585 2586

      "(class {})\n"
      "class E { static name () {}}\n",
2587 2588
  };

2589 2590
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ClassDeclarations.golden")));
2591 2592
}

2593
TEST(ClassAndSuperClass) {
2594 2595 2596 2597
  // Different bytecodes are generated with and without --future temporarily,
  // see crbug.com/v8/9237 . TODO(marja): remove this hack once --super-ic is on
  // by default.
  FLAG_super_ic = false;
2598
  InitializedIgnitionHandleScope scope;
2599
  BytecodeExpectationsPrinter printer(CcTest::isolate());
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
  printer.set_wrap(false);
  printer.set_test_function_name("test");
  const char* snippets[] = {
      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    method() { return 2; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    method() { return super.method() + 1; }\n"
      "  }\n"
      "  test = new B().method;\n"
      "  test();\n"
      "})();\n",

      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    get x() { return 1; }\n"
      "    set x(val) { return; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    method() { super.x = 2; return super.x; }\n"
      "  }\n"
      "  test = new B().method;\n"
      "  test();\n"
      "})();\n",

      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    constructor(x) { this.x_ = x; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    constructor() { super(1); this.y_ = 2; }\n"
      "  }\n"
      "  test = new B().constructor;\n"
      "})();\n",

      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    constructor() { this.x_ = 1; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    constructor() { super(); this.y_ = 2; }\n"
      "  }\n"
      "  test = new B().constructor;\n"
      "})();\n",
  };

2651 2652
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ClassAndSuperClass.golden")));
2653
}
2654

2655
TEST(PublicClassFields) {
2656 2657 2658
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
  const char* snippets[] = {
      "{\n"
      "  class A {\n"
      "    a;\n"
      "    ['b'];\n"
      "  }\n"
      "\n"
      "  class B {\n"
      "    a = 1;\n"
      "    ['b'] = this.a;\n"
      "  }\n"
      "  new A;\n"
      "  new B;\n"
      "}\n",

      "{\n"
      "  class A extends class {} {\n"
      "    a;\n"
      "    ['b'];\n"
      "  }\n"
      "\n"
      "  class B extends class {} {\n"
      "    a = 1;\n"
      "    ['b'] = this.a;\n"
      "    foo() { return 1; }\n"
      "    constructor() {\n"
      "      super();\n"
      "    }\n"
      "  }\n"
      "\n"
      "  class C extends B {\n"
      "    a = 1;\n"
      "    ['b'] = this.a;\n"
      "    constructor() {\n"
      "      (() => super())();\n"
      "    }\n"
      "  }\n"
      "\n"
      "  new A;\n"
      "  new B;\n"
      "  new C;\n"
      "}\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
2703
                     LoadGolden("PublicClassFields.golden")));
2704 2705
}

2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762
TEST(PrivateClassFields) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "{\n"
      "  class A {\n"
      "    #a;\n"
      "    constructor() {\n"
      "      this.#a = 1;\n"
      "    }\n"
      "  }\n"
      "\n"
      "  class B {\n"
      "    #a = 1;\n"
      "  }\n"
      "  new A;\n"
      "  new B;\n"
      "}\n",

      "{\n"
      "  class A extends class {} {\n"
      "    #a;\n"
      "    constructor() {\n"
      "      super();\n"
      "      this.#a = 1;\n"
      "    }\n"
      "  }\n"
      "\n"
      "  class B extends class {} {\n"
      "    #a = 1;\n"
      "    #b = this.#a;\n"
      "    foo() { return this.#a; }\n"
      "    bar(v) { this.#b = v; }\n"
      "    constructor() {\n"
      "      super();\n"
      "      this.foo();\n"
      "      this.bar(3);\n"
      "    }\n"
      "  }\n"
      "\n"
      "  class C extends B {\n"
      "    #a = 2;\n"
      "    constructor() {\n"
      "      (() => super())();\n"
      "    }\n"
      "  }\n"
      "\n"
      "  new A;\n"
      "  new B;\n"
      "  new C;\n"
      "};\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrivateClassFields.golden")));
}

2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
TEST(PrivateClassFieldAccess) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("test");

  const char* snippets[] = {
      "class A {\n"
      "  #a;\n"
      "  #b;\n"
      "  constructor() {\n"
      "    this.#a = this.#b;\n"
      "  }\n"
      "}\n"
      "\n"
      "var test = A;\n"
      "new test;\n",

      "class B {\n"
      "  #a;\n"
      "  #b;\n"
      "  constructor() {\n"
      "    this.#a = this.#b;\n"
      "  }\n"
      "  force(str) {\n"
      "    eval(str);\n"
      "  }\n"
      "}\n"
      "\n"
      "var test = B;\n"
      "new test;\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrivateClassFieldAccess.golden")));
}

2799
TEST(PrivateMethodDeclaration) {
2800 2801 2802 2803
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
2804 2805 2806 2807 2808
      "{\n"
      "  class A {\n"
      "    #a() { return 1; }\n"
      "  }\n"
      "}\n",
2809

2810 2811 2812 2813 2814 2815 2816
      "{\n"
      "  class D {\n"
      "    #d() { return 1; }\n"
      "  }\n"
      "  class E extends D {\n"
      "    #e() { return 2; }\n"
      "  }\n"
2817 2818 2819 2820 2821 2822 2823
      "}\n",

      "{\n"
      "  class A { foo() {} }\n"
      "  class C extends A {\n"
      "    #m() { return super.foo; }\n"
      "  }\n"
2824
      "}\n"};
2825

2826
  CHECK(CompareTexts(BuildActual(printer, snippets),
2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
                     LoadGolden("PrivateMethodDeclaration.golden")));
}

TEST(PrivateMethodAccess) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("test");

  const char* snippets[] = {
      "class A {\n"
      "  #a() { return 1; }\n"
      "  constructor() { return this.#a(); }\n"
      "}\n"
      "\n"
      "var test = A;\n"
      "new A;\n",

      "class B {\n"
      "  #b() { return 1; }\n"
      "  constructor() { this.#b = 1; }\n"
      "}\n"
      "\n"
      "var test = B;\n"
      "new test;\n",

      "class C {\n"
      "  #c() { return 1; }\n"
      "  constructor() { this.#c++; }\n"
      "}\n"
      "\n"
      "var test = C;\n"
2859 2860 2861 2862 2863 2864 2865 2866
      "new test;\n",

      "class D {\n"
      "  #d() { return 1; }\n"
      "  constructor() { (() => this)().#d(); }\n"
      "}\n"
      "\n"
      "var test = D;\n"
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
      "new test;\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrivateMethodAccess.golden")));
}

TEST(PrivateAccessorAccess) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("test");

  const char* snippets[] = {
      "class A {\n"
      "  get #a() { return 1; }\n"
      "  set #a(val) { }\n"
      "\n"
      "  constructor() {\n"
      "    this.#a++;\n"
      "    this.#a = 1;\n"
      "    return this.#a;\n"
      "  }\n"
      "}\n"
      "var test = A;\n"
      "new test;\n",

      "class B {\n"
      "  get #b() { return 1; }\n"
      "  constructor() { this.#b++; }\n"
      "}\n"
      "var test = B;\n"
      "new test;\n",

      "class C {\n"
      "  set #c(val) { }\n"
      "  constructor() { this.#c++; }\n"
      "}\n"
      "var test = C;\n"
      "new test;\n",

      "class D {\n"
      "  get #d() { return 1; }\n"
      "  constructor() { this.#d = 1; }\n"
      "}\n"
      "var test = D;\n"
      "new test;\n",

      "class E {\n"
      "  set #e(val) { }\n"
      "  constructor() { this.#e; }\n"
      "}\n"
      "var test = E;\n"
      "new test;\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("PrivateAccessorAccess.golden")));
2923 2924
}

2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
TEST(StaticPrivateMethodDeclaration) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "{\n"
      "  class A {\n"
      "    static #a() { return 1; }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class A {\n"
      "    static get #a() { return 1; }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class A {\n"
      "    static set #a(val) { }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class A {\n"
      "    static get #a() { return 1; }\n"
      "    static set #a(val) { }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class A {\n"
      "    static #a() { }\n"
      "    #b() { }\n"
      "  }\n"
      "}\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StaticPrivateMethodDeclaration.golden")));
}

2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
TEST(StaticPrivateMethodAccess) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("test");

  const char* snippets[] = {
      "class A {\n"
      "  static #a() { return 1; }\n"
      "  static test() { return this.#a(); }\n"
      "}\n"
      "\n"
      "var test = A.test;\n"
      "test();\n",

      "class B {\n"
      "  static #b() { return 1; }\n"
      "  static test() { this.#b = 1; }\n"
      "}\n"
      "\n"
      "var test = B.test;\n"
      "test();\n",

      "class C {\n"
      "  static #c() { return 1; }\n"
      "  static test() { this.#c++; }\n"
      "}\n"
      "\n"
      "var test = C.test;\n"
      "test();\n",

      "class D {\n"
      "  static get #d() { return 1; }\n"
      "  static set #d(val) { }\n"
      "\n"
      "  static test() {\n"
      "    this.#d++;\n"
      "    this.#d = 1;\n"
      "    return this.#d;\n"
      "  }\n"
      "}\n"
      "\n"
      "var test = D.test;\n"
      "test();\n",

      "class E {\n"
      "  static get #e() { return 1; }\n"
      "  static test() { this.#e++; }\n"
      "}\n"
      "var test = E.test;\n"
      "test();\n",

      "class F {\n"
      "  static set #f(val) { }\n"
      "  static test() { this.#f++; }\n"
      "}\n"
      "var test = F.test;\n"
      "test();\n",

      "class G {\n"
      "  static get #d() { return 1; }\n"
      "  static test() { this.#d = 1; }\n"
      "}\n"
      "var test = G.test;\n"
      "test();\n",

      "class H {\n"
      "  set #h(val) { }\n"
      "  static test() { this.#h; }\n"
      "}\n"
      "var test = H.test;\n"
      "test();\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StaticPrivateMethodAccess.golden")));
}

3043
TEST(PrivateAccessorDeclaration) {
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "{\n"
      "  class A {\n"
      "    get #a() { return 1; }\n"
      "    set #a(val) { }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class B {\n"
      "    get #b() { return 1; }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class C {\n"
      "    set #c(val) { }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class D {\n"
      "    get #d() { return 1; }\n"
      "    set #d(val) { }\n"
      "  }\n"
      "\n"
      "  class E extends D {\n"
      "    get #e() { return 2; }\n"
      "    set #e(val) { }\n"
      "  }\n"
      "}\n",

      "{\n"
      "  class A { foo() {} }\n"
      "  class C extends A {\n"
      "    get #a() { return super.foo; }\n"
      "  }\n"
      "  new C();\n"
      "}\n",

      "{\n"
      "  class A { foo(val) {} }\n"
      "  class C extends A {\n"
      "    set #a(val) { super.foo(val); }\n"
      "  }\n"
      "  new C();\n"
      "}\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
3096
                     LoadGolden("PrivateAccessorDeclaration.golden")));
3097 3098
}

3099 3100 3101 3102
TEST(StaticClassFields) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

3103
  const char* snippets[] = {
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
      "{\n"
      "  class A {\n"
      "    a;\n"
      "    ['b'];\n"
      "    static c;\n"
      "    static ['d'];\n"
      "  }\n"
      "\n"
      "  class B {\n"
      "    a = 1;\n"
      "    ['b'] = this.a;\n"
      "    static c = 3;\n"
      "    static ['d'] = this.c;\n"
      "  }\n"
      "  new A;\n"
      "  new B;\n"
      "}\n",

3122 3123 3124
      "{\n"
      "  class A extends class {} {\n"
      "    a;\n"
3125
      "    ['b'];\n"
3126
      "    static c;\n"
3127
      "    static ['d'];\n"
3128 3129 3130 3131
      "  }\n"
      "\n"
      "  class B extends class {} {\n"
      "    a = 1;\n"
3132
      "    ['b'] = this.a;\n"
3133
      "    static c = 3;\n"
3134 3135
      "    static ['d'] = this.c;\n"
      "    foo() { return 1; }\n"
3136 3137 3138 3139 3140
      "    constructor() {\n"
      "      super();\n"
      "    }\n"
      "  }\n"
      "\n"
3141
      "  class C extends B {\n"
3142
      "    a = 1;\n"
3143
      "    ['b'] = this.a;\n"
3144
      "    static c = 3;\n"
3145
      "    static ['d'] = super.foo();\n"
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
      "    constructor() {\n"
      "      (() => super())();\n"
      "    }\n"
      "  }\n"
      "\n"
      "  new A;\n"
      "  new B;\n"
      "  new C;\n"
      "}\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
3157
                     LoadGolden("StaticClassFields.golden")));
3158 3159
}

3160 3161
TEST(Generators) {
  InitializedIgnitionHandleScope scope;
3162
  BytecodeExpectationsPrinter printer(CcTest::isolate());
3163 3164 3165 3166
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
3167 3168
      "function* f() { }\n"
      "f();\n",
3169

3170 3171
      "function* f() { yield 42 }\n"
      "f();\n",
3172

3173 3174
      "function* f() { for (let x of [42]) yield x }\n"
      "f();\n",
3175 3176 3177 3178

      "function* g() { yield 42 }\n"
      "function* f() { yield* g() }\n"
      "f();\n",
3179 3180
  };

3181 3182
  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("Generators.golden")));
3183 3184
}

3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
TEST(AsyncGenerators) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "async function* f() { }\n"
      "f();\n",

      "async function* f() { yield 42 }\n"
      "f();\n",

      "async function* f() { for (let x of [42]) yield x }\n"
      "f();\n",

      "function* g() { yield 42 }\n"
      "async function* f() { yield* g() }\n"
      "f();\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("AsyncGenerators.golden")));
}

3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
TEST(Modules) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_module(true);
  printer.set_top_level(true);

  const char* snippets[] = {
      "import \"bar\";\n",

      "import {foo} from \"bar\";\n",

      "import {foo as goo} from \"bar\";\n"
      "goo(42);\n"
      "{ let x; { goo(42) } };\n",

      "export var foo = 42;\n"
      "foo++;\n"
      "{ let x; { foo++ } };\n",

      "export let foo = 42;\n"
      "foo++;\n"
      "{ let x; { foo++ } };\n",

      "export const foo = 42;\n"
      "foo++;\n"
      "{ let x; { foo++ } };\n",

      "export default (function () {});\n",

      "export default (class {});\n",

      "export {foo as goo} from \"bar\"\n",

      "export * from \"bar\"\n",
3245 3246 3247

      "import * as foo from \"bar\"\n"
      "foo.f(foo, foo.x);\n",
3248 3249 3250 3251 3252 3253
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("Modules.golden")));
}

3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
TEST(AsyncModules) {
  bool previous_top_level_await_flag = i::FLAG_harmony_top_level_await;
  i::FLAG_harmony_top_level_await = true;
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_module(true);
  printer.set_top_level(true);

  const char* snippets[] = {
      "await 42;\n",

      "await import(\"foo\");\n",

      "await 42;\n"
      "async function foo() {\n"
      "  await 42;\n"
      "}\n"
      "foo();\n",

      "import * as foo from \"bar\";\n"
      "await import(\"goo\");\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("AsyncModules.golden")));
  i::FLAG_harmony_top_level_await = previous_top_level_await_flag;
}

3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
TEST(SuperCallAndSpread) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("test");
  const char* snippets[] = {
      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    constructor(...args) { this.baseArgs = args; }\n"
      "  }\n"
      "  class B extends A {}\n"
      "  test = new B(1, 2, 3).constructor;\n"
      "})();\n",

      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    constructor(...args) { this.baseArgs = args; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    constructor(...args) { super(1, ...args); }\n"
      "  }\n"
      "  test = new B(1, 2, 3).constructor;\n"
      "})();\n",

      "var test;\n"
      "(function() {\n"
      "  class A {\n"
      "    constructor(...args) { this.baseArgs = args; }\n"
      "  }\n"
      "  class B extends A {\n"
      "    constructor(...args) { super(1, ...args, 1); }\n"
      "  }\n"
      "  test = new B(1, 2, 3).constructor;\n"
      "})();\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("SuperCallAndSpread.golden")));
}

3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
TEST(CallAndSpread) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  const char* snippets[] = {"Math.max(...[1, 2, 3]);\n",
                            "Math.max(0, ...[1, 2, 3]);\n",
                            "Math.max(0, ...[1, 2, 3], 4);\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("CallAndSpread.golden")));
}

TEST(NewAndSpread) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  const char* snippets[] = {
      "class A { constructor(...args) { this.args = args; } }\n"
      "new A(...[1, 2, 3]);\n",

      "class A { constructor(...args) { this.args = args; } }\n"
      "new A(0, ...[1, 2, 3]);\n",

      "class A { constructor(...args) { this.args = args; } }\n"
      "new A(0, ...[1, 2, 3], 4);\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("NewAndSpread.golden")));
}

3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
TEST(ForAwaitOf) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "async function f() {\n"
      "  for await (let x of [1, 2, 3]) {}\n"
      "}\n"
      "f();\n",

      "async function f() {\n"
      "  for await (let x of [1, 2, 3]) { return x; }\n"
      "}\n"
      "f();\n",

      "async function f() {\n"
      "  for await (let x of [10, 20, 30]) {\n"
      "    if (x == 10) continue;\n"
      "    if (x == 20) break;\n"
      "  }\n"
      "}\n"
      "f();\n",

      "async function f() {\n"
      "  var x = { 'a': 1, 'b': 2 };\n"
      "  for (x['a'] of [1,2,3]) { return x['a']; }\n"
      "}\n"
      "f();\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ForAwaitOf.golden")));
}

3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
TEST(StandardForLoop) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f() {\n"
      "  for (let x = 0; x < 10; ++x) { let y = x; }\n"
      "}\n"
      "f();\n",

      "function f() {\n"
      "  for (let x = 0; x < 10; ++x) { eval('1'); }\n"
      "}\n"
      "f();\n",

      "function f() {\n"
      "  for (let x = 0; x < 10; ++x) { (function() { return x; })(); }\n"
      "}\n"
      "f();\n",

      "function f() {\n"
      "  for (let { x, y } = { x: 0, y: 3 }; y > 0; --y) { let z = x + y; }\n"
      "}\n"
      "f();\n",

      "function* f() {\n"
      "  for (let x = 0; x < 10; ++x) { let y = x; }\n"
      "}\n"
      "f();\n",

      "function* f() {\n"
      "  for (let x = 0; x < 10; ++x) yield x;\n"
      "}\n"
      "f();\n",

      "async function f() {\n"
      "  for (let x = 0; x < 10; ++x) { let y = x; }\n"
      "}\n"
      "f();\n",

      "async function f() {\n"
      "  for (let x = 0; x < 10; ++x) await x;\n"
      "}\n"
      "f();\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StandardForLoop.golden")));
}

TEST(ForOfLoop) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());
  printer.set_wrap(false);
  printer.set_test_function_name("f");

  const char* snippets[] = {
      "function f(arr) {\n"
      "  for (let x of arr) { let y = x; }\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "function f(arr) {\n"
      "  for (let x of arr) { eval('1'); }\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "function f(arr) {\n"
      "  for (let x of arr) { (function() { return x; })(); }\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "function f(arr) {\n"
      "  for (let { x, y } of arr) { let z = x + y; }\n"
      "}\n"
      "f([{ x: 0, y: 3 }, { x: 1, y: 9 }, { x: -12, y: 17 }]);\n",

      "function* f(arr) {\n"
      "  for (let x of arr) { let y = x; }\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "function* f(arr) {\n"
      "  for (let x of arr) yield x;\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "async function f(arr) {\n"
      "  for (let x of arr) { let y = x; }\n"
      "}\n"
      "f([1, 2, 3]);\n",

      "async function f(arr) {\n"
      "  for (let x of arr) await x;\n"
      "}\n"
      "f([1, 2, 3]);\n"};

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("ForOfLoop.golden")));
}

3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
TEST(StringConcat) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "var a = 1;\n"
      "var b = 2;\n"
      "return a + b + 'string';\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return 'string' + a + b;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return a + 'string' + b;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return 'foo' + a + 'bar' + b + 'baz' + 1;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return (a + 'string') + ('string' + b);\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "function foo(a, b) { };\n"
      "return 'string' + foo(a, b) + a + b;\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("StringConcat.golden")));
}

3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
TEST(TemplateLiterals) {
  InitializedIgnitionHandleScope scope;
  BytecodeExpectationsPrinter printer(CcTest::isolate());

  const char* snippets[] = {
      "var a = 1;\n"
      "var b = 2;\n"
      "return `${a}${b}string`;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return `string${a}${b}`;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return `${a}string${b}`;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return `foo${a}bar${b}baz${1}`;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "return `${a}string` + `string${b}`;\n",

      "var a = 1;\n"
      "var b = 2;\n"
      "function foo(a, b) { };\n"
      "return `string${foo(a, b)}${a}${b}`;\n",
  };

  CHECK(CompareTexts(BuildActual(printer, snippets),
                     LoadGolden("TemplateLiterals.golden")));
}

3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
#undef XSTR
#undef STR
#undef UNIQUE_VAR
#undef REPEAT_2
#undef REPEAT_4
#undef REPEAT_8
#undef REPEAT_16
#undef REPEAT_32
#undef REPEAT_64
#undef REPEAT_128
#undef REPEAT_256
#undef REPEAT_127
#undef REPEAT_249
#undef REPEAT_2_UNIQUE_VARS
#undef REPEAT_4_UNIQUE_VARS
#undef REPEAT_8_UNIQUE_VARS
#undef REPEAT_16_UNIQUE_VARS
#undef REPEAT_32_UNIQUE_VARS
#undef REPEAT_64_UNIQUE_VARS
#undef REPEAT_128_UNIQUE_VARS
3580
#undef REPEAT_252_UNIQUE_VARS
3581 3582 3583 3584 3585 3586 3587 3588
#undef LOAD_UNIQUE_PROPERTY
#undef REPEAT_2_LOAD_UNIQUE_PROPERTY
#undef REPEAT_4_LOAD_UNIQUE_PROPERTY
#undef REPEAT_8_LOAD_UNIQUE_PROPERTY
#undef REPEAT_16_LOAD_UNIQUE_PROPERTY
#undef REPEAT_32_LOAD_UNIQUE_PROPERTY
#undef REPEAT_64_LOAD_UNIQUE_PROPERTY
#undef REPEAT_128_LOAD_UNIQUE_PROPERTY
3589 3590
#undef FUNC_ARG

3591 3592
}  // namespace interpreter
}  // namespace internal
3593
}  // namespace v8