v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
parse-info.cc
Go to the documentation of this file.
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
6
9#include "src/ast/ast.h"
10#include "src/base/logging.h"
11#include "src/common/globals.h"
13#include "src/heap/heap-inl.h"
15#include "src/logging/log.h"
19#include "src/zone/zone.h"
20
21namespace v8 {
22namespace internal {
23
25 int script_id)
26 : flags_(0),
27 script_id_(script_id),
28 function_kind_(FunctionKind::kNormalFunction),
29 function_syntax_kind_(FunctionSyntaxKind::kDeclaration),
30 parsing_while_debugging_(ParsingWhileDebugging::kNo) {
31 set_coverage_enabled(!isolate->is_best_effort_code_coverage());
32 set_block_coverage_enabled(isolate->is_block_code_coverage());
33 set_might_always_turbofan(v8_flags.always_turbofan ||
34 v8_flags.prepare_always_turbofan);
35 set_allow_natives_syntax(v8_flags.allow_natives_syntax);
36 set_allow_lazy_compile(true);
37 set_collect_source_positions(!v8_flags.enable_lazy_source_positions ||
38 isolate->NeedsDetailedOptimizedCodeLineInfo());
39 set_post_parallel_compile_tasks_for_eager_toplevel(
40 v8_flags.parallel_compile_tasks_for_eager_toplevel);
41 set_post_parallel_compile_tasks_for_lazy(
42 v8_flags.parallel_compile_tasks_for_lazy);
43}
44
45// static
47 Isolate* isolate, Tagged<SharedFunctionInfo> shared) {
48 Tagged<Script> script = Cast<Script>(shared->script());
49
50 UnoptimizedCompileFlags flags(isolate, script->id());
51
52 flags.SetFlagsForFunctionFromScript(script);
53 flags.SetFlagsFromFunction(shared);
54 flags.set_allow_lazy_parsing(true);
55 flags.set_is_lazy_compile(true);
56
57#if V8_ENABLE_WEBASSEMBLY
58 flags.set_is_asm_wasm_broken(shared->is_asm_wasm_broken());
59#endif // V8_ENABLE_WEBASSEMBLY
60 flags.set_is_repl_mode(script->is_repl_mode());
61
62 // Do not support re-parsing top-level function of a wrapped script.
63 DCHECK_IMPLIES(flags.is_toplevel(), !script->is_wrapped());
64
65 return flags;
66}
67
68// static
70 Isolate* isolate, Tagged<Script> script) {
71 UnoptimizedCompileFlags flags(isolate, script->id());
72
73 flags.SetFlagsForFunctionFromScript(script);
74 flags.SetFlagsForToplevelCompile(
75 script->IsUserJavaScript(), flags.outer_language_mode(),
76 construct_repl_mode(script->is_repl_mode()),
77 script->origin_options().IsModule() ? ScriptType::kModule
79 v8_flags.lazy);
80 if (script->is_wrapped()) {
81 flags.set_function_syntax_kind(FunctionSyntaxKind::kWrapped);
82 }
83
84 return flags;
85}
86
87// static
89 Isolate* isolate, bool is_user_javascript, LanguageMode language_mode,
90 REPLMode repl_mode, ScriptType type, bool lazy) {
91 UnoptimizedCompileFlags flags(isolate, isolate->GetNextScriptId());
92 flags.SetFlagsForToplevelCompile(is_user_javascript, language_mode, repl_mode,
93 type, lazy);
94 LOG(isolate, ScriptEvent(ScriptEventType::kReserveId, flags.script_id()));
95 return flags;
96}
97
98// static
100 const UnoptimizedCompileFlags toplevel_flags,
101 const FunctionLiteral* literal) {
102 DCHECK(toplevel_flags.is_toplevel());
103 DCHECK(!literal->is_toplevel());
104
105 // Replicate the toplevel flags, then setup the function-specific flags.
106 UnoptimizedCompileFlags flags = toplevel_flags;
108
109 return flags;
110}
111
112// static
116
117template <typename T>
119 set_outer_language_mode(function->language_mode());
120 set_function_kind(function->kind());
121 set_function_syntax_kind(function->syntax_kind());
122 set_requires_instance_members_initializer(
123 function->requires_instance_members_initializer());
124 set_class_scope_has_private_brand(function->class_scope_has_private_brand());
125 set_has_static_private_methods_or_accessors(
126 function->has_static_private_methods_or_accessors());
127 set_private_name_lookup_skips_outer_class(
128 function->private_name_lookup_skips_outer_class());
129 set_is_toplevel(function->is_toplevel());
130}
131
133 bool is_user_javascript, LanguageMode language_mode, REPLMode repl_mode,
134 ScriptType type, bool lazy) {
135 set_is_toplevel(true);
136 set_allow_lazy_parsing(lazy);
137 set_allow_lazy_compile(lazy);
138 set_outer_language_mode(
139 stricter_language_mode(outer_language_mode(), language_mode));
140 set_is_repl_mode((repl_mode == REPLMode::kYes));
141 set_is_module(type == ScriptType::kModule);
142 DCHECK_IMPLIES(is_eval(), !is_module());
143
144 set_block_coverage_enabled(block_coverage_enabled() && is_user_javascript);
145}
146
148 Tagged<Script> script) {
149 DCHECK_EQ(script_id(), script->id());
150
151 set_is_eval(script->compilation_type() == Script::CompilationType::kEval);
152 set_is_module(script->origin_options().IsModule());
153 DCHECK_IMPLIES(is_eval(), !is_module());
154
155 set_block_coverage_enabled(block_coverage_enabled() &&
156 script->IsUserJavaScript());
157}
158
160 Isolate* isolate)
161 : hash_seed_(HashSeed(isolate)),
162 allocator_(isolate->allocator()),
163 v8_file_logger_(isolate->v8_file_logger()),
164 dispatcher_(isolate->lazy_compile_dispatcher()),
165 ast_string_constants_(isolate->ast_string_constants()),
166 ast_raw_string_zone_(allocator_,
167 "unoptimized-compile-ast-raw-string-zone"),
168 single_parse_zone_(allocator_, "unoptimized-compile-parse-zone"),
169 ast_value_factory_(
170 new AstValueFactory(ast_raw_string_zone(), single_parse_zone(),
171 ast_string_constants(), hash_seed())) {}
172
174 LocalIsolate* isolate)
175 : hash_seed_(HashSeed(isolate)),
176 allocator_(isolate->allocator()),
177 v8_file_logger_(isolate->main_thread_logger()),
178 dispatcher_(isolate->lazy_compile_dispatcher()),
179 ast_string_constants_(isolate->ast_string_constants()),
180 ast_raw_string_zone_(allocator_,
181 "unoptimized-compile-ast-raw-string-zone"),
182 single_parse_zone_(allocator_, "unoptimized-compile-parse-zone"),
183 ast_value_factory_(
184 new AstValueFactory(ast_raw_string_zone(), single_parse_zone(),
185 ast_string_constants(), hash_seed())) {}
186
188
191 ReusableUnoptimizedCompileState* reusable_state,
192 uintptr_t stack_limit,
193 RuntimeCallStats* runtime_call_stats)
194 : flags_(flags),
195 state_(state),
196 reusable_state_(reusable_state),
197 extension_(nullptr),
198 script_scope_(nullptr),
199 stack_limit_(stack_limit),
200 parameters_end_pos_(kNoSourcePosition),
201 max_info_id_(kInvalidInfoId),
202 character_stream_(nullptr),
203 function_name_(nullptr),
204 runtime_call_stats_(runtime_call_stats),
205 source_range_map_(nullptr),
206 literal_(nullptr),
207 allow_eval_cache_(false),
208#if V8_ENABLE_WEBASSEMBLY
209 contains_asm_module_(false),
210#endif // V8_ENABLE_WEBASSEMBLY
211 language_mode_(flags.outer_language_mode()),
212 is_background_compilation_(false),
213 is_streaming_compilation_(false),
214 has_module_in_scope_chain_(flags.is_module()) {
215 if (flags.block_coverage_enabled()) {
216 AllocateSourceRangeMap();
217 }
218}
219
222 ReusableUnoptimizedCompileState* reusable_state)
223 : ParseInfo(flags, state, reusable_state,
224 isolate->stack_guard()->real_climit(),
225 isolate->counters()->runtime_call_stats()) {}
226
229 ReusableUnoptimizedCompileState* reusable_state,
230 uintptr_t stack_limit)
231 : ParseInfo(flags, state, reusable_state, stack_limit,
232 isolate->runtime_call_stats()) {}
233
235
237
238template <typename IsolateT>
240 IsolateT* isolate, DirectHandle<String> source,
241 MaybeDirectHandle<FixedArray> maybe_wrapped_arguments,
242 ScriptOriginOptions origin_options, NativesFlag natives) {
243 // Create a script object describing the script to be compiled.
244 DCHECK(flags().script_id() >= 0 ||
245 flags().script_id() == Script::kTemporaryScriptId);
246 auto event = ScriptEventType::kCreate;
251 } else if (is_background_compilation()) {
253 }
254 Handle<Script> script =
255 isolate->factory()->NewScriptWithId(source, flags().script_id(), event);
257 Tagged<Script> raw_script = *script;
258 switch (natives) {
259 case EXTENSION_CODE:
260 raw_script->set_type(Script::Type::kExtension);
261 break;
262 case INSPECTOR_CODE:
263 raw_script->set_type(Script::Type::kInspector);
264 break;
265 case NOT_NATIVES_CODE:
266 break;
267 }
268 raw_script->set_origin_options(origin_options);
269 raw_script->set_is_repl_mode(flags().is_repl_mode());
270
271 DCHECK_EQ(is_wrapped_as_function(), !maybe_wrapped_arguments.is_null());
273 raw_script->set_wrapped_arguments(
274 *maybe_wrapped_arguments.ToHandleChecked());
275 } else if (flags().is_eval()) {
276 raw_script->set_compilation_type(Script::CompilationType::kEval);
277 }
279
280 return script;
281}
282
285 Isolate* isolate, DirectHandle<String> source,
286 MaybeDirectHandle<FixedArray> maybe_wrapped_arguments,
287 ScriptOriginOptions origin_options, NativesFlag natives);
290 LocalIsolate* isolate, DirectHandle<String> source,
291 MaybeDirectHandle<FixedArray> maybe_wrapped_arguments,
292 ScriptOriginOptions origin_options, NativesFlag natives);
293
295 DCHECK(flags().block_coverage_enabled());
297 set_source_range_map(zone()->New<SourceRangeMap>(zone()));
298}
299
301
303 std::unique_ptr<Utf16CharacterStream> character_stream) {
306}
307
310 DCHECK(flags().is_toplevel());
311 DCHECK_EQ(flags().is_repl_mode(), script->is_repl_mode());
312
313 if (script->is_wrapped()) {
314 DCHECK_EQ(flags().function_syntax_kind(), FunctionSyntaxKind::kWrapped);
315 }
316}
317
319 DCHECK_EQ(flags().script_id(), script->id());
320 // We set "is_eval" for wrapped scripts to get an outer declaration scope.
321 // This is a bit hacky, but ok since we can't be both eval and wrapped.
322 DCHECK_EQ(flags().is_eval() && !script->is_wrapped(),
323 script->compilation_type() == Script::CompilationType::kEval);
324 DCHECK_EQ(flags().is_module(), script->origin_options().IsModule());
325 DCHECK_IMPLIES(flags().block_coverage_enabled() && script->IsUserJavaScript(),
326 source_range_map() != nullptr);
327}
328
329} // namespace internal
330} // namespace v8
RegisterAllocator * allocator_
DeclarationScope * scope() const
Definition ast.h:2315
V8_INLINE DirectHandle< T > ToHandleChecked() const
V8_INLINE bool is_null() const
Zone * zone() const
Definition parse-info.h:247
std::unique_ptr< Utf16CharacterStream > character_stream_
Definition parse-info.h:392
bool is_streaming_compilation() const
Definition parse-info.h:348
void CheckFlagsForToplevelCompileFromScript(Tagged< Script > script)
ParseInfo(Isolate *isolate, const UnoptimizedCompileFlags flags, UnoptimizedCompileState *state, ReusableUnoptimizedCompileState *reusable_state)
Utf16CharacterStream * character_stream() const
Definition parse-info.h:288
bool is_background_compilation() const
Definition parse-info.h:344
bool is_wrapped_as_function() const
Definition parse-info.h:329
const UnoptimizedCompileFlags & flags() const
Definition parse-info.h:249
DeclarationScope * scope() const
FunctionLiteral * literal() const
Definition parse-info.h:319
SourceRangeMap * source_range_map() const
Definition parse-info.h:337
void set_character_stream(std::unique_ptr< Utf16CharacterStream > character_stream)
ReusableUnoptimizedCompileState * reusable_state_
Definition parse-info.h:380
void CheckFlagsForFunctionFromScript(Tagged< Script > script)
Handle< Script > CreateScript(IsolateT *isolate, DirectHandle< String > source, MaybeDirectHandle< FixedArray > maybe_wrapped_arguments, ScriptOriginOptions origin_options, NativesFlag natives=NOT_NATIVES_CODE)
void set_source_range_map(SourceRangeMap *source_range_map)
Definition parse-info.h:338
static constexpr int kTemporaryScriptId
Definition script.h:42
static UnoptimizedCompileFlags ForToplevelCompile(Isolate *isolate, bool is_user_javascript, LanguageMode language_mode, REPLMode repl_mode, ScriptType type, bool lazy)
Definition parse-info.cc:88
UnoptimizedCompileFlags & set_function_kind(FunctionKind value)
Definition parse-info.h:113
static UnoptimizedCompileFlags ForFunctionCompile(Isolate *isolate, Tagged< SharedFunctionInfo > shared)
Definition parse-info.cc:46
UnoptimizedCompileFlags(Isolate *isolate, int script_id)
Definition parse-info.cc:24
static UnoptimizedCompileFlags ForToplevelFunction(const UnoptimizedCompileFlags toplevel_flags, const FunctionLiteral *literal)
Definition parse-info.cc:99
void SetFlagsForFunctionFromScript(Tagged< Script > script)
static UnoptimizedCompileFlags ForTest(Isolate *isolate)
UnoptimizedCompileFlags & set_function_syntax_kind(FunctionSyntaxKind value)
Definition parse-info.h:121
void SetFlagsForToplevelCompile(bool is_user_javascript, LanguageMode language_mode, REPLMode repl_mode, ScriptType type, bool lazy)
static UnoptimizedCompileFlags ForScriptCompile(Isolate *isolate, Tagged< Script > script)
Definition parse-info.cc:69
LanguageMode language_mode_
JSRegExp::Flags flags_
enum v8::internal::@1270::DeoptimizableCodeIterator::@67 state_
#define EXPORT_TEMPLATE_DEFINE(export)
FunctionLiteral * literal
Definition liveedit.cc:294
#define LOG(isolate, Call)
Definition log.h:78
LanguageMode stricter_language_mode(LanguageMode mode1, LanguageMode mode2)
Definition globals.h:792
constexpr int kNoSourcePosition
Definition globals.h:850
REPLMode construct_repl_mode(bool is_repl_mode)
Definition globals.h:1638
Flag flags[]
Definition flags.cc:3797
constexpr int kInvalidInfoId
Definition globals.h:2766
V8_EXPORT_PRIVATE FlagValues v8_flags
uint64_t HashSeed(Isolate *isolate)
kInterpreterTrampolineOffset script
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
ScriptType
Definition v8-script.h:397
const uintptr_t stack_limit_
#define DCHECK_NULL(val)
Definition logging.h:491
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
int script_id_