LCOV - code coverage report
Current view: top level - src/parsing - parse-info.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 100 103 97.1 %
Date: 2019-04-18 Functions: 18 18 100.0 %

          Line data    Source code
       1             : // Copyright 2016 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/parsing/parse-info.h"
       6             : 
       7             : #include "src/ast/ast-source-ranges.h"
       8             : #include "src/ast/ast-value-factory.h"
       9             : #include "src/ast/ast.h"
      10             : #include "src/base/template-utils.h"
      11             : #include "src/compiler-dispatcher/compiler-dispatcher.h"
      12             : #include "src/counters.h"
      13             : #include "src/hash-seed-inl.h"
      14             : #include "src/heap/heap-inl.h"
      15             : #include "src/log.h"
      16             : #include "src/objects-inl.h"
      17             : #include "src/objects/scope-info.h"
      18             : #include "src/zone/zone.h"
      19             : 
      20             : namespace v8 {
      21             : namespace internal {
      22             : 
      23     2468908 : ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
      24             :     : zone_(base::make_unique<Zone>(zone_allocator, ZONE_NAME)),
      25             :       flags_(0),
      26             :       extension_(nullptr),
      27             :       script_scope_(nullptr),
      28             :       stack_limit_(0),
      29             :       hash_seed_(0),
      30             :       function_kind_(FunctionKind::kNormalFunction),
      31             :       script_id_(-1),
      32             :       start_position_(0),
      33             :       end_position_(0),
      34             :       parameters_end_pos_(kNoSourcePosition),
      35             :       function_literal_id_(kFunctionLiteralIdInvalid),
      36             :       max_function_literal_id_(kFunctionLiteralIdInvalid),
      37             :       character_stream_(nullptr),
      38             :       ast_value_factory_(nullptr),
      39             :       ast_string_constants_(nullptr),
      40             :       function_name_(nullptr),
      41             :       runtime_call_stats_(nullptr),
      42             :       source_range_map_(nullptr),
      43     9875644 :       literal_(nullptr) {}
      44             : 
      45     2468828 : ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator)
      46     2468828 :     : ParseInfo(zone_allocator) {
      47             :   set_hash_seed(HashSeed(isolate));
      48             :   set_stack_limit(isolate->stack_guard()->real_climit());
      49             :   set_runtime_call_stats(isolate->counters()->runtime_call_stats());
      50             :   set_logger(isolate->logger());
      51             :   set_ast_string_constants(isolate->ast_string_constants());
      52     2468879 :   set_collect_source_positions(!FLAG_enable_lazy_source_positions ||
      53          40 :                                isolate->NeedsDetailedOptimizedCodeLineInfo());
      54     2468839 :   if (!isolate->is_best_effort_code_coverage()) set_coverage_enabled();
      55     2468839 :   if (isolate->is_block_code_coverage()) set_block_coverage_enabled();
      56     2468839 :   if (isolate->is_collecting_type_profile()) set_collect_type_profile();
      57     2468839 :   if (isolate->compiler_dispatcher()->IsEnabled()) {
      58          98 :     parallel_tasks_.reset(new ParallelTasks(isolate->compiler_dispatcher()));
      59             :   }
      60     2468832 :   set_might_always_opt(FLAG_always_opt || FLAG_prepare_always_opt);
      61     2468832 :   set_allow_lazy_compile(FLAG_lazy);
      62     2468832 :   set_allow_natives_syntax(FLAG_allow_natives_syntax);
      63     2468832 :   set_allow_harmony_public_fields(FLAG_harmony_public_fields);
      64     2468832 :   set_allow_harmony_static_fields(FLAG_harmony_static_fields);
      65     2468832 :   set_allow_harmony_dynamic_import(FLAG_harmony_dynamic_import);
      66     2468832 :   set_allow_harmony_import_meta(FLAG_harmony_import_meta);
      67     2468832 :   set_allow_harmony_numeric_separator(FLAG_harmony_numeric_separator);
      68     2468832 :   set_allow_harmony_private_fields(FLAG_harmony_private_fields);
      69     2468832 :   set_allow_harmony_private_methods(FLAG_harmony_private_methods);
      70     2468832 : }
      71             : 
      72     1134836 : ParseInfo::ParseInfo(Isolate* isolate)
      73     1134836 :     : ParseInfo(isolate, isolate->allocator()) {
      74     1134836 :   script_id_ = isolate->heap()->NextScriptId();
      75     1134836 :   LOG(isolate, ScriptEvent(Logger::ScriptEventType::kReserveId, script_id_));
      76     1134836 : }
      77             : 
      78             : template <typename T>
      79      722212 : void ParseInfo::SetFunctionInfo(T function) {
      80             :   set_is_named_expression(function->is_named_expression());
      81          80 :   set_language_mode(function->language_mode());
      82          80 :   set_function_kind(function->kind());
      83             :   set_declaration(function->is_declaration());
      84             :   set_requires_instance_members_initializer(
      85             :       function->requires_instance_members_initializer());
      86             :   set_toplevel(function->is_toplevel());
      87     1444272 :   set_is_oneshot_iife(function->is_oneshot_iife());
      88             :   set_wrapped_as_function(function->is_wrapped());
      89      722220 : }
      90             : 
      91      722134 : ParseInfo::ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared)
      92      722134 :     : ParseInfo(isolate, isolate->allocator()) {
      93             :   // Do not support re-parsing top-level function of a wrapped script.
      94             :   // TODO(yangguo): consider whether we need a top-level function in a
      95             :   //                wrapped script at all.
      96             :   DCHECK_IMPLIES(is_toplevel(), !Script::cast(shared->script())->is_wrapped());
      97             : 
      98             :   set_allow_lazy_parsing(true);
      99             :   set_asm_wasm_broken(shared->is_asm_wasm_broken());
     100             : 
     101     1444284 :   set_start_position(shared->StartPosition());
     102     1444276 :   set_end_position(shared->EndPosition());
     103      722134 :   function_literal_id_ = shared->FunctionLiteralId(isolate);
     104      722139 :   SetFunctionInfo(shared);
     105             : 
     106     1444284 :   Handle<Script> script(Script::cast(shared->script()), isolate);
     107      722143 :   set_script(script);
     108             : 
     109      722135 :   if (shared->HasOuterScopeInfo()) {
     110      890773 :     set_outer_scope_info(handle(shared->GetOuterScopeInfo(), isolate));
     111             :   }
     112             : 
     113             :   // CollectTypeProfile uses its own feedback slots. If we have existing
     114             :   // FeedbackMetadata, we can only collect type profile if the feedback vector
     115             :   // has the appropriate slots.
     116             :   set_collect_type_profile(
     117      722253 :       isolate->is_collecting_type_profile() &&
     118          56 :       (shared->HasFeedbackMetadata()
     119           0 :            ? shared->feedback_metadata()->HasTypeProfileSlot()
     120          56 :            : script->IsUserJavaScript()));
     121      722141 : }
     122             : 
     123      611859 : ParseInfo::ParseInfo(Isolate* isolate, Handle<Script> script)
     124      611859 :     : ParseInfo(isolate, isolate->allocator()) {
     125      611859 :   SetScriptForToplevelCompile(isolate, script);
     126      611859 :   set_collect_type_profile(isolate->is_collecting_type_profile() &&
     127           0 :                            script->IsUserJavaScript());
     128      611859 : }
     129             : 
     130             : // static
     131          80 : std::unique_ptr<ParseInfo> ParseInfo::FromParent(
     132             :     const ParseInfo* outer_parse_info, AccountingAllocator* zone_allocator,
     133             :     const FunctionLiteral* literal, const AstRawString* function_name) {
     134             :   std::unique_ptr<ParseInfo> result =
     135             :       base::make_unique<ParseInfo>(zone_allocator);
     136             : 
     137             :   // Replicate shared state of the outer_parse_info.
     138          80 :   result->flags_ = outer_parse_info->flags_;
     139          80 :   result->script_id_ = outer_parse_info->script_id_;
     140             :   result->set_logger(outer_parse_info->logger());
     141             :   result->set_ast_string_constants(outer_parse_info->ast_string_constants());
     142             :   result->set_hash_seed(outer_parse_info->hash_seed());
     143             : 
     144             :   DCHECK_EQ(outer_parse_info->parameters_end_pos(), kNoSourcePosition);
     145             :   DCHECK_NULL(outer_parse_info->extension());
     146             :   DCHECK(outer_parse_info->maybe_outer_scope_info().is_null());
     147             : 
     148             :   // Clone the function_name AstRawString into the ParseInfo's own
     149             :   // AstValueFactory.
     150             :   const AstRawString* cloned_function_name =
     151          80 :       result->GetOrCreateAstValueFactory()->CloneFromOtherFactory(
     152          80 :           function_name);
     153             : 
     154             :   // Setup function specific details.
     155             :   DCHECK(!literal->is_toplevel());
     156             :   result->set_function_name(cloned_function_name);
     157          80 :   result->set_start_position(literal->start_position());
     158          80 :   result->set_end_position(literal->end_position());
     159             :   result->set_function_literal_id(literal->function_literal_id());
     160          80 :   result->SetFunctionInfo(literal);
     161             : 
     162          80 :   return result;
     163             : }
     164             : 
     165             : ParseInfo::~ParseInfo() = default;
     166             : 
     167     2475349 : DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }
     168             : 
     169     1121680 : Handle<Script> ParseInfo::CreateScript(Isolate* isolate, Handle<String> source,
     170             :                                        ScriptOriginOptions origin_options,
     171             :                                        NativesFlag natives) {
     172             :   // Create a script object describing the script to be compiled.
     173             :   Handle<Script> script;
     174     1121680 :   if (script_id_ == -1) {
     175           0 :     script = isolate->factory()->NewScript(source);
     176             :   } else {
     177     1121680 :     script = isolate->factory()->NewScriptWithId(source, script_id_);
     178             :   }
     179     1121680 :   if (isolate->NeedsSourcePositionsForProfiling()) {
     180       26670 :     Script::InitLineEnds(script);
     181             :   }
     182     1121680 :   switch (natives) {
     183             :     case EXTENSION_CODE:
     184             :       script->set_type(Script::TYPE_EXTENSION);
     185        3394 :       break;
     186             :     case INSPECTOR_CODE:
     187             :       script->set_type(Script::TYPE_INSPECTOR);
     188         301 :       break;
     189             :     case NOT_NATIVES_CODE:
     190             :       break;
     191             :   }
     192     1121680 :   script->set_origin_options(origin_options);
     193             : 
     194     1121680 :   SetScriptForToplevelCompile(isolate, script);
     195     1121679 :   return script;
     196             : }
     197             : 
     198     2468948 : AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
     199     2468948 :   if (!ast_value_factory_.get()) {
     200     2468877 :     ast_value_factory_.reset(
     201     2468881 :         new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
     202             :   }
     203     2468946 :   return ast_value_factory();
     204             : }
     205             : 
     206         676 : void ParseInfo::AllocateSourceRangeMap() {
     207             :   DCHECK(block_coverage_enabled());
     208             :   set_source_range_map(new (zone()) SourceRangeMap(zone()));
     209         676 : }
     210             : 
     211     8122757 : void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }
     212             : 
     213     2468878 : void ParseInfo::set_character_stream(
     214             :     std::unique_ptr<Utf16CharacterStream> character_stream) {
     215             :   DCHECK_NULL(character_stream_);
     216             :   character_stream_.swap(character_stream);
     217     2468878 : }
     218             : 
     219     1733539 : void ParseInfo::SetScriptForToplevelCompile(Isolate* isolate,
     220             :                                             Handle<Script> script) {
     221     1733539 :   set_script(script);
     222             :   set_allow_lazy_parsing();
     223             :   set_toplevel();
     224     1733550 :   set_collect_type_profile(isolate->is_collecting_type_profile() &&
     225     1733550 :                            script->IsUserJavaScript());
     226     3467077 :   set_wrapped_as_function(script->is_wrapped());
     227     1733539 : }
     228             : 
     229     2455746 : void ParseInfo::set_script(Handle<Script> script) {
     230     2455746 :   script_ = script;
     231             :   DCHECK(script_id_ == -1 || script_id_ == script->id());
     232     2455746 :   script_id_ = script->id();
     233             : 
     234             :   set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
     235             :   set_module(script->origin_options().IsModule());
     236             :   DCHECK(!(is_eval() && is_module()));
     237             : 
     238     2456422 :   if (block_coverage_enabled() && script->IsUserJavaScript()) {
     239         676 :     AllocateSourceRangeMap();
     240             :   }
     241     2455746 : }
     242             : 
     243          55 : void ParseInfo::ParallelTasks::Enqueue(ParseInfo* outer_parse_info,
     244             :                                        const AstRawString* function_name,
     245             :                                        FunctionLiteral* literal) {
     246             :   base::Optional<CompilerDispatcher::JobId> job_id =
     247          55 :       dispatcher_->Enqueue(outer_parse_info, function_name, literal);
     248          55 :   if (job_id) {
     249         110 :     enqueued_jobs_.emplace_front(std::make_pair(literal, *job_id));
     250             :   }
     251          55 : }
     252             : 
     253             : }  // namespace internal
     254      122036 : }  // namespace v8

Generated by: LCOV version 1.10