Chromium Code Reviews| 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 897 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 if (!bad) { | 908 if (!bad) { |
| 909 i += input_offset; | 909 i += input_offset; |
| 910 character_length -= output_adjust; | 910 character_length -= output_adjust; |
| 911 } | 911 } |
| 912 } | 912 } |
| 913 } | 913 } |
| 914 return character_length; | 914 return character_length; |
| 915 } | 915 } |
| 916 | 916 |
| 917 | 917 |
| 918 TEST(ScopeUsesThisAndArguments) { | |
| 919 static const struct { | |
| 920 const char* prefix; | |
| 921 const char* suffix; | |
| 922 } surroundings[] = { | |
| 923 { "function f() {", "}" }, | |
| 924 { "function f() { \"use strict\";", "}" }, | |
| 925 { "\"use strict\"; function f() {", "}" }, | |
| 926 { "f = () => {", "}" }, | |
| 927 { "f = () => { \"use strict\";", "}" }, | |
|
rossberg
2014/10/15 12:56:32
It can't harm to keep these variants, you never kn
aperez
2014/10/15 17:09:51
The issue with keeping those is that making the fu
| |
| 928 { "\"use strict\"; f = () => {", "}" }, | |
| 929 }; | |
| 930 | |
| 931 static const struct { | |
| 932 const char* body; | |
| 933 bool uses_this; | |
| 934 bool uses_arguments; | |
| 935 bool inner_uses_this; | |
| 936 bool inner_uses_arguments; | |
| 937 } source_data[] = { | |
| 938 { "", | |
| 939 false, false, false, false }, | |
| 940 { "return this;", | |
| 941 true, false, false, false }, | |
| 942 { "return arguments;", | |
| 943 false, true, false, false }, | |
| 944 { "return arguments[0];", | |
| 945 false, true, false, false }, | |
| 946 { "return this + arguments[0];", | |
| 947 true, true, false, false }, | |
| 948 { "return () => this;", | |
| 949 false, false, true, false }, | |
| 950 { "var x = function () { this.foo = 42 };", | |
|
arv (Not doing code reviews)
2014/09/04 16:41:21
Why do we need to track the this reference inside
rossberg
2014/09/10 06:33:56
Indeed, this seems wrong. Although it should be tr
| |
| 951 false, false, true, false }, | |
| 952 { "this.foo = 42;", | |
| 953 true, false, false, false }, | |
| 954 { "this.foo();", | |
| 955 true, false, false, false }, | |
| 956 { "if (foo()) { this.f() }", | |
| 957 true, false, false, false }, | |
| 958 { "if (arguments.length) { this.f() }", | |
| 959 true, true, false, false }, | |
| 960 { "while (true) { this.f() }", | |
| 961 true, false, false, false }, | |
| 962 { "if (true) { while (true) this.foo(arguments) }", | |
| 963 true, true, false, false }, | |
| 964 { "var f = function () { return this }", | |
| 965 false, false, true, false }, | |
| 966 { "var f = function () { return arguments[0] }", | |
| 967 false, false, false, true }, | |
| 968 }; | |
|
rossberg
2014/09/10 06:33:56
We probably need some tests with block scopes as w
| |
| 969 | |
| 970 i::Isolate* isolate = CcTest::i_isolate(); | |
| 971 i::Factory* factory = isolate->factory(); | |
| 972 | |
| 973 v8::HandleScope handles(CcTest::isolate()); | |
| 974 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | |
| 975 v8::Context::Scope context_scope(context); | |
| 976 | |
| 977 isolate->stack_guard()->SetStackLimit(GetCurrentStackPosition() - 128 * 1024); | |
| 978 | |
| 979 for (unsigned j = 0; j < arraysize(surroundings); ++j) { | |
| 980 for (unsigned i = 0; i < arraysize(source_data); ++i) { | |
| 981 int kProgramByteSize = i::StrLength(surroundings[j].prefix) + | |
| 982 i::StrLength(surroundings[j].suffix) + | |
| 983 i::StrLength(source_data[i].body); | |
| 984 i::ScopedVector<char> program(kProgramByteSize + 1); | |
| 985 i::SNPrintF(program, "%s%s%s", surroundings[j].prefix, | |
| 986 source_data[i].body, surroundings[j].suffix); | |
| 987 i::Handle<i::String> source = | |
| 988 factory->NewStringFromUtf8(i::CStrVector(program.start())) | |
| 989 .ToHandleChecked(); | |
| 990 i::Handle<i::Script> script = factory->NewScript(source); | |
| 991 i::CompilationInfoWithZone info(script); | |
| 992 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), | |
| 993 isolate->heap()->HashSeed(), | |
| 994 isolate->unicode_cache()}; | |
| 995 i::Parser parser(&info, &parse_info); | |
| 996 parser.set_allow_arrow_functions(true); | |
| 997 info.MarkAsGlobal(); | |
| 998 parser.Parse(); | |
| 999 CHECK(i::Rewriter::Rewrite(&info)); | |
| 1000 CHECK(i::Scope::Analyze(&info)); | |
| 1001 CHECK(info.function() != NULL); | |
| 1002 | |
| 1003 i::Scope* scope = info.function()->scope(); | |
| 1004 CHECK(scope->is_global_scope()); | |
| 1005 CHECK_EQ(1, scope->inner_scopes()->length()); | |
| 1006 | |
| 1007 i::Scope* inner_scope = scope->inner_scopes()->at(0); | |
| 1008 CHECK_EQ(source_data[i].uses_this, inner_scope->uses_this()); | |
| 1009 CHECK_EQ(source_data[i].uses_arguments, inner_scope->uses_arguments()); | |
| 1010 CHECK_EQ(source_data[i].inner_uses_this, inner_scope->inner_uses_this()); | |
| 1011 CHECK_EQ(source_data[i].inner_uses_arguments, | |
| 1012 inner_scope->inner_uses_arguments()); | |
| 1013 } | |
| 1014 } | |
| 1015 } | |
| 1016 | |
| 1017 | |
| 918 TEST(ScopePositions) { | 1018 TEST(ScopePositions) { |
| 919 v8::internal::FLAG_harmony_scoping = true; | 1019 v8::internal::FLAG_harmony_scoping = true; |
| 920 | 1020 |
| 921 // Test the parser for correctly setting the start and end positions | 1021 // Test the parser for correctly setting the start and end positions |
| 922 // of a scope. We check the scope positions of exactly one scope | 1022 // of a scope. We check the scope positions of exactly one scope |
| 923 // nested in the global scope of a program. 'inner source' is the | 1023 // nested in the global scope of a program. 'inner source' is the |
| 924 // source code that determines the part of the source belonging | 1024 // source code that determines the part of the source belonging |
| 925 // to the nested scope. 'outer_prefix' and 'outer_suffix' are | 1025 // to the nested scope. 'outer_prefix' and 'outer_suffix' are |
| 926 // parts of the source that belong to the global scope. | 1026 // parts of the source that belong to the global scope. |
| 927 struct SourceData { | 1027 struct SourceData { |
| (...skipping 2454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3382 const char* statement_data[] = { | 3482 const char* statement_data[] = { |
| 3383 "super = x", | 3483 "super = x", |
| 3384 "y = super", | 3484 "y = super", |
| 3385 "f(super)", | 3485 "f(super)", |
| 3386 NULL}; | 3486 NULL}; |
| 3387 | 3487 |
| 3388 static const ParserFlag always_flags[] = {kAllowClasses}; | 3488 static const ParserFlag always_flags[] = {kAllowClasses}; |
| 3389 RunParserSyncTest(context_data, statement_data, kError, NULL, 0, | 3489 RunParserSyncTest(context_data, statement_data, kError, NULL, 0, |
| 3390 always_flags, arraysize(always_flags)); | 3490 always_flags, arraysize(always_flags)); |
| 3391 } | 3491 } |
| OLD | NEW |