V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
parse-info.cc
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/heap/heap-inl.h"
14 #include "src/objects-inl.h"
15 #include "src/objects/scope-info.h"
16 #include "src/zone/zone.h"
17 
18 namespace v8 {
19 namespace internal {
20 
21 ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
22  : zone_(base::make_unique<Zone>(zone_allocator, ZONE_NAME)),
23  flags_(0),
24  extension_(nullptr),
25  script_scope_(nullptr),
26  stack_limit_(0),
27  hash_seed_(0),
28  function_kind_(FunctionKind::kNormalFunction),
29  script_id_(-1),
30  start_position_(0),
31  end_position_(0),
32  parameters_end_pos_(kNoSourcePosition),
33  function_literal_id_(FunctionLiteral::kIdTypeInvalid),
34  max_function_literal_id_(FunctionLiteral::kIdTypeInvalid),
35  character_stream_(nullptr),
36  ast_value_factory_(nullptr),
37  ast_string_constants_(nullptr),
38  function_name_(nullptr),
39  runtime_call_stats_(nullptr),
40  source_range_map_(nullptr),
41  literal_(nullptr) {}
42 
43 ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator)
44  : ParseInfo(zone_allocator) {
45  set_hash_seed(isolate->heap()->HashSeed());
46  set_stack_limit(isolate->stack_guard()->real_climit());
47  set_runtime_call_stats(isolate->counters()->runtime_call_stats());
48  set_logger(isolate->logger());
49  set_ast_string_constants(isolate->ast_string_constants());
50  if (isolate->is_block_code_coverage()) set_block_coverage_enabled();
51  if (isolate->is_collecting_type_profile()) set_collect_type_profile();
52  if (isolate->compiler_dispatcher()->IsEnabled()) {
53  parallel_tasks_.reset(new ParallelTasks(isolate->compiler_dispatcher()));
54  }
55 }
56 
57 ParseInfo::ParseInfo(Isolate* isolate)
58  : ParseInfo(isolate, isolate->allocator()) {
59  script_id_ = isolate->heap()->NextScriptId();
60  LOG(isolate, ScriptEvent(Logger::ScriptEventType::kReserveId, script_id_));
61 }
62 
63 template <typename T>
64 void ParseInfo::SetFunctionInfo(T function) {
65  set_is_named_expression(function->is_named_expression());
66  set_language_mode(function->language_mode());
67  set_function_kind(function->kind());
68  set_declaration(function->is_declaration());
69  set_requires_instance_members_initializer(
70  function->requires_instance_members_initializer());
71  set_toplevel(function->is_toplevel());
72  set_wrapped_as_function(function->is_wrapped());
73 }
74 
75 ParseInfo::ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared)
76  : ParseInfo(isolate, isolate->allocator()) {
77  // Do not support re-parsing top-level function of a wrapped script.
78  // TODO(yangguo): consider whether we need a top-level function in a
79  // wrapped script at all.
80  DCHECK_IMPLIES(is_toplevel(), !Script::cast(shared->script())->is_wrapped());
81 
82  set_allow_lazy_parsing(true);
83  set_asm_wasm_broken(shared->is_asm_wasm_broken());
84 
85  set_start_position(shared->StartPosition());
86  set_end_position(shared->EndPosition());
87  function_literal_id_ = shared->FunctionLiteralId(isolate);
88  SetFunctionInfo(shared);
89 
90  Handle<Script> script(Script::cast(shared->script()), isolate);
91  set_script(script);
92 
93  if (shared->HasOuterScopeInfo()) {
94  set_outer_scope_info(handle(shared->GetOuterScopeInfo(), isolate));
95  }
96 
97  // CollectTypeProfile uses its own feedback slots. If we have existing
98  // FeedbackMetadata, we can only collect type profile if the feedback vector
99  // has the appropriate slots.
100  set_collect_type_profile(
101  isolate->is_collecting_type_profile() &&
102  (shared->HasFeedbackMetadata()
103  ? shared->feedback_metadata()->HasTypeProfileSlot()
104  : script->IsUserJavaScript()));
105 }
106 
107 ParseInfo::ParseInfo(Isolate* isolate, Handle<Script> script)
108  : ParseInfo(isolate, isolate->allocator()) {
109  SetScriptForToplevelCompile(isolate, script);
110  set_collect_type_profile(isolate->is_collecting_type_profile() &&
111  script->IsUserJavaScript());
112 }
113 
114 // static
115 std::unique_ptr<ParseInfo> ParseInfo::FromParent(
116  const ParseInfo* outer_parse_info, AccountingAllocator* zone_allocator,
117  const FunctionLiteral* literal, const AstRawString* function_name) {
118  std::unique_ptr<ParseInfo> result =
119  base::make_unique<ParseInfo>(zone_allocator);
120 
121  // Replicate shared state of the outer_parse_info.
122  result->flags_ = outer_parse_info->flags_;
123  result->script_id_ = outer_parse_info->script_id_;
124  result->set_logger(outer_parse_info->logger());
125  result->set_ast_string_constants(outer_parse_info->ast_string_constants());
126  result->set_hash_seed(outer_parse_info->hash_seed());
127 
128  DCHECK_EQ(outer_parse_info->parameters_end_pos(), kNoSourcePosition);
129  DCHECK_NULL(outer_parse_info->extension());
130  DCHECK(outer_parse_info->maybe_outer_scope_info().is_null());
131 
132  // Clone the function_name AstRawString into the ParseInfo's own
133  // AstValueFactory.
134  const AstRawString* cloned_function_name =
135  result->GetOrCreateAstValueFactory()->CloneFromOtherFactory(
136  function_name);
137 
138  // Setup function specific details.
139  DCHECK(!literal->is_toplevel());
140  result->set_function_name(cloned_function_name);
141  result->set_start_position(literal->start_position());
142  result->set_end_position(literal->end_position());
143  result->set_function_literal_id(literal->function_literal_id());
144  result->SetFunctionInfo(literal);
145 
146  return result;
147 }
148 
149 ParseInfo::~ParseInfo() = default;
150 
151 DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }
152 
153 Handle<Script> ParseInfo::CreateScript(Isolate* isolate, Handle<String> source,
154  ScriptOriginOptions origin_options,
155  NativesFlag natives) {
156  // Create a script object describing the script to be compiled.
157  Handle<Script> script;
158  if (script_id_ == -1) {
159  script = isolate->factory()->NewScript(source);
160  } else {
161  script = isolate->factory()->NewScriptWithId(source, script_id_);
162  }
163  if (isolate->NeedsSourcePositionsForProfiling()) {
164  Script::InitLineEnds(script);
165  }
166  switch (natives) {
167  case NATIVES_CODE:
168  script->set_type(Script::TYPE_NATIVE);
169  break;
170  case EXTENSION_CODE:
171  script->set_type(Script::TYPE_EXTENSION);
172  break;
173  case INSPECTOR_CODE:
174  script->set_type(Script::TYPE_INSPECTOR);
175  break;
176  case NOT_NATIVES_CODE:
177  break;
178  }
179  script->set_origin_options(origin_options);
180 
181  SetScriptForToplevelCompile(isolate, script);
182  return script;
183 }
184 
185 AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
186  if (!ast_value_factory_.get()) {
187  ast_value_factory_.reset(
188  new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
189  }
190  return ast_value_factory();
191 }
192 
193 void ParseInfo::AllocateSourceRangeMap() {
194  DCHECK(block_coverage_enabled());
195  set_source_range_map(new (zone()) SourceRangeMap(zone()));
196 }
197 
198 void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }
199 
200 void ParseInfo::set_character_stream(
201  std::unique_ptr<Utf16CharacterStream> character_stream) {
202  DCHECK_NULL(character_stream_);
203  character_stream_.swap(character_stream);
204 }
205 
206 void ParseInfo::SetScriptForToplevelCompile(Isolate* isolate,
207  Handle<Script> script) {
208  set_script(script);
209  set_allow_lazy_parsing();
210  set_toplevel();
211  set_collect_type_profile(isolate->is_collecting_type_profile() &&
212  script->IsUserJavaScript());
213  set_wrapped_as_function(script->is_wrapped());
214 }
215 
216 void ParseInfo::set_script(Handle<Script> script) {
217  script_ = script;
218  DCHECK(script_id_ == -1 || script_id_ == script->id());
219  script_id_ = script->id();
220 
221  set_native(script->type() == Script::TYPE_NATIVE);
222  set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
223  set_module(script->origin_options().IsModule());
224  DCHECK(!(is_eval() && is_module()));
225 
226  if (block_coverage_enabled() && script->IsUserJavaScript()) {
227  AllocateSourceRangeMap();
228  }
229 }
230 
231 void ParseInfo::ParallelTasks::Enqueue(ParseInfo* outer_parse_info,
232  const AstRawString* function_name,
233  FunctionLiteral* literal) {
234  base::Optional<CompilerDispatcher::JobId> job_id =
235  dispatcher_->Enqueue(outer_parse_info, function_name, literal);
236  if (job_id) {
237  enqueued_jobs_.emplace_front(std::make_pair(literal, *job_id));
238  }
239 }
240 
241 } // namespace internal
242 } // namespace v8
Definition: libplatform.h:13