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

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: An implementation 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
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( 1527 parser->set_allow_harmony_destructuring(
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 i::Isolate* isolate = CcTest::i_isolate(); 1539 i::Isolate* isolate = CcTest::i_isolate();
1537 i::Factory* factory = isolate->factory(); 1540 i::Factory* factory = isolate->factory();
(...skipping 5001 matching lines...) Expand 10 before | Expand all | Expand 10 after
6539 CHECK_EQ(0, 6542 CHECK_EQ(0,
6540 strcmp( 6543 strcmp(
6541 "ReferenceError: In strong mode, using an undeclared global " 6544 "ReferenceError: In strong mode, using an undeclared global "
6542 "variable 'not_there3' is not allowed", 6545 "variable 'not_there3' is not allowed",
6543 *exception)); 6546 *exception));
6544 } 6547 }
6545 } 6548 }
6546 6549
6547 6550
6548 TEST(DestructuringPositiveTests) { 6551 TEST(DestructuringPositiveTests) {
6549 i::FLAG_harmony_destructuring = true;
6550
6551 const char* context_data[][2] = {{"'use strict'; let ", " = {};"}, 6552 const char* context_data[][2] = {{"'use strict'; let ", " = {};"},
6552 {"var ", " = {};"}, 6553 {"var ", " = {};"},
6553 {"'use strict'; const ", " = {};"}, 6554 {"'use strict'; const ", " = {};"},
6554 {"function f(", ") {}"}, 6555 {"function f(", ") {}"},
6555 {"function f(argument1, ", ") {}"}, 6556 {"function f(argument1, ", ") {}"},
6556 {"var f = (", ") => {};"}, 6557 {"var f = (", ") => {};"},
6557 {"var f = (argument1,", ") => {};"}, 6558 {"var f = (argument1,", ") => {};"},
6558 {"try {} catch(", ") {}"}, 6559 {"try {} catch(", ") {}"},
6559 {NULL, NULL}}; 6560 {NULL, NULL}};
6560 6561
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
6768 NULL}; 6769 NULL};
6769 // clang-format on 6770 // clang-format on
6770 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 6771 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6771 kAllowHarmonySloppyLet}; 6772 kAllowHarmonySloppyLet};
6772 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, 6773 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
6773 arraysize(always_flags)); 6774 arraysize(always_flags));
6774 } 6775 }
6775 } 6776 }
6776 6777
6777 6778
6779 TEST(DestructuringAssignmentPositiveTests) {
6780 const char* context_data[][2] = {
6781 {"'use strict'; let x, y, z; (", " = {});"},
6782 {"var x, y, z; (", " = {});"},
6783 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6784 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6785 {"var x, y, z; for (x in ", " = {});"},
6786 {"var x, y, z; for (x of ", " = {});"},
6787 {NULL, NULL}};
6788
6789 // clang-format off
6790 const char* data[] = {
6791 "x",
6792
6793 "{ x : y }",
6794 "{ x : foo().y }",
6795 "{ x : foo()[y] }",
6796 "{ x : y.z }",
6797 "{ x : y[z] }",
6798 "{ x : { y } }",
6799 "{ x : { foo: y } }",
6800 "{ x : { foo: foo().y } }",
6801 "{ x : { foo: foo()[y] } }",
6802 "{ x : { foo: y.z } }",
6803 "{ x : { foo: y[z] } }",
6804 "{ x : [ y ] }",
6805 "{ x : [ foo().y ] }",
6806 "{ x : [ foo()[y] ] }",
6807 "{ x : [ y.z ] }",
6808 "{ x : [ y[z] ] }",
6809
6810 "{ x : y = 10 }",
6811 "{ x : foo().y = 10 }",
6812 "{ x : foo()[y] = 10 }",
6813 "{ x : y.z = 10 }",
6814 "{ x : y[z] = 10 }",
6815 "{ x : { y = 10 } = {} }",
6816 "{ x : { foo: y = 10 } = {} }",
6817 "{ x : { foo: foo().y = 10 } = {} }",
6818 "{ x : { foo: foo()[y] = 10 } = {} }",
6819 "{ x : { foo: y.z = 10 } = {} }",
6820 "{ x : { foo: y[z] = 10 } = {} }",
6821 "{ x : [ y = 10 ] = {} }",
6822 "{ x : [ foo().y = 10 ] = {} }",
6823 "{ x : [ foo()[y] = 10 ] = {} }",
6824 "{ x : [ y.z = 10 ] = {} }",
6825 "{ x : [ y[z] = 10 ] = {} }",
6826
6827 "[ x ]",
6828 "[ foo().x ]",
6829 "[ foo()[x] ]",
6830 "[ x.y ]",
6831 "[ x[y] ]",
6832 "[ { x } ]",
6833 "[ { x : y } ]",
6834 "[ { x : foo().y } ]",
6835 "[ { x : foo()[y] } ]",
6836 "[ { x : x.y } ]",
6837 "[ { x : x[y] } ]",
6838 "[ [ x ] ]",
6839 "[ [ foo().x ] ]",
6840 "[ [ foo()[x] ] ]",
6841 "[ [ x.y ] ]",
6842 "[ [ x[y] ] ]",
6843
6844 "[ x = 10 ]",
6845 "[ foo().x = 10 ]",
6846 "[ foo()[x] = 10 ]",
6847 "[ x.y = 10 ]",
6848 "[ x[y] = 10 ]",
6849 "[ { x = 10 } = {} ]",
6850 "[ { x : y = 10 } = {} ]",
6851 "[ { x : foo().y = 10 } = {} ]",
6852 "[ { x : foo()[y] = 10 } = {} ]",
6853 "[ { x : x.y = 10 } = {} ]",
6854 "[ { x : x[y] = 10 } = {} ]",
6855 "[ [ x = 10 ] = {} ]",
6856 "[ [ foo().x = 10 ] = {} ]",
6857 "[ [ foo()[x] = 10 ] = {} ]",
6858 "[ [ x.y = 10 ] = {} ]",
6859 "[ [ x[y] = 10 ] = {} ]",
6860
6861 "{ x : y }",
6862 "{ x : y = 1 }",
6863 "{ x }",
6864 "{ x, y, z }",
6865 "{ x = 1, y: z, z: y }",
6866 "{x = 42, y = 15}",
6867 "[x]",
6868 "[x = 1]",
6869 "[x,y,z]",
6870 "[x, y = 42, z]",
6871 "{ x : x, y : y }",
6872 "{ x : x = 1, y : y }",
6873 "{ x : x, y : y = 42 }",
6874 "[]",
6875 "{}",
6876 "[{x:x, y:y}, [,x,z,]]",
6877 "[{x:x = 1, y:y = 2}, [z = 3, z = 4, z = 5]]",
6878 "[x,,y]",
6879 "[(x),,(y)]",
6880 "[(x)]",
6881 "{42 : x}",
6882 "{42 : x = 42}",
6883 "{42e-2 : x}",
6884 "{42e-2 : x = 42}",
6885 "{'hi' : x}",
6886 "{'hi' : x = 42}",
6887 "{var: x}",
6888 "{var: x = 42}",
6889 "{var: (x) = 42}",
6890 "{[x] : z}",
6891 "{[1+1] : z}",
6892 "{[1+1] : (z)}",
6893 "{[foo()] : z}",
6894 "{[foo()] : (z)}",
6895 "{[foo()] : foo().bar}",
6896 "{[foo()] : foo()['bar']}",
6897 "{[foo()] : this.bar}",
6898 "{[foo()] : this['bar']}",
6899 "{[foo()] : 'foo'.bar}",
6900 "{[foo()] : 'foo'['bar']}",
6901 "[...x]",
6902 "[x,y,...z]",
6903 "[x,,...z]",
6904 "{ x: y } = z",
6905 "[x, y] = z",
6906 "{ x: y } = { z }",
6907 "[x, y] = { z }",
6908 "{ x: y } = [ z ]",
6909 "[x, y] = [ z ]",
6910 "[((x, y) => z).x]",
6911 "{x: ((y, z) => z).x}",
6912 "[((x, y) => z)['x']]",
6913 "{x: ((y, z) => z)['x']}",
6914
6915 "{x: { y = 10 } }",
6916 "[(({ x } = { x: 1 }) => x).a]",
6917 NULL};
6918 // clang-format on
6919 static const ParserFlag always_flags[] = {
6920 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
6921 kAllowHarmonyDefaultParameters};
6922 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
6923 arraysize(always_flags));
6924
6925 const char* empty_context_data[][2] = {
6926 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
6927
6928 // CoverInitializedName ambiguity handling in various contexts
6929 const char* ambiguity_data[] = {
6930 "var foo = { x = 10 } = {};",
6931 "var foo = { q } = { x = 10 } = {};",
6932 "var foo; foo = { x = 10 } = {};",
6933 "var foo; foo = { q } = { x = 10 } = {};",
6934 "var x; ({ x = 10 } = {});",
6935 "var q, x; ({ q } = { x = 10 } = {});",
6936 "var x; [{ x = 10 } = {}]",
6937 "var x; (true ? { x = true } = {} : { x = false } = {})",
6938 "var q, x; (q, { x = 10 } = {});",
6939 "var { x = 10 } = { x = 20 } = {};",
6940 "var { x = 10 } = (o = { x = 20 } = {});",
6941 "var x; (({ x = 10 } = { x = 20 } = {}) => x)({})",
6942 NULL,
6943 };
6944 RunParserSyncTest(empty_context_data, ambiguity_data, kSuccess, NULL, 0,
6945 always_flags, arraysize(always_flags));
6946 }
6947
6948
6949 TEST(DestructuringAssignmentNegativeTests) {
6950 const char* context_data[][2] = {
6951 {"'use strict'; let x, y, z; (", " = {});"},
6952 {"var x, y, z; (", " = {});"},
6953 {"'use strict'; let x, y, z; for (x in ", " = {});"},
6954 {"'use strict'; let x, y, z; for (x of ", " = {});"},
6955 {"var x, y, z; for (x in ", " = {});"},
6956 {"var x, y, z; for (x of ", " = {});"},
6957 {NULL, NULL}};
6958
6959 // clang-format off
6960 const char* data[] = {
6961 "{ x : ++y }",
6962 "{ x : y * 2 }",
6963 "{ ...x }",
6964 "{ get x() {} }",
6965 "{ set x() {} }",
6966 "{ x: y() }",
6967 "{ this }",
6968 "{ x: this }",
6969 "{ x: this = 1 }",
6970 "{ super }",
6971 "{ x: super }",
6972 "{ x: super = 1 }",
6973 "{ new.target }",
6974 "{ x: new.target }",
6975 "{ x: new.target = 1 }",
6976 "[x--]",
6977 "[--x = 1]",
6978 "[x()]",
6979 "[this]",
6980 "[this = 1]",
6981 "[new.target]",
6982 "[new.target = 1]",
6983 "[super]",
6984 "[super = 1]",
6985 "[function f() {}]",
6986 "[50]",
6987 "[(50)]",
6988 "[(function() {})]",
6989 "[(foo())]",
6990 "{ x: 50 }",
6991 "{ x: (50) }",
6992 "['str']",
6993 "{ x: 'str' }",
6994 "{ x: ('str') }",
6995 "{ x: (foo()) }",
6996 "{ x: (function() {}) }",
6997 "{ x: y } = 'str'",
6998 "[x, y] = 'str'",
6999 "[(x,y) => z]",
7000 "{x: (y) => z}",
7001 "[x, ...y, z]",
7002 "[...x,]",
7003 "[x, y, ...z = 1]",
7004 "[...z = 1]",
7005 NULL};
7006 // clang-format on
7007 static const ParserFlag always_flags[] = {
7008 kAllowHarmonyDestructuringAssignment, kAllowHarmonyDestructuring,
7009 kAllowHarmonyDefaultParameters};
7010 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
7011 arraysize(always_flags));
7012
7013 const char* empty_context_data[][2] = {
7014 {"'use strict';", ""}, {"", ""}, {NULL, NULL}};
7015
7016 // CoverInitializedName ambiguity handling in various contexts
7017 const char* ambiguity_data[] = {
7018 "var foo = { x = 10 };",
7019 "var foo = { q } = { x = 10 };",
7020 "var foo; foo = { x = 10 };",
7021 "var foo; foo = { q } = { x = 10 };",
7022 "var x; ({ x = 10 });",
7023 "var q, x; ({ q } = { x = 10 });",
7024 "var x; [{ x = 10 }]",
7025 "var x; (true ? { x = true } : { x = false })",
7026 "var q, x; (q, { x = 10 });",
7027 "var { x = 10 } = { x = 20 };",
7028 "var { x = 10 } = (o = { x = 20 });",
7029 "var x; (({ x = 10 } = { x = 20 }) => x)({})",
7030 NULL,
7031 };
7032 RunParserSyncTest(empty_context_data, ambiguity_data, kError, NULL, 0,
7033 always_flags, arraysize(always_flags));
7034 }
7035
7036
6778 TEST(DestructuringDisallowPatternsInForVarIn) { 7037 TEST(DestructuringDisallowPatternsInForVarIn) {
6779 i::FLAG_harmony_destructuring = true; 7038 i::FLAG_harmony_destructuring = true;
6780 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring}; 7039 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring};
6781 const char* context_data[][2] = { 7040 const char* context_data[][2] = {
6782 {"", ""}, {"function f() {", "}"}, {NULL, NULL}}; 7041 {"", ""}, {"function f() {", "}"}, {NULL, NULL}};
6783 // clang-format off 7042 // clang-format off
6784 const char* error_data[] = { 7043 const char* error_data[] = {
6785 "for (let x = {} in null);", 7044 "for (let x = {} in null);",
6786 "for (let x = {} of null);", 7045 "for (let x = {} of null);",
6787 NULL}; 7046 NULL};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
6961 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring, 7220 static const ParserFlag always_flags[] = {kAllowHarmonyDestructuring,
6962 kAllowHarmonyDefaultParameters, 7221 kAllowHarmonyDefaultParameters,
6963 kAllowStrongMode}; 7222 kAllowStrongMode};
6964 7223
6965 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess, 7224 RunParserSyncTest(sloppy_function_context_data, parameter_data, kSuccess,
6966 NULL, 0, always_flags, arraysize(always_flags)); 7225 NULL, 0, always_flags, arraysize(always_flags));
6967 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data, 7226 RunParserSyncTest(sloppy_function_context_data, destructuring_assignment_data,
6968 kSuccess, NULL, 0, always_flags, arraysize(always_flags)); 7227 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
6969 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL, 7228 RunParserSyncTest(sloppy_arrow_context_data, parameter_data, kSuccess, NULL,
6970 0, always_flags, arraysize(always_flags)); 7229 0, always_flags, arraysize(always_flags));
6971 // TODO(wingo): Will change to kSuccess when destructuring assignment lands.
6972 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data, 7230 RunParserSyncTest(sloppy_arrow_context_data, destructuring_assignment_data,
6973 kError, NULL, 0, always_flags, arraysize(always_flags)); 7231 kSuccess, NULL, 0, always_flags, arraysize(always_flags));
6974 7232
6975 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL, 7233 RunParserSyncTest(strict_function_context_data, parameter_data, kError, NULL,
6976 0, always_flags, arraysize(always_flags)); 7234 0, always_flags, arraysize(always_flags));
6977 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data, 7235 RunParserSyncTest(strict_function_context_data, destructuring_assignment_data,
6978 kError, NULL, 0, always_flags, arraysize(always_flags)); 7236 kError, NULL, 0, always_flags, arraysize(always_flags));
6979 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0, 7237 RunParserSyncTest(strict_arrow_context_data, parameter_data, kError, NULL, 0,
6980 always_flags, arraysize(always_flags)); 7238 always_flags, arraysize(always_flags));
6981 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data, 7239 RunParserSyncTest(strict_arrow_context_data, destructuring_assignment_data,
6982 kError, NULL, 0, always_flags, arraysize(always_flags)); 7240 kError, NULL, 0, always_flags, arraysize(always_flags));
6983 7241
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
7298 NULL 7556 NULL
7299 }; 7557 };
7300 // clang-format on 7558 // clang-format on
7301 7559
7302 static const ParserFlag fail_flags[] = { 7560 static const ParserFlag fail_flags[] = {
7303 kAllowHarmonySloppy, kAllowHarmonySloppyLet, kNoLegacyConst, 7561 kAllowHarmonySloppy, kAllowHarmonySloppyLet, kNoLegacyConst,
7304 kAllowHarmonyDestructuring}; 7562 kAllowHarmonyDestructuring};
7305 RunParserSyncTest(context_data, fail_data, kError, NULL, 0, fail_flags, 7563 RunParserSyncTest(context_data, fail_data, kError, NULL, 0, fail_flags,
7306 arraysize(fail_flags)); 7564 arraysize(fail_flags));
7307 } 7565 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698