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 901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 if (!bad) { | 912 if (!bad) { |
913 i += input_offset; | 913 i += input_offset; |
914 character_length -= output_adjust; | 914 character_length -= output_adjust; |
915 } | 915 } |
916 } | 916 } |
917 } | 917 } |
918 return character_length; | 918 return character_length; |
919 } | 919 } |
920 | 920 |
921 | 921 |
922 TEST(ScopeUsesThisAndArguments) { | 922 TEST(ScopeUsesArgumentsSuperThis) { |
923 static const struct { | 923 static const struct { |
924 const char* prefix; | 924 const char* prefix; |
925 const char* suffix; | 925 const char* suffix; |
926 } surroundings[] = { | 926 } surroundings[] = { |
927 { "function f() {", "}" }, | 927 { "function f() {", "}" }, |
928 { "var f = () => {", "}" }, | 928 { "var f = () => {", "}" }, |
929 }; | 929 }; |
930 | 930 |
| 931 enum Expected { |
| 932 NONE = 0, |
| 933 ARGUMENTS = 1, |
| 934 SUPER = 2, |
| 935 THIS = 4, |
| 936 INNER_ARGUMENTS = 8, |
| 937 INNER_SUPER = 16, |
| 938 INNER_THIS = 32 |
| 939 }; |
| 940 |
931 static const struct { | 941 static const struct { |
932 const char* body; | 942 const char* body; |
933 bool uses_this; | 943 int expected; |
934 bool uses_arguments; | |
935 bool inner_uses_this; | |
936 bool inner_uses_arguments; | |
937 } source_data[] = { | 944 } source_data[] = { |
938 { "", | 945 { "", NONE }, |
939 false, false, false, false }, | 946 { "return this", THIS }, |
940 { "return this", | 947 { "return arguments", ARGUMENTS }, |
941 true, false, false, false }, | 948 { "return super()", SUPER }, |
942 { "return arguments", | 949 { "return super.x", SUPER }, |
943 false, true, false, false }, | 950 { "return arguments[0]", ARGUMENTS }, |
944 { "return arguments[0]", | 951 { "return this + arguments[0]", ARGUMENTS | THIS }, |
945 false, true, false, false }, | 952 { "return this + arguments[0] + super.x", ARGUMENTS | SUPER | THIS }, |
946 { "return this + arguments[0]", | 953 { "return x => this + x", INNER_THIS }, |
947 true, true, false, false }, | 954 { "return x => super() + x", INNER_SUPER }, |
948 { "return x => this + x", | 955 { "this.foo = 42;", THIS }, |
949 false, false, true, false }, | 956 { "this.foo();", THIS }, |
950 { "this.foo = 42;", | 957 { "if (foo()) { this.f() }", THIS }, |
951 true, false, false, false }, | 958 { "if (foo()) { super.f() }", SUPER }, |
952 { "this.foo();", | 959 { "if (arguments.length) { this.f() }", ARGUMENTS | THIS }, |
953 true, false, false, false }, | 960 { "while (true) { this.f() }", THIS }, |
954 { "if (foo()) { this.f() }", | 961 { "while (true) { super.f() }", SUPER }, |
955 true, false, false, false }, | 962 { "if (true) { while (true) this.foo(arguments) }", ARGUMENTS | THIS }, |
956 { "if (arguments.length) { this.f() }", | |
957 true, true, false, false }, | |
958 { "while (true) { this.f() }", | |
959 true, false, false, false }, | |
960 { "if (true) { while (true) this.foo(arguments) }", | |
961 true, true, false, false }, | |
962 // Multiple nesting levels must work as well. | 963 // Multiple nesting levels must work as well. |
963 { "while (true) { while (true) { while (true) return this } }", | 964 { "while (true) { while (true) { while (true) return this } }", THIS }, |
964 true, false, false, false }, | 965 { "while (true) { while (true) { while (true) return super() } }", SUPER }, |
965 { "if (1) { return () => { while (true) new this() } }", | 966 { "if (1) { return () => { while (true) new this() } }", INNER_THIS }, |
966 false, false, true, false }, | 967 { "if (1) { return () => { while (true) new super() } }", INNER_SUPER }, |
967 // Note that propagation of the inner_uses_this() value does not | 968 // Note that propagation of the inner_uses_this() value does not |
968 // cross boundaries of normal functions onto parent scopes. | 969 // cross boundaries of normal functions onto parent scopes. |
969 { "return function (x) { return this + x }", | 970 { "return function (x) { return this + x }", NONE }, |
970 false, false, false, false }, | 971 { "return function (x) { return super() + x }", NONE }, |
971 { "var x = function () { this.foo = 42 };", | 972 { "var x = function () { this.foo = 42 };", NONE }, |
972 false, false, false, false }, | 973 { "var x = function () { super.foo = 42 };", NONE }, |
973 { "if (1) { return function () { while (true) new this() } }", | 974 { "if (1) { return function () { while (true) new this() } }", NONE }, |
974 false, false, false, false }, | 975 { "if (1) { return function () { while (true) new super() } }", NONE }, |
975 { "return function (x) { return () => this }", | 976 { "return function (x) { return () => this }", NONE }, |
976 false, false, false, false }, | 977 { "return function (x) { return () => super() }", NONE }, |
977 // Flags must be correctly set when using block scoping. | 978 // Flags must be correctly set when using block scoping. |
978 { "\"use strict\"; while (true) { let x; this, arguments; }", | 979 { "\"use strict\"; while (true) { let x; this, arguments; }", |
979 false, false, true, true }, | 980 INNER_ARGUMENTS | INNER_THIS }, |
980 { "\"use strict\"; if (foo()) { let x; this.f() }", | 981 { "\"use strict\"; while (true) { let x; this, super(), arguments; }", |
981 false, false, true, false }, | 982 INNER_ARGUMENTS | INNER_SUPER | INNER_THIS }, |
| 983 { "\"use strict\"; if (foo()) { let x; this.f() }", INNER_THIS }, |
| 984 { "\"use strict\"; if (foo()) { let x; super.f() }", INNER_SUPER }, |
982 { "\"use strict\"; if (1) {" | 985 { "\"use strict\"; if (1) {" |
983 " let x; return function () { return this + arguments }" | 986 " let x; return function () { return this + super() + arguments }" |
984 "}", | 987 "}", NONE }, |
985 false, false, false, false }, | |
986 }; | 988 }; |
987 | 989 |
988 i::Isolate* isolate = CcTest::i_isolate(); | 990 i::Isolate* isolate = CcTest::i_isolate(); |
989 i::Factory* factory = isolate->factory(); | 991 i::Factory* factory = isolate->factory(); |
990 | 992 |
991 v8::HandleScope handles(CcTest::isolate()); | 993 v8::HandleScope handles(CcTest::isolate()); |
992 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 994 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
993 v8::Context::Scope context_scope(context); | 995 v8::Context::Scope context_scope(context); |
994 | 996 |
995 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - | 997 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
(...skipping 10 matching lines...) Expand all Loading... |
1006 i::Handle<i::String> source = | 1008 i::Handle<i::String> source = |
1007 factory->NewStringFromUtf8(i::CStrVector(program.start())) | 1009 factory->NewStringFromUtf8(i::CStrVector(program.start())) |
1008 .ToHandleChecked(); | 1010 .ToHandleChecked(); |
1009 i::Handle<i::Script> script = factory->NewScript(source); | 1011 i::Handle<i::Script> script = factory->NewScript(source); |
1010 i::CompilationInfoWithZone info(script); | 1012 i::CompilationInfoWithZone info(script); |
1011 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), | 1013 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), |
1012 isolate->heap()->HashSeed(), | 1014 isolate->heap()->HashSeed(), |
1013 isolate->unicode_cache()}; | 1015 isolate->unicode_cache()}; |
1014 i::Parser parser(&info, &parse_info); | 1016 i::Parser parser(&info, &parse_info); |
1015 parser.set_allow_arrow_functions(true); | 1017 parser.set_allow_arrow_functions(true); |
| 1018 parser.set_allow_classes(true); |
1016 parser.set_allow_harmony_scoping(true); | 1019 parser.set_allow_harmony_scoping(true); |
1017 info.MarkAsGlobal(); | 1020 info.MarkAsGlobal(); |
1018 parser.Parse(); | 1021 parser.Parse(); |
1019 CHECK(i::Rewriter::Rewrite(&info)); | 1022 CHECK(i::Rewriter::Rewrite(&info)); |
1020 CHECK(i::Scope::Analyze(&info)); | 1023 CHECK(i::Scope::Analyze(&info)); |
1021 CHECK(info.function() != NULL); | 1024 CHECK(info.function() != NULL); |
1022 | 1025 |
1023 i::Scope* global_scope = info.function()->scope(); | 1026 i::Scope* global_scope = info.function()->scope(); |
1024 CHECK(global_scope->is_global_scope()); | 1027 CHECK(global_scope->is_global_scope()); |
1025 CHECK_EQ(1, global_scope->inner_scopes()->length()); | 1028 CHECK_EQ(1, global_scope->inner_scopes()->length()); |
1026 | 1029 |
1027 i::Scope* scope = global_scope->inner_scopes()->at(0); | 1030 i::Scope* scope = global_scope->inner_scopes()->at(0); |
1028 CHECK_EQ(source_data[i].uses_this, scope->uses_this()); | 1031 CHECK_EQ((source_data[i].expected & ARGUMENTS) != 0, |
1029 CHECK_EQ(source_data[i].uses_arguments, scope->uses_arguments()); | 1032 scope->uses_arguments()); |
1030 CHECK_EQ(source_data[i].inner_uses_this, scope->inner_uses_this()); | 1033 CHECK_EQ((source_data[i].expected & SUPER) != 0, scope->uses_super()); |
1031 CHECK_EQ(source_data[i].inner_uses_arguments, | 1034 CHECK_EQ((source_data[i].expected & THIS) != 0, scope->uses_this()); |
| 1035 CHECK_EQ((source_data[i].expected & INNER_ARGUMENTS) != 0, |
1032 scope->inner_uses_arguments()); | 1036 scope->inner_uses_arguments()); |
| 1037 CHECK_EQ((source_data[i].expected & INNER_SUPER) != 0, |
| 1038 scope->inner_uses_super()); |
| 1039 CHECK_EQ((source_data[i].expected & INNER_THIS) != 0, |
| 1040 scope->inner_uses_this()); |
1033 } | 1041 } |
1034 } | 1042 } |
1035 } | 1043 } |
1036 | 1044 |
1037 | 1045 |
1038 TEST(ScopePositions) { | 1046 TEST(ScopePositions) { |
1039 v8::internal::FLAG_harmony_scoping = true; | 1047 v8::internal::FLAG_harmony_scoping = true; |
1040 | 1048 |
1041 // Test the parser for correctly setting the start and end positions | 1049 // Test the parser for correctly setting the start and end positions |
1042 // of a scope. We check the scope positions of exactly one scope | 1050 // of a scope. We check the scope positions of exactly one scope |
(...skipping 3258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4301 const char* data[] = { | 4309 const char* data[] = { |
4302 "var foob\\u123r = 0;", | 4310 "var foob\\u123r = 0;", |
4303 "var \\u123roo = 0;", | 4311 "var \\u123roo = 0;", |
4304 "\"foob\\u123rr\"", | 4312 "\"foob\\u123rr\"", |
4305 // No escapes allowed in regexp flags | 4313 // No escapes allowed in regexp flags |
4306 "/regex/\\u0069g", | 4314 "/regex/\\u0069g", |
4307 "/regex/\\u006g", | 4315 "/regex/\\u006g", |
4308 NULL}; | 4316 NULL}; |
4309 RunParserSyncTest(context_data, data, kError); | 4317 RunParserSyncTest(context_data, data, kError); |
4310 } | 4318 } |
OLD | NEW |