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 8665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8676 } | 8676 } |
8677 | 8677 |
8678 namespace v8 { | 8678 namespace v8 { |
8679 namespace internal { | 8679 namespace internal { |
8680 | 8680 |
8681 class ScopeTestHelper { | 8681 class ScopeTestHelper { |
8682 public: | 8682 public: |
8683 static bool MustAllocateInContext(Variable* var) { | 8683 static bool MustAllocateInContext(Variable* var) { |
8684 return var->scope()->MustAllocateInContext(var); | 8684 return var->scope()->MustAllocateInContext(var); |
8685 } | 8685 } |
| 8686 |
| 8687 static void CompareScopeToData(Scope* scope, const PreParsedScopeData* data, |
| 8688 size_t& index) { |
| 8689 CHECK_EQ(data->backing_store_[index++], scope->scope_type()); |
| 8690 CHECK_EQ(data->backing_store_[index++], scope->start_position()); |
| 8691 CHECK_EQ(data->backing_store_[index++], scope->end_position()); |
| 8692 |
| 8693 int inner_scope_count = 0; |
| 8694 for (Scope* inner = scope->inner_scope(); inner != nullptr; |
| 8695 inner = inner->sibling()) { |
| 8696 ++inner_scope_count; |
| 8697 } |
| 8698 CHECK_EQ(data->backing_store_[index++], inner_scope_count); |
| 8699 |
| 8700 // Variable count is 0. TODO(marja): implement. |
| 8701 CHECK_EQ(data->backing_store_[index++], 0); |
| 8702 |
| 8703 for (Scope* inner = scope->inner_scope(); inner != nullptr; |
| 8704 inner = inner->sibling()) { |
| 8705 CompareScopeToData(inner, data, index); |
| 8706 } |
| 8707 } |
8686 }; | 8708 }; |
8687 } // namespace internal | 8709 } // namespace internal |
8688 } // namespace v8 | 8710 } // namespace v8 |
8689 | 8711 |
8690 // Test that lazily parsed inner functions don't result in overly pessimistic | 8712 // Test that lazily parsed inner functions don't result in overly pessimistic |
8691 // context allocations. | 8713 // context allocations. |
8692 TEST(NoPessimisticContextAllocation) { | 8714 TEST(NoPessimisticContextAllocation) { |
8693 i::FLAG_lazy_inner_functions = true; | 8715 i::FLAG_lazy_inner_functions = true; |
8694 i::Isolate* isolate = CcTest::i_isolate(); | 8716 i::Isolate* isolate = CcTest::i_isolate(); |
8695 i::Factory* factory = isolate->factory(); | 8717 i::Factory* factory = isolate->factory(); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8953 DCHECK_NULL(scope->sibling()); | 8975 DCHECK_NULL(scope->sibling()); |
8954 DCHECK(scope->is_function_scope()); | 8976 DCHECK(scope->is_function_scope()); |
8955 const i::AstRawString* var_name = | 8977 const i::AstRawString* var_name = |
8956 info.ast_value_factory()->GetOneByteString("my_var"); | 8978 info.ast_value_factory()->GetOneByteString("my_var"); |
8957 i::Variable* var = scope->Lookup(var_name); | 8979 i::Variable* var = scope->Lookup(var_name); |
8958 CHECK_EQ(inners[i].ctxt_allocate, | 8980 CHECK_EQ(inners[i].ctxt_allocate, |
8959 i::ScopeTestHelper::MustAllocateInContext(var)); | 8981 i::ScopeTestHelper::MustAllocateInContext(var)); |
8960 } | 8982 } |
8961 } | 8983 } |
8962 } | 8984 } |
| 8985 |
| 8986 TEST(PreParserScopeAnalysis) { |
| 8987 i::FLAG_lazy_inner_functions = true; |
| 8988 i::FLAG_preparser_scope_analysis = true; |
| 8989 i::Isolate* isolate = CcTest::i_isolate(); |
| 8990 i::Factory* factory = isolate->factory(); |
| 8991 i::HandleScope scope(isolate); |
| 8992 LocalContext env; |
| 8993 |
| 8994 const char* prefix = "(function outer() { "; |
| 8995 const char* suffix = " })();"; |
| 8996 int prefix_len = Utf8LengthHelper(prefix); |
| 8997 int suffix_len = Utf8LengthHelper(suffix); |
| 8998 |
| 8999 // The scope start positions must match; note the extra space in lazy_inner. |
| 9000 const char* lazy_inner = " function inner(%s) { %s }"; |
| 9001 const char* eager_inner = "(function inner(%s) { %s })()"; |
| 9002 |
| 9003 struct { |
| 9004 const char* params; |
| 9005 const char* source; |
| 9006 } inners[] = { |
| 9007 {"", "var1"}, |
| 9008 {"", "if (true) {}"}, |
| 9009 {"", "function f1() {}"}, |
| 9010 {"", "if (true) { function f1() {} }"}, |
| 9011 }; |
| 9012 |
| 9013 for (unsigned i = 0; i < arraysize(inners); ++i) { |
| 9014 // First compile with the lazy inner function and extract the scope data. |
| 9015 const char* inner_function = lazy_inner; |
| 9016 int inner_function_len = Utf8LengthHelper(inner_function) - 4; |
| 9017 |
| 9018 int params_len = Utf8LengthHelper(inners[i].params); |
| 9019 int source_len = Utf8LengthHelper(inners[i].source); |
| 9020 int len = |
| 9021 prefix_len + inner_function_len + params_len + source_len + suffix_len; |
| 9022 |
| 9023 i::ScopedVector<char> lazy_program(len + 1); |
| 9024 i::SNPrintF(lazy_program, "%s", prefix); |
| 9025 i::SNPrintF(lazy_program + prefix_len, inner_function, inners[i].params, |
| 9026 inners[i].source); |
| 9027 i::SNPrintF(lazy_program + prefix_len + inner_function_len + params_len + |
| 9028 source_len, |
| 9029 "%s", suffix); |
| 9030 |
| 9031 i::Handle<i::String> source = |
| 9032 factory->InternalizeUtf8String(lazy_program.start()); |
| 9033 source->PrintOn(stdout); |
| 9034 printf("\n"); |
| 9035 |
| 9036 i::Handle<i::Script> script = factory->NewScript(source); |
| 9037 i::ParseInfo lazy_info(script); |
| 9038 |
| 9039 // No need to run scope analysis; preparser scope data is produced when |
| 9040 // parsing. |
| 9041 CHECK(i::parsing::ParseProgram(&lazy_info)); |
| 9042 |
| 9043 // Then parse eagerly and check against the scope data. |
| 9044 inner_function = eager_inner; |
| 9045 inner_function_len = Utf8LengthHelper(inner_function) - 4; |
| 9046 len = |
| 9047 prefix_len + inner_function_len + params_len + source_len + suffix_len; |
| 9048 |
| 9049 i::ScopedVector<char> eager_program(len + 1); |
| 9050 i::SNPrintF(eager_program, "%s", prefix); |
| 9051 i::SNPrintF(eager_program + prefix_len, inner_function, inners[i].params, |
| 9052 inners[i].source); |
| 9053 i::SNPrintF(eager_program + prefix_len + inner_function_len + params_len + |
| 9054 source_len, |
| 9055 "%s", suffix); |
| 9056 |
| 9057 source = factory->InternalizeUtf8String(eager_program.start()); |
| 9058 source->PrintOn(stdout); |
| 9059 printf("\n"); |
| 9060 |
| 9061 script = factory->NewScript(source); |
| 9062 i::ParseInfo eager_info(script); |
| 9063 |
| 9064 CHECK(i::parsing::ParseProgram(&eager_info)); |
| 9065 CHECK(i::Compiler::Analyze(&eager_info)); |
| 9066 |
| 9067 i::Scope* scope = |
| 9068 eager_info.literal()->scope()->inner_scope()->inner_scope(); |
| 9069 DCHECK_NOT_NULL(scope); |
| 9070 DCHECK_NULL(scope->sibling()); |
| 9071 DCHECK(scope->is_function_scope()); |
| 9072 |
| 9073 size_t index = 0; |
| 9074 i::ScopeTestHelper::CompareScopeToData( |
| 9075 scope, lazy_info.preparsed_scope_data(), index); |
| 9076 } |
| 9077 } |
OLD | NEW |