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

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

Issue 1309813007: [es6] implement destructuring assignment (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase*** oops Created 5 years 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/typing-asm.cc ('k') | test/mjsunit/harmony/destructuring-assignment.js » ('j') | 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 1493 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 1504
1505 1505
1506 enum ParserFlag { 1506 enum ParserFlag {
1507 kAllowLazy, 1507 kAllowLazy,
1508 kAllowNatives, 1508 kAllowNatives,
1509 kAllowHarmonyDefaultParameters, 1509 kAllowHarmonyDefaultParameters,
1510 kAllowHarmonyRestParameters, 1510 kAllowHarmonyRestParameters,
1511 kAllowHarmonySloppy, 1511 kAllowHarmonySloppy,
1512 kAllowHarmonySloppyLet, 1512 kAllowHarmonySloppyLet,
1513 kAllowHarmonyDestructuring, 1513 kAllowHarmonyDestructuring,
1514 kAllowHarmonyDestructuringAssignment,
1514 kAllowHarmonyNewTarget, 1515 kAllowHarmonyNewTarget,
1515 kAllowStrongMode, 1516 kAllowStrongMode,
1516 kNoLegacyConst 1517 kNoLegacyConst
1517 }; 1518 };
1518 1519
1519 1520
1520 enum ParserSyncTestResult { 1521 enum ParserSyncTestResult {
1521 kSuccessOrError, 1522 kSuccessOrError,
1522 kSuccess, 1523 kSuccess,
1523 kError 1524 kError
1524 }; 1525 };
1525 1526
1526 template <typename Traits> 1527 template <typename Traits>
1527 void SetParserFlags(i::ParserBase<Traits>* parser, 1528 void SetParserFlags(i::ParserBase<Traits>* parser,
1528 i::EnumSet<ParserFlag> flags) { 1529 i::EnumSet<ParserFlag> flags) {
1529 parser->set_allow_lazy(flags.Contains(kAllowLazy)); 1530 parser->set_allow_lazy(flags.Contains(kAllowLazy));
1530 parser->set_allow_natives(flags.Contains(kAllowNatives)); 1531 parser->set_allow_natives(flags.Contains(kAllowNatives));
1531 parser->set_allow_harmony_default_parameters( 1532 parser->set_allow_harmony_default_parameters(
1532 flags.Contains(kAllowHarmonyDefaultParameters)); 1533 flags.Contains(kAllowHarmonyDefaultParameters));
1533 parser->set_allow_harmony_rest_parameters( 1534 parser->set_allow_harmony_rest_parameters(
1534 flags.Contains(kAllowHarmonyRestParameters)); 1535 flags.Contains(kAllowHarmonyRestParameters));
1535 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); 1536 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy));
1536 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); 1537 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet));
1537 parser->set_allow_harmony_destructuring_bind( 1538 parser->set_allow_harmony_destructuring_bind(
1538 flags.Contains(kAllowHarmonyDestructuring)); 1539 flags.Contains(kAllowHarmonyDestructuring));
1540 parser->set_allow_harmony_destructuring_assignment(
1541 flags.Contains(kAllowHarmonyDestructuringAssignment));
1539 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode)); 1542 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode));
1540 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst)); 1543 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst));
1541 } 1544 }
1542 1545
1543 1546
1544 void TestParserSyncWithFlags(i::Handle<i::String> source, 1547 void TestParserSyncWithFlags(i::Handle<i::String> source,
1545 i::EnumSet<ParserFlag> flags, 1548 i::EnumSet<ParserFlag> flags,
1546 ParserSyncTestResult result, 1549 ParserSyncTestResult result,
1547 bool is_module = false) { 1550 bool is_module = false) {
1548 i::Isolate* isolate = CcTest::i_isolate(); 1551 i::Isolate* isolate = CcTest::i_isolate();
(...skipping 5264 matching lines...) Expand 10 before | Expand all | Expand 10 after
6813 NULL}; 6816 NULL};
6814 // clang-format on 6817 // clang-format on
6815 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 6818 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6816 kAllowHarmonySloppyLet}; 6819 kAllowHarmonySloppyLet};
6817 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6820 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6818 arraysize(always_flags)); 6821 arraysize(always_flags));
6819 } 6822 }
6820 } 6823 }
6821 6824
6822 6825
6826 TEST(DestructuringAssignmentPositiveTests) {
6827 const char* context_data[][2] = {
6828 {"'use strict'; let x, y, z; (", " = {});"},
6829 {"var x, y, z; (", " = {});"},
6830 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6831 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6832 {"var x, y, z; for (x in ", " = {});"},
6833 {"var x, y, z; for (x of ", " = {});"},
6834 {NULL, NULL}};
6835
6836 // clang-format off
6837 const char* data[] = {
6838 "x",
6839
6840 "{ x : y }",
6841 "{ x : foo().y }",
6842 "{ x : foo()[y] }",
6843 "{ x : y.z }",
6844 "{ x : y[z] }",
6845 "{ x : { y } }",
6846 "{ x : { foo: y } }",
6847 "{ x : { foo: foo().y } }",
6848 "{ x : { foo: foo()[y] } }",
6849 "{ x : { foo: y.z } }",
6850 "{ x : { foo: y[z] } }",
6851 "{ x : [ y ] }",
6852 "{ x : [ foo().y ] }",
6853 "{ x : [ foo()[y] ] }",
6854 "{ x : [ y.z ] }",
6855 "{ x : [ y[z] ] }",
6856
6857 "{ x : y = 10 }",
6858 "{ x : foo().y = 10 }",
6859 "{ x : foo()[y] = 10 }",
6860 "{ x : y.z = 10 }",
6861 "{ x : y[z] = 10 }",
6862 "{ x : { y = 10 } = {} }",
6863 "{ x : { foo: y = 10 } = {} }",
6864 "{ x : { foo: foo().y = 10 } = {} }",
6865 "{ x : { foo: foo()[y] = 10 } = {} }",
6866 "{ x : { foo: y.z = 10 } = {} }",
6867 "{ x : { foo: y[z] = 10 } = {} }",
6868 "{ x : [ y = 10 ] = {} }",
6869 "{ x : [ foo().y = 10 ] = {} }",
6870 "{ x : [ foo()[y] = 10 ] = {} }",
6871 "{ x : [ y.z = 10 ] = {} }",
6872 "{ x : [ y[z] = 10 ] = {} }",
6873
6874 "[ x ]",
6875 "[ foo().x ]",
6876 "[ foo()[x] ]",
6877 "[ x.y ]",
6878 "[ x[y] ]",
6879 "[ { x } ]",
6880 "[ { x : y } ]",
6881 "[ { x : foo().y } ]",
6882 "[ { x : foo()[y] } ]",
6883 "[ { x : x.y } ]",
6884 "[ { x : x[y] } ]",
6885 "[ [ x ] ]",
6886 "[ [ foo().x ] ]",
6887 "[ [ foo()[x] ] ]",
6888 "[ [ x.y ] ]",
6889 "[ [ x[y] ] ]",
6890
6891 "[ x = 10 ]",
6892 "[ foo().x = 10 ]",
6893 "[ foo()[x] = 10 ]",
6894 "[ x.y = 10 ]",
6895 "[ x[y] = 10 ]",
6896 "[ { x = 10 } = {} ]",
6897 "[ { x : y = 10 } = {} ]",
6898 "[ { x : foo().y = 10 } = {} ]",
6899 "[ { x : foo()[y] = 10 } = {} ]",
6900 "[ { x : x.y = 10 } = {} ]",
6901 "[ { x : x[y] = 10 } = {} ]",
6902 "[ [ x = 10 ] = {} ]",
6903 "[ [ foo().x = 10 ] = {} ]",
6904 "[ [ foo()[x] = 10 ] = {} ]",
6905 "[ [ x.y = 10 ] = {} ]",
6906 "[ [ x[y] = 10 ] = {} ]",
6907
6908 "{ x : y }",
6909 "{ x : y = 1 }",
6910 "{ x }",
6911 "{ x, y, z }",
6912 "{ x = 1, y: z, z: y }",
6913 "{x = 42, y = 15}",
6914 "[x]",
6915 "[x = 1]",
6916 "[x,y,z]",
6917 "[x, y = 42, z]",
6918 "{ x : x, y : y }",
6919 "{ x : x = 1, y : y }",
6920 "{ x : x, y : y = 42 }",
6921 "[]",
6922 "{}",
6923 "[{x:x, y:y}, [,x,z,]]",
6924 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6925 "[x,,y]",
6926 "[(x),,(y)]",
6927 "[(x)]",
6928 "{42 : x}",
6929 "{42 : x = 42}",
6930 "{42e-2 : x}",
6931 "{42e-2 : x = 42}",
6932 "{'hi' : x}",
6933 "{'hi' : x = 42}",
6934 "{var: x}",
6935 "{var: x = 42}",
6936 "{var: (x) = 42}",
6937 "{[x] : z}",
6938 "{[1+1] : z}",
6939 "{[1+1] : (z)}",
6940 "{[foo()] : z}",
6941 "{[foo()] : (z)}",
6942 "{[foo()] : foo().bar}",
6943 "{[foo()] : foo()['bar']}",
6944 "{[foo()] : this.bar}",
6945 "{[foo()] : this['bar']}",
6946 "{[foo()] : 'foo'.bar}",
6947 "{[foo()] : 'foo'['bar']}",
6948 "[...x]",
6949 "[x,y,...z]",
6950 "[x,,...z]",
6951 "{ x: y } = z",
6952 "[x, y] = z",
6953 "{ x: y } = { z }",
6954 "[x, y] = { z }",
6955 "{ x: y } = [ z ]",
6956 "[x, y] = [ z ]",
6957 "[((x, y) => z).x]",
6958 "{x: ((y, z) => z).x}",
6959 "[((x, y) => z)['x']]",
6960 "{x: ((y, z) => z)['x']}",
6961
6962 "{x: { y = 10 } }",
6963 "[(({ x } = { x: 1 }) => x).a]",
6964 NULL};
6965 // clang-format on
6966 static const ParserFlag always_flags[] = {
6967 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6968 kAllowHarmonyDefaultParameters};
6969 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6970 arraysize(always_flags));
6971
6972 const char* empty_context_data[][2] = {
6973 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6974
6975 // CoverInitializedName ambiguity handling in various contexts
6976 const char* ambiguity_data[] = {
6977 "var foo = { x = 10 } = {};",
6978 "var foo = { q } = { x = 10 } = {};",
6979 "var foo; foo = { x = 10 } = {};",
6980 "var foo; foo = { q } = { x = 10 } = {};",
6981 "var x; ({ x = 10 } = {});",
6982 "var q, x; ({ q } = { x = 10 } = {});",
6983 "var x; [{ x = 10 } = {}]",
6984 "var x; (true ? { x = true } = {} : { x = false } = {})",
6985 "var q, x; (q, { x = 10 } = {});",
6986 "var { x = 10 } = { x = 20 } = {};",
6987 "var { x = 10 } = (o = { x = 20 } = {});",
6988 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6989 NULL,
6990 };
6991 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6992 always_flags, arraysize(always_flags));
6993 }
6994
6995
6996 TEST(DestructuringAssignmentNegativeTests) {
6997 const char* context_data[][2] = {
6998 {"'use strict'; let x, y, z; (", " = {});"},
6999 {"var x, y, z; (", " = {});"},
7000 {"'use strict'; let x, y, z; for (x in ", " = {});"},
7001 {"'use strict'; let x, y, z; for (x of ", " = {});"},
7002 {"var x, y, z; for (x in ", " = {});"},
7003 {"var x, y, z; for (x of ", " = {});"},
7004 {NULL, NULL}};
7005
7006 // clang-format off
7007 const char* data[] = {
7008 "{ x : ++y }",
7009 "{ x : y * 2 }",
7010 "{ ...x }",
7011 "{ get x() {} }",
7012 "{ set x() {} }",
7013 "{ x: y() }",
7014 "{ this }",
7015 "{ x: this }",
7016 "{ x: this = 1 }",
7017 "{ super }",
7018 "{ x: super }",
7019 "{ x: super = 1 }",
7020 "{ new.target }",
7021 "{ x: new.target }",
7022 "{ x: new.target = 1 }",
7023 "[x--]",
7024 "[--x = 1]",
7025 "[x()]",
7026 "[this]",
7027 "[this = 1]",
7028 "[new.target]",
7029 "[new.target = 1]",
7030 "[super]",
7031 "[super = 1]",
7032 "[function f() {}]",
7033 "[50]",
7034 "[(50)]",
7035 "[(function() {})]",
7036 "[(foo())]",
7037 "{ x: 50 }",
7038 "{ x: (50) }",
7039 "['str']",
7040 "{ x: 'str' }",
7041 "{ x: ('str') }",
7042 "{ x: (foo()) }",
7043 "{ x: (function() {}) }",
7044 "{ x: y } = 'str'",
7045 "[x, y] = 'str'",
7046 "[(x,y) => z]",
7047 "{x: (y) => z}",
7048 "[x, ...y, z]",
7049 "[...x,]",
7050 "[x, y, ...z = 1]",
7051 "[...z = 1]",
7052 NULL};
7053 // clang-format on
7054 static const ParserFlag always_flags[] = {
7055 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
7056 kAllowHarmonyDefaultParameters};
7057 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
7058 arraysize(always_flags));
7059
7060 const char* empty_context_data[][2] = {
7061 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
7062
7063 // CoverInitializedName ambiguity handling in various contexts
7064 const char* ambiguity_data[] = {
7065 "var foo = { x = 10 };",
7066 "var foo = { q } = { x = 10 };",
7067 "var foo; foo = { x = 10 };",
7068 "var foo; foo = { q } = { x = 10 };",
7069 "var x; ({ x = 10 });",
7070 "var q, x; ({ q } = { x = 10 });",
7071 "var x; [{ x = 10 }]",
7072 "var x; (true ? { x = true } : { x = false })",
7073 "var q, x; (q, { x = 10 });",
7074 "var { x = 10 } = { x = 20 };",
7075 "var { x = 10 } = (o = { x = 20 });",
7076 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
7077 NULL,
7078 };
7079 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
7080 always_flags, arraysize(always_flags));
7081 }
7082
7083
6823 TEST(DestructuringDisallowPatternsInForVarIn) { 7084 TEST(DestructuringDisallowPatternsInForVarIn) {
6824 i::FLAG_harmony_destructuring_bind = true; 7085 i::FLAG_harmony_destructuring_bind = true;
6825 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 7086 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6826 const char* context_data[][2] = { 7087 const char* context_data[][2] = {
6827 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 7088 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6828 // clang-format off 7089 // clang-format off
6829 const char* error_data[] = { 7090 const char* error_data[] = {
6830 "for (let x = {} in null);", 7091 "for (let x = {} in null);",
6831 "for (let x = {} of null);", 7092 "for (let x = {} of null);",
6832 NULL}; 7093 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
7006 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7267 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
7007 kAllowHarmonyDefaultParameters, 7268 kAllowHarmonyDefaultParameters,
7008 kAllowStrongMode}; 7269 kAllowStrongMode};
7009 7270
7010 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7271 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
7011 NULL, 0, always_flags, arraysize(always_flags)); 7272 NULL, 0, always_flags, arraysize(always_flags));
7012 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7273 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
7013 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7274 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7014 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7275 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
7015 0, always_flags, arraysize(always_flags)); 7276 0, always_flags, arraysize(always_flags));
7016 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
7017 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7277 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
7018 kError, NULL, 0, always_flags, arraysize(always_flags)); 7278 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7019 7279
7020 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7280 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
7021 0, always_flags, arraysize(always_flags)); 7281 0, always_flags, arraysize(always_flags));
7022 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7282 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
7023 kError, NULL, 0, always_flags, arraysize(always_flags)); 7283 kError, NULL, 0, always_flags, arraysize(always_flags));
7024 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7284 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
7025 always_flags, arraysize(always_flags)); 7285 always_flags, arraysize(always_flags));
7026 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7286 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
7027 kError, NULL, 0, always_flags, arraysize(always_flags)); 7287 kError, NULL, 0, always_flags, arraysize(always_flags));
7028 7288
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
7491 "var publ\\u0069c = 1;", 7751 "var publ\\u0069c = 1;",
7492 "var { publ\\u0069c } = {};", 7752 "var { publ\\u0069c } = {};",
7493 NULL}; 7753 NULL};
7494 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0, 7754 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0,
7495 always_flags, arraysize(always_flags)); 7755 always_flags, arraysize(always_flags));
7496 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7756 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7497 always_flags, arraysize(always_flags)); 7757 always_flags, arraysize(always_flags));
7498 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7758 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7499 always_flags, arraysize(always_flags)); 7759 always_flags, arraysize(always_flags));
7500 } 7760 }
OLDNEW
« no previous file with comments | « src/typing-asm.cc ('k') | test/mjsunit/harmony/destructuring-assignment.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698