parse-info.cc 8.21 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/parsing/parse-info.h"

7
#include "src/api.h"
8
#include "src/ast/ast-source-ranges.h"
9 10
#include "src/ast/ast-value-factory.h"
#include "src/ast/ast.h"
11
#include "src/heap/heap-inl.h"
marja's avatar
marja committed
12
#include "src/objects-inl.h"
13
#include "src/objects/scope-info.h"
14
#include "src/zone/zone.h"
15 16 17 18

namespace v8 {
namespace internal {

19 20
ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
    : zone_(std::make_shared<Zone>(zone_allocator, ZONE_NAME)),
21 22 23 24 25 26
      flags_(0),
      extension_(nullptr),
      script_scope_(nullptr),
      unicode_cache_(nullptr),
      stack_limit_(0),
      hash_seed_(0),
27
      function_flags_(0),
28 29
      start_position_(0),
      end_position_(0),
30
      parameters_end_pos_(kNoSourcePosition),
31
      function_literal_id_(FunctionLiteral::kIdTypeInvalid),
32
      max_function_literal_id_(FunctionLiteral::kIdTypeInvalid),
33
      character_stream_(nullptr),
34
      ast_value_factory_(nullptr),
35
      ast_string_constants_(nullptr),
36
      function_name_(nullptr),
37
      runtime_call_stats_(nullptr),
38
      source_range_map_(nullptr),
39
      literal_(nullptr) {}
40

41 42
ParseInfo::ParseInfo(Handle<SharedFunctionInfo> shared)
    : ParseInfo(shared->GetIsolate()->allocator()) {
43
  Isolate* isolate = shared->GetIsolate();
44
  InitFromIsolate(isolate);
45

46 47 48 49 50
  // Do not support re-parsing top-level function of a wrapped script.
  // TODO(yangguo): consider whether we need a top-level function in a
  //                wrapped script at all.
  DCHECK_IMPLIES(is_toplevel(), !Script::cast(shared->script())->is_wrapped());

51
  set_toplevel(shared->is_toplevel());
52
  set_wrapped_as_function(shared->is_wrapped());
53
  set_allow_lazy_parsing(FLAG_lazy_inner_functions);
54
  set_is_named_expression(shared->is_named_expression());
55
  set_function_flags(shared->flags());
56 57
  set_start_position(shared->StartPosition());
  set_end_position(shared->EndPosition());
58
  function_literal_id_ = shared->function_literal_id();
59
  set_language_mode(shared->language_mode());
60
  set_asm_wasm_broken(shared->is_asm_wasm_broken());
61 62 63

  Handle<Script> script(Script::cast(shared->script()));
  set_script(script);
64 65
  set_native(script->type() == Script::TYPE_NATIVE);
  set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
66 67
  set_module(script->origin_options().IsModule());
  DCHECK(!(is_eval() && is_module()));
68

69 70
  if (shared->HasOuterScopeInfo()) {
    set_outer_scope_info(handle(shared->GetOuterScopeInfo()));
71
  }
72

73 74 75 76
  // CollectTypeProfile uses its own feedback slots. If we have existing
  // FeedbackMetadata, we can only collect type profile if the feedback vector
  // has the appropriate slots.
  set_collect_type_profile(
77
      isolate->is_collecting_type_profile() &&
78 79 80
      (shared->HasFeedbackMetadata()
           ? shared->feedback_metadata()->HasTypeProfileSlot()
           : script->IsUserJavaScript()));
81 82 83
  if (block_coverage_enabled() && script->IsUserJavaScript()) {
    AllocateSourceRangeMap();
  }
84 85 86 87
}

ParseInfo::ParseInfo(Handle<Script> script)
    : ParseInfo(script->GetIsolate()->allocator()) {
88 89
  InitFromIsolate(script->GetIsolate());

90
  set_allow_lazy_parsing();
91
  set_toplevel();
92
  set_script(script);
93
  set_wrapped_as_function(script->is_wrapped());
94

95 96
  set_native(script->type() == Script::TYPE_NATIVE);
  set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
97 98
  set_module(script->origin_options().IsModule());
  DCHECK(!(is_eval() && is_module()));
99

100 101
  set_collect_type_profile(script->GetIsolate()->is_collecting_type_profile() &&
                           script->IsUserJavaScript());
102 103 104
  if (block_coverage_enabled() && script->IsUserJavaScript()) {
    AllocateSourceRangeMap();
  }
105 106
}

107 108
ParseInfo::~ParseInfo() {}

109 110 111 112 113
// static
ParseInfo* ParseInfo::AllocateWithoutScript(Handle<SharedFunctionInfo> shared) {
  Isolate* isolate = shared->GetIsolate();
  ParseInfo* p = new ParseInfo(isolate->allocator());

114
  p->InitFromIsolate(isolate);
115 116 117
  p->set_toplevel(shared->is_toplevel());
  p->set_allow_lazy_parsing(FLAG_lazy_inner_functions);
  p->set_is_named_expression(shared->is_named_expression());
118
  p->set_function_flags(shared->flags());
119 120
  p->set_start_position(shared->StartPosition());
  p->set_end_position(shared->EndPosition());
121 122 123 124 125 126 127 128 129 130 131 132 133
  p->function_literal_id_ = shared->function_literal_id();
  p->set_language_mode(shared->language_mode());

  // BUG(5946): This function exists as a workaround until we can
  // get rid of %SetCode in our native functions. The ParseInfo
  // is explicitly set up for the case that:
  // a) you have a native built-in,
  // b) it's being run for the 2nd-Nth time in an isolate,
  // c) we've already compiled bytecode and therefore don't need
  //    to parse.
  // We tolerate a ParseInfo without a Script in this case.
  p->set_native(true);
  p->set_eval(false);
134 135
  p->set_module(false);
  DCHECK_NE(shared->kind(), FunctionKind::kModule);
136

137
  Handle<HeapObject> scope_info(shared->GetOuterScopeInfo());
138 139 140 141 142 143 144
  if (!scope_info->IsTheHole(isolate) &&
      Handle<ScopeInfo>::cast(scope_info)->length() > 0) {
    p->set_outer_scope_info(Handle<ScopeInfo>::cast(scope_info));
  }
  return p;
}

145 146 147
DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }

bool ParseInfo::is_declaration() const {
148
  return SharedFunctionInfo::IsDeclarationBit::decode(function_flags_);
149 150 151
}

FunctionKind ParseInfo::function_kind() const {
152
  return SharedFunctionInfo::FunctionKindBits::decode(function_flags_);
153 154
}

155 156
bool ParseInfo::requires_instance_fields_initializer() const {
  return SharedFunctionInfo::RequiresInstanceFieldsInitializer::decode(
157
      function_flags_);
158 159
}

160 161 162 163 164 165
void ParseInfo::InitFromIsolate(Isolate* isolate) {
  DCHECK_NOT_NULL(isolate);
  set_hash_seed(isolate->heap()->HashSeed());
  set_stack_limit(isolate->stack_guard()->real_climit());
  set_unicode_cache(isolate->unicode_cache());
  set_runtime_call_stats(isolate->counters()->runtime_call_stats());
166
  set_logger(isolate->logger());
167
  set_ast_string_constants(isolate->ast_string_constants());
168 169
  if (isolate->is_block_code_coverage()) set_block_coverage_enabled();
  if (isolate->is_collecting_type_profile()) set_collect_type_profile();
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
void ParseInfo::EmitBackgroundParseStatisticsOnBackgroundThread() {
  // If runtime call stats was enabled by tracing, emit a trace event at the
  // end of background parsing on the background thread.
  if (runtime_call_stats_ &&
      (FLAG_runtime_stats &
       v8::tracing::TracingCategoryObserver::ENABLED_BY_TRACING)) {
    auto value = v8::tracing::TracedValue::Create();
    runtime_call_stats_->Dump(value.get());
    TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("v8.runtime_stats"),
                         "V8.RuntimeStats", TRACE_EVENT_SCOPE_THREAD,
                         "runtime-call-stats", std::move(value));
  }
}

void ParseInfo::UpdateBackgroundParseStatisticsOnMainThread(Isolate* isolate) {
187 188 189 190 191 192 193 194 195 196 197 198 199
  // Copy over the counters from the background thread to the main counters on
  // the isolate.
  RuntimeCallStats* main_call_stats = isolate->counters()->runtime_call_stats();
  if (FLAG_runtime_stats ==
      v8::tracing::TracingCategoryObserver::ENABLED_BY_NATIVE) {
    DCHECK_NE(main_call_stats, runtime_call_stats());
    DCHECK_NOT_NULL(main_call_stats);
    DCHECK_NOT_NULL(runtime_call_stats());
    main_call_stats->Add(runtime_call_stats());
  }
  set_runtime_call_stats(main_call_stats);
}

200 201 202 203 204
void ParseInfo::ShareZone(ParseInfo* other) {
  DCHECK_EQ(0, zone_->allocation_size());
  zone_ = other->zone_;
}

205 206 207 208 209 210 211 212 213 214 215 216 217
AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
  if (!ast_value_factory_.get()) {
    ast_value_factory_.reset(
        new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
  }
  return ast_value_factory();
}

void ParseInfo::ShareAstValueFactory(ParseInfo* other) {
  DCHECK(!ast_value_factory_.get());
  ast_value_factory_ = other->ast_value_factory_;
}

218 219 220
void ParseInfo::AllocateSourceRangeMap() {
  DCHECK(block_coverage_enabled());
  set_source_range_map(new (zone()) SourceRangeMap(zone()));
221 222
}

223 224 225 226
void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }

void ParseInfo::set_character_stream(
    std::unique_ptr<Utf16CharacterStream> character_stream) {
227
  DCHECK_NULL(character_stream_);
228 229 230
  character_stream_.swap(character_stream);
}

231 232
}  // namespace internal
}  // namespace v8