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 943 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
954 { "var f = () => {", "};" }, | 954 { "var f = () => {", "};" }, |
955 { "class C { constructor() {", "} }" }, | 955 { "class C { constructor() {", "} }" }, |
956 }; | 956 }; |
957 | 957 |
958 enum Expected { | 958 enum Expected { |
959 NONE = 0, | 959 NONE = 0, |
960 ARGUMENTS = 1, | 960 ARGUMENTS = 1, |
961 SUPER_PROPERTY = 1 << 1, | 961 SUPER_PROPERTY = 1 << 1, |
962 THIS = 1 << 2, | 962 THIS = 1 << 2, |
963 INNER_ARGUMENTS = 1 << 3, | 963 INNER_ARGUMENTS = 1 << 3, |
964 INNER_SUPER_PROPERTY = 1 << 4, | |
965 }; | 964 }; |
966 | 965 |
967 static const struct { | 966 static const struct { |
968 const char* body; | 967 const char* body; |
969 int expected; | 968 int expected; |
970 } source_data[] = { | 969 } source_data[] = { |
971 {"", NONE}, | 970 {"", NONE}, |
972 {"return this", THIS}, | 971 {"return this", THIS}, |
973 {"return arguments", ARGUMENTS}, | 972 {"return arguments", ARGUMENTS}, |
974 {"return super.x", SUPER_PROPERTY}, | 973 {"return super.x", SUPER_PROPERTY}, |
975 {"return arguments[0]", ARGUMENTS}, | 974 {"return arguments[0]", ARGUMENTS}, |
976 {"return this + arguments[0]", ARGUMENTS | THIS}, | 975 {"return this + arguments[0]", ARGUMENTS | THIS}, |
977 {"return this + arguments[0] + super.x", | 976 {"return this + arguments[0] + super.x", |
978 ARGUMENTS | SUPER_PROPERTY | THIS}, | 977 ARGUMENTS | SUPER_PROPERTY | THIS}, |
979 {"return x => this + x", THIS}, | 978 {"return x => this + x", THIS}, |
980 {"return x => super.f() + x", INNER_SUPER_PROPERTY}, | 979 {"return x => super.f() + x", SUPER_PROPERTY}, |
981 {"this.foo = 42;", THIS}, | 980 {"this.foo = 42;", THIS}, |
982 {"this.foo();", THIS}, | 981 {"this.foo();", THIS}, |
983 {"if (foo()) { this.f() }", THIS}, | 982 {"if (foo()) { this.f() }", THIS}, |
984 {"if (foo()) { super.f() }", SUPER_PROPERTY}, | 983 {"if (foo()) { super.f() }", SUPER_PROPERTY}, |
985 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, | 984 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, |
986 {"while (true) { this.f() }", THIS}, | 985 {"while (true) { this.f() }", THIS}, |
987 {"while (true) { super.f() }", SUPER_PROPERTY}, | 986 {"while (true) { super.f() }", SUPER_PROPERTY}, |
988 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, | 987 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, |
989 // Multiple nesting levels must work as well. | 988 // Multiple nesting levels must work as well. |
990 {"while (true) { while (true) { while (true) return this } }", THIS}, | 989 {"while (true) { while (true) { while (true) return this } }", THIS}, |
991 {"while (true) { while (true) { while (true) return super.f() } }", | 990 {"while (true) { while (true) { while (true) return super.f() } }", |
992 SUPER_PROPERTY}, | 991 SUPER_PROPERTY}, |
993 {"if (1) { return () => { while (true) new this() } }", THIS}, | 992 {"if (1) { return () => { while (true) new this() } }", THIS}, |
994 {"return function (x) { return this + x }", NONE}, | 993 {"return function (x) { return this + x }", NONE}, |
995 {"return { m(x) { return super.m() + x } }", NONE}, | 994 {"return { m(x) { return super.m() + x } }", NONE}, |
996 {"var x = function () { this.foo = 42 };", NONE}, | 995 {"var x = function () { this.foo = 42 };", NONE}, |
997 {"var x = { m() { super.foo = 42 } };", NONE}, | 996 {"var x = { m() { super.foo = 42 } };", NONE}, |
998 {"if (1) { return function () { while (true) new this() } }", NONE}, | 997 {"if (1) { return function () { while (true) new this() } }", NONE}, |
999 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, | 998 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, |
1000 {"return function (x) { return () => this }", NONE}, | 999 {"return function (x) { return () => this }", NONE}, |
1001 {"return { m(x) { return () => super.m() } }", NONE}, | 1000 {"return { m(x) { return () => super.m() } }", NONE}, |
1002 // Flags must be correctly set when using block scoping. | 1001 // Flags must be correctly set when using block scoping. |
1003 {"\"use strict\"; while (true) { let x; this, arguments; }", | 1002 {"\"use strict\"; while (true) { let x; this, arguments; }", |
1004 INNER_ARGUMENTS | THIS}, | 1003 INNER_ARGUMENTS | THIS}, |
1005 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", | 1004 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", |
1006 INNER_ARGUMENTS | INNER_SUPER_PROPERTY | THIS}, | 1005 INNER_ARGUMENTS | SUPER_PROPERTY | THIS}, |
1007 {"\"use strict\"; if (foo()) { let x; this.f() }", THIS}, | 1006 {"\"use strict\"; if (foo()) { let x; this.f() }", THIS}, |
1008 {"\"use strict\"; if (foo()) { let x; super.f() }", | 1007 {"\"use strict\"; if (foo()) { let x; super.f() }", SUPER_PROPERTY}, |
1009 INNER_SUPER_PROPERTY}, | |
1010 {"\"use strict\"; if (1) {" | 1008 {"\"use strict\"; if (1) {" |
1011 " let x; return { m() { return this + super.m() + arguments } }" | 1009 " let x; return { m() { return this + super.m() + arguments } }" |
1012 "}", | 1010 "}", |
1013 NONE}, | 1011 NONE}, |
1014 }; | 1012 }; |
1015 | 1013 |
1016 i::Isolate* isolate = CcTest::i_isolate(); | 1014 i::Isolate* isolate = CcTest::i_isolate(); |
1017 i::Factory* factory = isolate->factory(); | 1015 i::Factory* factory = isolate->factory(); |
1018 | 1016 |
1019 v8::HandleScope handles(CcTest::isolate()); | 1017 v8::HandleScope handles(CcTest::isolate()); |
1020 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 1018 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
1021 v8::Context::Scope context_scope(context); | 1019 v8::Context::Scope context_scope(context); |
1022 | 1020 |
1023 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - | 1021 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
1024 128 * 1024); | 1022 128 * 1024); |
1025 | 1023 |
1026 for (unsigned j = 0; j < arraysize(surroundings); ++j) { | 1024 for (unsigned j = 0; j < arraysize(surroundings); ++j) { |
1027 for (unsigned i = 0; i < arraysize(source_data); ++i) { | 1025 for (unsigned i = 0; i < arraysize(source_data); ++i) { |
1028 // Super property is only allowed in constructor and method. | 1026 // Super property is only allowed in constructor and method. |
1029 if (((source_data[i].expected & SUPER_PROPERTY) || | 1027 if (((source_data[i].expected & SUPER_PROPERTY) || |
1030 (source_data[i].expected & INNER_SUPER_PROPERTY) || | |
1031 (source_data[i].expected == NONE)) && j != 2) { | 1028 (source_data[i].expected == NONE)) && j != 2) { |
1032 continue; | 1029 continue; |
1033 } | 1030 } |
1034 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + | 1031 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + |
1035 i::StrLength(surroundings[j].suffix) + | 1032 i::StrLength(surroundings[j].suffix) + |
1036 i::StrLength(source_data[i].body); | 1033 i::StrLength(source_data[i].body); |
1037 i::ScopedVector<char> program(kProgramByteSize + 1); | 1034 i::ScopedVector<char> program(kProgramByteSize + 1); |
1038 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, | 1035 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, |
1039 source_data[i].body, surroundings[j].suffix); | 1036 source_data[i].body, surroundings[j].suffix); |
1040 i::Handle<i::String> source = | 1037 i::Handle<i::String> source = |
(...skipping 27 matching lines...) Expand all Loading... |
1068 scope->uses_arguments()); | 1065 scope->uses_arguments()); |
1069 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, | 1066 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, |
1070 scope->uses_super_property()); | 1067 scope->uses_super_property()); |
1071 if ((source_data[i].expected & THIS) != 0) { | 1068 if ((source_data[i].expected & THIS) != 0) { |
1072 // Currently the is_used() flag is conservative; all variables in a | 1069 // Currently the is_used() flag is conservative; all variables in a |
1073 // script scope are marked as used. | 1070 // script scope are marked as used. |
1074 CHECK(scope->LookupThis()->is_used()); | 1071 CHECK(scope->LookupThis()->is_used()); |
1075 } | 1072 } |
1076 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, | 1073 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, |
1077 scope->inner_uses_arguments()); | 1074 scope->inner_uses_arguments()); |
1078 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, | |
1079 scope->inner_uses_super_property()); | |
1080 } | 1075 } |
1081 } | 1076 } |
1082 } | 1077 } |
1083 | 1078 |
1084 | 1079 |
1085 TEST(ScopePositions) { | 1080 TEST(ScopePositions) { |
1086 // Test the parser for correctly setting the start and end positions | 1081 // Test the parser for correctly setting the start and end positions |
1087 // of a scope. We check the scope positions of exactly one scope | 1082 // of a scope. We check the scope positions of exactly one scope |
1088 // nested in the global scope of a program. 'inner source' is the | 1083 // nested in the global scope of a program. 'inner source' is the |
1089 // source code that determines the part of the source belonging | 1084 // source code that determines the part of the source belonging |
(...skipping 5423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6513 const char* data[] = { | 6508 const char* data[] = { |
6514 "yield", | 6509 "yield", |
6515 "[yield]", | 6510 "[yield]", |
6516 "{ x : yield }", | 6511 "{ x : yield }", |
6517 NULL}; | 6512 NULL}; |
6518 // clang-format on | 6513 // clang-format on |
6519 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, | 6514 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, |
6520 arraysize(always_flags)); | 6515 arraysize(always_flags)); |
6521 } | 6516 } |
6522 } | 6517 } |
OLD | NEW |