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 958 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 { "var f = () => {", "};" }, | 969 { "var f = () => {", "};" }, |
970 { "class C { constructor() {", "} }" }, | 970 { "class C { constructor() {", "} }" }, |
971 }; | 971 }; |
972 | 972 |
973 enum Expected { | 973 enum Expected { |
974 NONE = 0, | 974 NONE = 0, |
975 ARGUMENTS = 1, | 975 ARGUMENTS = 1, |
976 SUPER_PROPERTY = 1 << 1, | 976 SUPER_PROPERTY = 1 << 1, |
977 THIS = 1 << 2, | 977 THIS = 1 << 2, |
978 INNER_ARGUMENTS = 1 << 3, | 978 INNER_ARGUMENTS = 1 << 3, |
979 INNER_SUPER_PROPERTY = 1 << 4, | |
980 }; | 979 }; |
981 | 980 |
| 981 // clang-format off |
982 static const struct { | 982 static const struct { |
983 const char* body; | 983 const char* body; |
984 int expected; | 984 int expected; |
985 } source_data[] = { | 985 } source_data[] = { |
986 {"", NONE}, | 986 {"", NONE}, |
987 {"return this", THIS}, | 987 {"return this", THIS}, |
988 {"return arguments", ARGUMENTS}, | 988 {"return arguments", ARGUMENTS}, |
989 {"return super.x", SUPER_PROPERTY}, | 989 {"return super.x", SUPER_PROPERTY}, |
990 {"return arguments[0]", ARGUMENTS}, | 990 {"return arguments[0]", ARGUMENTS}, |
991 {"return this + arguments[0]", ARGUMENTS | THIS}, | 991 {"return this + arguments[0]", ARGUMENTS | THIS}, |
992 {"return this + arguments[0] + super.x", | 992 {"return this + arguments[0] + super.x", |
993 ARGUMENTS | SUPER_PROPERTY | THIS}, | 993 ARGUMENTS | SUPER_PROPERTY | THIS}, |
994 {"return x => this + x", THIS}, | 994 {"return x => this + x", THIS}, |
995 {"return x => super.f() + x", INNER_SUPER_PROPERTY}, | 995 {"return x => super.f() + x", SUPER_PROPERTY}, |
996 {"this.foo = 42;", THIS}, | 996 {"this.foo = 42;", THIS}, |
997 {"this.foo();", THIS}, | 997 {"this.foo();", THIS}, |
998 {"if (foo()) { this.f() }", THIS}, | 998 {"if (foo()) { this.f() }", THIS}, |
999 {"if (foo()) { super.f() }", SUPER_PROPERTY}, | 999 {"if (foo()) { super.f() }", SUPER_PROPERTY}, |
1000 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, | 1000 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, |
1001 {"while (true) { this.f() }", THIS}, | 1001 {"while (true) { this.f() }", THIS}, |
1002 {"while (true) { super.f() }", SUPER_PROPERTY}, | 1002 {"while (true) { super.f() }", SUPER_PROPERTY}, |
1003 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, | 1003 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, |
1004 // Multiple nesting levels must work as well. | 1004 // Multiple nesting levels must work as well. |
1005 {"while (true) { while (true) { while (true) return this } }", THIS}, | 1005 {"while (true) { while (true) { while (true) return this } }", THIS}, |
1006 {"while (true) { while (true) { while (true) return super.f() } }", | 1006 {"while (true) { while (true) { while (true) return super.f() } }", |
1007 SUPER_PROPERTY}, | 1007 SUPER_PROPERTY}, |
1008 {"if (1) { return () => { while (true) new this() } }", THIS}, | 1008 {"if (1) { return () => { while (true) new this() } }", THIS}, |
1009 {"return function (x) { return this + x }", NONE}, | 1009 {"return function (x) { return this + x }", NONE}, |
1010 {"return { m(x) { return super.m() + x } }", NONE}, | 1010 {"return { m(x) { return super.m() + x } }", NONE}, |
1011 {"var x = function () { this.foo = 42 };", NONE}, | 1011 {"var x = function () { this.foo = 42 };", NONE}, |
1012 {"var x = { m() { super.foo = 42 } };", NONE}, | 1012 {"var x = { m() { super.foo = 42 } };", NONE}, |
1013 {"if (1) { return function () { while (true) new this() } }", NONE}, | 1013 {"if (1) { return function () { while (true) new this() } }", NONE}, |
1014 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, | 1014 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, |
1015 {"return function (x) { return () => this }", NONE}, | 1015 {"return function (x) { return () => this }", NONE}, |
1016 {"return { m(x) { return () => super.m() } }", NONE}, | 1016 {"return { m(x) { return () => super.m() } }", NONE}, |
1017 // Flags must be correctly set when using block scoping. | 1017 // Flags must be correctly set when using block scoping. |
1018 {"\"use strict\"; while (true) { let x; this, arguments; }", | 1018 {"\"use strict\"; while (true) { let x; this, arguments; }", |
1019 INNER_ARGUMENTS | THIS}, | 1019 INNER_ARGUMENTS | THIS}, |
1020 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", | 1020 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", |
1021 INNER_ARGUMENTS | INNER_SUPER_PROPERTY | THIS}, | 1021 INNER_ARGUMENTS | SUPER_PROPERTY | THIS}, |
1022 {"\"use strict\"; if (foo()) { let x; this.f() }", THIS}, | 1022 {"\"use strict\"; if (foo()) { let x; this.f() }", THIS}, |
1023 {"\"use strict\"; if (foo()) { let x; super.f() }", | 1023 {"\"use strict\"; if (foo()) { let x; super.f() }", SUPER_PROPERTY}, |
1024 INNER_SUPER_PROPERTY}, | |
1025 {"\"use strict\"; if (1) {" | 1024 {"\"use strict\"; if (1) {" |
1026 " let x; return { m() { return this + super.m() + arguments } }" | 1025 " let x; return { m() { return this + super.m() + arguments } }" |
1027 "}", | 1026 "}", |
1028 NONE}, | 1027 NONE}, |
1029 }; | 1028 }; |
| 1029 // clang-format on |
1030 | 1030 |
1031 i::Isolate* isolate = CcTest::i_isolate(); | 1031 i::Isolate* isolate = CcTest::i_isolate(); |
1032 i::Factory* factory = isolate->factory(); | 1032 i::Factory* factory = isolate->factory(); |
1033 | 1033 |
1034 v8::HandleScope handles(CcTest::isolate()); | 1034 v8::HandleScope handles(CcTest::isolate()); |
1035 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 1035 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
1036 v8::Context::Scope context_scope(context); | 1036 v8::Context::Scope context_scope(context); |
1037 | 1037 |
1038 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - | 1038 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
1039 128 * 1024); | 1039 128 * 1024); |
1040 | 1040 |
1041 for (unsigned j = 0; j < arraysize(surroundings); ++j) { | 1041 for (unsigned j = 0; j < arraysize(surroundings); ++j) { |
1042 for (unsigned i = 0; i < arraysize(source_data); ++i) { | 1042 for (unsigned i = 0; i < arraysize(source_data); ++i) { |
1043 // Super property is only allowed in constructor and method. | 1043 // Super property is only allowed in constructor and method. |
1044 if (((source_data[i].expected & SUPER_PROPERTY) || | 1044 if (((source_data[i].expected & SUPER_PROPERTY) || |
1045 (source_data[i].expected & INNER_SUPER_PROPERTY) || | |
1046 (source_data[i].expected == NONE)) && j != 2) { | 1045 (source_data[i].expected == NONE)) && j != 2) { |
1047 continue; | 1046 continue; |
1048 } | 1047 } |
1049 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + | 1048 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + |
1050 i::StrLength(surroundings[j].suffix) + | 1049 i::StrLength(surroundings[j].suffix) + |
1051 i::StrLength(source_data[i].body); | 1050 i::StrLength(source_data[i].body); |
1052 i::ScopedVector<char> program(kProgramByteSize + 1); | 1051 i::ScopedVector<char> program(kProgramByteSize + 1); |
1053 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, | 1052 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, |
1054 source_data[i].body, surroundings[j].suffix); | 1053 source_data[i].body, surroundings[j].suffix); |
1055 i::Handle<i::String> source = | 1054 i::Handle<i::String> source = |
(...skipping 27 matching lines...) Expand all Loading... |
1083 scope->uses_arguments()); | 1082 scope->uses_arguments()); |
1084 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, | 1083 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, |
1085 scope->uses_super_property()); | 1084 scope->uses_super_property()); |
1086 if ((source_data[i].expected & THIS) != 0) { | 1085 if ((source_data[i].expected & THIS) != 0) { |
1087 // Currently the is_used() flag is conservative; all variables in a | 1086 // Currently the is_used() flag is conservative; all variables in a |
1088 // script scope are marked as used. | 1087 // script scope are marked as used. |
1089 CHECK(scope->LookupThis()->is_used()); | 1088 CHECK(scope->LookupThis()->is_used()); |
1090 } | 1089 } |
1091 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, | 1090 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, |
1092 scope->inner_uses_arguments()); | 1091 scope->inner_uses_arguments()); |
1093 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, | |
1094 scope->inner_uses_super_property()); | |
1095 } | 1092 } |
1096 } | 1093 } |
1097 } | 1094 } |
1098 | 1095 |
1099 | 1096 |
1100 TEST(ScopePositions) { | 1097 TEST(ScopePositions) { |
1101 // Test the parser for correctly setting the start and end positions | 1098 // Test the parser for correctly setting the start and end positions |
1102 // of a scope. We check the scope positions of exactly one scope | 1099 // of a scope. We check the scope positions of exactly one scope |
1103 // nested in the global scope of a program. 'inner source' is the | 1100 // nested in the global scope of a program. 'inner source' is the |
1104 // source code that determines the part of the source belonging | 1101 // source code that determines the part of the source belonging |
(...skipping 5499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6604 "[a, ...]", | 6601 "[a, ...]", |
6605 "[..., ]", | 6602 "[..., ]", |
6606 "[..., ...]", | 6603 "[..., ...]", |
6607 "[ (...a)]", | 6604 "[ (...a)]", |
6608 NULL}; | 6605 NULL}; |
6609 // clang-format on | 6606 // clang-format on |
6610 static const ParserFlag always_flags[] = {kAllowHarmonySpreadArrays}; | 6607 static const ParserFlag always_flags[] = {kAllowHarmonySpreadArrays}; |
6611 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, | 6608 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, |
6612 arraysize(always_flags)); | 6609 arraysize(always_flags)); |
6613 } | 6610 } |
OLD | NEW |