| 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 944 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, | 964 INNER_SUPER_PROPERTY = 1 << 4, |
| 965 INNER_THIS = 1 << 5 |
| 965 }; | 966 }; |
| 966 | 967 |
| 967 static const struct { | 968 static const struct { |
| 968 const char* body; | 969 const char* body; |
| 969 int expected; | 970 int expected; |
| 970 } source_data[] = { | 971 } source_data[] = { |
| 971 {"", NONE}, | 972 {"", NONE}, |
| 972 {"return this", THIS}, | 973 {"return this", THIS}, |
| 973 {"return arguments", ARGUMENTS}, | 974 {"return arguments", ARGUMENTS}, |
| 974 {"return super.x", SUPER_PROPERTY}, | 975 {"return super.x", SUPER_PROPERTY}, |
| 975 {"return arguments[0]", ARGUMENTS}, | 976 {"return arguments[0]", ARGUMENTS}, |
| 976 {"return this + arguments[0]", ARGUMENTS | THIS}, | 977 {"return this + arguments[0]", ARGUMENTS | THIS}, |
| 977 {"return this + arguments[0] + super.x", | 978 {"return this + arguments[0] + super.x", |
| 978 ARGUMENTS | SUPER_PROPERTY | THIS}, | 979 ARGUMENTS | SUPER_PROPERTY | THIS}, |
| 979 {"return x => this + x", THIS}, | 980 {"return x => this + x", INNER_THIS}, |
| 980 {"return x => super.f() + x", INNER_SUPER_PROPERTY}, | 981 {"return x => super.f() + x", INNER_SUPER_PROPERTY}, |
| 981 {"this.foo = 42;", THIS}, | 982 {"this.foo = 42;", THIS}, |
| 982 {"this.foo();", THIS}, | 983 {"this.foo();", THIS}, |
| 983 {"if (foo()) { this.f() }", THIS}, | 984 {"if (foo()) { this.f() }", THIS}, |
| 984 {"if (foo()) { super.f() }", SUPER_PROPERTY}, | 985 {"if (foo()) { super.f() }", SUPER_PROPERTY}, |
| 985 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, | 986 {"if (arguments.length) { this.f() }", ARGUMENTS | THIS}, |
| 986 {"while (true) { this.f() }", THIS}, | 987 {"while (true) { this.f() }", THIS}, |
| 987 {"while (true) { super.f() }", SUPER_PROPERTY}, | 988 {"while (true) { super.f() }", SUPER_PROPERTY}, |
| 988 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, | 989 {"if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS}, |
| 989 // Multiple nesting levels must work as well. | 990 // Multiple nesting levels must work as well. |
| 990 {"while (true) { while (true) { while (true) return this } }", THIS}, | 991 {"while (true) { while (true) { while (true) return this } }", THIS}, |
| 991 {"while (true) { while (true) { while (true) return super.f() } }", | 992 {"while (true) { while (true) { while (true) return super.f() } }", |
| 992 SUPER_PROPERTY}, | 993 SUPER_PROPERTY}, |
| 993 {"if (1) { return () => { while (true) new this() } }", THIS}, | 994 {"if (1) { return () => { while (true) new this() } }", INNER_THIS}, |
| 995 // Note that propagation of the inner_uses_this() value does not |
| 996 // cross boundaries of normal functions onto parent scopes. |
| 994 {"return function (x) { return this + x }", NONE}, | 997 {"return function (x) { return this + x }", NONE}, |
| 995 {"return { m(x) { return super.m() + x } }", NONE}, | 998 {"return { m(x) { return super.m() + x } }", NONE}, |
| 996 {"var x = function () { this.foo = 42 };", NONE}, | 999 {"var x = function () { this.foo = 42 };", NONE}, |
| 997 {"var x = { m() { super.foo = 42 } };", NONE}, | 1000 {"var x = { m() { super.foo = 42 } };", NONE}, |
| 998 {"if (1) { return function () { while (true) new this() } }", NONE}, | 1001 {"if (1) { return function () { while (true) new this() } }", NONE}, |
| 999 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, | 1002 {"if (1) { return { m() { while (true) super.m() } } }", NONE}, |
| 1000 {"return function (x) { return () => this }", NONE}, | 1003 {"return function (x) { return () => this }", NONE}, |
| 1001 {"return { m(x) { return () => super.m() } }", NONE}, | 1004 {"return { m(x) { return () => super.m() } }", NONE}, |
| 1002 // Flags must be correctly set when using block scoping. | 1005 // Flags must be correctly set when using block scoping. |
| 1003 {"\"use strict\"; while (true) { let x; this, arguments; }", | 1006 {"\"use strict\"; while (true) { let x; this, arguments; }", |
| 1004 INNER_ARGUMENTS | THIS}, | 1007 INNER_ARGUMENTS | INNER_THIS}, |
| 1005 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", | 1008 {"\"use strict\"; while (true) { let x; this, super.f(), arguments; }", |
| 1006 INNER_ARGUMENTS | INNER_SUPER_PROPERTY | THIS}, | 1009 INNER_ARGUMENTS | INNER_SUPER_PROPERTY | INNER_THIS}, |
| 1007 {"\"use strict\"; if (foo()) { let x; this.f() }", THIS}, | 1010 {"\"use strict\"; if (foo()) { let x; this.f() }", INNER_THIS}, |
| 1008 {"\"use strict\"; if (foo()) { let x; super.f() }", | 1011 {"\"use strict\"; if (foo()) { let x; super.f() }", |
| 1009 INNER_SUPER_PROPERTY}, | 1012 INNER_SUPER_PROPERTY}, |
| 1010 {"\"use strict\"; if (1) {" | 1013 {"\"use strict\"; if (1) {" |
| 1011 " let x; return { m() { return this + super.m() + arguments } }" | 1014 " let x; return { m() { return this + super.m() + arguments } }" |
| 1012 "}", | 1015 "}", |
| 1013 NONE}, | 1016 NONE}, |
| 1014 }; | 1017 }; |
| 1015 | 1018 |
| 1016 i::Isolate* isolate = CcTest::i_isolate(); | 1019 i::Isolate* isolate = CcTest::i_isolate(); |
| 1017 i::Factory* factory = isolate->factory(); | 1020 i::Factory* factory = isolate->factory(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1061 i::Scope* scope = script_scope->inner_scopes()->at(0); | 1064 i::Scope* scope = script_scope->inner_scopes()->at(0); |
| 1062 // Adjust for constructor scope. | 1065 // Adjust for constructor scope. |
| 1063 if (j == 2) { | 1066 if (j == 2) { |
| 1064 CHECK_EQ(1, scope->inner_scopes()->length()); | 1067 CHECK_EQ(1, scope->inner_scopes()->length()); |
| 1065 scope = scope->inner_scopes()->at(0); | 1068 scope = scope->inner_scopes()->at(0); |
| 1066 } | 1069 } |
| 1067 CHECK_EQ((source_data[i].expected & ARGUMENTS) != 0, | 1070 CHECK_EQ((source_data[i].expected & ARGUMENTS) != 0, |
| 1068 scope->uses_arguments()); | 1071 scope->uses_arguments()); |
| 1069 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, | 1072 CHECK_EQ((source_data[i].expected & SUPER_PROPERTY) != 0, |
| 1070 scope->uses_super_property()); | 1073 scope->uses_super_property()); |
| 1071 if ((source_data[i].expected & THIS) != 0) { | 1074 CHECK_EQ((source_data[i].expected & THIS) != 0, scope->uses_this()); |
| 1072 // Currently the is_used() flag is conservative; all variables in a | |
| 1073 // script scope are marked as used. | |
| 1074 CHECK(scope->LookupThis()->is_used()); | |
| 1075 } | |
| 1076 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, | 1075 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, |
| 1077 scope->inner_uses_arguments()); | 1076 scope->inner_uses_arguments()); |
| 1078 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, | 1077 CHECK_EQ((source_data[i].expected & INNER_SUPER_PROPERTY) != 0, |
| 1079 scope->inner_uses_super_property()); | 1078 scope->inner_uses_super_property()); |
| 1079 CHECK_EQ((source_data[i].expected & INNER_THIS) != 0, |
| 1080 scope->inner_uses_this()); |
| 1080 } | 1081 } |
| 1081 } | 1082 } |
| 1082 } | 1083 } |
| 1083 | 1084 |
| 1084 | 1085 |
| 1085 TEST(ScopePositions) { | 1086 TEST(ScopePositions) { |
| 1086 // Test the parser for correctly setting the start and end positions | 1087 // Test the parser for correctly setting the start and end positions |
| 1087 // of a scope. We check the scope positions of exactly one scope | 1088 // 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 | 1089 // nested in the global scope of a program. 'inner source' is the |
| 1089 // source code that determines the part of the source belonging | 1090 // source code that determines the part of the source belonging |
| (...skipping 5381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6471 const char* data[] = { | 6472 const char* data[] = { |
| 6472 "yield", | 6473 "yield", |
| 6473 "[yield]", | 6474 "[yield]", |
| 6474 "{ x : yield }", | 6475 "{ x : yield }", |
| 6475 NULL}; | 6476 NULL}; |
| 6476 // clang-format on | 6477 // clang-format on |
| 6477 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, | 6478 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, |
| 6478 arraysize(always_flags)); | 6479 arraysize(always_flags)); |
| 6479 } | 6480 } |
| 6480 } | 6481 } |
| OLD | NEW |