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

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

Issue 1411203002: [es6] implement destructuring assignment [clean diff] (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rename `recorded_first()` to `FirstRecorded()` because non-accessor Created 5 years, 1 month 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
« no previous file with comments | « src/preparser.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 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 1493
1494 enum ParserFlag { 1494 enum ParserFlag {
1495 kAllowLazy, 1495 kAllowLazy,
1496 kAllowNatives, 1496 kAllowNatives,
1497 kAllowHarmonyDefaultParameters, 1497 kAllowHarmonyDefaultParameters,
1498 kAllowHarmonyRestParameters, 1498 kAllowHarmonyRestParameters,
1499 kAllowHarmonySloppy, 1499 kAllowHarmonySloppy,
1500 kAllowHarmonySloppyLet, 1500 kAllowHarmonySloppyLet,
1501 kAllowHarmonySpreadCalls, 1501 kAllowHarmonySpreadCalls,
1502 kAllowHarmonyDestructuring, 1502 kAllowHarmonyDestructuring,
1503 kAllowHarmonyDestructuringAssignment,
1503 kAllowHarmonySpreadArrays, 1504 kAllowHarmonySpreadArrays,
1504 kAllowHarmonyNewTarget, 1505 kAllowHarmonyNewTarget,
1505 kAllowStrongMode, 1506 kAllowStrongMode,
1506 kNoLegacyConst 1507 kNoLegacyConst
1507 }; 1508 };
1508 1509
1509 1510
1510 enum ParserSyncTestResult { 1511 enum ParserSyncTestResult {
1511 kSuccessOrError, 1512 kSuccessOrError,
1512 kSuccess, 1513 kSuccess,
1513 kError 1514 kError
1514 }; 1515 };
1515 1516
1516 template <typename Traits> 1517 template <typename Traits>
1517 void SetParserFlags(i::ParserBase<Traits>* parser, 1518 void SetParserFlags(i::ParserBase<Traits>* parser,
1518 i::EnumSet<ParserFlag> flags) { 1519 i::EnumSet<ParserFlag> flags) {
1519 parser->set_allow_lazy(flags.Contains(kAllowLazy)); 1520 parser->set_allow_lazy(flags.Contains(kAllowLazy));
1520 parser->set_allow_natives(flags.Contains(kAllowNatives)); 1521 parser->set_allow_natives(flags.Contains(kAllowNatives));
1521 parser->set_allow_harmony_default_parameters( 1522 parser->set_allow_harmony_default_parameters(
1522 flags.Contains(kAllowHarmonyDefaultParameters)); 1523 flags.Contains(kAllowHarmonyDefaultParameters));
1523 parser->set_allow_harmony_rest_parameters( 1524 parser->set_allow_harmony_rest_parameters(
1524 flags.Contains(kAllowHarmonyRestParameters)); 1525 flags.Contains(kAllowHarmonyRestParameters));
1525 parser->set_allow_harmony_spread_calls( 1526 parser->set_allow_harmony_spread_calls(
1526 flags.Contains(kAllowHarmonySpreadCalls)); 1527 flags.Contains(kAllowHarmonySpreadCalls));
1527 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); 1528 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy));
1528 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); 1529 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet));
1529 parser->set_allow_harmony_destructuring( 1530 parser->set_allow_harmony_destructuring(
1530 flags.Contains(kAllowHarmonyDestructuring)); 1531 flags.Contains(kAllowHarmonyDestructuring));
1532 parser->set_allow_harmony_destructuring_assignment(
1533 flags.Contains(kAllowHarmonyDestructuringAssignment));
1531 parser->set_allow_harmony_spread_arrays( 1534 parser->set_allow_harmony_spread_arrays(
1532 flags.Contains(kAllowHarmonySpreadArrays)); 1535 flags.Contains(kAllowHarmonySpreadArrays));
1533 parser->set_allow_harmony_new_target(flags.Contains(kAllowHarmonyNewTarget)); 1536 parser->set_allow_harmony_new_target(flags.Contains(kAllowHarmonyNewTarget));
1534 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode)); 1537 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode));
1535 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst)); 1538 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst));
1536 } 1539 }
1537 1540
1538 1541
1539 void TestParserSyncWithFlags(i::Handle<i::String> source, 1542 void TestParserSyncWithFlags(i::Handle<i::String> source,
1540 i::EnumSet<ParserFlag> flags, 1543 i::EnumSet<ParserFlag> flags,
(...skipping 4969 matching lines...) Expand 10 before | Expand all | Expand 10 after
6510 CHECK_EQ(0, 6513 CHECK_EQ(0,
6511 strcmp( 6514 strcmp(
6512 "ReferenceError: In strong mode, using an undeclared global " 6515 "ReferenceError: In strong mode, using an undeclared global "
6513 "variable 'not_there3' is not allowed", 6516 "variable 'not_there3' is not allowed",
6514 *exception)); 6517 *exception));
6515 } 6518 }
6516 } 6519 }
6517 6520
6518 6521
6519 TEST(DestructuringPositiveTests) { 6522 TEST(DestructuringPositiveTests) {
6520 i::FLAG_harmony_destructuring = true;
6521
6522 const char* context_data[][2] = {{"'use strict'; let ", " = {};"}, 6523 const char* context_data[][2] = {{"'use strict'; let ", " = {};"},
6523 {"var ", " = {};"}, 6524 {"var ", " = {};"},
6524 {"'use strict'; const ", " = {};"}, 6525 {"'use strict'; const ", " = {};"},
6525 {"function f(", ") {}"}, 6526 {"function f(", ") {}"},
6526 {"function f(argument1, ", ") {}"}, 6527 {"function f(argument1, ", ") {}"},
6527 {"var f = (", ") => {};"}, 6528 {"var f = (", ") => {};"},
6528 {"var f = (argument1,", ") => {};"}, 6529 {"var f = (argument1,", ") => {};"},
6529 {NULL, NULL}}; 6530 {NULL, NULL}};
6530 6531
6531 // clang-format off 6532 // clang-format off
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
6710 "[yield]", 6711 "[yield]",
6711 "{ x : yield }", 6712 "{ x : yield }",
6712 NULL}; 6713 NULL};
6713 // clang-format on 6714 // clang-format on
6714 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6715 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6715 arraysize(always_flags)); 6716 arraysize(always_flags));
6716 } 6717 }
6717 } 6718 }
6718 6719
6719 6720
6721 TEST(DestructuringAssignmentPositiveTests) {
6722 const char* context_data[][2] = {
6723 {"'use strict'; let x, y, z; (", " = {});"},
6724 {"var x, y, z; (", " = {});"},
6725 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6726 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6727 {"var x, y, z; for (x in ", " = {});"},
6728 {"var x, y, z; for (x of ", " = {});"},
6729 {NULL, NULL}};
6730
6731 // clang-format off
6732 const char* data[] = {
6733 "x",
6734
6735 "{ x : y }",
6736 "{ x : foo().y }",
6737 "{ x : foo()[y] }",
6738 "{ x : y.z }",
6739 "{ x : y[z] }",
6740 "{ x : { y } }",
6741 "{ x : { foo: y } }",
6742 "{ x : { foo: foo().y } }",
6743 "{ x : { foo: foo()[y] } }",
6744 "{ x : { foo: y.z } }",
6745 "{ x : { foo: y[z] } }",
6746 "{ x : [ y ] }",
6747 "{ x : [ foo().y ] }",
6748 "{ x : [ foo()[y] ] }",
6749 "{ x : [ y.z ] }",
6750 "{ x : [ y[z] ] }",
6751
6752 "{ x : y = 10 }",
6753 "{ x : foo().y = 10 }",
6754 "{ x : foo()[y] = 10 }",
6755 "{ x : y.z = 10 }",
6756 "{ x : y[z] = 10 }",
6757 "{ x : { y = 10 } = {} }",
6758 "{ x : { foo: y = 10 } = {} }",
6759 "{ x : { foo: foo().y = 10 } = {} }",
6760 "{ x : { foo: foo()[y] = 10 } = {} }",
6761 "{ x : { foo: y.z = 10 } = {} }",
6762 "{ x : { foo: y[z] = 10 } = {} }",
6763 "{ x : [ y = 10 ] = {} }",
6764 "{ x : [ foo().y = 10 ] = {} }",
6765 "{ x : [ foo()[y] = 10 ] = {} }",
6766 "{ x : [ y.z = 10 ] = {} }",
6767 "{ x : [ y[z] = 10 ] = {} }",
6768
6769 "[ x ]",
6770 "[ foo().x ]",
6771 "[ foo()[x] ]",
6772 "[ x.y ]",
6773 "[ x[y] ]",
6774 "[ { x } ]",
6775 "[ { x : y } ]",
6776 "[ { x : foo().y } ]",
6777 "[ { x : foo()[y] } ]",
6778 "[ { x : x.y } ]",
6779 "[ { x : x[y] } ]",
6780 "[ [ x ] ]",
6781 "[ [ foo().x ] ]",
6782 "[ [ foo()[x] ] ]",
6783 "[ [ x.y ] ]",
6784 "[ [ x[y] ] ]",
6785
6786 "[ x = 10 ]",
6787 "[ foo().x = 10 ]",
6788 "[ foo()[x] = 10 ]",
6789 "[ x.y = 10 ]",
6790 "[ x[y] = 10 ]",
6791 "[ { x = 10 } = {} ]",
6792 "[ { x : y = 10 } = {} ]",
6793 "[ { x : foo().y = 10 } = {} ]",
6794 "[ { x : foo()[y] = 10 } = {} ]",
6795 "[ { x : x.y = 10 } = {} ]",
6796 "[ { x : x[y] = 10 } = {} ]",
6797 "[ [ x = 10 ] = {} ]",
6798 "[ [ foo().x = 10 ] = {} ]",
6799 "[ [ foo()[x] = 10 ] = {} ]",
6800 "[ [ x.y = 10 ] = {} ]",
6801 "[ [ x[y] = 10 ] = {} ]",
6802
6803 "{ x : y }",
6804 "{ x : y = 1 }",
6805 "{ x }",
6806 "{ x, y, z }",
6807 "{ x = 1, y: z, z: y }",
6808 "{x = 42, y = 15}",
6809 "[x]",
6810 "[x = 1]",
6811 "[x,y,z]",
6812 "[x, y = 42, z]",
6813 "{ x : x, y : y }",
6814 "{ x : x = 1, y : y }",
6815 "{ x : x, y : y = 42 }",
6816 "[]",
6817 "{}",
6818 "[{x:x, y:y}, [,x,z,]]",
6819 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6820 "[x,,y]",
6821 "[(x),,(y)]",
6822 "[(x)]",
6823 "{42 : x}",
6824 "{42 : x = 42}",
6825 "{42e-2 : x}",
6826 "{42e-2 : x = 42}",
6827 "{'hi' : x}",
6828 "{'hi' : x = 42}",
6829 "{var: x}",
6830 "{var: x = 42}",
6831 "{var: (x) = 42}",
6832 "{[x] : z}",
6833 "{[1+1] : z}",
6834 "{[1+1] : (z)}",
6835 "{[foo()] : z}",
6836 "{[foo()] : (z)}",
6837 "{[foo()] : foo().bar}",
6838 "{[foo()] : foo()['bar']}",
6839 "{[foo()] : this.bar}",
6840 "{[foo()] : this['bar']}",
6841 "{[foo()] : 'foo'.bar}",
6842 "{[foo()] : 'foo'['bar']}",
6843 "[...x]",
6844 "[x,y,...z]",
6845 "[x,,...z]",
6846 "{ x: y } = z",
6847 "[x, y] = z",
6848 "{ x: y } = { z }",
6849 "[x, y] = { z }",
6850 "{ x: y } = [ z ]",
6851 "[x, y] = [ z ]",
6852 "[((x, y) => z).x]",
6853 "{x: ((y, z) => z).x}",
6854 "[((x, y) => z)['x']]",
6855 "{x: ((y, z) => z)['x']}",
6856
6857 "{x: { y = 10 } }",
6858 "[(({ x } = { x: 1 }) => x).a]",
6859 NULL};
6860 // clang-format on
6861 static const ParserFlag always_flags[] = {
6862 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6863 kAllowHarmonyDefaultParameters};
6864 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6865 arraysize(always_flags));
6866
6867 const char* empty_context_data[][2] = {
6868 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6869
6870 // CoverInitializedName ambiguity handling in various contexts
6871 const char* ambiguity_data[] = {
6872 "var foo = { x = 10 } = {};",
6873 "var foo = { q } = { x = 10 } = {};",
6874 "var foo; foo = { x = 10 } = {};",
6875 "var foo; foo = { q } = { x = 10 } = {};",
6876 "var x; ({ x = 10 } = {});",
6877 "var q, x; ({ q } = { x = 10 } = {});",
6878 "var x; [{ x = 10 } = {}]",
6879 "var x; (true ? { x = true } = {} : { x = false } = {})",
6880 "var q, x; (q, { x = 10 } = {});",
6881 "var { x = 10 } = { x = 20 } = {};",
6882 "var { x = 10 } = (o = { x = 20 } = {});",
6883 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6884 NULL,
6885 };
6886 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6887 always_flags, arraysize(always_flags));
6888 }
6889
6890
6891 TEST(DestructuringAssignmentNegativeTests) {
6892 const char* context_data[][2] = {
6893 {"'use strict'; let x, y, z; (", " = {});"},
6894 {"var x, y, z; (", " = {});"},
6895 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6896 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6897 {"var x, y, z; for (x in ", " = {});"},
6898 {"var x, y, z; for (x of ", " = {});"},
6899 {NULL, NULL}};
6900
6901 // clang-format off
6902 const char* data[] = {
6903 "{ x : ++y }",
6904 "{ x : y * 2 }",
6905 "{ ...x }",
6906 "{ get x() {} }",
6907 "{ set x() {} }",
6908 "{ x: y() }",
6909 "{ this }",
6910 "{ x: this }",
6911 "{ x: this = 1 }",
6912 "{ super }",
6913 "{ x: super }",
6914 "{ x: super = 1 }",
6915 "{ new.target }",
6916 "{ x: new.target }",
6917 "{ x: new.target = 1 }",
6918 "[x--]",
6919 "[--x = 1]",
6920 "[x()]",
6921 "[this]",
6922 "[this = 1]",
6923 "[new.target]",
6924 "[new.target = 1]",
6925 "[super]",
6926 "[super = 1]",
6927 "[function f() {}]",
6928 "[50]",
6929 "[(50)]",
6930 "[(function() {})]",
6931 "[(foo())]",
6932 "{ x: 50 }",
6933 "{ x: (50) }",
6934 "['str']",
6935 "{ x: 'str' }",
6936 "{ x: ('str') }",
6937 "{ x: (foo()) }",
6938 "{ x: (function() {}) }",
6939 "{ x: y } = 'str'",
6940 "[x, y] = 'str'",
6941 "[(x,y) => z]",
6942 "{x: (y) => z}",
6943 "[x, ...y, z]",
6944 "[...x,]",
6945 "[x, y, ...z = 1]",
6946 "[...z = 1]",
6947 NULL};
6948 // clang-format on
6949 static const ParserFlag always_flags[] = {
6950 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6951 kAllowHarmonyDefaultParameters};
6952 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6953 arraysize(always_flags));
6954
6955 const char* empty_context_data[][2] = {
6956 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6957
6958 // CoverInitializedName ambiguity handling in various contexts
6959 const char* ambiguity_data[] = {
6960 "var foo = { x = 10 };",
6961 "var foo = { q } = { x = 10 };",
6962 "var foo; foo = { x = 10 };",
6963 "var foo; foo = { q } = { x = 10 };",
6964 "var x; ({ x = 10 });",
6965 "var q, x; ({ q } = { x = 10 });",
6966 "var x; [{ x = 10 }]",
6967 "var x; (true ? { x = true } : { x = false })",
6968 "var q, x; (q, { x = 10 });",
6969 "var { x = 10 } = { x = 20 };",
6970 "var { x = 10 } = (o = { x = 20 });",
6971 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
6972 NULL,
6973 };
6974 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
6975 always_flags, arraysize(always_flags));
6976 }
6977
6978
6720 TEST(DestructuringDisallowPatternsInForVarIn) { 6979 TEST(DestructuringDisallowPatternsInForVarIn) {
6721 i::FLAG_harmony_destructuring = true; 6980 i::FLAG_harmony_destructuring = true;
6722 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 6981 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6723 const char* context_data[][2] = { 6982 const char* context_data[][2] = {
6724 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 6983 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6725 // clang-format off 6984 // clang-format off
6726 const char* error_data[] = { 6985 const char* error_data[] = {
6727 "for (let x = {} in null);", 6986 "for (let x = {} in null);",
6728 "for (let x = {} of null);", 6987 "for (let x = {} of null);",
6729 NULL}; 6988 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
6903 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7162 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6904 kAllowHarmonyDefaultParameters, 7163 kAllowHarmonyDefaultParameters,
6905 kAllowStrongMode}; 7164 kAllowStrongMode};
6906 7165
6907 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7166 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
6908 NULL, 0, always_flags, arraysize(always_flags)); 7167 NULL, 0, always_flags, arraysize(always_flags));
6909 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7168 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
6910 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7169 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
6911 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7170 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
6912 0, always_flags, arraysize(always_flags)); 7171 0, always_flags, arraysize(always_flags));
6913 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
6914 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7172 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
6915 kError, NULL, 0, always_flags, arraysize(always_flags)); 7173 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
6916 7174
6917 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7175 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
6918 0, always_flags, arraysize(always_flags)); 7176 0, always_flags, arraysize(always_flags));
6919 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7177 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
6920 kError, NULL, 0, always_flags, arraysize(always_flags)); 7178 kError, NULL, 0, always_flags, arraysize(always_flags));
6921 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7179 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
6922 always_flags, arraysize(always_flags)); 7180 always_flags, arraysize(always_flags));
6923 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7181 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
6924 kError, NULL, 0, always_flags, arraysize(always_flags)); 7182 kError, NULL, 0, always_flags, arraysize(always_flags));
6925 7183
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
7248 NULL 7506 NULL
7249 }; 7507 };
7250 // clang-format on 7508 // clang-format on
7251 7509
7252 static const ParserFlag fail_flags[] = { 7510 static const ParserFlag fail_flags[] = {
7253 kAllowHarmonySloppy, kAllowHarmonySloppyLet, kNoLegacyConst, 7511 kAllowHarmonySloppy, kAllowHarmonySloppyLet, kNoLegacyConst,
7254 kAllowHarmonyDestructuring}; 7512 kAllowHarmonyDestructuring};
7255 RunParserSyncTest(context_data, fail_data, kError, NULL, 0, fail_flags, 7513 RunParserSyncTest(context_data, fail_data, kError, NULL, 0, fail_flags,
7256 arraysize(fail_flags)); 7514 arraysize(fail_flags));
7257 } 7515 }
OLDNEW
« no previous file with comments | « src/preparser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698