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/api.h"
8 : #include "src/ast/ast-value-factory.h"
9 : #include "src/ast/ast.h"
10 : #include "src/heap/heap-inl.h"
11 : #include "src/objects-inl.h"
12 : #include "src/objects/scope-info.h"
13 : #include "src/zone/zone.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 4842047 : ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
19 : : zone_(std::make_shared<Zone>(zone_allocator, ZONE_NAME)),
20 : flags_(0),
21 : source_stream_(nullptr),
22 : source_stream_encoding_(ScriptCompiler::StreamedSource::ONE_BYTE),
23 : character_stream_(nullptr),
24 : extension_(nullptr),
25 : compile_options_(ScriptCompiler::kNoCompileOptions),
26 : script_scope_(nullptr),
27 : asm_function_scope_(nullptr),
28 : unicode_cache_(nullptr),
29 : stack_limit_(0),
30 : hash_seed_(0),
31 : compiler_hints_(0),
32 : start_position_(0),
33 : end_position_(0),
34 : parameters_end_pos_(kNoSourcePosition),
35 : function_literal_id_(FunctionLiteral::kIdTypeInvalid),
36 : max_function_literal_id_(FunctionLiteral::kIdTypeInvalid),
37 : cached_data_(nullptr),
38 : ast_value_factory_(nullptr),
39 : ast_string_constants_(nullptr),
40 : function_name_(nullptr),
41 : runtime_call_stats_(nullptr),
42 : literal_(nullptr),
43 9684122 : deferred_handles_(nullptr) {}
44 :
45 1989900 : ParseInfo::ParseInfo(Handle<SharedFunctionInfo> shared)
46 1989900 : : ParseInfo(shared->GetIsolate()->allocator()) {
47 : Isolate* isolate = shared->GetIsolate();
48 : InitFromIsolate(isolate);
49 :
50 : set_toplevel(shared->is_toplevel());
51 1989900 : set_allow_lazy_parsing(FLAG_lazy_inner_functions);
52 : set_is_named_expression(shared->is_named_expression());
53 : set_compiler_hints(shared->compiler_hints());
54 : set_start_position(shared->start_position());
55 : set_end_position(shared->end_position());
56 1989900 : function_literal_id_ = shared->function_literal_id();
57 : set_language_mode(shared->language_mode());
58 : set_shared_info(shared);
59 : set_module(shared->kind() == FunctionKind::kModule);
60 :
61 : Handle<Script> script(Script::cast(shared->script()));
62 : set_script(script);
63 : set_native(script->type() == Script::TYPE_NATIVE);
64 : set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
65 :
66 : Handle<HeapObject> scope_info(shared->outer_scope_info());
67 3741966 : if (!scope_info->IsTheHole(isolate) &&
68 : Handle<ScopeInfo>::cast(scope_info)->length() > 0) {
69 : set_outer_scope_info(Handle<ScopeInfo>::cast(scope_info));
70 : }
71 1989900 : }
72 :
73 122588 : ParseInfo::ParseInfo(Handle<SharedFunctionInfo> shared,
74 : std::shared_ptr<Zone> zone)
75 122588 : : ParseInfo(shared) {
76 : zone_.swap(zone);
77 122588 : }
78 :
79 2851988 : ParseInfo::ParseInfo(Handle<Script> script)
80 2851988 : : ParseInfo(script->GetIsolate()->allocator()) {
81 : InitFromIsolate(script->GetIsolate());
82 :
83 : set_allow_lazy_parsing();
84 : set_toplevel();
85 : set_script(script);
86 :
87 : set_native(script->type() == Script::TYPE_NATIVE);
88 : set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
89 2851998 : }
90 :
91 9684063 : ParseInfo::~ParseInfo() {
92 4842031 : if (ast_value_factory_owned()) {
93 6859566 : delete ast_value_factory_;
94 : set_ast_value_factory_owned(false);
95 : }
96 4842032 : ast_value_factory_ = nullptr;
97 4842047 : }
98 :
99 : // static
100 0 : ParseInfo* ParseInfo::AllocateWithoutScript(Handle<SharedFunctionInfo> shared) {
101 0 : Isolate* isolate = shared->GetIsolate();
102 0 : ParseInfo* p = new ParseInfo(isolate->allocator());
103 :
104 : p->InitFromIsolate(isolate);
105 : p->set_toplevel(shared->is_toplevel());
106 0 : p->set_allow_lazy_parsing(FLAG_lazy_inner_functions);
107 : p->set_is_named_expression(shared->is_named_expression());
108 : p->set_compiler_hints(shared->compiler_hints());
109 : p->set_start_position(shared->start_position());
110 : p->set_end_position(shared->end_position());
111 0 : p->function_literal_id_ = shared->function_literal_id();
112 : p->set_language_mode(shared->language_mode());
113 : p->set_shared_info(shared);
114 : p->set_module(shared->kind() == FunctionKind::kModule);
115 :
116 : // BUG(5946): This function exists as a workaround until we can
117 : // get rid of %SetCode in our native functions. The ParseInfo
118 : // is explicitly set up for the case that:
119 : // a) you have a native built-in,
120 : // b) it's being run for the 2nd-Nth time in an isolate,
121 : // c) we've already compiled bytecode and therefore don't need
122 : // to parse.
123 : // We tolerate a ParseInfo without a Script in this case.
124 : p->set_native(true);
125 : p->set_eval(false);
126 :
127 : Handle<HeapObject> scope_info(shared->outer_scope_info());
128 0 : if (!scope_info->IsTheHole(isolate) &&
129 : Handle<ScopeInfo>::cast(scope_info)->length() > 0) {
130 : p->set_outer_scope_info(Handle<ScopeInfo>::cast(scope_info));
131 : }
132 0 : return p;
133 : }
134 :
135 33746341 : DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }
136 :
137 1304247 : bool ParseInfo::is_declaration() const {
138 1304247 : return (compiler_hints_ & (1 << SharedFunctionInfo::kIsDeclaration)) != 0;
139 : }
140 :
141 2315103 : FunctionKind ParseInfo::function_kind() const {
142 4630206 : return SharedFunctionInfo::FunctionKindBits::decode(compiler_hints_);
143 : }
144 :
145 5 : void ParseInfo::set_deferred_handles(
146 : std::shared_ptr<DeferredHandles> deferred_handles) {
147 : DCHECK(deferred_handles_.get() == nullptr);
148 : deferred_handles_.swap(deferred_handles);
149 5 : }
150 :
151 153 : void ParseInfo::set_deferred_handles(DeferredHandles* deferred_handles) {
152 : DCHECK(deferred_handles_.get() == nullptr);
153 153 : deferred_handles_.reset(deferred_handles);
154 153 : }
155 :
156 19368392 : void ParseInfo::InitFromIsolate(Isolate* isolate) {
157 : DCHECK_NOT_NULL(isolate);
158 : set_hash_seed(isolate->heap()->HashSeed());
159 4842058 : set_stack_limit(isolate->stack_guard()->real_climit());
160 : set_unicode_cache(isolate->unicode_cache());
161 : set_tail_call_elimination_enabled(
162 : isolate->is_tail_call_elimination_enabled());
163 4842058 : set_runtime_call_stats(isolate->counters()->runtime_call_stats());
164 : set_ast_string_constants(isolate->ast_string_constants());
165 160 : }
166 :
167 : #ifdef DEBUG
168 : bool ParseInfo::script_is_native() const {
169 : return script_->type() == Script::TYPE_NATIVE;
170 : }
171 : #endif // DEBUG
172 :
173 : } // namespace internal
174 : } // namespace v8
|