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_H_
6 : #define V8_OBJECTS_SHARED_FUNCTION_INFO_H_
7 :
8 : #include "src/bailout-reason.h"
9 : #include "src/objects.h"
10 : #include "src/objects/builtin-function-id.h"
11 : #include "src/objects/script.h"
12 : #include "src/objects/smi.h"
13 : #include "src/objects/struct.h"
14 :
15 : // Has to be the last include (doesn't have include guards):
16 : #include "src/objects/object-macros.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 : class AsmWasmData;
22 : class BytecodeArray;
23 : class CoverageInfo;
24 : class DebugInfo;
25 : class IsCompiledScope;
26 : class WasmExportedFunctionData;
27 :
28 : // Data collected by the pre-parser storing information about scopes and inner
29 : // functions.
30 : //
31 : // PreparseData Layout:
32 : // +-------------------------------+
33 : // | data_length | children_length |
34 : // +-------------------------------+
35 : // | Scope Byte Data ... |
36 : // | ... |
37 : // +-------------------------------+
38 : // | [Padding] |
39 : // +-------------------------------+
40 : // | Inner PreparseData 1 |
41 : // +-------------------------------+
42 : // | ... |
43 : // +-------------------------------+
44 : // | Inner PreparseData N |
45 : // +-------------------------------+
46 : class PreparseData : public HeapObject {
47 : public:
48 : DECL_INT_ACCESSORS(data_length)
49 : DECL_INT_ACCESSORS(children_length)
50 :
51 : inline int inner_start_offset() const;
52 : inline ObjectSlot inner_data_start() const;
53 :
54 : inline byte get(int index) const;
55 : inline void set(int index, byte value);
56 : inline void copy_in(int index, const byte* buffer, int length);
57 :
58 : inline PreparseData get_child(int index) const;
59 : inline void set_child(int index, PreparseData value,
60 : WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
61 :
62 : // Clear uninitialized padding space.
63 : inline void clear_padding();
64 :
65 : DECL_CAST(PreparseData)
66 : DECL_PRINTER(PreparseData)
67 : DECL_VERIFIER(PreparseData)
68 :
69 : // Layout description.
70 : #define PREPARSE_DATA_FIELDS(V) \
71 : V(kDataLengthOffset, kInt32Size) \
72 : V(kInnerLengthOffset, kInt32Size) \
73 : /* Header size. */ \
74 : V(kDataStartOffset, 0) \
75 : V(kHeaderSize, 0)
76 :
77 : DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize, PREPARSE_DATA_FIELDS)
78 : #undef PREPARSE_DATA_FIELDS
79 :
80 : class BodyDescriptor;
81 :
82 : static int InnerOffset(int data_length) {
83 482379 : return RoundUp(kDataStartOffset + data_length * kByteSize, kTaggedSize);
84 : }
85 :
86 : static int SizeFor(int data_length, int children_length) {
87 340616 : return InnerOffset(data_length) + children_length * kTaggedSize;
88 : }
89 :
90 55499 : OBJECT_CONSTRUCTORS(PreparseData, HeapObject);
91 :
92 : private:
93 : inline Object get_child_raw(int index) const;
94 : };
95 :
96 : // Abstract class representing extra data for an uncompiled function, which is
97 : // not stored in the SharedFunctionInfo.
98 : class UncompiledData : public HeapObject {
99 : public:
100 : DECL_ACCESSORS(inferred_name, String)
101 : DECL_INT32_ACCESSORS(start_position)
102 : DECL_INT32_ACCESSORS(end_position)
103 : DECL_INT32_ACCESSORS(function_literal_id)
104 :
105 : DECL_CAST(UncompiledData)
106 :
107 : inline static void Initialize(
108 : UncompiledData data, String inferred_name, int start_position,
109 : int end_position, int function_literal_id,
110 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
111 : gc_notify_updated_slot =
112 : [](HeapObject object, ObjectSlot slot, HeapObject target) {});
113 :
114 : // Layout description.
115 : #define UNCOMPILED_DATA_FIELDS(V) \
116 : V(kStartOfPointerFieldsOffset, 0) \
117 : V(kInferredNameOffset, kTaggedSize) \
118 : V(kEndOfTaggedFieldsOffset, 0) \
119 : /* Raw data fields. */ \
120 : V(kStartPositionOffset, kInt32Size) \
121 : V(kEndPositionOffset, kInt32Size) \
122 : V(kFunctionLiteralIdOffset, kInt32Size) \
123 : V(kOptionalPaddingOffset, POINTER_SIZE_PADDING(kOptionalPaddingOffset)) \
124 : /* Header size. */ \
125 : V(kSize, 0)
126 :
127 : DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize, UNCOMPILED_DATA_FIELDS)
128 : #undef UNCOMPILED_DATA_FIELDS
129 :
130 : typedef FixedBodyDescriptor<kStartOfPointerFieldsOffset,
131 : kEndOfTaggedFieldsOffset, kSize>
132 : BodyDescriptor;
133 :
134 : // Clear uninitialized padding space.
135 : inline void clear_padding();
136 :
137 : OBJECT_CONSTRUCTORS(UncompiledData, HeapObject);
138 : };
139 :
140 : // Class representing data for an uncompiled function that does not have any
141 : // data from the pre-parser, either because it's a leaf function or because the
142 : // pre-parser bailed out.
143 : class UncompiledDataWithoutPreparseData : public UncompiledData {
144 : public:
145 : DECL_CAST(UncompiledDataWithoutPreparseData)
146 : DECL_PRINTER(UncompiledDataWithoutPreparseData)
147 : DECL_VERIFIER(UncompiledDataWithoutPreparseData)
148 :
149 : static const int kSize = UncompiledData::kSize;
150 :
151 : // No extra fields compared to UncompiledData.
152 : typedef UncompiledData::BodyDescriptor BodyDescriptor;
153 :
154 5227568 : OBJECT_CONSTRUCTORS(UncompiledDataWithoutPreparseData, UncompiledData);
155 : };
156 :
157 : // Class representing data for an uncompiled function that has pre-parsed scope
158 : // data.
159 : class UncompiledDataWithPreparseData : public UncompiledData {
160 : public:
161 : DECL_ACCESSORS(preparse_data, PreparseData)
162 :
163 : DECL_CAST(UncompiledDataWithPreparseData)
164 : DECL_PRINTER(UncompiledDataWithPreparseData)
165 : DECL_VERIFIER(UncompiledDataWithPreparseData)
166 :
167 : inline static void Initialize(
168 : UncompiledDataWithPreparseData data, String inferred_name,
169 : int start_position, int end_position, int function_literal_id,
170 : PreparseData scope_data,
171 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
172 : gc_notify_updated_slot =
173 : [](HeapObject object, ObjectSlot slot, HeapObject target) {});
174 :
175 : // Layout description.
176 :
177 : #define UNCOMPILED_DATA_WITH_PREPARSE_DATA_FIELDS(V) \
178 : V(kStartOfPointerFieldsOffset, 0) \
179 : V(kPreparseDataOffset, kTaggedSize) \
180 : V(kEndOfTaggedFieldsOffset, 0) \
181 : /* Total size. */ \
182 : V(kSize, 0)
183 :
184 : DEFINE_FIELD_OFFSET_CONSTANTS(UncompiledData::kSize,
185 : UNCOMPILED_DATA_WITH_PREPARSE_DATA_FIELDS)
186 : #undef UNCOMPILED_DATA_WITH_PREPARSE_DATA_FIELDS
187 :
188 : // Make sure the size is aligned
189 : STATIC_ASSERT(kSize == POINTER_SIZE_ALIGN(kSize));
190 :
191 : typedef SubclassBodyDescriptor<
192 : UncompiledData::BodyDescriptor,
193 : FixedBodyDescriptor<kStartOfPointerFieldsOffset, kEndOfTaggedFieldsOffset,
194 : kSize>>
195 : BodyDescriptor;
196 :
197 48062 : OBJECT_CONSTRUCTORS(UncompiledDataWithPreparseData, UncompiledData);
198 : };
199 :
200 : class InterpreterData : public Struct {
201 : public:
202 : DECL_ACCESSORS(bytecode_array, BytecodeArray)
203 : DECL_ACCESSORS(interpreter_trampoline, Code)
204 :
205 : // Layout description.
206 : #define INTERPRETER_DATA_FIELDS(V) \
207 : V(kBytecodeArrayOffset, kTaggedSize) \
208 : V(kInterpreterTrampolineOffset, kTaggedSize) \
209 : /* Total size. */ \
210 : V(kSize, 0)
211 :
212 : DEFINE_FIELD_OFFSET_CONSTANTS(Struct::kHeaderSize, INTERPRETER_DATA_FIELDS)
213 : #undef INTERPRETER_DATA_FIELDS
214 :
215 : DECL_CAST(InterpreterData)
216 : DECL_PRINTER(InterpreterData)
217 : DECL_VERIFIER(InterpreterData)
218 :
219 : OBJECT_CONSTRUCTORS(InterpreterData, Struct);
220 : };
221 :
222 : // SharedFunctionInfo describes the JSFunction information that can be
223 : // shared by multiple instances of the function.
224 : class SharedFunctionInfo : public HeapObject {
225 : public:
226 : NEVER_READ_ONLY_SPACE
227 : static constexpr Object const kNoSharedNameSentinel = Smi::kZero;
228 :
229 : // [name]: Returns shared name if it exists or an empty string otherwise.
230 : inline String Name() const;
231 : inline void SetName(String name);
232 :
233 : // Get the code object which represents the execution of this function.
234 : Code GetCode() const;
235 :
236 : // Get the abstract code associated with the function, which will either be
237 : // a Code object or a BytecodeArray.
238 : inline AbstractCode abstract_code();
239 :
240 : // Tells whether or not this shared function info is interpreted.
241 : //
242 : // Note: function->IsInterpreted() does not necessarily return the same value
243 : // as function->shared()->IsInterpreted() because the closure might have been
244 : // optimized.
245 : inline bool IsInterpreted() const;
246 :
247 : // Set up the link between shared function info and the script. The shared
248 : // function info is added to the list on the script.
249 : V8_EXPORT_PRIVATE static void SetScript(
250 : Handle<SharedFunctionInfo> shared, Handle<Object> script_object,
251 : int function_literal_id, bool reset_preparsed_scope_data = true);
252 :
253 : // Layout description of the optimized code map.
254 : static const int kEntriesStart = 0;
255 : static const int kContextOffset = 0;
256 : static const int kCachedCodeOffset = 1;
257 : static const int kEntryLength = 2;
258 : static const int kInitialLength = kEntriesStart + kEntryLength;
259 :
260 : static const int kNotFound = -1;
261 : static const uint16_t kInvalidLength = static_cast<uint16_t>(-1);
262 :
263 : // [scope_info]: Scope info.
264 : DECL_ACCESSORS(scope_info, ScopeInfo)
265 :
266 : // End position of this function in the script source.
267 : V8_EXPORT_PRIVATE int EndPosition() const;
268 :
269 : // Start position of this function in the script source.
270 : V8_EXPORT_PRIVATE int StartPosition() const;
271 :
272 : // Set the start and end position of this function in the script source.
273 : // Updates the scope info if available.
274 : V8_EXPORT_PRIVATE void SetPosition(int start_position, int end_position);
275 :
276 : // [outer scope info | feedback metadata] Shared storage for outer scope info
277 : // (on uncompiled functions) and feedback metadata (on compiled functions).
278 : DECL_ACCESSORS(raw_outer_scope_info_or_feedback_metadata, HeapObject)
279 :
280 : // Get the outer scope info whether this function is compiled or not.
281 : inline bool HasOuterScopeInfo() const;
282 : inline ScopeInfo GetOuterScopeInfo() const;
283 :
284 : // [feedback metadata] Metadata template for feedback vectors of instances of
285 : // this function.
286 : inline bool HasFeedbackMetadata() const;
287 : DECL_ACCESSORS(feedback_metadata, FeedbackMetadata)
288 :
289 : // Returns if this function has been compiled yet. Note: with bytecode
290 : // flushing, any GC after this call is made could cause the function
291 : // to become uncompiled. If you need to ensure the function remains compiled
292 : // for some period of time, use IsCompiledScope instead.
293 : inline bool is_compiled() const;
294 :
295 : // Returns an IsCompiledScope which reports whether the function is compiled,
296 : // and if compiled, will avoid the function becoming uncompiled while it is
297 : // held.
298 : inline IsCompiledScope is_compiled_scope() const;
299 :
300 : // [length]: The function length - usually the number of declared parameters.
301 : // Use up to 2^16-2 parameters (16 bits of values, where one is reserved for
302 : // kDontAdaptArgumentsSentinel). The value is only reliable when the function
303 : // has been compiled.
304 : inline uint16_t GetLength() const;
305 : inline bool HasLength() const;
306 : inline void set_length(int value);
307 :
308 : // [internal formal parameter count]: The declared number of parameters.
309 : // For subclass constructors, also includes new.target.
310 : // The size of function's frame is internal_formal_parameter_count + 1.
311 : DECL_UINT16_ACCESSORS(internal_formal_parameter_count)
312 :
313 : // Set the formal parameter count so the function code will be
314 : // called without using argument adaptor frames.
315 : inline void DontAdaptArguments();
316 :
317 : // [expected_nof_properties]: Expected number of properties for the
318 : // function. The value is only reliable when the function has been compiled.
319 : DECL_UINT8_ACCESSORS(expected_nof_properties)
320 :
321 : #if V8_SFI_HAS_UNIQUE_ID
322 : // [unique_id] - For --trace-maps purposes, an identifier that's persistent
323 : // even if the GC moves this SharedFunctionInfo.
324 : DECL_INT_ACCESSORS(unique_id)
325 : #endif
326 :
327 : // [function data]: This field holds some additional data for function.
328 : // Currently it has one of:
329 : // - a FunctionTemplateInfo to make benefit the API [IsApiFunction()].
330 : // - a BytecodeArray for the interpreter [HasBytecodeArray()].
331 : // - a InterpreterData with the BytecodeArray and a copy of the
332 : // interpreter trampoline [HasInterpreterData()]
333 : // - an AsmWasmData with Asm->Wasm conversion [HasAsmWasmData()].
334 : // - a Smi containing the builtin id [HasBuiltinId()]
335 : // - a UncompiledDataWithoutPreparseData for lazy compilation
336 : // [HasUncompiledDataWithoutPreparseData()]
337 : // - a UncompiledDataWithPreparseData for lazy compilation
338 : // [HasUncompiledDataWithPreparseData()]
339 : // - a WasmExportedFunctionData for Wasm [HasWasmExportedFunctionData()]
340 : DECL_ACCESSORS(function_data, Object)
341 :
342 : inline bool IsApiFunction() const;
343 : inline FunctionTemplateInfo get_api_func_data();
344 : inline void set_api_func_data(FunctionTemplateInfo data);
345 : inline bool HasBytecodeArray() const;
346 : inline BytecodeArray GetBytecodeArray() const;
347 : inline void set_bytecode_array(BytecodeArray bytecode);
348 : inline Code InterpreterTrampoline() const;
349 : inline bool HasInterpreterData() const;
350 : inline InterpreterData interpreter_data() const;
351 : inline void set_interpreter_data(InterpreterData interpreter_data);
352 : inline BytecodeArray GetDebugBytecodeArray() const;
353 : inline void SetDebugBytecodeArray(BytecodeArray bytecode);
354 : inline bool HasAsmWasmData() const;
355 : inline AsmWasmData asm_wasm_data() const;
356 : inline void set_asm_wasm_data(AsmWasmData data);
357 :
358 : // A brief note to clear up possible confusion:
359 : // builtin_id corresponds to the auto-generated
360 : // Builtins::Name id, while builtin_function_id corresponds to
361 : // BuiltinFunctionId (a manually maintained list of 'interesting' functions
362 : // mainly used during optimization).
363 : inline bool HasBuiltinId() const;
364 : inline int builtin_id() const;
365 : inline void set_builtin_id(int builtin_id);
366 : inline bool HasUncompiledData() const;
367 : inline UncompiledData uncompiled_data() const;
368 : inline void set_uncompiled_data(UncompiledData data);
369 : inline bool HasUncompiledDataWithPreparseData() const;
370 : inline UncompiledDataWithPreparseData uncompiled_data_with_preparse_data()
371 : const;
372 : inline void set_uncompiled_data_with_preparse_data(
373 : UncompiledDataWithPreparseData data);
374 : inline bool HasUncompiledDataWithoutPreparseData() const;
375 : inline bool HasWasmExportedFunctionData() const;
376 : WasmExportedFunctionData wasm_exported_function_data() const;
377 :
378 : // Clear out pre-parsed scope data from UncompiledDataWithPreparseData,
379 : // turning it into UncompiledDataWithoutPreparseData.
380 : inline void ClearPreparseData();
381 :
382 : // [raw_builtin_function_id]: The id of the built-in function this function
383 : // represents, used during optimization to improve code generation.
384 : // TODO(leszeks): Once there are no more JS builtins, this can be replaced
385 : // by BuiltinId.
386 : DECL_UINT8_ACCESSORS(raw_builtin_function_id)
387 : inline bool HasBuiltinFunctionId();
388 : inline BuiltinFunctionId builtin_function_id();
389 : inline void set_builtin_function_id(BuiltinFunctionId id);
390 : // Make sure BuiltinFunctionIds fit in a uint8_t
391 : STATIC_ASSERT((std::is_same<std::underlying_type<BuiltinFunctionId>::type,
392 : uint8_t>::value));
393 :
394 : // The inferred_name is inferred from variable or property assignment of this
395 : // function. It is used to facilitate debugging and profiling of JavaScript
396 : // code written in OO style, where almost all functions are anonymous but are
397 : // assigned to object properties.
398 : inline bool HasInferredName();
399 : inline String inferred_name();
400 :
401 : // Get the function literal id associated with this function, for parsing.
402 : V8_EXPORT_PRIVATE int FunctionLiteralId(Isolate* isolate) const;
403 :
404 : // Break infos are contained in DebugInfo, this is a convenience method
405 : // to simplify access.
406 : bool HasBreakInfo() const;
407 : bool BreakAtEntry() const;
408 :
409 : // Coverage infos are contained in DebugInfo, this is a convenience method
410 : // to simplify access.
411 : bool HasCoverageInfo() const;
412 : CoverageInfo GetCoverageInfo() const;
413 :
414 : // The function's name if it is non-empty, otherwise the inferred name.
415 : String DebugName();
416 :
417 : // Used for flags such as --turbo-filter.
418 : bool PassesFilter(const char* raw_filter);
419 :
420 : // [script_or_debug_info]: One of:
421 : // - Script from which the function originates.
422 : // - a DebugInfo which holds the actual script [HasDebugInfo()].
423 : DECL_ACCESSORS(script_or_debug_info, Object)
424 :
425 : inline Object script() const;
426 : inline void set_script(Object script);
427 :
428 : // The function is subject to debugging if a debug info is attached.
429 : inline bool HasDebugInfo() const;
430 : inline DebugInfo GetDebugInfo() const;
431 : inline void SetDebugInfo(DebugInfo debug_info);
432 :
433 : // The offset of the 'function' token in the script source relative to the
434 : // start position. Can return kFunctionTokenOutOfRange if offset doesn't
435 : // fit in 16 bits.
436 : DECL_UINT16_ACCESSORS(raw_function_token_offset)
437 :
438 : // The position of the 'function' token in the script source. Can return
439 : // kNoSourcePosition if raw_function_token_offset() returns
440 : // kFunctionTokenOutOfRange.
441 : inline int function_token_position() const;
442 :
443 : // Returns true if the function has shared name.
444 : inline bool HasSharedName() const;
445 :
446 : // [flags] Bit field containing various flags about the function.
447 : DECL_INT32_ACCESSORS(flags)
448 :
449 : // Is this function a named function expression in the source code.
450 : DECL_BOOLEAN_ACCESSORS(is_named_expression)
451 :
452 : // Is this function a top-level function (scripts, evals).
453 : DECL_BOOLEAN_ACCESSORS(is_toplevel)
454 :
455 : // Indicates if this function can be lazy compiled.
456 : DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
457 :
458 : // Indicates the language mode.
459 : inline LanguageMode language_mode() const;
460 : inline void set_language_mode(LanguageMode language_mode);
461 :
462 : // Indicates whether the source is implicitly wrapped in a function.
463 : DECL_BOOLEAN_ACCESSORS(is_wrapped)
464 :
465 : // True if the function has any duplicated parameter names.
466 : DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
467 :
468 : // Indicates whether the function is a native function.
469 : // These needs special treatment in .call and .apply since
470 : // null passed as the receiver should not be translated to the
471 : // global object.
472 : DECL_BOOLEAN_ACCESSORS(native)
473 :
474 : // Whether this function was created from a FunctionDeclaration.
475 : DECL_BOOLEAN_ACCESSORS(is_declaration)
476 :
477 : // Indicates that asm->wasm conversion failed and should not be re-attempted.
478 : DECL_BOOLEAN_ACCESSORS(is_asm_wasm_broken)
479 :
480 : // Indicates that the function was created by the Function function.
481 : // Though it's anonymous, toString should treat it as if it had the name
482 : // "anonymous". We don't set the name itself so that the system does not
483 : // see a binding for it.
484 : DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
485 :
486 : // Indicates that the function is either an anonymous expression
487 : // or an arrow function (the name field can be set through the API,
488 : // which does not change this flag).
489 : DECL_BOOLEAN_ACCESSORS(is_anonymous_expression)
490 :
491 : // Indicates that the function represented by the shared function info was
492 : // classed as an immediately invoked function execution (IIFE) function.
493 : DECL_BOOLEAN_ACCESSORS(is_iife)
494 :
495 : // Indicates that the function has been reported for binary code coverage.
496 : DECL_BOOLEAN_ACCESSORS(has_reported_binary_coverage)
497 :
498 : inline FunctionKind kind() const;
499 :
500 : // Defines the index in a native context of closure's map instantiated using
501 : // this shared function info.
502 : DECL_INT_ACCESSORS(function_map_index)
503 :
504 : // Clear uninitialized padding space. This ensures that the snapshot content
505 : // is deterministic.
506 : inline void clear_padding();
507 :
508 : // Recalculates the |map_index| value after modifications of this shared info.
509 : inline void UpdateFunctionMapIndex();
510 :
511 : // Indicates whether optimizations have been disabled for this shared function
512 : // info. If we cannot optimize the function we disable optimization to avoid
513 : // spending time attempting to optimize it again.
514 : inline bool optimization_disabled() const;
515 :
516 : // The reason why optimization was disabled.
517 : inline BailoutReason disable_optimization_reason() const;
518 :
519 : // Disable (further) attempted optimization of all functions sharing this
520 : // shared function info.
521 : void DisableOptimization(BailoutReason reason);
522 :
523 : // This class constructor needs to call out to an instance fields
524 : // initializer. This flag is set when creating the
525 : // SharedFunctionInfo as a reminder to emit the initializer call
526 : // when generating code later.
527 : DECL_BOOLEAN_ACCESSORS(requires_instance_members_initializer)
528 :
529 : // [source code]: Source code for the function.
530 : bool HasSourceCode() const;
531 : static Handle<Object> GetSourceCode(Handle<SharedFunctionInfo> shared);
532 : static Handle<Object> GetSourceCodeHarmony(Handle<SharedFunctionInfo> shared);
533 :
534 : // Tells whether this function should be subject to debugging, e.g. for
535 : // - scope inspection
536 : // - internal break points
537 : // - coverage and type profile
538 : // - error stack trace
539 : inline bool IsSubjectToDebugging();
540 :
541 : // Whether this function is defined in user-provided JavaScript code.
542 : inline bool IsUserJavaScript();
543 :
544 : // True if one can flush compiled code from this function, in such a way that
545 : // it can later be re-compiled.
546 : inline bool CanDiscardCompiled() const;
547 :
548 : // Flush compiled data from this function, setting it back to CompileLazy and
549 : // clearing any compiled metadata.
550 : static void DiscardCompiled(Isolate* isolate,
551 : Handle<SharedFunctionInfo> shared_info);
552 :
553 : // Discard the compiled metadata. If called during GC then
554 : // |gc_notify_updated_slot| should be used to record any slot updates.
555 : void DiscardCompiledMetadata(
556 : Isolate* isolate,
557 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
558 : gc_notify_updated_slot =
559 : [](HeapObject object, ObjectSlot slot, HeapObject target) {});
560 :
561 : // Returns true if the function has old bytecode that could be flushed.
562 : inline bool ShouldFlushBytecode();
563 :
564 : // Check whether or not this function is inlineable.
565 : bool IsInlineable();
566 :
567 : // Source size of this function.
568 : int SourceSize();
569 :
570 : // Returns `false` if formal parameters include rest parameters, optional
571 : // parameters, or destructuring parameters.
572 : // TODO(caitp): make this a flag set during parsing
573 : inline bool has_simple_parameters();
574 :
575 : // Initialize a SharedFunctionInfo from a parsed function literal.
576 : static void InitFromFunctionLiteral(Handle<SharedFunctionInfo> shared_info,
577 : FunctionLiteral* lit, bool is_toplevel);
578 :
579 : // Sets the expected number of properties based on estimate from parser.
580 : void SetExpectedNofPropertiesFromEstimate(FunctionLiteral* literal);
581 :
582 : // Sets the FunctionTokenOffset field based on the given token position and
583 : // start position.
584 : void SetFunctionTokenPosition(int function_token_position,
585 : int start_position);
586 :
587 : inline bool construct_as_builtin() const;
588 :
589 : // Determines and sets the ConstructAsBuiltinBit in |flags|, based on the
590 : // |function_data|. Must be called when creating the SFI after other fields
591 : // are initialized. The ConstructAsBuiltinBit determines whether
592 : // JSBuiltinsConstructStub or JSConstructStubGeneric should be called to
593 : // construct this function.
594 : inline void CalculateConstructAsBuiltin();
595 :
596 : // Dispatched behavior.
597 : DECL_PRINTER(SharedFunctionInfo)
598 : DECL_VERIFIER(SharedFunctionInfo)
599 : #ifdef OBJECT_PRINT
600 : void PrintSourceCode(std::ostream& os);
601 : #endif
602 :
603 : // Iterate over all shared function infos in a given script.
604 : class ScriptIterator {
605 : public:
606 : ScriptIterator(Isolate* isolate, Script script);
607 : ScriptIterator(Isolate* isolate,
608 : Handle<WeakFixedArray> shared_function_infos);
609 : SharedFunctionInfo Next();
610 108214 : int CurrentIndex() const { return index_ - 1; }
611 :
612 : // Reset the iterator to run on |script|.
613 : void Reset(Script script);
614 :
615 : private:
616 : Isolate* isolate_;
617 : Handle<WeakFixedArray> shared_function_infos_;
618 : int index_;
619 : DISALLOW_COPY_AND_ASSIGN(ScriptIterator);
620 : };
621 :
622 : // Iterate over all shared function infos on the heap.
623 : class GlobalIterator {
624 : public:
625 : explicit GlobalIterator(Isolate* isolate);
626 : SharedFunctionInfo Next();
627 :
628 : private:
629 : Script::Iterator script_iterator_;
630 : WeakArrayList::Iterator noscript_sfi_iterator_;
631 : SharedFunctionInfo::ScriptIterator sfi_iterator_;
632 : DISALLOW_HEAP_ALLOCATION(no_gc_);
633 : DISALLOW_COPY_AND_ASSIGN(GlobalIterator);
634 : };
635 :
636 : DECL_CAST(SharedFunctionInfo)
637 :
638 : // Constants.
639 : static const uint16_t kDontAdaptArgumentsSentinel = static_cast<uint16_t>(-1);
640 :
641 : static const int kMaximumFunctionTokenOffset = kMaxUInt16 - 1;
642 : static const uint16_t kFunctionTokenOutOfRange = static_cast<uint16_t>(-1);
643 : STATIC_ASSERT(kMaximumFunctionTokenOffset + 1 == kFunctionTokenOutOfRange);
644 :
645 : #if V8_SFI_HAS_UNIQUE_ID
646 : static const int kUniqueIdFieldSize = kInt32Size;
647 : #else
648 : // Just to not break the postmortrem support with conditional offsets
649 : static const int kUniqueIdFieldSize = 0;
650 : #endif
651 :
652 : // Layout description.
653 : #define SHARED_FUNCTION_INFO_FIELDS(V) \
654 : /* Pointer fields. */ \
655 : V(kStartOfPointerFieldsOffset, 0) \
656 : V(kFunctionDataOffset, kTaggedSize) \
657 : V(kStartOfAlwaysStrongPointerFieldsOffset, 0) \
658 : V(kNameOrScopeInfoOffset, kTaggedSize) \
659 : V(kOuterScopeInfoOrFeedbackMetadataOffset, kTaggedSize) \
660 : V(kScriptOrDebugInfoOffset, kTaggedSize) \
661 : V(kEndOfTaggedFieldsOffset, 0) \
662 : /* Raw data fields. */ \
663 : V(kUniqueIdOffset, kUniqueIdFieldSize) \
664 : V(kLengthOffset, kUInt16Size) \
665 : V(kFormalParameterCountOffset, kUInt16Size) \
666 : V(kExpectedNofPropertiesOffset, kUInt8Size) \
667 : V(kBuiltinFunctionId, kUInt8Size) \
668 : V(kFunctionTokenOffsetOffset, kUInt16Size) \
669 : V(kFlagsOffset, kInt32Size) \
670 : /* Total size. */ \
671 : V(kSize, 0)
672 :
673 : DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
674 : SHARED_FUNCTION_INFO_FIELDS)
675 : #undef SHARED_FUNCTION_INFO_FIELDS
676 :
677 : static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
678 :
679 : class BodyDescriptor;
680 :
681 : // Bit positions in |flags|.
682 : #define FLAGS_BIT_FIELDS(V, _) \
683 : V(IsNativeBit, bool, 1, _) \
684 : V(IsStrictBit, bool, 1, _) \
685 : V(IsWrappedBit, bool, 1, _) \
686 : V(IsClassConstructorBit, bool, 1, _) \
687 : V(IsDerivedConstructorBit, bool, 1, _) \
688 : V(FunctionKindBits, FunctionKind, 5, _) \
689 : V(HasDuplicateParametersBit, bool, 1, _) \
690 : V(AllowLazyCompilationBit, bool, 1, _) \
691 : V(NeedsHomeObjectBit, bool, 1, _) \
692 : V(IsDeclarationBit, bool, 1, _) \
693 : V(IsAsmWasmBrokenBit, bool, 1, _) \
694 : V(FunctionMapIndexBits, int, 5, _) \
695 : V(DisabledOptimizationReasonBits, BailoutReason, 4, _) \
696 : V(RequiresInstanceMembersInitializer, bool, 1, _) \
697 : V(ConstructAsBuiltinBit, bool, 1, _) \
698 : V(IsAnonymousExpressionBit, bool, 1, _) \
699 : V(NameShouldPrintAsAnonymousBit, bool, 1, _) \
700 : V(HasReportedBinaryCoverageBit, bool, 1, _) \
701 : V(IsNamedExpressionBit, bool, 1, _) \
702 : V(IsTopLevelBit, bool, 1, _) \
703 : V(IsIIFEBit, bool, 1, _)
704 : DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
705 : #undef FLAGS_BIT_FIELDS
706 :
707 : // Bailout reasons must fit in the DisabledOptimizationReason bitfield.
708 : STATIC_ASSERT(BailoutReason::kLastErrorMessage <=
709 : DisabledOptimizationReasonBits::kMax);
710 :
711 : STATIC_ASSERT(kLastFunctionKind <= FunctionKindBits::kMax);
712 :
713 : // Indicates that this function uses a super property (or an eval that may
714 : // use a super property).
715 : // This is needed to set up the [[HomeObject]] on the function instance.
716 : inline bool needs_home_object() const;
717 :
718 : private:
719 : // [name_or_scope_info]: Function name string, kNoSharedNameSentinel or
720 : // ScopeInfo.
721 : DECL_ACCESSORS(name_or_scope_info, Object)
722 :
723 : // [outer scope info] The outer scope info, needed to lazily parse this
724 : // function.
725 : DECL_ACCESSORS(outer_scope_info, HeapObject)
726 :
727 : inline void set_kind(FunctionKind kind);
728 :
729 : inline void set_needs_home_object(bool value);
730 :
731 : friend class Factory;
732 : friend class V8HeapExplorer;
733 : FRIEND_TEST(PreParserTest, LazyFunctionLength);
734 :
735 : inline uint16_t length() const;
736 :
737 : // Find the index of this function in the parent script. Slow path of
738 : // FunctionLiteralId.
739 : int FindIndexInScript(Isolate* isolate) const;
740 :
741 20987776 : OBJECT_CONSTRUCTORS(SharedFunctionInfo, HeapObject);
742 : };
743 :
744 : // Printing support.
745 : struct SourceCodeOf {
746 : explicit SourceCodeOf(SharedFunctionInfo v, int max = -1)
747 27 : : value(v), max_length(max) {}
748 : const SharedFunctionInfo value;
749 : int max_length;
750 : };
751 :
752 : // IsCompiledScope enables a caller to check if a function is compiled, and
753 : // ensure it remains compiled (i.e., doesn't have it's bytecode flushed) while
754 : // the scope is retained.
755 : class IsCompiledScope {
756 : public:
757 : inline IsCompiledScope(const SharedFunctionInfo shared, Isolate* isolate);
758 11562522 : inline IsCompiledScope() : retain_bytecode_(), is_compiled_(false) {}
759 :
760 285271 : inline bool is_compiled() const { return is_compiled_; }
761 :
762 : private:
763 : MaybeHandle<BytecodeArray> retain_bytecode_;
764 : bool is_compiled_;
765 : };
766 :
767 : std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v);
768 :
769 : } // namespace internal
770 : } // namespace v8
771 :
772 : #include "src/objects/object-macros-undef.h"
773 :
774 : #endif // V8_OBJECTS_SHARED_FUNCTION_INFO_H_
|