OLD | NEW |
---|---|
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 942 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 } surroundings[] = { | 953 } surroundings[] = { |
954 { "function f() {", "}" }, | 954 { "function f() {", "}" }, |
955 { "var f = () => {", "};" }, | 955 { "var f = () => {", "};" }, |
956 { "class C { constructor() {", "} }" }, | 956 { "class C { constructor() {", "} }" }, |
957 }; | 957 }; |
958 | 958 |
959 enum Expected { | 959 enum Expected { |
960 NONE = 0, | 960 NONE = 0, |
961 ARGUMENTS = 1, | 961 ARGUMENTS = 1, |
962 SUPER_PROPERTY = 1 << 1, | 962 SUPER_PROPERTY = 1 << 1, |
963 SUPER_CONSTRUCTOR_CALL = 1 << 2, | 963 THIS = 1 << 2, |
arv (Not doing code reviews)
2015/02/12 21:11:27
I decided to remove these since I have another cha
| |
964 THIS = 1 << 3, | 964 INNER_ARGUMENTS = 1 << 3, |
965 INNER_ARGUMENTS = 1 << 4, | 965 INNER_SUPER_PROPERTY = 1 << 4, |
966 INNER_SUPER_PROPERTY = 1 << 5, | 966 INNER_THIS = 1 << 5 |
967 INNER_SUPER_CONSTRUCTOR_CALL = 1 << 6, | |
968 INNER_THIS = 1 << 7 | |
969 }; | 967 }; |
970 | 968 |
971 static const struct { | 969 static const struct { |
972 const char* body; | 970 const char* body; |
973 int expected; | 971 int expected; |
974 } source_data[] = { | 972 } source_data[] = { |
975 {"", NONE}, | 973 {"", NONE}, |
976 {"return this", THIS}, | 974 {"return this", THIS}, |
977 {"return arguments", ARGUMENTS}, | 975 {"return arguments", ARGUMENTS}, |
978 {"return super()", SUPER_CONSTRUCTOR_CALL}, | |
979 {"return super.x", SUPER_PROPERTY}, | 976 {"return super.x", SUPER_PROPERTY}, |
980 {"return arguments[0]", ARGUMENTS}, | 977 {"return arguments[0]", ARGUMENTS}, |
981 {"return this + arguments[0]", ARGUMENTS | THIS}, | 978 {"return this + arguments[0]", ARGUMENTS | THIS}, |
982 {"return this + arguments[0] + super.x", | 979 {"return this + arguments[0] + super.x", |
983 ARGUMENTS | SUPER_PROPERTY | THIS}, | 980 ARGUMENTS | SUPER_PROPERTY | THIS}, |
984 {"return x => this + x", INNER_THIS}, | 981 {"return x => this + x", INNER_THIS}, |
985 {"return x => super() + x", INNER_SUPER_CONSTRUCTOR_CALL}, | 982 {"return x => super.f() + x", INNER_SUPER_PROPERTY}, |
986 {"this.foo = 42;", THIS}, | 983 {"this.foo = 42;", THIS}, |
987 {"this.foo();", THIS}, | 984 {"this.foo();", THIS}, |
988 {"if (foo()) { this.f() }", THIS}, | 985 {"if (foo()) { this.f() }", THIS}, |
989 {"if (foo()) { super.f() }", SUPER_PROPERTY}, | 986 {"if (foo()) { super.f() }", SUPER_PROPERTY}, |
990 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, | 987 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, |
991 {"while (true) { this.f() }", THIS}, | 988 {"while (true) { this.f() }", THIS}, |
992 {"while (true) { super.f() }", SUPER_PROPERTY}, | 989 {"while (true) { super.f() }", SUPER_PROPERTY}, |
993 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, | 990 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, |
994 // Multiple nesting levels must work as well. | 991 // Multiple nesting levels must work as well. |
995 {"while (true) { while (true) { while (true) return this } }", THIS}, | 992 {"while (true) { while (true) { while (true) return this } }", THIS}, |
996 {"while (true) { while (true) { while (true) return super() } }", | 993 {"while (true) { while (true) { while (true) return super.f() } }", |
997 SUPER_CONSTRUCTOR_CALL}, | 994 SUPER_PROPERTY}, |
998 {"if (1) { return () => { while (true) new this() } }", INNER_THIS}, | 995 {"if (1) { return () => { while (true) new this() } }", INNER_THIS}, |
999 // Note that propagation of the inner_uses_this() value does not | 996 // Note that propagation of the inner_uses_this() value does not |
1000 // cross boundaries of normal functions onto parent scopes. | 997 // cross boundaries of normal functions onto parent scopes. |
1001 {"return function (x) { return this + x }", NONE}, | 998 {"return function (x) { return this + x }", NONE}, |
1002 {"return { m(x) { return super.m() + x } }", NONE}, | 999 {"return { m(x) { return super.m() + x } }", NONE}, |
1003 {"var x = function () { this.foo = 42 };", NONE}, | 1000 {"var x = function () { this.foo = 42 };", NONE}, |
1004 {"var x = { m() { super.foo = 42 } };", NONE}, | 1001 {"var x = { m() { super.foo = 42 } };", NONE}, |
1005 {"if (1) { return function () { while (true) new this() } }", NONE}, | 1002 {"if (1) { return function () { while (true) new this() } }", NONE}, |
1006 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, | 1003 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, |
1007 {"return function (x) { return () => this }", NONE}, | 1004 {"return function (x) { return () => this }", NONE}, |
1008 {"return { m(x) { return () => super.m() } }", NONE}, | 1005 {"return { m(x) { return () => super.m() } }", NONE}, |
1009 // Flags must be correctly set when using block scoping. | 1006 // Flags must be correctly set when using block scoping. |
1010 {"\"use strict\"; while (true) { let x; this, arguments; }", | 1007 {"\"use strict\"; while (true) { let x; this, arguments; }", |
1011 INNER_ARGUMENTS | INNER_THIS}, | 1008 INNER_ARGUMENTS | INNER_THIS}, |
1012 {"\"use strict\"; while (true) { let x; this, super(), arguments; }", | 1009 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", |
1013 INNER_ARGUMENTS | INNER_SUPER_CONSTRUCTOR_CALL | INNER_THIS}, | 1010 INNER_ARGUMENTS | INNER_SUPER_PROPERTY | INNER_THIS}, |
1014 {"\"use strict\"; if (foo()) { let x; this.f() }", INNER_THIS}, | 1011 {"\"use strict\"; if (foo()) { let x; this.f() }", INNER_THIS}, |
1015 {"\"use strict\"; if (foo()) { let x; super.f() }", | 1012 {"\"use strict\"; if (foo()) { let x; super.f() }", |
1016 INNER_SUPER_PROPERTY}, | 1013 INNER_SUPER_PROPERTY}, |
1017 {"\"use strict\"; if (1) {" | 1014 {"\"use strict\"; if (1) {" |
1018 " let x; return { m() { return this + super.m() + arguments } }" | 1015 " let x; return { m() { return this + super.m() + arguments } }" |
1019 "}", | 1016 "}", |
1020 NONE}, | 1017 NONE}, |
1021 }; | 1018 }; |
1022 | 1019 |
1023 i::Isolate* isolate = CcTest::i_isolate(); | 1020 i::Isolate* isolate = CcTest::i_isolate(); |
1024 i::Factory* factory = isolate->factory(); | 1021 i::Factory* factory = isolate->factory(); |
1025 | 1022 |
1026 v8::HandleScope handles(CcTest::isolate()); | 1023 v8::HandleScope handles(CcTest::isolate()); |
1027 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 1024 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
1028 v8::Context::Scope context_scope(context); | 1025 v8::Context::Scope context_scope(context); |
1029 | 1026 |
1030 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - | 1027 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
1031 128 * 1024); | 1028 128 * 1024); |
1032 | 1029 |
1033 for (unsigned j = 0; j < arraysize(surroundings); ++j) { | 1030 for (unsigned j = 0; j < arraysize(surroundings); ++j) { |
1034 for (unsigned i = 0; i < arraysize(source_data); ++i) { | 1031 for (unsigned i = 0; i < arraysize(source_data); ++i) { |
1035 // Super constructor call is only allowed in constructor. | |
1036 // Super property is only allowed in constructor and method. | 1032 // Super property is only allowed in constructor and method. |
1037 if (((source_data[i].expected & SUPER_CONSTRUCTOR_CALL) || | 1033 if (((source_data[i].expected & SUPER_PROPERTY) || |
1038 (source_data[i].expected & SUPER_PROPERTY) || | |
1039 (source_data[i].expected & INNER_SUPER_CONSTRUCTOR_CALL) || | |
1040 (source_data[i].expected & INNER_SUPER_PROPERTY) || | 1034 (source_data[i].expected & INNER_SUPER_PROPERTY) || |
1041 (source_data[i].expected == NONE)) && j != 2) { | 1035 (source_data[i].expected == NONE)) && j != 2) { |
1042 continue; | 1036 continue; |
1043 } | 1037 } |
1044 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + | 1038 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + |
1045 i::StrLength(surroundings[j].suffix) + | 1039 i::StrLength(surroundings[j].suffix) + |
1046 i::StrLength(source_data[i].body); | 1040 i::StrLength(source_data[i].body); |
1047 i::ScopedVector<char> program(kProgramByteSize + 1); | 1041 i::ScopedVector<char> program(kProgramByteSize + 1); |
1048 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, | 1042 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, |
1049 source_data[i].body, surroundings[j].suffix); | 1043 source_data[i].body, surroundings[j].suffix); |
(...skipping 22 matching lines...) Expand all Loading... | |
1072 i::Scope* scope = script_scope->inner_scopes()->at(0); | 1066 i::Scope* scope = script_scope->inner_scopes()->at(0); |
1073 // Adjust for constructor scope. | 1067 // Adjust for constructor scope. |
1074 if (j == 2) { | 1068 if (j == 2) { |
1075 CHECK_EQ(1, scope->inner_scopes()->length()); | 1069 CHECK_EQ(1, scope->inner_scopes()->length()); |
1076 scope = scope->inner_scopes()->at(0); | 1070 scope = scope->inner_scopes()->at(0); |
1077 } | 1071 } |
1078 CHECK_EQ((source_data[i].expected & ARGUMENTS) != 0, | 1072 CHECK_EQ((source_data[i].expected & ARGUMENTS) != 0, |
1079 scope->uses_arguments()); | 1073 scope->uses_arguments()); |
1080 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, | 1074 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, |
1081 scope->uses_super_property()); | 1075 scope->uses_super_property()); |
1082 CHECK_EQ((source_data[i].expected & SUPER_CONSTRUCTOR_CALL) != 0, | |
1083 scope->uses_super_constructor_call()); | |
1084 CHECK_EQ((source_data[i].expected & THIS) != 0, scope->uses_this()); | 1076 CHECK_EQ((source_data[i].expected & THIS) != 0, scope->uses_this()); |
1085 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, | 1077 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, |
1086 scope->inner_uses_arguments()); | 1078 scope->inner_uses_arguments()); |
1087 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, | 1079 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, |
1088 scope->inner_uses_super_property()); | 1080 scope->inner_uses_super_property()); |
1089 CHECK_EQ((source_data[i].expected & INNER_SUPER_CONSTRUCTOR_CALL) != 0, | |
1090 scope->inner_uses_super_constructor_call()); | |
1091 CHECK_EQ((source_data[i].expected & INNER_THIS) != 0, | 1081 CHECK_EQ((source_data[i].expected & INNER_THIS) != 0, |
1092 scope->inner_uses_this()); | 1082 scope->inner_uses_this()); |
1093 } | 1083 } |
1094 } | 1084 } |
1095 } | 1085 } |
1096 | 1086 |
1097 | 1087 |
1098 TEST(ScopePositions) { | 1088 TEST(ScopePositions) { |
1099 v8::internal::FLAG_harmony_scoping = true; | 1089 v8::internal::FLAG_harmony_scoping = true; |
1100 | 1090 |
(...skipping 2605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3706 RunParserSyncTest(context_data, expression_data, kError, NULL, 0, | 3696 RunParserSyncTest(context_data, expression_data, kError, NULL, 0, |
3707 always_flags, arraysize(always_flags)); | 3697 always_flags, arraysize(always_flags)); |
3708 } | 3698 } |
3709 | 3699 |
3710 | 3700 |
3711 TEST(SuperCall) { | 3701 TEST(SuperCall) { |
3712 const char* context_data[][2] = {{"", ""}, | 3702 const char* context_data[][2] = {{"", ""}, |
3713 {NULL, NULL}}; | 3703 {NULL, NULL}}; |
3714 | 3704 |
3715 const char* success_data[] = { | 3705 const char* success_data[] = { |
3716 "class C { constructor() { super(); } }", | |
3717 "class C extends B { constructor() { super(); } }", | 3706 "class C extends B { constructor() { super(); } }", |
3718 "class C extends B { constructor() { () => super(); } }", | 3707 "class C extends B { constructor() { () => super(); } }", |
3719 NULL | 3708 NULL |
3720 }; | 3709 }; |
3721 | 3710 |
3722 static const ParserFlag always_flags[] = { | 3711 static const ParserFlag always_flags[] = { |
3723 kAllowHarmonyArrowFunctions, | 3712 kAllowHarmonyArrowFunctions, |
3724 kAllowHarmonyClasses, | 3713 kAllowHarmonyClasses, |
3725 kAllowHarmonyObjectLiterals, | 3714 kAllowHarmonyObjectLiterals, |
3726 kAllowHarmonySloppy | 3715 kAllowHarmonySloppy |
3727 }; | 3716 }; |
3728 RunParserSyncTest(context_data, success_data, kSuccess, NULL, 0, | 3717 RunParserSyncTest(context_data, success_data, kSuccess, NULL, 0, |
3729 always_flags, arraysize(always_flags)); | 3718 always_flags, arraysize(always_flags)); |
3730 | 3719 |
3731 const char* error_data[] = { | 3720 const char* error_data[] = { |
3721 "class C { constructor() { super(); } }", | |
3732 "class C { method() { super(); } }", | 3722 "class C { method() { super(); } }", |
3733 "class C { method() { () => super(); } }", | 3723 "class C { method() { () => super(); } }", |
3734 "class C { *method() { super(); } }", | 3724 "class C { *method() { super(); } }", |
3735 "class C { get x() { super(); } }", | 3725 "class C { get x() { super(); } }", |
3736 "class C { set x(_) { super(); } }", | 3726 "class C { set x(_) { super(); } }", |
3737 "({ method() { super(); } })", | 3727 "({ method() { super(); } })", |
3738 "({ *method() { super(); } })", | 3728 "({ *method() { super(); } })", |
3739 "({ get x() { super(); } })", | 3729 "({ get x() { super(); } })", |
3740 "({ set x(_) { super(); } })", | 3730 "({ set x(_) { super(); } })", |
3741 "({ f: function() { super(); } })", | 3731 "({ f: function() { super(); } })", |
(...skipping 1577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5319 "class C {static set arguments(_) {}}", | 5309 "class C {static set arguments(_) {}}", |
5320 | 5310 |
5321 NULL}; | 5311 NULL}; |
5322 | 5312 |
5323 static const ParserFlag always_flags[] = { | 5313 static const ParserFlag always_flags[] = { |
5324 kAllowHarmonyClasses, kAllowHarmonyObjectLiterals, kAllowHarmonyScoping, | 5314 kAllowHarmonyClasses, kAllowHarmonyObjectLiterals, kAllowHarmonyScoping, |
5325 kAllowStrongMode}; | 5315 kAllowStrongMode}; |
5326 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, | 5316 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
5327 always_flags, arraysize(always_flags)); | 5317 always_flags, arraysize(always_flags)); |
5328 } | 5318 } |
OLD | NEW |