Line data Source code
1 : // Copyright 2017 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 : #ifndef V8_OBJECTS_SHARED_FUNCTION_INFO_INL_H_
6 : #define V8_OBJECTS_SHARED_FUNCTION_INFO_INL_H_
7 :
8 : #include "src/objects/shared-function-info.h"
9 :
10 : #include "src/feedback-vector-inl.h"
11 : #include "src/handles-inl.h"
12 : #include "src/heap/heap-inl.h"
13 : #include "src/objects/debug-objects-inl.h"
14 : #include "src/objects/scope-info.h"
15 : #include "src/objects/templates.h"
16 : #include "src/wasm/wasm-objects-inl.h"
17 :
18 : // Has to be the last include (doesn't have include guards):
19 : #include "src/objects/object-macros.h"
20 :
21 : namespace v8 {
22 : namespace internal {
23 :
24 608212 : OBJECT_CONSTRUCTORS_IMPL(PreparseData, HeapObject)
25 :
26 304115 : CAST_ACCESSOR(PreparseData)
27 707308 : INT_ACCESSORS(PreparseData, data_length, kDataLengthOffset)
28 408707 : INT_ACCESSORS(PreparseData, children_length, kInnerLengthOffset)
29 :
30 : int PreparseData::inner_start_offset() const {
31 : return InnerOffset(data_length());
32 : }
33 :
34 : ObjectSlot PreparseData::inner_data_start() const {
35 : return RawField(inner_start_offset());
36 : }
37 :
38 63487 : void PreparseData::clear_padding() {
39 63487 : int data_end_offset = kDataStartOffset + data_length();
40 63487 : int padding_size = inner_start_offset() - data_end_offset;
41 : DCHECK_LE(0, padding_size);
42 126974 : if (padding_size == 0) return;
43 121134 : memset(reinterpret_cast<void*>(address() + data_end_offset), 0, padding_size);
44 : }
45 :
46 : byte PreparseData::get(int index) const {
47 : DCHECK_LE(0, index);
48 : DCHECK_LT(index, data_length());
49 1258534 : int offset = kDataStartOffset + index * kByteSize;
50 1258534 : return READ_BYTE_FIELD(*this, offset);
51 : }
52 :
53 : void PreparseData::set(int index, byte value) {
54 : DCHECK_LE(0, index);
55 : DCHECK_LT(index, data_length());
56 : int offset = kDataStartOffset + index * kByteSize;
57 : WRITE_BYTE_FIELD(*this, offset, value);
58 : }
59 :
60 : void PreparseData::copy_in(int index, const byte* buffer, int length) {
61 : DCHECK(index >= 0 && length >= 0 && length <= kMaxInt - index &&
62 : index + length <= this->data_length());
63 63487 : Address dst_addr = FIELD_ADDR(this, kDataStartOffset + index * kByteSize);
64 63487 : memcpy(reinterpret_cast<void*>(dst_addr), buffer, length);
65 : }
66 :
67 3220 : PreparseData PreparseData::get_child(int index) const {
68 6440 : return PreparseData::cast(get_child_raw(index));
69 : }
70 :
71 3220 : Object PreparseData::get_child_raw(int index) const {
72 : DCHECK_LE(0, index);
73 : DCHECK_LT(index, this->children_length());
74 3220 : int offset = inner_start_offset() + index * kTaggedSize;
75 6440 : return RELAXED_READ_FIELD(*this, offset);
76 : }
77 :
78 6970 : void PreparseData::set_child(int index, PreparseData value,
79 : WriteBarrierMode mode) {
80 : DCHECK_LE(0, index);
81 : DCHECK_LT(index, this->children_length());
82 6970 : int offset = inner_start_offset() + index * kTaggedSize;
83 6970 : RELAXED_WRITE_FIELD(*this, offset, value);
84 20910 : CONDITIONAL_WRITE_BARRIER(*this, offset, value, mode);
85 6970 : }
86 :
87 13012762 : OBJECT_CONSTRUCTORS_IMPL(UncompiledData, HeapObject)
88 19304474 : OBJECT_CONSTRUCTORS_IMPL(UncompiledDataWithoutPreparseData, UncompiledData)
89 308416 : OBJECT_CONSTRUCTORS_IMPL(UncompiledDataWithPreparseData, UncompiledData)
90 6506373 : CAST_ACCESSOR(UncompiledData)
91 13939071 : ACCESSORS(UncompiledData, inferred_name, String, kInferredNameOffset)
92 4883776 : INT32_ACCESSORS(UncompiledData, start_position, kStartPositionOffset)
93 3860216 : INT32_ACCESSORS(UncompiledData, end_position, kEndPositionOffset)
94 2655499 : INT32_ACCESSORS(UncompiledData, function_literal_id, kFunctionLiteralIdOffset)
95 :
96 2043940 : void UncompiledData::clear_padding() {
97 2043940 : if (FIELD_SIZE(kOptionalPaddingOffset) == 0) return;
98 : DCHECK_EQ(4, FIELD_SIZE(kOptionalPaddingOffset));
99 2043940 : memset(reinterpret_cast<void*>(address() + kOptionalPaddingOffset), 0,
100 2043940 : FIELD_SIZE(kOptionalPaddingOffset));
101 : }
102 :
103 9651939 : CAST_ACCESSOR(UncompiledDataWithoutPreparseData)
104 :
105 154208 : CAST_ACCESSOR(UncompiledDataWithPreparseData)
106 408083 : ACCESSORS(UncompiledDataWithPreparseData, preparse_data, PreparseData,
107 : kPreparseDataOffset)
108 :
109 350 : OBJECT_CONSTRUCTORS_IMPL(InterpreterData, Struct)
110 :
111 175 : CAST_ACCESSOR(InterpreterData)
112 439 : ACCESSORS(InterpreterData, bytecode_array, BytecodeArray, kBytecodeArrayOffset)
113 331 : ACCESSORS(InterpreterData, interpreter_trampoline, Code,
114 : kInterpreterTrampolineOffset)
115 :
116 617260898 : OBJECT_CONSTRUCTORS_IMPL(SharedFunctionInfo, HeapObject)
117 : NEVER_READ_ONLY_SPACE_IMPL(SharedFunctionInfo)
118 308644824 : CAST_ACCESSOR(SharedFunctionInfo)
119 490524 : DEFINE_DEOPT_ELEMENT_ACCESSORS(SharedFunctionInfo, Object)
120 :
121 235497310 : ACCESSORS(SharedFunctionInfo, name_or_scope_info, Object,
122 : kNameOrScopeInfoOffset)
123 336520848 : ACCESSORS(SharedFunctionInfo, script_or_debug_info, Object,
124 : kScriptOrDebugInfoOffset)
125 :
126 : #if V8_SFI_HAS_UNIQUE_ID
127 : INT_ACCESSORS(SharedFunctionInfo, unique_id, kUniqueIdOffset)
128 : #endif
129 23018702 : UINT16_ACCESSORS(SharedFunctionInfo, length, kLengthOffset)
130 36189020 : UINT16_ACCESSORS(SharedFunctionInfo, internal_formal_parameter_count,
131 : kFormalParameterCountOffset)
132 13820371 : UINT8_ACCESSORS(SharedFunctionInfo, expected_nof_properties,
133 : kExpectedNofPropertiesOffset)
134 23861631 : UINT8_ACCESSORS(SharedFunctionInfo, raw_builtin_function_id, kBuiltinFunctionId)
135 14856680 : UINT16_ACCESSORS(SharedFunctionInfo, raw_function_token_offset,
136 : kFunctionTokenOffsetOffset)
137 330245274 : RELAXED_INT32_ACCESSORS(SharedFunctionInfo, flags, kFlagsOffset)
138 :
139 28508991 : bool SharedFunctionInfo::HasSharedName() const {
140 28508991 : Object value = name_or_scope_info();
141 28509036 : if (value->IsScopeInfo()) {
142 3162963 : return ScopeInfo::cast(value)->HasSharedFunctionName();
143 : }
144 25346073 : return value != kNoSharedNameSentinel;
145 : }
146 :
147 9742892 : String SharedFunctionInfo::Name() const {
148 9754715 : if (!HasSharedName()) return GetReadOnlyRoots().empty_string();
149 9731104 : Object value = name_or_scope_info();
150 9731108 : if (value->IsScopeInfo()) {
151 3092651 : if (ScopeInfo::cast(value)->HasFunctionName()) {
152 6185301 : return String::cast(ScopeInfo::cast(value)->FunctionName());
153 : }
154 0 : return GetReadOnlyRoots().empty_string();
155 : }
156 : return String::cast(value);
157 : }
158 :
159 640 : void SharedFunctionInfo::SetName(String name) {
160 640 : Object maybe_scope_info = name_or_scope_info();
161 640 : if (maybe_scope_info->IsScopeInfo()) {
162 5 : ScopeInfo::cast(maybe_scope_info)->SetFunctionName(name);
163 : } else {
164 : DCHECK(maybe_scope_info->IsString() ||
165 : maybe_scope_info == kNoSharedNameSentinel);
166 635 : set_name_or_scope_info(name);
167 : }
168 640 : UpdateFunctionMapIndex();
169 640 : }
170 :
171 3590267 : AbstractCode SharedFunctionInfo::abstract_code() {
172 3590267 : if (HasBytecodeArray()) {
173 5145454 : return AbstractCode::cast(GetBytecodeArray());
174 : } else {
175 2035080 : return AbstractCode::cast(GetCode());
176 : }
177 : }
178 :
179 : Object SharedFunctionInfo::function_data() const {
180 312059021 : return RELAXED_READ_FIELD(*this, kFunctionDataOffset);
181 : }
182 :
183 13681499 : void SharedFunctionInfo::set_function_data(Object data, WriteBarrierMode mode) {
184 13681499 : RELAXED_WRITE_FIELD(*this, kFunctionDataOffset, data);
185 30018333 : CONDITIONAL_WRITE_BARRIER(*this, kFunctionDataOffset, data, mode);
186 13681520 : }
187 :
188 1783471 : int SharedFunctionInfo::function_token_position() const {
189 1783471 : int offset = raw_function_token_offset();
190 1783471 : if (offset == kFunctionTokenOutOfRange) {
191 : return kNoSourcePosition;
192 : } else {
193 1783399 : return StartPosition() - offset;
194 : }
195 : }
196 :
197 29258038 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_wrapped,
198 : SharedFunctionInfo::IsWrappedBit)
199 8817140 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, allows_lazy_compilation,
200 : SharedFunctionInfo::AllowLazyCompilationBit)
201 21308440 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, has_duplicate_parameters,
202 : SharedFunctionInfo::HasDuplicateParametersBit)
203 8567310 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_declaration,
204 : SharedFunctionInfo::IsDeclarationBit)
205 :
206 3737272 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, native,
207 : SharedFunctionInfo::IsNativeBit)
208 1439026 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_asm_wasm_broken,
209 : SharedFunctionInfo::IsAsmWasmBrokenBit)
210 8567320 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags,
211 : requires_instance_members_initializer,
212 : SharedFunctionInfo::RequiresInstanceMembersInitializer)
213 :
214 2428386 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, name_should_print_as_anonymous,
215 : SharedFunctionInfo::NameShouldPrintAsAnonymousBit)
216 7128692 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_anonymous_expression,
217 : SharedFunctionInfo::IsAnonymousExpressionBit)
218 40580 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, has_reported_binary_coverage,
219 : SharedFunctionInfo::HasReportedBinaryCoverageBit)
220 :
221 8567308 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_named_expression,
222 : SharedFunctionInfo::IsNamedExpressionBit)
223 35594492 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_toplevel,
224 : SharedFunctionInfo::IsTopLevelBit)
225 4178710 : BIT_FIELD_ACCESSORS(SharedFunctionInfo, flags, is_iife,
226 : SharedFunctionInfo::IsIIFEBit)
227 :
228 2278458 : bool SharedFunctionInfo::optimization_disabled() const {
229 2278458 : return disable_optimization_reason() != BailoutReason::kNoReason;
230 : }
231 :
232 12027 : BailoutReason SharedFunctionInfo::disable_optimization_reason() const {
233 12027 : return DisabledOptimizationReasonBits::decode(flags());
234 : }
235 :
236 35614152 : LanguageMode SharedFunctionInfo::language_mode() const {
237 : STATIC_ASSERT(LanguageModeSize == 2);
238 35614152 : return construct_language_mode(IsStrictBit::decode(flags()));
239 : }
240 :
241 5509639 : void SharedFunctionInfo::set_language_mode(LanguageMode language_mode) {
242 : STATIC_ASSERT(LanguageModeSize == 2);
243 : // We only allow language mode transitions that set the same language mode
244 : // again or go up in the chain:
245 : DCHECK(is_sloppy(this->language_mode()) || is_strict(language_mode));
246 : int hints = flags();
247 : hints = IsStrictBit::update(hints, is_strict(language_mode));
248 : set_flags(hints);
249 5509639 : UpdateFunctionMapIndex();
250 5509642 : }
251 :
252 21854 : FunctionKind SharedFunctionInfo::kind() const {
253 21854 : return FunctionKindBits::decode(flags());
254 : }
255 :
256 9507732 : void SharedFunctionInfo::set_kind(FunctionKind kind) {
257 : int hints = flags();
258 : hints = FunctionKindBits::update(hints, kind);
259 : hints = IsClassConstructorBit::update(hints, IsClassConstructor(kind));
260 : hints = IsDerivedConstructorBit::update(hints, IsDerivedConstructor(kind));
261 : set_flags(hints);
262 9507732 : UpdateFunctionMapIndex();
263 9507738 : }
264 :
265 19059582 : bool SharedFunctionInfo::needs_home_object() const {
266 19059582 : return NeedsHomeObjectBit::decode(flags());
267 : }
268 :
269 3564335 : void SharedFunctionInfo::set_needs_home_object(bool value) {
270 : int hints = flags();
271 : hints = NeedsHomeObjectBit::update(hints, value);
272 : set_flags(hints);
273 3564335 : UpdateFunctionMapIndex();
274 3564347 : }
275 :
276 7049301 : bool SharedFunctionInfo::construct_as_builtin() const {
277 7049301 : return ConstructAsBuiltinBit::decode(flags());
278 : }
279 :
280 9507716 : void SharedFunctionInfo::CalculateConstructAsBuiltin() {
281 : bool uses_builtins_construct_stub = false;
282 9507716 : if (HasBuiltinId()) {
283 5513084 : int id = builtin_id();
284 5513099 : if (id != Builtins::kCompileLazy && id != Builtins::kEmptyFunction) {
285 : uses_builtins_construct_stub = true;
286 : }
287 3994630 : } else if (IsApiFunction()) {
288 : uses_builtins_construct_stub = true;
289 : }
290 :
291 : int f = flags();
292 : f = ConstructAsBuiltinBit::update(f, uses_builtins_construct_stub);
293 : set_flags(f);
294 9507731 : }
295 :
296 20353664 : int SharedFunctionInfo::function_map_index() const {
297 : // Note: Must be kept in sync with the FastNewClosure builtin.
298 : int index =
299 20353664 : Context::FIRST_FUNCTION_MAP_INDEX + FunctionMapIndexBits::decode(flags());
300 : DCHECK_LE(index, Context::LAST_FUNCTION_MAP_INDEX);
301 20353664 : return index;
302 : }
303 :
304 18582575 : void SharedFunctionInfo::set_function_map_index(int index) {
305 : STATIC_ASSERT(Context::LAST_FUNCTION_MAP_INDEX <=
306 : Context::FIRST_FUNCTION_MAP_INDEX + FunctionMapIndexBits::kMax);
307 : DCHECK_LE(Context::FIRST_FUNCTION_MAP_INDEX, index);
308 : DCHECK_LE(index, Context::LAST_FUNCTION_MAP_INDEX);
309 18582575 : index -= Context::FIRST_FUNCTION_MAP_INDEX;
310 : set_flags(FunctionMapIndexBits::update(flags(), index));
311 18582575 : }
312 :
313 9507738 : void SharedFunctionInfo::clear_padding() {
314 9507738 : memset(reinterpret_cast<void*>(this->address() + kSize), 0,
315 9507738 : kAlignedSize - kSize);
316 9507738 : }
317 :
318 18582327 : void SharedFunctionInfo::UpdateFunctionMapIndex() {
319 : int map_index = Context::FunctionMapIndex(
320 37164663 : language_mode(), kind(), true, HasSharedName(), needs_home_object());
321 18582354 : set_function_map_index(map_index);
322 18582357 : }
323 :
324 : void SharedFunctionInfo::DontAdaptArguments() {
325 : // TODO(leszeks): Revise this DCHECK now that the code field is gone.
326 : DCHECK(!HasWasmExportedFunctionData());
327 : set_internal_formal_parameter_count(kDontAdaptArgumentsSentinel);
328 : }
329 :
330 546153 : bool SharedFunctionInfo::IsInterpreted() const { return HasBytecodeArray(); }
331 :
332 3948838 : ScopeInfo SharedFunctionInfo::scope_info() const {
333 3948838 : Object maybe_scope_info = name_or_scope_info();
334 3948842 : if (maybe_scope_info->IsScopeInfo()) {
335 : return ScopeInfo::cast(maybe_scope_info);
336 : }
337 3564362 : return ScopeInfo::Empty(GetIsolate());
338 : }
339 :
340 2089479 : void SharedFunctionInfo::set_scope_info(ScopeInfo scope_info,
341 : WriteBarrierMode mode) {
342 : // Move the existing name onto the ScopeInfo.
343 2089479 : Object name = name_or_scope_info();
344 2089500 : if (name->IsScopeInfo()) {
345 301 : name = ScopeInfo::cast(name)->FunctionName();
346 : }
347 : DCHECK(name->IsString() || name == kNoSharedNameSentinel);
348 : // Only set the function name for function scopes.
349 2089500 : scope_info->SetFunctionName(name);
350 2658066 : if (HasInferredName() && inferred_name()->length() != 0) {
351 53050 : scope_info->SetInferredFunctionName(inferred_name());
352 : }
353 2089471 : WRITE_FIELD(*this, kNameOrScopeInfoOffset, scope_info);
354 6268419 : CONDITIONAL_WRITE_BARRIER(*this, kNameOrScopeInfoOffset, scope_info, mode);
355 2089485 : }
356 :
357 210215563 : ACCESSORS(SharedFunctionInfo, raw_outer_scope_info_or_feedback_metadata,
358 : HeapObject, kOuterScopeInfoOrFeedbackMetadataOffset)
359 :
360 : HeapObject SharedFunctionInfo::outer_scope_info() const {
361 : DCHECK(!is_compiled());
362 : DCHECK(!HasFeedbackMetadata());
363 1471338 : return raw_outer_scope_info_or_feedback_metadata();
364 : }
365 :
366 719309 : bool SharedFunctionInfo::HasOuterScopeInfo() const {
367 : ScopeInfo outer_info;
368 719309 : if (!is_compiled()) {
369 1222906 : if (!outer_scope_info()->IsScopeInfo()) return false;
370 : outer_info = ScopeInfo::cast(outer_scope_info());
371 : } else {
372 107857 : if (!scope_info()->HasOuterScopeInfo()) return false;
373 21006 : outer_info = scope_info()->OuterScopeInfo();
374 : }
375 460043 : return outer_info->length() > 0;
376 : }
377 :
378 441863 : ScopeInfo SharedFunctionInfo::GetOuterScopeInfo() const {
379 : DCHECK(HasOuterScopeInfo());
380 441863 : if (!is_compiled()) return ScopeInfo::cast(outer_scope_info());
381 21006 : return scope_info()->OuterScopeInfo();
382 : }
383 :
384 : void SharedFunctionInfo::set_outer_scope_info(HeapObject value,
385 : WriteBarrierMode mode) {
386 : DCHECK(!is_compiled());
387 : DCHECK(raw_outer_scope_info_or_feedback_metadata()->IsTheHole());
388 : DCHECK(value->IsScopeInfo() || value->IsTheHole());
389 2161213 : set_raw_outer_scope_info_or_feedback_metadata(value, mode);
390 : }
391 :
392 3076244 : bool SharedFunctionInfo::HasFeedbackMetadata() const {
393 6152487 : return raw_outer_scope_info_or_feedback_metadata()->IsFeedbackMetadata();
394 : }
395 :
396 48615456 : FeedbackMetadata SharedFunctionInfo::feedback_metadata() const {
397 : DCHECK(HasFeedbackMetadata());
398 97230949 : return FeedbackMetadata::cast(raw_outer_scope_info_or_feedback_metadata());
399 : }
400 :
401 : void SharedFunctionInfo::set_feedback_metadata(FeedbackMetadata value,
402 : WriteBarrierMode mode) {
403 : DCHECK(!HasFeedbackMetadata());
404 : DCHECK(value->IsFeedbackMetadata());
405 2089371 : set_raw_outer_scope_info_or_feedback_metadata(value, mode);
406 : }
407 :
408 49130131 : bool SharedFunctionInfo::is_compiled() const {
409 49130131 : Object data = function_data();
410 96739699 : return data != Smi::FromEnum(Builtins::kCompileLazy) &&
411 49130169 : !data->IsUncompiledData();
412 : }
413 :
414 21554126 : IsCompiledScope SharedFunctionInfo::is_compiled_scope() const {
415 21554150 : return IsCompiledScope(*this, GetIsolate());
416 : }
417 :
418 21554149 : IsCompiledScope::IsCompiledScope(const SharedFunctionInfo shared,
419 : Isolate* isolate)
420 21554149 : : retain_bytecode_(shared->HasBytecodeArray()
421 : ? handle(shared->GetBytecodeArray(), isolate)
422 : : MaybeHandle<BytecodeArray>()),
423 29327062 : is_compiled_(shared->is_compiled()) {
424 : DCHECK_IMPLIES(!retain_bytecode_.is_null(), is_compiled());
425 21554167 : }
426 :
427 : uint16_t SharedFunctionInfo::GetLength() const {
428 : DCHECK(is_compiled());
429 : DCHECK(HasLength());
430 : return length();
431 : }
432 :
433 : bool SharedFunctionInfo::HasLength() const {
434 : return length() != kInvalidLength;
435 : }
436 :
437 63562 : bool SharedFunctionInfo::has_simple_parameters() {
438 127124 : return scope_info()->HasSimpleParameters();
439 : }
440 :
441 32934324 : bool SharedFunctionInfo::IsApiFunction() const {
442 65868675 : return function_data()->IsFunctionTemplateInfo();
443 : }
444 :
445 5010270 : FunctionTemplateInfo SharedFunctionInfo::get_api_func_data() {
446 : DCHECK(IsApiFunction());
447 5010271 : return FunctionTemplateInfo::cast(function_data());
448 : }
449 :
450 50620472 : bool SharedFunctionInfo::HasBytecodeArray() const {
451 177441700 : return function_data()->IsBytecodeArray() ||
452 126821228 : function_data()->IsInterpreterData();
453 : }
454 :
455 18844155 : BytecodeArray SharedFunctionInfo::GetBytecodeArray() const {
456 : DCHECK(HasBytecodeArray());
457 18844155 : if (HasDebugInfo() && GetDebugInfo()->HasInstrumentedBytecodeArray()) {
458 130856 : return GetDebugInfo()->OriginalBytecodeArray();
459 37426678 : } else if (function_data()->IsBytecodeArray()) {
460 : return BytecodeArray::cast(function_data());
461 : } else {
462 : DCHECK(function_data()->IsInterpreterData());
463 88 : return InterpreterData::cast(function_data())->bytecode_array();
464 : }
465 : }
466 :
467 : BytecodeArray SharedFunctionInfo::GetDebugBytecodeArray() const {
468 : DCHECK(HasBytecodeArray());
469 : DCHECK(HasDebugInfo() && GetDebugInfo()->HasInstrumentedBytecodeArray());
470 : if (function_data()->IsBytecodeArray()) {
471 : return BytecodeArray::cast(function_data());
472 : } else {
473 : DCHECK(function_data()->IsInterpreterData());
474 : return InterpreterData::cast(function_data())->bytecode_array();
475 : }
476 : }
477 :
478 21076 : void SharedFunctionInfo::SetDebugBytecodeArray(BytecodeArray bytecode) {
479 : DCHECK(HasBytecodeArray());
480 42152 : if (function_data()->IsBytecodeArray()) {
481 21076 : set_function_data(bytecode);
482 : } else {
483 : DCHECK(function_data()->IsInterpreterData());
484 0 : interpreter_data()->set_bytecode_array(bytecode);
485 : }
486 21076 : }
487 :
488 : void SharedFunctionInfo::set_bytecode_array(BytecodeArray bytecode) {
489 : DCHECK(function_data() == Smi::FromEnum(Builtins::kCompileLazy) ||
490 : HasUncompiledData());
491 2086350 : set_function_data(bytecode);
492 : }
493 :
494 67992779 : bool SharedFunctionInfo::ShouldFlushBytecode() {
495 67992779 : if (!FLAG_flush_bytecode) return false;
496 :
497 : // TODO(rmcilroy): Enable bytecode flushing for resumable functions amd class
498 : // member initializers.
499 214580 : if (IsResumableFunction(kind()) ||
500 143100 : IsClassMembersInitializerFunction(kind()) || !allows_lazy_compilation()) {
501 : return false;
502 : }
503 :
504 : // Get a snapshot of the function data field, and if it is a bytecode array,
505 : // check if it is old. Note, this is done this way since this function can be
506 : // called by the concurrent marker.
507 2142 : Object data = function_data();
508 2142 : if (!data->IsBytecodeArray()) return false;
509 :
510 264 : if (FLAG_stress_flush_bytecode) return true;
511 :
512 104 : BytecodeArray bytecode = BytecodeArray::cast(data);
513 :
514 104 : return bytecode->IsOld();
515 : }
516 :
517 : Code SharedFunctionInfo::InterpreterTrampoline() const {
518 : DCHECK(HasInterpreterData());
519 52 : return interpreter_data()->interpreter_trampoline();
520 : }
521 :
522 : bool SharedFunctionInfo::HasInterpreterData() const {
523 : return function_data()->IsInterpreterData();
524 : }
525 :
526 52 : InterpreterData SharedFunctionInfo::interpreter_data() const {
527 : DCHECK(HasInterpreterData());
528 52 : return InterpreterData::cast(function_data());
529 : }
530 :
531 : void SharedFunctionInfo::set_interpreter_data(
532 : InterpreterData interpreter_data) {
533 : DCHECK(FLAG_interpreted_frames_native_stack);
534 35 : set_function_data(interpreter_data);
535 : }
536 :
537 7593632 : bool SharedFunctionInfo::HasAsmWasmData() const {
538 15187267 : return function_data()->IsAsmWasmData();
539 : }
540 :
541 5636 : AsmWasmData SharedFunctionInfo::asm_wasm_data() const {
542 : DCHECK(HasAsmWasmData());
543 5636 : return AsmWasmData::cast(function_data());
544 : }
545 :
546 : void SharedFunctionInfo::set_asm_wasm_data(AsmWasmData data) {
547 : DCHECK(function_data() == Smi::FromEnum(Builtins::kCompileLazy) ||
548 : HasUncompiledData() || HasAsmWasmData());
549 2985 : set_function_data(data);
550 : }
551 :
552 24997838 : bool SharedFunctionInfo::HasBuiltinId() const {
553 49995683 : return function_data()->IsSmi();
554 : }
555 :
556 15025547 : int SharedFunctionInfo::builtin_id() const {
557 : DCHECK(HasBuiltinId());
558 15025547 : int id = Smi::ToInt(function_data());
559 : DCHECK(Builtins::IsBuiltinId(id));
560 15025554 : return id;
561 : }
562 :
563 : void SharedFunctionInfo::set_builtin_id(int builtin_id) {
564 : DCHECK(Builtins::IsBuiltinId(builtin_id));
565 5513100 : set_function_data(Smi::FromInt(builtin_id), SKIP_WRITE_BARRIER);
566 : }
567 :
568 10518763 : bool SharedFunctionInfo::HasUncompiledData() const {
569 21037559 : return function_data()->IsUncompiledData();
570 : }
571 :
572 6506262 : UncompiledData SharedFunctionInfo::uncompiled_data() const {
573 : DCHECK(HasUncompiledData());
574 6506275 : return UncompiledData::cast(function_data());
575 : }
576 :
577 : void SharedFunctionInfo::set_uncompiled_data(UncompiledData uncompiled_data) {
578 : DCHECK(function_data() == Smi::FromEnum(Builtins::kCompileLazy));
579 : DCHECK(uncompiled_data->IsUncompiledData());
580 2043586 : set_function_data(uncompiled_data);
581 : }
582 :
583 736702 : bool SharedFunctionInfo::HasUncompiledDataWithPreparseData() const {
584 1473404 : return function_data()->IsUncompiledDataWithPreparseData();
585 : }
586 :
587 : UncompiledDataWithPreparseData
588 37137 : SharedFunctionInfo::uncompiled_data_with_preparse_data() const {
589 : DCHECK(HasUncompiledDataWithPreparseData());
590 37137 : return UncompiledDataWithPreparseData::cast(function_data());
591 : }
592 :
593 : void SharedFunctionInfo::set_uncompiled_data_with_preparse_data(
594 : UncompiledDataWithPreparseData uncompiled_data_with_preparse_data) {
595 : DCHECK(function_data() == Smi::FromEnum(Builtins::kCompileLazy));
596 : DCHECK(
597 : uncompiled_data_with_preparse_data->IsUncompiledDataWithPreparseData());
598 : set_function_data(uncompiled_data_with_preparse_data);
599 : }
600 :
601 : bool SharedFunctionInfo::HasUncompiledDataWithoutPreparseData() const {
602 : return function_data()->IsUncompiledDataWithoutPreparseData();
603 : }
604 :
605 21 : void SharedFunctionInfo::ClearPreparseData() {
606 : DCHECK(HasUncompiledDataWithPreparseData());
607 21 : UncompiledDataWithPreparseData data = uncompiled_data_with_preparse_data();
608 :
609 : // Trim off the pre-parsed scope data from the uncompiled data by swapping the
610 : // map, leaving only an uncompiled data without pre-parsed scope.
611 : DisallowHeapAllocation no_gc;
612 : Heap* heap = Heap::FromWritableHeapObject(data);
613 :
614 : // Swap the map.
615 : heap->NotifyObjectLayoutChange(data, UncompiledDataWithPreparseData::kSize,
616 21 : no_gc);
617 : STATIC_ASSERT(UncompiledDataWithoutPreparseData::kSize <
618 : UncompiledDataWithPreparseData::kSize);
619 : STATIC_ASSERT(UncompiledDataWithoutPreparseData::kSize ==
620 : UncompiledData::kSize);
621 : data->synchronized_set_map(
622 42 : GetReadOnlyRoots().uncompiled_data_without_preparse_data_map());
623 :
624 : // Fill the remaining space with filler.
625 : heap->CreateFillerObjectAt(
626 : data->address() + UncompiledDataWithoutPreparseData::kSize,
627 : UncompiledDataWithPreparseData::kSize -
628 : UncompiledDataWithoutPreparseData::kSize,
629 21 : ClearRecordedSlots::kNo);
630 :
631 : // Ensure that the clear was successful.
632 : DCHECK(HasUncompiledDataWithoutPreparseData());
633 21 : }
634 :
635 : // static
636 2043943 : void UncompiledData::Initialize(
637 : UncompiledData data, String inferred_name, int start_position,
638 : int end_position, int function_literal_id,
639 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
640 : gc_notify_updated_slot) {
641 2043943 : data->set_inferred_name(inferred_name);
642 : gc_notify_updated_slot(
643 2043943 : data, data->RawField(UncompiledData::kInferredNameOffset), inferred_name);
644 : data->set_start_position(start_position);
645 : data->set_end_position(end_position);
646 : data->set_function_literal_id(function_literal_id);
647 2043941 : data->clear_padding();
648 2043940 : }
649 :
650 59347 : void UncompiledDataWithPreparseData::Initialize(
651 : UncompiledDataWithPreparseData data, String inferred_name,
652 : int start_position, int end_position, int function_literal_id,
653 : PreparseData scope_data,
654 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
655 : gc_notify_updated_slot) {
656 : UncompiledData::Initialize(data, inferred_name, start_position, end_position,
657 118694 : function_literal_id, gc_notify_updated_slot);
658 59347 : data->set_preparse_data(scope_data);
659 : gc_notify_updated_slot(
660 : data, data->RawField(UncompiledDataWithPreparseData::kPreparseDataOffset),
661 59347 : scope_data);
662 59347 : }
663 :
664 : bool SharedFunctionInfo::HasWasmExportedFunctionData() const {
665 : return function_data()->IsWasmExportedFunctionData();
666 : }
667 :
668 61157725 : Object SharedFunctionInfo::script() const {
669 61157725 : Object maybe_script = script_or_debug_info();
670 61157777 : if (maybe_script->IsDebugInfo()) {
671 2362366 : return DebugInfo::cast(maybe_script)->script();
672 : }
673 58795411 : return maybe_script;
674 : }
675 :
676 3565604 : void SharedFunctionInfo::set_script(Object script) {
677 3565604 : Object maybe_debug_info = script_or_debug_info();
678 3565616 : if (maybe_debug_info->IsDebugInfo()) {
679 83 : DebugInfo::cast(maybe_debug_info)->set_script(script);
680 : } else {
681 3565533 : set_script_or_debug_info(script);
682 : }
683 3565609 : }
684 :
685 29419428 : bool SharedFunctionInfo::HasDebugInfo() const {
686 58838916 : return script_or_debug_info()->IsDebugInfo();
687 : }
688 :
689 2168333 : DebugInfo SharedFunctionInfo::GetDebugInfo() const {
690 : DCHECK(HasDebugInfo());
691 4336666 : return DebugInfo::cast(script_or_debug_info());
692 : }
693 :
694 : void SharedFunctionInfo::SetDebugInfo(DebugInfo debug_info) {
695 : DCHECK(!HasDebugInfo());
696 : DCHECK_EQ(debug_info->script(), script_or_debug_info());
697 28764 : set_script_or_debug_info(debug_info);
698 : }
699 :
700 : bool SharedFunctionInfo::HasBuiltinFunctionId() {
701 7044140 : return builtin_function_id() != BuiltinFunctionId::kInvalidBuiltinFunctionId;
702 : }
703 :
704 : BuiltinFunctionId SharedFunctionInfo::builtin_function_id() {
705 : return static_cast<BuiltinFunctionId>(raw_builtin_function_id());
706 : }
707 :
708 : void SharedFunctionInfo::set_builtin_function_id(BuiltinFunctionId id) {
709 : set_raw_builtin_function_id(static_cast<uint8_t>(id));
710 : }
711 :
712 2089469 : bool SharedFunctionInfo::HasInferredName() {
713 2089469 : Object scope_info = name_or_scope_info();
714 2089482 : if (scope_info->IsScopeInfo()) {
715 301 : return ScopeInfo::cast(scope_info)->HasInferredFunctionName();
716 : }
717 2089181 : return HasUncompiledData();
718 : }
719 :
720 3286384 : String SharedFunctionInfo::inferred_name() {
721 3286384 : Object maybe_scope_info = name_or_scope_info();
722 3286405 : if (maybe_scope_info->IsScopeInfo()) {
723 1383818 : ScopeInfo scope_info = ScopeInfo::cast(maybe_scope_info);
724 1383818 : if (scope_info->HasInferredFunctionName()) {
725 617430 : Object name = ScopeInfo::cast(maybe_scope_info)->InferredFunctionName();
726 632253 : if (name->IsString()) return String::cast(name);
727 : }
728 1902586 : } else if (HasUncompiledData()) {
729 1236842 : return uncompiled_data()->inferred_name();
730 : }
731 4069470 : return GetReadOnlyRoots().empty_string();
732 : }
733 :
734 11900697 : bool SharedFunctionInfo::IsUserJavaScript() {
735 11900697 : Object script_obj = script();
736 11900715 : if (script_obj->IsUndefined()) return false;
737 9147660 : Script script = Script::cast(script_obj);
738 9937818 : return script->IsUserJavaScript();
739 : }
740 :
741 4575691 : bool SharedFunctionInfo::IsSubjectToDebugging() {
742 4575691 : return IsUserJavaScript() && !HasAsmWasmData();
743 : }
744 :
745 148579 : bool SharedFunctionInfo::CanDiscardCompiled() const {
746 297128 : bool can_decompile = (HasBytecodeArray() || HasAsmWasmData() ||
747 148549 : HasUncompiledDataWithPreparseData());
748 148579 : return can_decompile;
749 : }
750 :
751 : } // namespace internal
752 : } // namespace v8
753 :
754 : #include "src/objects/object-macros-undef.h"
755 :
756 : #endif // V8_OBJECTS_SHARED_FUNCTION_INFO_INL_H_
|