OLD | NEW |
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 1640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1651 "var interface;", | 1651 "var interface;", |
1652 "var foo, interface;", | 1652 "var foo, interface;", |
1653 "try { } catch (interface) { }", | 1653 "try { } catch (interface) { }", |
1654 "function interface() { }", | 1654 "function interface() { }", |
1655 "function foo(interface) { }", | 1655 "function foo(interface) { }", |
1656 "function foo(bar, interface) { }", | 1656 "function foo(bar, interface) { }", |
1657 "interface = 1;", | 1657 "interface = 1;", |
1658 "var foo = interface = 1;", | 1658 "var foo = interface = 1;", |
1659 "++interface;", | 1659 "++interface;", |
1660 "interface++;", | 1660 "interface++;", |
| 1661 "var yield = 13;", |
1661 NULL | 1662 NULL |
1662 }; | 1663 }; |
1663 | 1664 |
1664 RunParserSyncTest(context_data, statement_data, kError); | 1665 RunParserSyncTest(context_data, statement_data, kError); |
1665 } | 1666 } |
1666 | 1667 |
1667 | 1668 |
1668 TEST(NoErrorsFutureStrictReservedWords) { | 1669 TEST(NoErrorsFutureStrictReservedWords) { |
1669 const char* context_data[][2] = { | 1670 const char* context_data[][2] = { |
1670 { "", "" }, | 1671 { "", "" }, |
1671 { "function test_func() {", "}"}, | 1672 { "function test_func() {", "}"}, |
1672 { NULL, NULL } | 1673 { NULL, NULL } |
1673 }; | 1674 }; |
1674 | 1675 |
1675 const char* statement_data[] = { | 1676 const char* statement_data[] = { |
1676 "var interface;", | 1677 "var interface;", |
1677 "var foo, interface;", | 1678 "var foo, interface;", |
1678 "try { } catch (interface) { }", | 1679 "try { } catch (interface) { }", |
1679 "function interface() { }", | 1680 "function interface() { }", |
1680 "function foo(interface) { }", | 1681 "function foo(interface) { }", |
1681 "function foo(bar, interface) { }", | 1682 "function foo(bar, interface) { }", |
1682 "interface = 1;", | 1683 "interface = 1;", |
1683 "var foo = interface = 1;", | 1684 "var foo = interface = 1;", |
1684 "++interface;", | 1685 "++interface;", |
1685 "interface++;", | 1686 "interface++;", |
| 1687 "var yield = 13;", |
1686 NULL | 1688 NULL |
1687 }; | 1689 }; |
1688 | 1690 |
1689 RunParserSyncTest(context_data, statement_data, kSuccess); | 1691 RunParserSyncTest(context_data, statement_data, kSuccess); |
1690 } | 1692 } |
1691 | 1693 |
1692 | 1694 |
1693 TEST(ErrorsReservedWords) { | 1695 TEST(ErrorsReservedWords) { |
1694 // Tests that both preparsing and parsing produce the right kind of errors for | 1696 // Tests that both preparsing and parsing produce the right kind of errors for |
1695 // using future reserved words as identifiers. These tests don't depend on the | 1697 // using future reserved words as identifiers. These tests don't depend on the |
(...skipping 18 matching lines...) Expand all Loading... |
1714 "++super;", | 1716 "++super;", |
1715 "super++;", | 1717 "super++;", |
1716 "function foo super", | 1718 "function foo super", |
1717 NULL | 1719 NULL |
1718 }; | 1720 }; |
1719 | 1721 |
1720 RunParserSyncTest(context_data, statement_data, kError); | 1722 RunParserSyncTest(context_data, statement_data, kError); |
1721 } | 1723 } |
1722 | 1724 |
1723 | 1725 |
1724 TEST(NoErrorsYieldSloppy) { | 1726 TEST(NoErrorsYieldSloppyAllModes) { |
1725 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a | 1727 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a |
1726 // generator (see next test). | 1728 // generator (see next test). |
1727 const char* context_data[][2] = { | 1729 const char* context_data[][2] = { |
1728 { "", "" }, | 1730 { "", "" }, |
1729 { "function is_not_gen() {", "}" }, | 1731 { "function not_gen() {", "}" }, |
| 1732 { "(function not_gen() {", "})" }, |
1730 { NULL, NULL } | 1733 { NULL, NULL } |
1731 }; | 1734 }; |
1732 | 1735 |
1733 const char* statement_data[] = { | 1736 const char* statement_data[] = { |
1734 "var yield;", | 1737 "var yield;", |
1735 "var foo, yield;", | 1738 "var foo, yield;", |
1736 "try { } catch (yield) { }", | 1739 "try { } catch (yield) { }", |
1737 "function yield() { }", | 1740 "function yield() { }", |
| 1741 "(function yield() { })", |
1738 "function foo(yield) { }", | 1742 "function foo(yield) { }", |
1739 "function foo(bar, yield) { }", | 1743 "function foo(bar, yield) { }", |
1740 "yield = 1;", | 1744 "yield = 1;", |
1741 "var foo = yield = 1;", | 1745 "var foo = yield = 1;", |
1742 "++yield;", | 1746 "++yield;", |
1743 "yield++;", | 1747 "yield++;", |
| 1748 "yield: 34", |
| 1749 "function yield(yield) { yield: yield (yield + yield (0)); }", |
| 1750 "({ yield: 1 })", |
| 1751 "({ get yield() { 1 } })", |
| 1752 "yield (100)", |
1744 NULL | 1753 NULL |
1745 }; | 1754 }; |
1746 | 1755 |
1747 RunParserSyncTest(context_data, statement_data, kSuccess); | 1756 RunParserSyncTest(context_data, statement_data, kSuccess); |
1748 } | 1757 } |
1749 | 1758 |
1750 | 1759 |
1751 TEST(ErrorsYieldSloppyGenerator) { | 1760 TEST(NoErrorsYieldSloppyGeneratorsEnabled) { |
| 1761 // In sloppy mode, it's okay to use "yield" as identifier, *except* inside a |
| 1762 // generator (see next test). |
1752 const char* context_data[][2] = { | 1763 const char* context_data[][2] = { |
1753 { "function * is_gen() {", "}" }, | 1764 { "", "" }, |
| 1765 { "function not_gen() {", "}" }, |
| 1766 { "function * gen() { function not_gen() {", "} }" }, |
| 1767 { "(function not_gen() {", "})" }, |
| 1768 { "(function * gen() { (function not_gen() {", "}) })" }, |
1754 { NULL, NULL } | 1769 { NULL, NULL } |
1755 }; | 1770 }; |
1756 | 1771 |
1757 const char* statement_data[] = { | 1772 const char* statement_data[] = { |
1758 "var yield;", | 1773 "var yield;", |
1759 "var foo, yield;", | 1774 "var foo, yield;", |
1760 "try { } catch (yield) { }", | 1775 "try { } catch (yield) { }", |
1761 "function yield() { }", | 1776 "function yield() { }", |
1762 // BUG: These should not be allowed, but they are (if kAllowGenerators is | 1777 "(function yield() { })", |
1763 // set) | 1778 "function foo(yield) { }", |
1764 // "function foo(yield) { }", | 1779 "function foo(bar, yield) { }", |
1765 // "function foo(bar, yield) { }", | 1780 "function * yield() { }", |
| 1781 "(function * yield() { })", |
1766 "yield = 1;", | 1782 "yield = 1;", |
1767 "var foo = yield = 1;", | 1783 "var foo = yield = 1;", |
1768 "++yield;", | 1784 "++yield;", |
1769 "yield++;", | 1785 "yield++;", |
| 1786 "yield: 34", |
| 1787 "function yield(yield) { yield: yield (yield + yield (0)); }", |
| 1788 "({ yield: 1 })", |
| 1789 "({ get yield() { 1 } })", |
| 1790 "yield (100)", |
1770 NULL | 1791 NULL |
1771 }; | 1792 }; |
1772 | 1793 |
1773 // If generators are not allowed, the error will be produced at the '*' token, | 1794 // This test requires kAllowGenerators to succeed. |
1774 // so this test works both with and without the kAllowGenerators flag. | 1795 static const ParserFlag always_true_flags[] = { |
1775 RunParserSyncTest(context_data, statement_data, kError); | 1796 kAllowGenerators |
| 1797 }; |
| 1798 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
| 1799 always_true_flags, 1); |
1776 } | 1800 } |
1777 | 1801 |
1778 | 1802 |
1779 TEST(ErrorsYieldStrict) { | 1803 TEST(ErrorsYieldStrict) { |
1780 const char* context_data[][2] = { | 1804 const char* context_data[][2] = { |
1781 { "\"use strict\";", "" }, | 1805 { "\"use strict\";", "" }, |
1782 { "\"use strict\"; function is_not_gen() {", "}" }, | 1806 { "\"use strict\"; function not_gen() {", "}" }, |
1783 { "function test_func() {\"use strict\"; ", "}"}, | 1807 { "function test_func() {\"use strict\"; ", "}"}, |
| 1808 { "\"use strict\"; function * gen() { function not_gen() {", "} }" }, |
| 1809 { "\"use strict\"; (function not_gen() {", "})" }, |
| 1810 { "\"use strict\"; (function * gen() { (function not_gen() {", "}) })" }, |
1784 { NULL, NULL } | 1811 { NULL, NULL } |
1785 }; | 1812 }; |
1786 | 1813 |
1787 const char* statement_data[] = { | 1814 const char* statement_data[] = { |
1788 "var yield;", | 1815 "var yield;", |
1789 "var foo, yield;", | 1816 "var foo, yield;", |
1790 "try { } catch (yield) { }", | 1817 "try { } catch (yield) { }", |
1791 "function yield() { }", | 1818 "function yield() { }", |
| 1819 "(function yield() { })", |
1792 "function foo(yield) { }", | 1820 "function foo(yield) { }", |
1793 "function foo(bar, yield) { }", | 1821 "function foo(bar, yield) { }", |
| 1822 "function * yield() { }", |
| 1823 "(function * yield() { })", |
1794 "yield = 1;", | 1824 "yield = 1;", |
1795 "var foo = yield = 1;", | 1825 "var foo = yield = 1;", |
1796 "++yield;", | 1826 "++yield;", |
1797 "yield++;", | 1827 "yield++;", |
| 1828 "yield: 34;", |
1798 NULL | 1829 NULL |
1799 }; | 1830 }; |
1800 | 1831 |
1801 RunParserSyncTest(context_data, statement_data, kError); | 1832 RunParserSyncTest(context_data, statement_data, kError); |
1802 } | 1833 } |
1803 | 1834 |
1804 | 1835 |
1805 TEST(NoErrorsYield) { | 1836 TEST(NoErrorsGenerator) { |
1806 const char* context_data[][2] = { | 1837 const char* context_data[][2] = { |
1807 { "function * is_gen() {", "}" }, | 1838 { "function * gen() {", "}" }, |
| 1839 { "(function * gen() {", "})" }, |
| 1840 { "(function * () {", "})" }, |
1808 { NULL, NULL } | 1841 { NULL, NULL } |
1809 }; | 1842 }; |
1810 | 1843 |
1811 const char* statement_data[] = { | 1844 const char* statement_data[] = { |
1812 "yield 2;", // this is legal inside generator | 1845 // A generator without a body is valid. |
1813 "yield * 2;", // this is legal inside generator | 1846 "" |
| 1847 // Valid yield expressions inside generators. |
| 1848 "yield 2;", |
| 1849 "yield * 2;", |
| 1850 "yield * \n 2;", |
| 1851 "yield yield 1;", |
| 1852 "yield * yield * 1;", |
| 1853 "yield 3 + (yield 4);", |
| 1854 "yield * 3 + (yield * 4);", |
| 1855 "(yield * 3) + (yield * 4);", |
| 1856 "yield 3; yield 4;", |
| 1857 "yield * 3; yield * 4;", |
| 1858 "(function (yield) { })", |
| 1859 // You can return in a generator. |
| 1860 "yield 1; return", |
| 1861 "yield * 1; return", |
| 1862 "yield 1; return 37", |
| 1863 "yield * 1; return 37", |
| 1864 "yield 1; return 37; yield 'dead';", |
| 1865 "yield * 1; return 37; yield * 'dead';", |
| 1866 // Yield is still a valid key in object literals. |
| 1867 "({ yield: 1 })", |
| 1868 "({ get yield() { } })", |
| 1869 // TODO(348893007): Invalid (no newline allowed between yield and *). |
| 1870 "yield\n*3", |
1814 NULL | 1871 NULL |
1815 }; | 1872 }; |
1816 | 1873 |
1817 // This test requires kAllowGenerators to succeed. | 1874 // This test requires kAllowGenerators to succeed. |
1818 static const ParserFlag always_true_flags[] = { | 1875 static const ParserFlag always_true_flags[] = { |
1819 kAllowGenerators | 1876 kAllowGenerators |
1820 }; | 1877 }; |
1821 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, | 1878 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
1822 always_true_flags, 1); | 1879 always_true_flags, 1); |
1823 } | 1880 } |
1824 | 1881 |
1825 | 1882 |
| 1883 TEST(ErrorsYieldGenerator) { |
| 1884 const char* context_data[][2] = { |
| 1885 { "function * gen() {", "}" }, |
| 1886 { "\"use strict\"; function * gen() {", "}" }, |
| 1887 { NULL, NULL } |
| 1888 }; |
| 1889 |
| 1890 const char* statement_data[] = { |
| 1891 // Invalid yield expressions inside generators. |
| 1892 "var yield;", |
| 1893 "var foo, yield;", |
| 1894 "try { } catch (yield) { }", |
| 1895 "function yield() { }", |
| 1896 // The name of the NFE is let-bound in the generator, which does not permit |
| 1897 // yield to be an identifier. |
| 1898 "(function yield() { })", |
| 1899 "(function * yield() { })", |
| 1900 // Yield isn't valid as a formal parameter for generators. |
| 1901 "function * foo(yield) { }", |
| 1902 "(function * foo(yield) { })", |
| 1903 "yield = 1;", |
| 1904 "var foo = yield = 1;", |
| 1905 "++yield;", |
| 1906 "yield++;", |
| 1907 "yield *", |
| 1908 "(yield *)", |
| 1909 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which |
| 1910 // is invalid. |
| 1911 "yield 3 + yield 4;", |
| 1912 "yield: 34", |
| 1913 "yield ? 1 : 2", |
| 1914 // Parses as yield (/ yield): invalid. |
| 1915 "yield / yield", |
| 1916 // TODO(348893007): The rest of these should be valid. |
| 1917 "yield;", |
| 1918 "yield", |
| 1919 "yield\n", |
| 1920 "(yield)", |
| 1921 "[yield]", |
| 1922 "{yield}", |
| 1923 "yield, yield", |
| 1924 "yield; yield", |
| 1925 "(yield) ? yield : yield", |
| 1926 "(yield) \n ? yield : yield", |
| 1927 // Parses as yield (+ yield). |
| 1928 "yield + yield", |
| 1929 NULL |
| 1930 }; |
| 1931 |
| 1932 RunParserSyncTest(context_data, statement_data, kError); |
| 1933 } |
| 1934 |
| 1935 |
1826 TEST(ErrorsNameOfStrictFunction) { | 1936 TEST(ErrorsNameOfStrictFunction) { |
1827 // Tests that illegal tokens as names of a strict function produce the correct | 1937 // Tests that illegal tokens as names of a strict function produce the correct |
1828 // errors. | 1938 // errors. |
1829 const char* context_data[][2] = { | 1939 const char* context_data[][2] = { |
1830 { "", ""}, | 1940 { "function ", ""}, |
1831 { "\"use strict\";", ""}, | 1941 { "\"use strict\"; function", ""}, |
| 1942 { "function * ", ""}, |
| 1943 { "\"use strict\"; function * ", ""}, |
1832 { NULL, NULL } | 1944 { NULL, NULL } |
1833 }; | 1945 }; |
1834 | 1946 |
1835 const char* statement_data[] = { | 1947 const char* statement_data[] = { |
1836 "function eval() {\"use strict\";}", | 1948 "eval() {\"use strict\";}", |
1837 "function arguments() {\"use strict\";}", | 1949 "arguments() {\"use strict\";}", |
1838 "function interface() {\"use strict\";}", | 1950 "interface() {\"use strict\";}", |
1839 "function yield() {\"use strict\";}", | 1951 "yield() {\"use strict\";}", |
1840 // Future reserved words are always illegal | 1952 // Future reserved words are always illegal |
1841 "function super() { }", | 1953 "function super() { }", |
1842 "function super() {\"use strict\";}", | 1954 "function super() {\"use strict\";}", |
1843 NULL | 1955 NULL |
1844 }; | 1956 }; |
1845 | 1957 |
1846 RunParserSyncTest(context_data, statement_data, kError); | 1958 RunParserSyncTest(context_data, statement_data, kError); |
1847 } | 1959 } |
1848 | 1960 |
1849 | 1961 |
1850 TEST(NoErrorsNameOfStrictFunction) { | 1962 TEST(NoErrorsNameOfStrictFunction) { |
1851 const char* context_data[][2] = { | 1963 const char* context_data[][2] = { |
1852 { "", ""}, | 1964 { "function ", ""}, |
1853 { NULL, NULL } | 1965 { NULL, NULL } |
1854 }; | 1966 }; |
1855 | 1967 |
1856 const char* statement_data[] = { | 1968 const char* statement_data[] = { |
1857 "function eval() { }", | 1969 "eval() { }", |
1858 "function arguments() { }", | 1970 "arguments() { }", |
1859 "function interface() { }", | 1971 "interface() { }", |
1860 "function yield() { }", | 1972 "yield() { }", |
1861 NULL | 1973 NULL |
1862 }; | 1974 }; |
1863 | 1975 |
1864 RunParserSyncTest(context_data, statement_data, kSuccess); | 1976 RunParserSyncTest(context_data, statement_data, kSuccess); |
1865 } | 1977 } |
1866 | 1978 |
1867 | 1979 |
| 1980 TEST(NoErrorsNameOfStrictGenerator) { |
| 1981 const char* context_data[][2] = { |
| 1982 { "function * ", ""}, |
| 1983 { NULL, NULL } |
| 1984 }; |
| 1985 |
| 1986 const char* statement_data[] = { |
| 1987 "eval() { }", |
| 1988 "arguments() { }", |
| 1989 "interface() { }", |
| 1990 "yield() { }", |
| 1991 NULL |
| 1992 }; |
| 1993 |
| 1994 // This test requires kAllowGenerators to succeed. |
| 1995 static const ParserFlag always_true_flags[] = { |
| 1996 kAllowGenerators |
| 1997 }; |
| 1998 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
| 1999 always_true_flags, 1); |
| 2000 } |
| 2001 |
1868 | 2002 |
1869 TEST(ErrorsIllegalWordsAsLabelsSloppy) { | 2003 TEST(ErrorsIllegalWordsAsLabelsSloppy) { |
1870 // Using future reserved words as labels is always an error. | 2004 // Using future reserved words as labels is always an error. |
1871 const char* context_data[][2] = { | 2005 const char* context_data[][2] = { |
1872 { "", ""}, | 2006 { "", ""}, |
1873 { "function test_func() {", "}" }, | 2007 { "function test_func() {", "}" }, |
1874 { NULL, NULL } | 2008 { NULL, NULL } |
1875 }; | 2009 }; |
1876 | 2010 |
1877 const char* statement_data[] = { | 2011 const char* statement_data[] = { |
(...skipping 886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2764 LocalContext env; | 2898 LocalContext env; |
2765 int use_counts[v8::Isolate::kUseCounterFeatureCount] = {}; | 2899 int use_counts[v8::Isolate::kUseCounterFeatureCount] = {}; |
2766 global_use_counts = use_counts; | 2900 global_use_counts = use_counts; |
2767 CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback); | 2901 CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback); |
2768 CompileRun("\"use asm\";\n" | 2902 CompileRun("\"use asm\";\n" |
2769 "var foo = 1;\n" | 2903 "var foo = 1;\n" |
2770 "\"use asm\";\n" // Only the first one counts. | 2904 "\"use asm\";\n" // Only the first one counts. |
2771 "function bar() { \"use asm\"; var baz = 1; }"); | 2905 "function bar() { \"use asm\"; var baz = 1; }"); |
2772 CHECK_EQ(2, use_counts[v8::Isolate::kUseAsm]); | 2906 CHECK_EQ(2, use_counts[v8::Isolate::kUseAsm]); |
2773 } | 2907 } |
OLD | NEW |