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

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: Cache te right scope in DeclareAndInitializeVariables() 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 5264 matching lines...) Expand 10 before | Expand all | Expand 10 after
6802 NULL}; 6805 NULL};
6803 // clang-format on 6806 // clang-format on
6804 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 6807 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6805 kAllowHarmonySloppyLet}; 6808 kAllowHarmonySloppyLet};
6806 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6809 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6807 arraysize(always_flags)); 6810 arraysize(always_flags));
6808 } 6811 }
6809 } 6812 }
6810 6813
6811 6814
6815 TEST(DestructuringAssignmentPositiveTests) {
6816 const char* context_data[][2] = {
6817 {"'use strict'; let x, y, z; (", " = {});"},
6818 {"var x, y, z; (", " = {});"},
6819 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6820 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6821 {"var x, y, z; for (x in ", " = {});"},
6822 {"var x, y, z; for (x of ", " = {});"},
6823 {NULL, NULL}};
6824
6825 // clang-format off
6826 const char* data[] = {
6827 "x",
6828
6829 "{ x : y }",
6830 "{ x : foo().y }",
6831 "{ x : foo()[y] }",
6832 "{ x : y.z }",
6833 "{ x : y[z] }",
6834 "{ x : { y } }",
6835 "{ x : { foo: y } }",
6836 "{ x : { foo: foo().y } }",
6837 "{ x : { foo: foo()[y] } }",
6838 "{ x : { foo: y.z } }",
6839 "{ x : { foo: y[z] } }",
6840 "{ x : [ y ] }",
6841 "{ x : [ foo().y ] }",
6842 "{ x : [ foo()[y] ] }",
6843 "{ x : [ y.z ] }",
6844 "{ x : [ y[z] ] }",
6845
6846 "{ x : y = 10 }",
6847 "{ x : foo().y = 10 }",
6848 "{ x : foo()[y] = 10 }",
6849 "{ x : y.z = 10 }",
6850 "{ x : y[z] = 10 }",
6851 "{ x : { y = 10 } = {} }",
6852 "{ x : { foo: y = 10 } = {} }",
6853 "{ x : { foo: foo().y = 10 } = {} }",
6854 "{ x : { foo: foo()[y] = 10 } = {} }",
6855 "{ x : { foo: y.z = 10 } = {} }",
6856 "{ x : { foo: y[z] = 10 } = {} }",
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
6863 "[ x ]",
6864 "[ foo().x ]",
6865 "[ foo()[x] ]",
6866 "[ x.y ]",
6867 "[ x[y] ]",
6868 "[ { x } ]",
6869 "[ { x : y } ]",
6870 "[ { x : foo().y } ]",
6871 "[ { x : foo()[y] } ]",
6872 "[ { x : x.y } ]",
6873 "[ { x : x[y] } ]",
6874 "[ [ x ] ]",
6875 "[ [ foo().x ] ]",
6876 "[ [ foo()[x] ] ]",
6877 "[ [ x.y ] ]",
6878 "[ [ x[y] ] ]",
6879
6880 "[ x = 10 ]",
6881 "[ foo().x = 10 ]",
6882 "[ foo()[x] = 10 ]",
6883 "[ x.y = 10 ]",
6884 "[ x[y] = 10 ]",
6885 "[ { x = 10 } = {} ]",
6886 "[ { x : y = 10 } = {} ]",
6887 "[ { x : foo().y = 10 } = {} ]",
6888 "[ { x : foo()[y] = 10 } = {} ]",
6889 "[ { x : x.y = 10 } = {} ]",
6890 "[ { x : x[y] = 10 } = {} ]",
6891 "[ [ x = 10 ] = {} ]",
6892 "[ [ foo().x = 10 ] = {} ]",
6893 "[ [ foo()[x] = 10 ] = {} ]",
6894 "[ [ x.y = 10 ] = {} ]",
6895 "[ [ x[y] = 10 ] = {} ]",
6896
6897 "{ x : y }",
6898 "{ x : y = 1 }",
6899 "{ x }",
6900 "{ x, y, z }",
6901 "{ x = 1, y: z, z: y }",
6902 "{x = 42, y = 15}",
6903 "[x]",
6904 "[x = 1]",
6905 "[x,y,z]",
6906 "[x, y = 42, z]",
6907 "{ x : x, y : y }",
6908 "{ x : x = 1, y : y }",
6909 "{ x : x, y : y = 42 }",
6910 "[]",
6911 "{}",
6912 "[{x:x, y:y}, [,x,z,]]",
6913 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6914 "[x,,y]",
6915 "[(x),,(y)]",
6916 "[(x)]",
6917 "{42 : x}",
6918 "{42 : x = 42}",
6919 "{42e-2 : x}",
6920 "{42e-2 : x = 42}",
6921 "{'hi' : x}",
6922 "{'hi' : x = 42}",
6923 "{var: x}",
6924 "{var: x = 42}",
6925 "{var: (x) = 42}",
6926 "{[x] : z}",
6927 "{[1+1] : z}",
6928 "{[1+1] : (z)}",
6929 "{[foo()] : z}",
6930 "{[foo()] : (z)}",
6931 "{[foo()] : foo().bar}",
6932 "{[foo()] : foo()['bar']}",
6933 "{[foo()] : this.bar}",
6934 "{[foo()] : this['bar']}",
6935 "{[foo()] : 'foo'.bar}",
6936 "{[foo()] : 'foo'['bar']}",
6937 "[...x]",
6938 "[x,y,...z]",
6939 "[x,,...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 ]",
6946 "[((x, y) => z).x]",
6947 "{x: ((y, z) => z).x}",
6948 "[((x, y) => z)['x']]",
6949 "{x: ((y, z) => z)['x']}",
6950
6951 "{x: { y = 10 } }",
6952 "[(({ x } = { x: 1 }) => x).a]",
6953 NULL};
6954 // clang-format on
6955 static const ParserFlag always_flags[] = {
6956 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6957 kAllowHarmonyDefaultParameters};
6958 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6959 arraysize(always_flags));
6960
6961 const char* empty_context_data[][2] = {
6962 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6963
6964 // CoverInitializedName ambiguity handling in various contexts
6965 const char* ambiguity_data[] = {
6966 "var foo = { x = 10 } = {};",
6967 "var foo = { q } = { x = 10 } = {};",
6968 "var foo; foo = { x = 10 } = {};",
6969 "var foo; foo = { q } = { x = 10 } = {};",
6970 "var x; ({ x = 10 } = {});",
6971 "var q, x; ({ q } = { x = 10 } = {});",
6972 "var x; [{ x = 10 } = {}]",
6973 "var x; (true ? { x = true } = {} : { x = false } = {})",
6974 "var q, x; (q, { x = 10 } = {});",
6975 "var { x = 10 } = { x = 20 } = {};",
6976 "var { x = 10 } = (o = { x = 20 } = {});",
6977 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6978 NULL,
6979 };
6980 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6981 always_flags, arraysize(always_flags));
6982 }
6983
6984
6985 TEST(DestructuringAssignmentNegativeTests) {
6986 const char* context_data[][2] = {
6987 {"'use strict'; let x, y, z; (", " = {});"},
6988 {"var x, y, z; (", " = {});"},
6989 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6990 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6991 {"var x, y, z; for (x in ", " = {});"},
6992 {"var x, y, z; for (x of ", " = {});"},
6993 {NULL, NULL}};
6994
6995 // clang-format off
6996 const char* data[] = {
6997 "{ x : ++y }",
6998 "{ x : y * 2 }",
6999 "{ ...x }",
7000 "{ get x() {} }",
7001 "{ set x() {} }",
7002 "{ x: y() }",
7003 "{ this }",
7004 "{ x: this }",
7005 "{ x: this = 1 }",
7006 "{ super }",
7007 "{ x: super }",
7008 "{ x: super = 1 }",
7009 "{ new.target }",
7010 "{ x: new.target }",
7011 "{ x: new.target = 1 }",
7012 "[x--]",
7013 "[--x = 1]",
7014 "[x()]",
7015 "[this]",
7016 "[this = 1]",
7017 "[new.target]",
7018 "[new.target = 1]",
7019 "[super]",
7020 "[super = 1]",
7021 "[function f() {}]",
7022 "[50]",
7023 "[(50)]",
7024 "[(function() {})]",
7025 "[(foo())]",
7026 "{ x: 50 }",
7027 "{ x: (50) }",
7028 "['str']",
7029 "{ x: 'str' }",
7030 "{ x: ('str') }",
7031 "{ x: (foo()) }",
7032 "{ x: (function() {}) }",
7033 "{ x: y } = 'str'",
7034 "[x, y] = 'str'",
7035 "[(x,y) => z]",
7036 "{x: (y) => z}",
7037 "[x, ...y, z]",
7038 "[...x,]",
7039 "[x, y, ...z = 1]",
7040 "[...z = 1]",
7041 NULL};
7042 // clang-format on
7043 static const ParserFlag always_flags[] = {
7044 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
7045 kAllowHarmonyDefaultParameters};
7046 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
7047 arraysize(always_flags));
7048
7049 const char* empty_context_data[][2] = {
7050 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
7051
7052 // CoverInitializedName ambiguity handling in various contexts
7053 const char* ambiguity_data[] = {
7054 "var foo = { x = 10 };",
7055 "var foo = { q } = { x = 10 };",
7056 "var foo; foo = { x = 10 };",
7057 "var foo; foo = { q } = { x = 10 };",
7058 "var x; ({ x = 10 });",
7059 "var q, x; ({ q } = { x = 10 });",
7060 "var x; [{ x = 10 }]",
7061 "var x; (true ? { x = true } : { x = false })",
7062 "var q, x; (q, { x = 10 });",
7063 "var { x = 10 } = { x = 20 };",
7064 "var { x = 10 } = (o = { x = 20 });",
7065 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
7066 NULL,
7067 };
7068 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
7069 always_flags, arraysize(always_flags));
7070 }
7071
7072
6812 TEST(DestructuringDisallowPatternsInForVarIn) { 7073 TEST(DestructuringDisallowPatternsInForVarIn) {
6813 i::FLAG_harmony_destructuring_bind = true; 7074 i::FLAG_harmony_destructuring_bind = true;
6814 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 7075 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6815 const char* context_data[][2] = { 7076 const char* context_data[][2] = {
6816 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 7077 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6817 // clang-format off 7078 // clang-format off
6818 const char* error_data[] = { 7079 const char* error_data[] = {
6819 "for (let x = {} in null);", 7080 "for (let x = {} in null);",
6820 "for (let x = {} of null);", 7081 "for (let x = {} of null);",
6821 NULL}; 7082 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
6995 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7256 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6996 kAllowHarmonyDefaultParameters, 7257 kAllowHarmonyDefaultParameters,
6997 kAllowStrongMode}; 7258 kAllowStrongMode};
6998 7259
6999 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7260 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
7000 NULL, 0, always_flags, arraysize(always_flags)); 7261 NULL, 0, always_flags, arraysize(always_flags));
7001 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7262 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
7002 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7263 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7003 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7264 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
7004 0, always_flags, arraysize(always_flags)); 7265 0, always_flags, arraysize(always_flags));
7005 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
7006 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7266 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
7007 kError, NULL, 0, always_flags, arraysize(always_flags)); 7267 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
7008 7268
7009 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7269 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
7010 0, always_flags, arraysize(always_flags)); 7270 0, always_flags, arraysize(always_flags));
7011 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7271 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
7012 kError, NULL, 0, always_flags, arraysize(always_flags)); 7272 kError, NULL, 0, always_flags, arraysize(always_flags));
7013 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7273 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
7014 always_flags, arraysize(always_flags)); 7274 always_flags, arraysize(always_flags));
7015 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7275 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
7016 kError, NULL, 0, always_flags, arraysize(always_flags)); 7276 kError, NULL, 0, always_flags, arraysize(always_flags));
7017 7277
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
7480 "var publ\\u0069c = 1;", 7740 "var publ\\u0069c = 1;",
7481 "var { publ\\u0069c } = {};", 7741 "var { publ\\u0069c } = {};",
7482 NULL}; 7742 NULL};
7483 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0, 7743 RunParserSyncTest(sloppy_context_data, valid_data, kSuccess, NULL, 0,
7484 always_flags, arraysize(always_flags)); 7744 always_flags, arraysize(always_flags));
7485 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7745 RunParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7486 always_flags, arraysize(always_flags)); 7746 always_flags, arraysize(always_flags));
7487 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0, 7747 RunModuleParserSyncTest(strict_context_data, valid_data, kError, NULL, 0,
7488 always_flags, arraysize(always_flags)); 7748 always_flags, arraysize(always_flags));
7489 } 7749 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698