Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1291)

Side by Side Diff: test/cctest/test-parsing.cc

Issue 160073006: Implement handling of arrow functions in the parser (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: What "git cl format" produces Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/token.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 } 276 }
277 277
278 278
279 TEST(StandAlonePreParser) { 279 TEST(StandAlonePreParser) {
280 v8::V8::Initialize(); 280 v8::V8::Initialize();
281 281
282 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - 282 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() -
283 128 * 1024); 283 128 * 1024);
284 284
285 const char* programs[] = { 285 const char* programs[] = {
286 "{label: 42}", 286 "{label: 42}", "var x = 42;",
287 "var x = 42;", 287 "function foo(x, y) { return x + y; }", "%ArgleBargle(glop);",
288 "function foo(x, y) { return x + y; }", 288 "var x = new new Function('this.x = 42');", "var f = (x, y) => x + y;",
289 "%ArgleBargle(glop);", 289 NULL};
290 "var x = new new Function('this.x = 42');",
291 NULL
292 };
293 290
294 uintptr_t stack_limit = CcTest::i_isolate()->stack_guard()->real_climit(); 291 uintptr_t stack_limit = CcTest::i_isolate()->stack_guard()->real_climit();
295 for (int i = 0; programs[i]; i++) { 292 for (int i = 0; programs[i]; i++) {
296 const char* program = programs[i]; 293 const char* program = programs[i];
297 i::Utf8ToUtf16CharacterStream stream( 294 i::Utf8ToUtf16CharacterStream stream(
298 reinterpret_cast<const i::byte*>(program), 295 reinterpret_cast<const i::byte*>(program),
299 static_cast<unsigned>(strlen(program))); 296 static_cast<unsigned>(strlen(program)));
300 i::CompleteParserRecorder log; 297 i::CompleteParserRecorder log;
301 i::Scanner scanner(CcTest::i_isolate()->unicode_cache()); 298 i::Scanner scanner(CcTest::i_isolate()->unicode_cache());
302 scanner.Initialize(&stream); 299 scanner.Initialize(&stream);
303 300
304 i::PreParser preparser(&scanner, &log, stack_limit); 301 i::PreParser preparser(&scanner, &log, stack_limit);
305 preparser.set_allow_lazy(true); 302 preparser.set_allow_lazy(true);
306 preparser.set_allow_natives_syntax(true); 303 preparser.set_allow_natives_syntax(true);
304 preparser.set_allow_arrow_functions(true);
307 i::PreParser::PreParseResult result = preparser.PreParseProgram(); 305 i::PreParser::PreParseResult result = preparser.PreParseProgram();
308 CHECK_EQ(i::PreParser::kPreParseSuccess, result); 306 CHECK_EQ(i::PreParser::kPreParseSuccess, result);
309 i::ScriptData data(log.ExtractData()); 307 i::ScriptData data(log.ExtractData());
310 CHECK(!data.has_error()); 308 CHECK(!data.has_error());
311 } 309 }
312 } 310 }
313 311
314 312
315 TEST(StandAlonePreParserNoNatives) { 313 TEST(StandAlonePreParserNoNatives) {
316 v8::V8::Initialize(); 314 v8::V8::Initialize();
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 } 1159 }
1162 1160
1163 1161
1164 enum ParserFlag { 1162 enum ParserFlag {
1165 kAllowLazy, 1163 kAllowLazy,
1166 kAllowNativesSyntax, 1164 kAllowNativesSyntax,
1167 kAllowHarmonyScoping, 1165 kAllowHarmonyScoping,
1168 kAllowModules, 1166 kAllowModules,
1169 kAllowGenerators, 1167 kAllowGenerators,
1170 kAllowForOf, 1168 kAllowForOf,
1171 kAllowHarmonyNumericLiterals 1169 kAllowHarmonyNumericLiterals,
1170 kAllowArrowFunctions
1172 }; 1171 };
1173 1172
1174 1173
1175 enum ParserSyncTestResult { 1174 enum ParserSyncTestResult {
1176 kSuccessOrError, 1175 kSuccessOrError,
1177 kSuccess, 1176 kSuccess,
1178 kError 1177 kError
1179 }; 1178 };
1180 1179
1181 template <typename Traits> 1180 template <typename Traits>
1182 void SetParserFlags(i::ParserBase<Traits>* parser, 1181 void SetParserFlags(i::ParserBase<Traits>* parser,
1183 i::EnumSet<ParserFlag> flags) { 1182 i::EnumSet<ParserFlag> flags) {
1184 parser->set_allow_lazy(flags.Contains(kAllowLazy)); 1183 parser->set_allow_lazy(flags.Contains(kAllowLazy));
1185 parser->set_allow_natives_syntax(flags.Contains(kAllowNativesSyntax)); 1184 parser->set_allow_natives_syntax(flags.Contains(kAllowNativesSyntax));
1186 parser->set_allow_harmony_scoping(flags.Contains(kAllowHarmonyScoping)); 1185 parser->set_allow_harmony_scoping(flags.Contains(kAllowHarmonyScoping));
1187 parser->set_allow_modules(flags.Contains(kAllowModules)); 1186 parser->set_allow_modules(flags.Contains(kAllowModules));
1188 parser->set_allow_generators(flags.Contains(kAllowGenerators)); 1187 parser->set_allow_generators(flags.Contains(kAllowGenerators));
1189 parser->set_allow_for_of(flags.Contains(kAllowForOf)); 1188 parser->set_allow_for_of(flags.Contains(kAllowForOf));
1190 parser->set_allow_harmony_numeric_literals( 1189 parser->set_allow_harmony_numeric_literals(
1191 flags.Contains(kAllowHarmonyNumericLiterals)); 1190 flags.Contains(kAllowHarmonyNumericLiterals));
1191 parser->set_allow_arrow_functions(flags.Contains(kAllowArrowFunctions));
1192 } 1192 }
1193 1193
1194 1194
1195 void TestParserSyncWithFlags(i::Handle<i::String> source, 1195 void TestParserSyncWithFlags(i::Handle<i::String> source,
1196 i::EnumSet<ParserFlag> flags, 1196 i::EnumSet<ParserFlag> flags,
1197 ParserSyncTestResult result) { 1197 ParserSyncTestResult result) {
1198 i::Isolate* isolate = CcTest::i_isolate(); 1198 i::Isolate* isolate = CcTest::i_isolate();
1199 i::Factory* factory = isolate->factory(); 1199 i::Factory* factory = isolate->factory();
1200 1200
1201 uintptr_t stack_limit = isolate->stack_guard()->real_climit(); 1201 uintptr_t stack_limit = isolate->stack_guard()->real_climit();
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
1383 NULL 1383 NULL
1384 }; 1384 };
1385 1385
1386 v8::HandleScope handles(CcTest::isolate()); 1386 v8::HandleScope handles(CcTest::isolate());
1387 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); 1387 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate());
1388 v8::Context::Scope context_scope(context); 1388 v8::Context::Scope context_scope(context);
1389 1389
1390 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - 1390 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() -
1391 128 * 1024); 1391 128 * 1024);
1392 1392
1393 static const ParserFlag flags1[] = { 1393 static const ParserFlag flags1[] = {kAllowLazy, kAllowHarmonyScoping,
1394 kAllowLazy, kAllowHarmonyScoping, kAllowModules, kAllowGenerators, 1394 kAllowModules, kAllowGenerators,
1395 kAllowForOf 1395 kAllowForOf, kAllowArrowFunctions};
1396 };
1397 for (int i = 0; context_data[i][0] != NULL; ++i) { 1396 for (int i = 0; context_data[i][0] != NULL; ++i) {
1398 for (int j = 0; statement_data[j] != NULL; ++j) { 1397 for (int j = 0; statement_data[j] != NULL; ++j) {
1399 for (int k = 0; termination_data[k] != NULL; ++k) { 1398 for (int k = 0; termination_data[k] != NULL; ++k) {
1400 int kPrefixLen = i::StrLength(context_data[i][0]); 1399 int kPrefixLen = i::StrLength(context_data[i][0]);
1401 int kStatementLen = i::StrLength(statement_data[j]); 1400 int kStatementLen = i::StrLength(statement_data[j]);
1402 int kTerminationLen = i::StrLength(termination_data[k]); 1401 int kTerminationLen = i::StrLength(termination_data[k]);
1403 int kSuffixLen = i::StrLength(context_data[i][1]); 1402 int kSuffixLen = i::StrLength(context_data[i][1]);
1404 int kProgramSize = kPrefixLen + kStatementLen + kTerminationLen 1403 int kProgramSize = kPrefixLen + kStatementLen + kTerminationLen
1405 + kSuffixLen + i::StrLength("label: for (;;) { }"); 1404 + kSuffixLen + i::StrLength("label: for (;;) { }");
1406 1405
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1461 const ParserFlag* always_true_flags = NULL, 1460 const ParserFlag* always_true_flags = NULL,
1462 int always_true_flags_len = 0) { 1461 int always_true_flags_len = 0) {
1463 v8::HandleScope handles(CcTest::isolate()); 1462 v8::HandleScope handles(CcTest::isolate());
1464 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); 1463 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate());
1465 v8::Context::Scope context_scope(context); 1464 v8::Context::Scope context_scope(context);
1466 1465
1467 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - 1466 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() -
1468 128 * 1024); 1467 128 * 1024);
1469 1468
1470 static const ParserFlag default_flags[] = { 1469 static const ParserFlag default_flags[] = {
1471 kAllowLazy, kAllowHarmonyScoping, kAllowModules, kAllowGenerators, 1470 kAllowLazy, kAllowHarmonyScoping, kAllowModules, kAllowGenerators,
1472 kAllowForOf, kAllowNativesSyntax 1471 kAllowForOf, kAllowNativesSyntax, kAllowArrowFunctions};
1473 };
1474 ParserFlag* generated_flags = NULL; 1472 ParserFlag* generated_flags = NULL;
1475 if (flags == NULL) { 1473 if (flags == NULL) {
1476 flags = default_flags; 1474 flags = default_flags;
1477 flags_len = ARRAY_SIZE(default_flags); 1475 flags_len = ARRAY_SIZE(default_flags);
1478 if (always_true_flags != NULL) { 1476 if (always_true_flags != NULL) {
1479 // Remove always_true_flags from default_flags. 1477 // Remove always_true_flags from default_flags.
1480 CHECK(always_true_flags_len < flags_len); 1478 CHECK(always_true_flags_len < flags_len);
1481 generated_flags = new ParserFlag[flags_len - always_true_flags_len]; 1479 generated_flags = new ParserFlag[flags_len - always_true_flags_len];
1482 int flag_index = 0; 1480 int flag_index = 0;
1483 for (int i = 0; i < flags_len; ++i) { 1481 for (int i = 0; i < flags_len; ++i) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 // using "eval" and "arguments" as identifiers. Without the strict mode, it's 1525 // using "eval" and "arguments" as identifiers. Without the strict mode, it's
1528 // ok to use "eval" or "arguments" as identifiers. With the strict mode, it 1526 // ok to use "eval" or "arguments" as identifiers. With the strict mode, it
1529 // isn't. 1527 // isn't.
1530 const char* context_data[][2] = { 1528 const char* context_data[][2] = {
1531 { "\"use strict\";", "" }, 1529 { "\"use strict\";", "" },
1532 { "var eval; function test_func() {\"use strict\"; ", "}"}, 1530 { "var eval; function test_func() {\"use strict\"; ", "}"},
1533 { NULL, NULL } 1531 { NULL, NULL }
1534 }; 1532 };
1535 1533
1536 const char* statement_data[] = { 1534 const char* statement_data[] = {
1537 "var eval;", 1535 "var eval;", "var arguments",
1538 "var arguments", 1536 "var foo, eval;", "var foo, arguments;",
1539 "var foo, eval;", 1537 "try { } catch (eval) { }", "try { } catch (arguments) { }",
1540 "var foo, arguments;", 1538 "function eval() { }", "function arguments() { }",
1541 "try { } catch (eval) { }", 1539 "function foo(eval) { }", "function foo(arguments) { }",
1542 "try { } catch (arguments) { }", 1540 "function foo(bar, eval) { }", "function foo(bar, arguments) { }",
1543 "function eval() { }", 1541 "(eval) => { }", "(arguments) => { }",
1544 "function arguments() { }", 1542 "(foo, eval) => { }", "(foo, arguments) => { }",
1545 "function foo(eval) { }", 1543 "eval = 1;", "arguments = 1;",
1546 "function foo(arguments) { }", 1544 "var foo = eval = 1;", "var foo = arguments = 1;",
1547 "function foo(bar, eval) { }", 1545 "++eval;", "++arguments;",
1548 "function foo(bar, arguments) { }", 1546 "eval++;", "arguments++;",
1549 "eval = 1;", 1547 NULL};
1550 "arguments = 1;",
1551 "var foo = eval = 1;",
1552 "var foo = arguments = 1;",
1553 "++eval;",
1554 "++arguments;",
1555 "eval++;",
1556 "arguments++;",
1557 NULL
1558 };
1559 1548
1560 RunParserSyncTest(context_data, statement_data, kError); 1549 RunParserSyncTest(context_data, statement_data, kError);
1561 } 1550 }
1562 1551
1563 1552
1564 TEST(NoErrorsEvalAndArgumentsSloppy) { 1553 TEST(NoErrorsEvalAndArgumentsSloppy) {
1565 // Tests that both preparsing and parsing accept "eval" and "arguments" as 1554 // Tests that both preparsing and parsing accept "eval" and "arguments" as
1566 // identifiers when needed. 1555 // identifiers when needed.
1567 const char* context_data[][2] = { 1556 const char* context_data[][2] = {
1568 { "", "" }, 1557 { "", "" },
(...skipping 24 matching lines...) Expand all
1593 "arguments++;", 1582 "arguments++;",
1594 NULL 1583 NULL
1595 }; 1584 };
1596 1585
1597 RunParserSyncTest(context_data, statement_data, kSuccess); 1586 RunParserSyncTest(context_data, statement_data, kSuccess);
1598 } 1587 }
1599 1588
1600 1589
1601 TEST(NoErrorsEvalAndArgumentsStrict) { 1590 TEST(NoErrorsEvalAndArgumentsStrict) {
1602 const char* context_data[][2] = { 1591 const char* context_data[][2] = {
1603 { "\"use strict\";", "" }, 1592 {"\"use strict\";", ""},
1604 { "function test_func() { \"use strict\";", "}" }, 1593 {"function test_func() { \"use strict\";", "}"},
1605 { NULL, NULL } 1594 {"() => { \"use strict\"; ", "}"},
1606 }; 1595 {NULL, NULL}};
1607 1596
1608 const char* statement_data[] = { 1597 const char* statement_data[] = {
1609 "eval;", 1598 "eval;",
1610 "arguments;", 1599 "arguments;",
1611 "var foo = eval;", 1600 "var foo = eval;",
1612 "var foo = arguments;", 1601 "var foo = arguments;",
1613 "var foo = { eval: 1 };", 1602 "var foo = { eval: 1 };",
1614 "var foo = { arguments: 1 };", 1603 "var foo = { arguments: 1 };",
1615 "var foo = { }; foo.eval = {};", 1604 "var foo = { }; foo.eval = {};",
1616 "var foo = { }; foo.arguments = {};", 1605 "var foo = { }; foo.arguments = {};",
1617 NULL 1606 NULL
1618 }; 1607 };
1619 1608
1620 RunParserSyncTest(context_data, statement_data, kSuccess); 1609 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
1610 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
1611 always_flags, ARRAY_SIZE(always_flags));
1621 } 1612 }
1622 1613
1623 1614
1624 TEST(ErrorsFutureStrictReservedWords) { 1615 TEST(ErrorsFutureStrictReservedWords) {
1625 // Tests that both preparsing and parsing produce the right kind of errors for 1616 // Tests that both preparsing and parsing produce the right kind of errors for
1626 // using future strict reserved words as identifiers. Without the strict mode, 1617 // using future strict reserved words as identifiers. Without the strict mode,
1627 // it's ok to use future strict reserved words as identifiers. With the strict 1618 // it's ok to use future strict reserved words as identifiers. With the strict
1628 // mode, it isn't. 1619 // mode, it isn't.
1629 const char* context_data[][2] = { 1620 const char* context_data[][2] = {
1630 { "\"use strict\";", "" }, 1621 {"\"use strict\";", ""},
1631 { "function test_func() {\"use strict\"; ", "}"}, 1622 {"function test_func() {\"use strict\"; ", "}"},
1632 { NULL, NULL } 1623 {"() => { \"use strict\"; ", "}"},
1633 }; 1624 {NULL, NULL}};
1634 1625
1635 const char* statement_data[] = { 1626 const char* statement_data[] = {
1636 "var interface;", 1627 "var interface;",
1637 "var foo, interface;", 1628 "var foo, interface;",
1638 "try { } catch (interface) { }", 1629 "try { } catch (interface) { }",
1639 "function interface() { }", 1630 "function interface() { }",
1640 "function foo(interface) { }", 1631 "function foo(interface) { }",
1641 "function foo(bar, interface) { }", 1632 "function foo(bar, interface) { }",
1642 "interface = 1;", 1633 "interface = 1;",
1643 "var foo = interface = 1;", 1634 "var foo = interface = 1;",
1644 "++interface;", 1635 "++interface;",
1645 "interface++;", 1636 "interface++;",
1646 "var yield = 13;", 1637 "var yield = 13;",
1647 NULL 1638 NULL
1648 }; 1639 };
1649 1640
1650 RunParserSyncTest(context_data, statement_data, kError); 1641 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
1642 RunParserSyncTest(context_data, statement_data, kError, NULL, 0, always_flags,
1643 ARRAY_SIZE(always_flags));
1651 } 1644 }
1652 1645
1653 1646
1654 TEST(NoErrorsFutureStrictReservedWords) { 1647 TEST(NoErrorsFutureStrictReservedWords) {
1655 const char* context_data[][2] = { 1648 const char* context_data[][2] = {{"", ""},
1656 { "", "" }, 1649 {"function test_func() {", "}"},
1657 { "function test_func() {", "}"}, 1650 {"() => {", "}"},
1658 { NULL, NULL } 1651 {NULL, NULL}};
1659 };
1660 1652
1661 const char* statement_data[] = { 1653 const char* statement_data[] = {
1662 "var interface;", 1654 "var interface;",
1663 "var foo, interface;", 1655 "var foo, interface;",
1664 "try { } catch (interface) { }", 1656 "try { } catch (interface) { }",
1665 "function interface() { }", 1657 "function interface() { }",
1666 "function foo(interface) { }", 1658 "function foo(interface) { }",
1667 "function foo(bar, interface) { }", 1659 "function foo(bar, interface) { }",
1668 "interface = 1;", 1660 "interface = 1;",
1669 "var foo = interface = 1;", 1661 "var foo = interface = 1;",
1670 "++interface;", 1662 "++interface;",
1671 "interface++;", 1663 "interface++;",
1672 "var yield = 13;", 1664 "var yield = 13;",
1673 NULL 1665 NULL
1674 }; 1666 };
1675 1667
1676 RunParserSyncTest(context_data, statement_data, kSuccess); 1668 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
1669 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
1670 always_flags, ARRAY_SIZE(always_flags));
1677 } 1671 }
1678 1672
1679 1673
1680 TEST(ErrorsReservedWords) { 1674 TEST(ErrorsReservedWords) {
1681 // Tests that both preparsing and parsing produce the right kind of errors for 1675 // Tests that both preparsing and parsing produce the right kind of errors for
1682 // using future reserved words as identifiers. These tests don't depend on the 1676 // using future reserved words as identifiers. These tests don't depend on the
1683 // strict mode. 1677 // strict mode.
1684 const char* context_data[][2] = { 1678 const char* context_data[][2] = {
1685 { "", "" }, 1679 {"", ""},
1686 { "\"use strict\";", "" }, 1680 {"\"use strict\";", ""},
1687 { "var eval; function test_func() {", "}"}, 1681 {"var eval; function test_func() {", "}"},
1688 { "var eval; function test_func() {\"use strict\"; ", "}"}, 1682 {"var eval; function test_func() {\"use strict\"; ", "}"},
1689 { NULL, NULL } 1683 {"var eval; () => {", "}"},
1690 }; 1684 {"var eval; () => {\"use strict\"; ", "}"},
1685 {NULL, NULL}};
1691 1686
1692 const char* statement_data[] = { 1687 const char* statement_data[] = {
1693 "var super;", 1688 "var super;", "var foo, super;",
1694 "var foo, super;", 1689 "try { } catch (super) { }", "function super() { }",
1695 "try { } catch (super) { }", 1690 "function foo(super) { }", "function foo(bar, super) { }",
1696 "function super() { }", 1691 "(super) => { }", "(bar, super) => { }",
1697 "function foo(super) { }", 1692 "super = 1;", "var foo = super = 1;",
1698 "function foo(bar, super) { }", 1693 "++super;", "super++;",
1699 "super = 1;", 1694 "function foo super", NULL};
1700 "var foo = super = 1;",
1701 "++super;",
1702 "super++;",
1703 "function foo super",
1704 NULL
1705 };
1706 1695
1707 RunParserSyncTest(context_data, statement_data, kError); 1696 RunParserSyncTest(context_data, statement_data, kError);
1708 } 1697 }
1709 1698
1710 1699
1711 TEST(NoErrorsYieldSloppyAllModes) { 1700 TEST(NoErrorsYieldSloppyAllModes) {
1712 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a 1701 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a
1713 // generator (see next test). 1702 // generator (see next test).
1714 const char* context_data[][2] = { 1703 const char* context_data[][2] = {
1715 { "", "" }, 1704 { "", "" },
(...skipping 15 matching lines...) Expand all
1731 "++yield;", 1720 "++yield;",
1732 "yield++;", 1721 "yield++;",
1733 "yield: 34", 1722 "yield: 34",
1734 "function yield(yield) { yield: yield (yield + yield (0)); }", 1723 "function yield(yield) { yield: yield (yield + yield (0)); }",
1735 "({ yield: 1 })", 1724 "({ yield: 1 })",
1736 "({ get yield() { 1 } })", 1725 "({ get yield() { 1 } })",
1737 "yield (100)", 1726 "yield (100)",
1738 NULL 1727 NULL
1739 }; 1728 };
1740 1729
1741 RunParserSyncTest(context_data, statement_data, kSuccess); 1730 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
1731 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
1732 always_flags, ARRAY_SIZE(always_flags));
1742 } 1733 }
1743 1734
1744 1735
1745 TEST(NoErrorsYieldSloppyGeneratorsEnabled) { 1736 TEST(NoErrorsYieldSloppyGeneratorsEnabled) {
1746 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a 1737 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a
1747 // generator (see next test). 1738 // generator (see next test).
1748 const char* context_data[][2] = { 1739 const char* context_data[][2] = {
1749 { "", "" }, 1740 { "", "" },
1750 { "function not_gen() {", "}" }, 1741 { "function not_gen() {", "}" },
1751 { "function * gen() { function not_gen() {", "} }" }, 1742 { "function * gen() { function not_gen() {", "} }" },
(...skipping 28 matching lines...) Expand all
1780 static const ParserFlag always_true_flags[] = { 1771 static const ParserFlag always_true_flags[] = {
1781 kAllowGenerators 1772 kAllowGenerators
1782 }; 1773 };
1783 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, 1774 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
1784 always_true_flags, 1); 1775 always_true_flags, 1);
1785 } 1776 }
1786 1777
1787 1778
1788 TEST(ErrorsYieldStrict) { 1779 TEST(ErrorsYieldStrict) {
1789 const char* context_data[][2] = { 1780 const char* context_data[][2] = {
1790 { "\"use strict\";", "" }, 1781 {"\"use strict\";", ""},
1791 { "\"use strict\"; function not_gen() {", "}" }, 1782 {"\"use strict\"; function not_gen() {", "}"},
1792 { "function test_func() {\"use strict\"; ", "}"}, 1783 {"function test_func() {\"use strict\"; ", "}"},
1793 { "\"use strict\"; function * gen() { function not_gen() {", "} }" }, 1784 {"\"use strict\"; function * gen() { function not_gen() {", "} }"},
1794 { "\"use strict\"; (function not_gen() {", "})" }, 1785 {"\"use strict\"; (function not_gen() {", "})"},
1795 { "\"use strict\"; (function * gen() { (function not_gen() {", "}) })" }, 1786 {"\"use strict\"; (function * gen() { (function not_gen() {", "}) })"},
1796 { NULL, NULL } 1787 {"() => {\"use strict\"; ", "}"},
1797 }; 1788 {NULL, NULL}};
1798 1789
1799 const char* statement_data[] = { 1790 const char* statement_data[] = {
1800 "var yield;", 1791 "var yield;",
1801 "var foo, yield;", 1792 "var foo, yield;",
1802 "try { } catch (yield) { }", 1793 "try { } catch (yield) { }",
1803 "function yield() { }", 1794 "function yield() { }",
1804 "(function yield() { })", 1795 "(function yield() { })",
1805 "function foo(yield) { }", 1796 "function foo(yield) { }",
1806 "function foo(bar, yield) { }", 1797 "function foo(bar, yield) { }",
1807 "function * yield() { }", 1798 "function * yield() { }",
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
1993 static const ParserFlag always_true_flags[] = { 1984 static const ParserFlag always_true_flags[] = {
1994 kAllowGenerators 1985 kAllowGenerators
1995 }; 1986 };
1996 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, 1987 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
1997 always_true_flags, 1); 1988 always_true_flags, 1);
1998 } 1989 }
1999 1990
2000 1991
2001 TEST(ErrorsIllegalWordsAsLabelsSloppy) { 1992 TEST(ErrorsIllegalWordsAsLabelsSloppy) {
2002 // Using future reserved words as labels is always an error. 1993 // Using future reserved words as labels is always an error.
2003 const char* context_data[][2] = { 1994 const char* context_data[][2] = {{"", ""},
2004 { "", ""}, 1995 {"function test_func() {", "}"},
2005 { "function test_func() {", "}" }, 1996 {"() => {", "}"},
2006 { NULL, NULL } 1997 {NULL, NULL}};
2007 };
2008 1998
2009 const char* statement_data[] = { 1999 const char* statement_data[] = {
2010 "super: while(true) { break super; }", 2000 "super: while(true) { break super; }",
2011 NULL 2001 NULL
2012 }; 2002 };
2013 2003
2014 RunParserSyncTest(context_data, statement_data, kError); 2004 RunParserSyncTest(context_data, statement_data, kError);
2015 } 2005 }
2016 2006
2017 2007
2018 TEST(ErrorsIllegalWordsAsLabelsStrict) { 2008 TEST(ErrorsIllegalWordsAsLabelsStrict) {
2019 // Tests that illegal tokens as labels produce the correct errors. 2009 // Tests that illegal tokens as labels produce the correct errors.
2020 const char* context_data[][2] = { 2010 const char* context_data[][2] = {
2021 { "\"use strict\";", "" }, 2011 {"\"use strict\";", ""},
2022 { "function test_func() {\"use strict\"; ", "}"}, 2012 {"function test_func() {\"use strict\"; ", "}"},
2023 { NULL, NULL } 2013 {"() => {\"use strict\"; ", "}"},
2024 }; 2014 {NULL, NULL}};
2025 2015
2026 const char* statement_data[] = { 2016 const char* statement_data[] = {
2027 "super: while(true) { break super; }", 2017 "super: while(true) { break super; }",
2028 "interface: while(true) { break interface; }", 2018 "interface: while(true) { break interface; }",
2029 "yield: while(true) { break yield; }", 2019 "yield: while(true) { break yield; }",
2030 NULL 2020 NULL
2031 }; 2021 };
2032 2022
2033 RunParserSyncTest(context_data, statement_data, kError); 2023 RunParserSyncTest(context_data, statement_data, kError);
2034 } 2024 }
2035 2025
2036 2026
2037 TEST(NoErrorsIllegalWordsAsLabels) { 2027 TEST(NoErrorsIllegalWordsAsLabels) {
2038 // Using eval and arguments as labels is legal even in strict mode. 2028 // Using eval and arguments as labels is legal even in strict mode.
2039 const char* context_data[][2] = { 2029 const char* context_data[][2] = {
2040 { "", ""}, 2030 {"", ""},
2041 { "function test_func() {", "}" }, 2031 {"function test_func() {", "}"},
2042 { "\"use strict\";", "" }, 2032 {"() => {", "}"},
2043 { "\"use strict\"; function test_func() {", "}" }, 2033 {"\"use strict\";", ""},
2044 { NULL, NULL } 2034 {"\"use strict\"; function test_func() {", "}"},
2045 }; 2035 {"\"use strict\"; () => {", "}"},
2036 {NULL, NULL}};
2046 2037
2047 const char* statement_data[] = { 2038 const char* statement_data[] = {
2048 "mylabel: while(true) { break mylabel; }", 2039 "mylabel: while(true) { break mylabel; }",
2049 "eval: while(true) { break eval; }", 2040 "eval: while(true) { break eval; }",
2050 "arguments: while(true) { break arguments; }", 2041 "arguments: while(true) { break arguments; }",
2051 NULL 2042 NULL
2052 }; 2043 };
2053 2044
2054 RunParserSyncTest(context_data, statement_data, kSuccess); 2045 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
2046 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
2047 always_flags, ARRAY_SIZE(always_flags));
2055 } 2048 }
2056 2049
2057 2050
2058 TEST(ErrorsParenthesizedLabels) { 2051 TEST(ErrorsParenthesizedLabels) {
2059 // Parenthesized identifiers shouldn't be recognized as labels. 2052 // Parenthesized identifiers shouldn't be recognized as labels.
2060 const char* context_data[][2] = { 2053 const char* context_data[][2] = {{"", ""},
2061 { "", ""}, 2054 {"function test_func() {", "}"},
2062 { "function test_func() {", "}" }, 2055 {"() => {", "}"},
2063 { NULL, NULL } 2056 {NULL, NULL}};
2064 };
2065 2057
2066 const char* statement_data[] = { 2058 const char* statement_data[] = {
2067 "(mylabel): while(true) { break mylabel; }", 2059 "(mylabel): while(true) { break mylabel; }",
2068 NULL 2060 NULL
2069 }; 2061 };
2070 2062
2071 RunParserSyncTest(context_data, statement_data, kError); 2063 RunParserSyncTest(context_data, statement_data, kError);
2072 } 2064 }
2073 2065
2074 2066
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
2895 LocalContext env; 2887 LocalContext env;
2896 int use_counts[v8::Isolate::kUseCounterFeatureCount] = {}; 2888 int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
2897 global_use_counts = use_counts; 2889 global_use_counts = use_counts;
2898 CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback); 2890 CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
2899 CompileRun("\"use asm\";\n" 2891 CompileRun("\"use asm\";\n"
2900 "var foo = 1;\n" 2892 "var foo = 1;\n"
2901 "\"use asm\";\n" // Only the first one counts. 2893 "\"use asm\";\n" // Only the first one counts.
2902 "function bar() { \"use asm\"; var baz = 1; }"); 2894 "function bar() { \"use asm\"; var baz = 1; }");
2903 CHECK_EQ(2, use_counts[v8::Isolate::kUseAsm]); 2895 CHECK_EQ(2, use_counts[v8::Isolate::kUseAsm]);
2904 } 2896 }
2897
2898
2899 TEST(ErrorsArrowFunctions) {
2900 // Tests that parser and preparser generate the same kind of errors
2901 // on invalid arrow function syntax.
2902 const char* context_data[][2] = {{"", ";"},
2903 {"v = ", ";"},
2904 {"bar ? (", ") : baz;"},
2905 {"bar ? baz : (", ");"},
2906 {"bar[", "];"},
2907 {"bar, ", ";"},
2908 {"", ", bar;"},
2909 {NULL, NULL}};
2910
2911 const char* statement_data[] = {
2912 "=> 0", "=>",
2913 "() =>", "=> {}",
2914 ") => {}", ", => {}",
2915 "(,) => {}", "return => {}",
2916 "() => {'value': 42}",
2917
2918 // Check that the early return introduced in ParsePrimaryExpression
2919 // does not accept stray closing parentheses.
2920 ")", ") => 0",
2921 "foo[()]", "()",
2922
2923 // Parameter lists with extra parens should be recognized as errors.
2924 "(()) => 0", "((x)) => 0",
2925 "((x, y)) => 0", "(x, (y)) => 0",
2926 "((x, y, z)) => 0", "(x, (y, z)) => 0",
2927 "((x, y), z) => 0",
2928
2929 // Parameter lists are always validated as strict, so those are errors.
2930 "eval => {}", "arguments => {}",
2931 "yield => {}", "interface => {}",
2932 "(eval) => {}", "(arguments) => {}",
2933 "(yield) => {}", "(interface) => {}",
2934 "(eval, bar) => {}", "(bar, eval) => {}",
2935 "(bar, arguments) => {}", "(bar, yield) => {}",
2936 "(bar, interface) => {}",
2937 // TODO(aperez): Detecting duplicates does not work in PreParser.
2938 // "(bar, bar) => {}",
2939
2940 // The parameter list is parsed as an expression, but only
2941 // a comma-separated list of identifier is valid.
2942 "32 => {}", "(32) => {}",
2943 "(a, 32) => {}", "if => {}",
2944 "(if) => {}", "(a, if) => {}",
2945 "a + b => {}", "(a + b) => {}",
2946 "(a + b, c) => {}", "(a, b - c) => {}",
2947 "\"a\" => {}", "(\"a\") => {}",
2948 "(\"a\", b) => {}", "(a, \"b\") => {}",
2949 "-a => {}", "(-a) => {}",
2950 "(-a, b) => {}", "(a, -b) => {}",
2951 "{} => {}", "({}) => {}",
2952 "(a, {}) => {}", "({}, a) => {}",
2953 "a++ => {}", "(a++) => {}",
2954 "(a++, b) => {}", "(a, b++) => {}",
2955 "[] => {}", "([]) => {}",
2956 "(a, []) => {}", "([], a) => {}",
2957 "(a = b) => {}", "(a = b, c) => {}",
2958 "(a, b = c) => {}", "(foo ? bar : baz) => {}",
2959 "(a, foo ? bar : baz) => {}", "(foo ? bar : baz, a) => {}",
2960 NULL};
2961
2962 RunParserSyncTest(context_data, statement_data, kError);
2963 }
2964
2965
2966 TEST(NoErrorsArrowFunctions) {
2967 // Tests that parser and preparser accept valid arrow functions syntax.
2968 const char* context_data[][2] = {{"", ";"},
2969 {"bar ? (", ") : baz;"},
2970 {"bar ? baz : (", ");"},
2971 {"bar, ", ";"},
2972 {"", ", bar;"},
2973 {NULL, NULL}};
2974
2975 const char* statement_data[] = {
2976 "() => {}", "() => { return 42 }",
2977 "x => { return x; }", "(x) => { return x; }",
2978 "(x, y) => { return x + y; }", "(x, y, z) => { return x + y + z; }",
2979 "(x, y) => { x.a = y; }", "() => 42",
2980 "x => x", "x => x * x",
2981 "(x) => x", "(x) => x * x",
2982 "(x, y) => x + y", "(x, y, z) => x, y, z",
2983 "(x, y) => x.a = y", "() => ({'value': 42})",
2984 "x => y => x + y", "(x, y) => (u, v) => x*u + y*v",
2985 "(x, y) => z => z * (x + y)", "x => (y, z) => z * (x + y)",
2986
2987 // Those are comma-separated expressions, with arrow functions as items.
2988 // They stress the code for validating arrow function parameter lists.
2989 "a, b => 0", "a, b, (c, d) => 0",
2990 "(a, b, (c, d) => 0)", "(a, b) => 0, (c, d) => 1",
2991 "(a, b => {}, a => a + 1)", "((a, b) => {}, (a => a + 1))",
2992 "(a, (a, (b, c) => 0))",
2993
2994 // Arrow has more precedence, this is the same as: foo ? bar : (baz = {})
2995 "foo ? bar : baz => {}", NULL};
2996
2997 static const ParserFlag always_flags[] = {kAllowArrowFunctions};
2998 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
2999 always_flags, ARRAY_SIZE(always_flags));
3000 }
OLDNEW
« no previous file with comments | « src/token.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698