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

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: Add explicit placeholder RewritableExpression for rewriting 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
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 1494
1495 enum ParserFlag { 1495 enum ParserFlag {
1496 kAllowLazy, 1496 kAllowLazy,
1497 kAllowNatives, 1497 kAllowNatives,
1498 kAllowHarmonyDefaultParameters, 1498 kAllowHarmonyDefaultParameters,
1499 kAllowHarmonyRestParameters, 1499 kAllowHarmonyRestParameters,
1500 kAllowHarmonySloppy, 1500 kAllowHarmonySloppy,
1501 kAllowHarmonySloppyLet, 1501 kAllowHarmonySloppyLet,
1502 kAllowHarmonyDestructuring, 1502 kAllowHarmonyDestructuring,
1503 kAllowHarmonyDestructuringAssignment,
1503 kAllowHarmonyNewTarget, 1504 kAllowHarmonyNewTarget,
1504 kAllowStrongMode, 1505 kAllowStrongMode,
1505 kNoLegacyConst 1506 kNoLegacyConst
1506 }; 1507 };
1507 1508
1508 1509
1509 enum ParserSyncTestResult { 1510 enum ParserSyncTestResult {
1510 kSuccessOrError, 1511 kSuccessOrError,
1511 kSuccess, 1512 kSuccess,
1512 kError 1513 kError
1513 }; 1514 };
1514 1515
1515 template <typename Traits> 1516 template <typename Traits>
1516 void SetParserFlags(i::ParserBase<Traits>* parser, 1517 void SetParserFlags(i::ParserBase<Traits>* parser,
1517 i::EnumSet<ParserFlag> flags) { 1518 i::EnumSet<ParserFlag> flags) {
1518 parser->set_allow_lazy(flags.Contains(kAllowLazy)); 1519 parser->set_allow_lazy(flags.Contains(kAllowLazy));
1519 parser->set_allow_natives(flags.Contains(kAllowNatives)); 1520 parser->set_allow_natives(flags.Contains(kAllowNatives));
1520 parser->set_allow_harmony_default_parameters( 1521 parser->set_allow_harmony_default_parameters(
1521 flags.Contains(kAllowHarmonyDefaultParameters)); 1522 flags.Contains(kAllowHarmonyDefaultParameters));
1522 parser->set_allow_harmony_rest_parameters( 1523 parser->set_allow_harmony_rest_parameters(
1523 flags.Contains(kAllowHarmonyRestParameters)); 1524 flags.Contains(kAllowHarmonyRestParameters));
1524 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); 1525 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy));
1525 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); 1526 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet));
1526 parser->set_allow_harmony_destructuring_bind( 1527 parser->set_allow_harmony_destructuring_bind(
1527 flags.Contains(kAllowHarmonyDestructuring)); 1528 flags.Contains(kAllowHarmonyDestructuring));
1529 parser->set_allow_harmony_destructuring_assignment(
1530 flags.Contains(kAllowHarmonyDestructuringAssignment));
1528 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode)); 1531 parser->set_allow_strong_mode(flags.Contains(kAllowStrongMode));
1529 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst)); 1532 parser->set_allow_legacy_const(!flags.Contains(kNoLegacyConst));
1530 } 1533 }
1531 1534
1532 1535
1533 void TestParserSyncWithFlags(i::Handle<i::String> source, 1536 void TestParserSyncWithFlags(i::Handle<i::String> source,
1534 i::EnumSet<ParserFlag> flags, 1537 i::EnumSet<ParserFlag> flags,
1535 ParserSyncTestResult result, 1538 ParserSyncTestResult result,
1536 bool is_module = false) { 1539 bool is_module = false) {
1537 i::Isolate* isolate = CcTest::i_isolate(); 1540 i::Isolate* isolate = CcTest::i_isolate();
(...skipping 5263 matching lines...) Expand 10 before | Expand all | Expand 10 after
6801 NULL}; 6804 NULL};
6802 // clang-format on 6805 // clang-format on
6803 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 6806 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6804 kAllowHarmonySloppyLet}; 6807 kAllowHarmonySloppyLet};
6805 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6808 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6806 arraysize(always_flags)); 6809 arraysize(always_flags));
6807 } 6810 }
6808 } 6811 }
6809 6812
6810 6813
6814 TEST(DestructuringAssignmentPositiveTests) {
6815 const char* context_data[][2] = {
6816 {"'use strict'; let x, y, z; (", " = {});"},
6817 {"var x, y, z; (", " = {});"},
6818 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6819 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6820 {"var x, y, z; for (x in ", " = {});"},
6821 {"var x, y, z; for (x of ", " = {});"},
6822 {NULL, NULL}};
6823
6824 // clang-format off
6825 const char* data[] = {
6826 "x",
6827
6828 "{ x : y }",
6829 "{ x : foo().y }",
6830 "{ x : foo()[y] }",
6831 "{ x : y.z }",
6832 "{ x : y[z] }",
6833 "{ x : { y } }",
6834 "{ x : { foo: y } }",
6835 "{ x : { foo: foo().y } }",
6836 "{ x : { foo: foo()[y] } }",
6837 "{ x : { foo: y.z } }",
6838 "{ x : { foo: y[z] } }",
6839 "{ x : [ y ] }",
6840 "{ x : [ foo().y ] }",
6841 "{ x : [ foo()[y] ] }",
6842 "{ x : [ y.z ] }",
6843 "{ x : [ y[z] ] }",
6844
6845 "{ x : y = 10 }",
6846 "{ x : foo().y = 10 }",
6847 "{ x : foo()[y] = 10 }",
6848 "{ x : y.z = 10 }",
6849 "{ x : y[z] = 10 }",
6850 "{ x : { y = 10 } = {} }",
6851 "{ x : { foo: y = 10 } = {} }",
6852 "{ x : { foo: foo().y = 10 } = {} }",
6853 "{ x : { foo: foo()[y] = 10 } = {} }",
6854 "{ x : { foo: y.z = 10 } = {} }",
6855 "{ x : { foo: y[z] = 10 } = {} }",
6856 "{ x : [ y = 10 ] = {} }",
6857 "{ x : [ foo().y = 10 ] = {} }",
6858 "{ x : [ foo()[y] = 10 ] = {} }",
6859 "{ x : [ y.z = 10 ] = {} }",
6860 "{ x : [ y[z] = 10 ] = {} }",
6861
6862 "[ x ]",
6863 "[ foo().x ]",
6864 "[ foo()[x] ]",
6865 "[ x.y ]",
6866 "[ x[y] ]",
6867 "[ { x } ]",
6868 "[ { x : y } ]",
6869 "[ { x : foo().y } ]",
6870 "[ { x : foo()[y] } ]",
6871 "[ { x : x.y } ]",
6872 "[ { x : x[y] } ]",
6873 "[ [ x ] ]",
6874 "[ [ foo().x ] ]",
6875 "[ [ foo()[x] ] ]",
6876 "[ [ x.y ] ]",
6877 "[ [ x[y] ] ]",
6878
6879 "[ x = 10 ]",
6880 "[ foo().x = 10 ]",
6881 "[ foo()[x] = 10 ]",
6882 "[ x.y = 10 ]",
6883 "[ x[y] = 10 ]",
6884 "[ { x = 10 } = {} ]",
6885 "[ { x : y = 10 } = {} ]",
6886 "[ { x : foo().y = 10 } = {} ]",
6887 "[ { x : foo()[y] = 10 } = {} ]",
6888 "[ { x : x.y = 10 } = {} ]",
6889 "[ { x : x[y] = 10 } = {} ]",
6890 "[ [ x = 10 ] = {} ]",
6891 "[ [ foo().x = 10 ] = {} ]",
6892 "[ [ foo()[x] = 10 ] = {} ]",
6893 "[ [ x.y = 10 ] = {} ]",
6894 "[ [ x[y] = 10 ] = {} ]",
6895
6896 "{ x : y }",
6897 "{ x : y = 1 }",
6898 "{ x }",
6899 "{ x, y, z }",
6900 "{ x = 1, y: z, z: y }",
6901 "{x = 42, y = 15}",
6902 "[x]",
6903 "[x = 1]",
6904 "[x,y,z]",
6905 "[x, y = 42, z]",
6906 "{ x : x, y : y }",
6907 "{ x : x = 1, y : y }",
6908 "{ x : x, y : y = 42 }",
6909 "[]",
6910 "{}",
6911 "[{x:x, y:y}, [,x,z,]]",
6912 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6913 "[x,,y]",
6914 "[(x),,(y)]",
6915 "[(x)]",
6916 "{42 : x}",
6917 "{42 : x = 42}",
6918 "{42e-2 : x}",
6919 "{42e-2 : x = 42}",
6920 "{'hi' : x}",
6921 "{'hi' : x = 42}",
6922 "{var: x}",
6923 "{var: x = 42}",
6924 "{var: (x) = 42}",
6925 "{[x] : z}",
6926 "{[1+1] : z}",
6927 "{[1+1] : (z)}",
6928 "{[foo()] : z}",
6929 "{[foo()] : (z)}",
6930 "{[foo()] : foo().bar}",
6931 "{[foo()] : foo()['bar']}",
6932 "{[foo()] : this.bar}",
6933 "{[foo()] : this['bar']}",
6934 "{[foo()] : 'foo'.bar}",
6935 "{[foo()] : 'foo'['bar']}",
6936 "[...x]",
6937 "[x,y,...z]",
6938 "[x,,...z]",
6939 "{ x: y } = z",
6940 "[x, y] = z",
6941 "{ x: y } = { z }",
6942 "[x, y] = { z }",
6943 "{ x: y } = [ z ]",
6944 "[x, y] = [ z ]",
6945 "[((x, y) => z).x]",
6946 "{x: ((y, z) => z).x}",
6947 "[((x, y) => z)['x']]",
6948 "{x: ((y, z) => z)['x']}",
6949
6950 "{x: { y = 10 } }",
6951 "[(({ x } = { x: 1 }) => x).a]",
6952 NULL};
6953 // clang-format on
6954 static const ParserFlag always_flags[] = {
6955 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6956 kAllowHarmonyDefaultParameters};
6957 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6958 arraysize(always_flags));
6959
6960 const char* empty_context_data[][2] = {
6961 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6962
6963 // CoverInitializedName ambiguity handling in various contexts
6964 const char* ambiguity_data[] = {
6965 "var foo = { x = 10 } = {};",
6966 "var foo = { q } = { x = 10 } = {};",
6967 "var foo; foo = { x = 10 } = {};",
6968 "var foo; foo = { q } = { x = 10 } = {};",
6969 "var x; ({ x = 10 } = {});",
6970 "var q, x; ({ q } = { x = 10 } = {});",
6971 "var x; [{ x = 10 } = {}]",
6972 "var x; (true ? { x = true } = {} : { x = false } = {})",
6973 "var q, x; (q, { x = 10 } = {});",
6974 "var { x = 10 } = { x = 20 } = {};",
6975 "var { x = 10 } = (o = { x = 20 } = {});",
6976 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6977 NULL,
6978 };
6979 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6980 always_flags, arraysize(always_flags));
6981 }
6982
6983
6984 TEST(DestructuringAssignmentNegativeTests) {
6985 const char* context_data[][2] = {
6986 {"'use strict'; let x, y, z; (", " = {});"},
6987 {"var x, y, z; (", " = {});"},
6988 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6989 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6990 {"var x, y, z; for (x in ", " = {});"},
6991 {"var x, y, z; for (x of ", " = {});"},
6992 {NULL, NULL}};
6993
6994 // clang-format off
6995 const char* data[] = {
6996 "{ x : ++y }",
6997 "{ x : y * 2 }",
6998 "{ ...x }",
6999 "{ get x() {} }",
7000 "{ set x() {} }",
7001 "{ x: y() }",
7002 "{ this }",
7003 "{ x: this }",
7004 "{ x: this = 1 }",
7005 "{ super }",
7006 "{ x: super }",
7007 "{ x: super = 1 }",
7008 "{ new.target }",
7009 "{ x: new.target }",
7010 "{ x: new.target = 1 }",
7011 "[x--]",
7012 "[--x = 1]",
7013 "[x()]",
7014 "[this]",
7015 "[this = 1]",
7016 "[new.target]",
7017 "[new.target = 1]",
7018 "[super]",
7019 "[super = 1]",
7020 "[function f() {}]",
7021 "[50]",
7022 "[(50)]",
7023 "[(function() {})]",
7024 "[(foo())]",
7025 "{ x: 50 }",
7026 "{ x: (50) }",
7027 "['str']",
7028 "{ x: 'str' }",
7029 "{ x: ('str') }",
7030 "{ x: (foo()) }",
7031 "{ x: (function() {}) }",
7032 "{ x: y } = 'str'",
7033 "[x, y] = 'str'",
7034 "[(x,y) => z]",
7035 "{x: (y) => z}",
7036 "[x, ...y, z]",
7037 "[...x,]",
7038 "[x, y, ...z = 1]",
7039 "[...z = 1]",
7040 NULL};
7041 // clang-format on
7042 static const ParserFlag always_flags[] = {
7043 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
7044 kAllowHarmonyDefaultParameters};
7045 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
7046 arraysize(always_flags));
7047
7048 const char* empty_context_data[][2] = {
7049 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
7050
7051 // CoverInitializedName ambiguity handling in various contexts
7052 const char* ambiguity_data[] = {
7053 "var foo = { x = 10 };",
7054 "var foo = { q } = { x = 10 };",
7055 "var foo; foo = { x = 10 };",
7056 "var foo; foo = { q } = { x = 10 };",
7057 "var x; ({ x = 10 });",
7058 "var q, x; ({ q } = { x = 10 });",
7059 "var x; [{ x = 10 }]",
7060 "var x; (true ? { x = true } : { x = false })",
7061 "var q, x; (q, { x = 10 });",
7062 "var { x = 10 } = { x = 20 };",
7063 "var { x = 10 } = (o = { x = 20 });",
7064 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
7065 NULL,
7066 };
7067 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
7068 always_flags, arraysize(always_flags));
7069 }
7070
7071
6811 TEST(DestructuringDisallowPatternsInForVarIn) { 7072 TEST(DestructuringDisallowPatternsInForVarIn) {
6812 i::FLAG_harmony_destructuring_bind = true; 7073 i::FLAG_harmony_destructuring_bind = true;
6813 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 7074 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6814 const char* context_data[][2] = { 7075 const char* context_data[][2] = {
6815 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 7076 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6816 // clang-format off 7077 // clang-format off
6817 const char* error_data[] = { 7078 const char* error_data[] = {
6818 "for (let x = {} in null);", 7079 "for (let x = {} in null);",
6819 "for (let x = {} of null);", 7080 "for (let x = {} of null);",
6820 NULL}; 7081 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
6994 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7255 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6995 kAllowHarmonyDefaultParameters, 7256 kAllowHarmonyDefaultParameters,
6996 kAllowStrongMode}; 7257 kAllowStrongMode};
6997 7258
6998 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7259 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
6999 NULL, 0, always_flags, arraysize(always_flags)); 7260 NULL, 0, always_flags, arraysize(always_flags));
7000 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7261 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
7001 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7262 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7002 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7263 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
7003 0, always_flags, arraysize(always_flags)); 7264 0, always_flags, arraysize(always_flags));
7004 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
7005 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7265 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
7006 kError, NULL, 0, always_flags, arraysize(always_flags)); 7266 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7007 7267
7008 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7268 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
7009 0, always_flags, arraysize(always_flags)); 7269 0, always_flags, arraysize(always_flags));
7010 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7270 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
7011 kError, NULL, 0, always_flags, arraysize(always_flags)); 7271 kError, NULL, 0, always_flags, arraysize(always_flags));
7012 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7272 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
7013 always_flags, arraysize(always_flags)); 7273 always_flags, arraysize(always_flags));
7014 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7274 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
7015 kError, NULL, 0, always_flags, arraysize(always_flags)); 7275 kError, NULL, 0, always_flags, arraysize(always_flags));
7016 7276
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
7479 "var publ\\u0069c = 1;", 7739 "var publ\\u0069c = 1;",
7480 "var { publ\\u0069c } = {};", 7740 "var { publ\\u0069c } = {};",
7481 NULL}; 7741 NULL};
7482 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0, 7742 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0,
7483 always_flags, arraysize(always_flags)); 7743 always_flags, arraysize(always_flags));
7484 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7744 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7485 always_flags, arraysize(always_flags)); 7745 always_flags, arraysize(always_flags));
7486 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7746 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7487 always_flags, arraysize(always_flags)); 7747 always_flags, arraysize(always_flags));
7488 } 7748 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698