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/function-kind.h"
10 : #include "src/objects.h"
11 : #include "src/objects/compressed-slots.h"
12 : #include "src/objects/script.h"
13 : #include "src/objects/slots.h"
14 : #include "src/objects/smi.h"
15 : #include "src/objects/struct.h"
16 : #include "testing/gtest/include/gtest/gtest_prod.h"
17 : #include "torque-generated/class-definitions-from-dsl.h"
18 :
19 : // Has to be the last include (doesn't have include guards):
20 : #include "src/objects/object-macros.h"
21 :
22 : namespace v8 {
23 : namespace internal {
24 :
25 : class AsmWasmData;
26 : class BytecodeArray;
27 : class CoverageInfo;
28 : class DebugInfo;
29 : class IsCompiledScope;
30 : class WasmExportedFunctionData;
31 :
32 : // Data collected by the pre-parser storing information about scopes and inner
33 : // functions.
34 : //
35 : // PreparseData Layout:
36 : // +-------------------------------+
37 : // | data_length | children_length |
38 : // +-------------------------------+
39 : // | Scope Byte Data ... |
40 : // | ... |
41 : // +-------------------------------+
42 : // | [Padding] |
43 : // +-------------------------------+
44 : // | Inner PreparseData 1 |
45 : // +-------------------------------+
46 : // | ... |
47 : // +-------------------------------+
48 : // | Inner PreparseData N |
49 : // +-------------------------------+
50 : class PreparseData : public HeapObject {
51 : public:
52 : DECL_INT_ACCESSORS(data_length)
53 : DECL_INT_ACCESSORS(children_length)
54 :
55 : inline int inner_start_offset() const;
56 : inline ObjectSlot inner_data_start() const;
57 :
58 : inline byte get(int index) const;
59 : inline void set(int index, byte value);
60 : inline void copy_in(int index, const byte* buffer, int length);
61 :
62 : inline PreparseData get_child(int index) const;
63 : inline void set_child(int index, PreparseData value,
64 : WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
65 :
66 : // Clear uninitialized padding space.
67 : inline void clear_padding();
68 :
69 : DECL_CAST(PreparseData)
70 : DECL_PRINTER(PreparseData)
71 : DECL_VERIFIER(PreparseData)
72 :
73 : DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
74 : TORQUE_GENERATED_PREPARSE_DATA_FIELDS)
75 : static const int kDataStartOffset = kSize;
76 :
77 : class BodyDescriptor;
78 :
79 : static int InnerOffset(int data_length) {
80 433458 : return RoundUp(kDataStartOffset + data_length * kByteSize, kTaggedSize);
81 : }
82 :
83 : static int SizeFor(int data_length, int children_length) {
84 299404 : return InnerOffset(data_length) + children_length * kTaggedSize;
85 : }
86 :
87 : OBJECT_CONSTRUCTORS(PreparseData, HeapObject);
88 :
89 : private:
90 : inline Object get_child_raw(int index) const;
91 : };
92 :
93 : // Abstract class representing extra data for an uncompiled function, which is
94 : // not stored in the SharedFunctionInfo.
95 : class UncompiledData : public HeapObject {
96 : public:
97 : DECL_ACCESSORS(inferred_name, String)
98 : DECL_INT32_ACCESSORS(start_position)
99 : DECL_INT32_ACCESSORS(end_position)
100 : DECL_INT32_ACCESSORS(function_literal_id)
101 :
102 : // Returns true if the UncompiledData contains a valid function_literal_id.
103 : inline bool has_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 : 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(IsAligned(kSize, kTaggedSize));
190 :
191 : typedef SubclassBodyDescriptor<
192 : UncompiledData::BodyDescriptor,
193 : FixedBodyDescriptor<kStartOfPointerFieldsOffset, kEndOfTaggedFieldsOffset,
194 : kSize>>
195 : BodyDescriptor;
196 :
197 : 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_UINT16_ACCESSORS(expected_nof_properties)
320 :
321 : // [function data]: This field holds some additional data for function.
322 : // Currently it has one of:
323 : // - a FunctionTemplateInfo to make benefit the API [IsApiFunction()].
324 : // - a BytecodeArray for the interpreter [HasBytecodeArray()].
325 : // - a InterpreterData with the BytecodeArray and a copy of the
326 : // interpreter trampoline [HasInterpreterData()]
327 : // - an AsmWasmData with Asm->Wasm conversion [HasAsmWasmData()].
328 : // - a Smi containing the builtin id [HasBuiltinId()]
329 : // - a UncompiledDataWithoutPreparseData for lazy compilation
330 : // [HasUncompiledDataWithoutPreparseData()]
331 : // - a UncompiledDataWithPreparseData for lazy compilation
332 : // [HasUncompiledDataWithPreparseData()]
333 : // - a WasmExportedFunctionData for Wasm [HasWasmExportedFunctionData()]
334 : DECL_ACCESSORS(function_data, Object)
335 :
336 : inline bool IsApiFunction() const;
337 : inline FunctionTemplateInfo get_api_func_data();
338 : inline void set_api_func_data(FunctionTemplateInfo data);
339 : inline bool HasBytecodeArray() const;
340 : inline BytecodeArray GetBytecodeArray() const;
341 : inline void set_bytecode_array(BytecodeArray bytecode);
342 : inline Code InterpreterTrampoline() const;
343 : inline bool HasInterpreterData() const;
344 : inline InterpreterData interpreter_data() const;
345 : inline void set_interpreter_data(InterpreterData interpreter_data);
346 : inline BytecodeArray GetDebugBytecodeArray() const;
347 : inline void SetDebugBytecodeArray(BytecodeArray bytecode);
348 : inline bool HasAsmWasmData() const;
349 : inline AsmWasmData asm_wasm_data() const;
350 : inline void set_asm_wasm_data(AsmWasmData data);
351 :
352 : // builtin_id corresponds to the auto-generated Builtins::Name id.
353 : inline bool HasBuiltinId() const;
354 : inline int builtin_id() const;
355 : inline void set_builtin_id(int builtin_id);
356 : inline bool HasUncompiledData() const;
357 : inline UncompiledData uncompiled_data() const;
358 : inline void set_uncompiled_data(UncompiledData data);
359 : inline bool HasUncompiledDataWithPreparseData() const;
360 : inline UncompiledDataWithPreparseData uncompiled_data_with_preparse_data()
361 : const;
362 : inline void set_uncompiled_data_with_preparse_data(
363 : UncompiledDataWithPreparseData data);
364 : inline bool HasUncompiledDataWithoutPreparseData() const;
365 : inline bool HasWasmExportedFunctionData() const;
366 : WasmExportedFunctionData wasm_exported_function_data() const;
367 :
368 : // Clear out pre-parsed scope data from UncompiledDataWithPreparseData,
369 : // turning it into UncompiledDataWithoutPreparseData.
370 : inline void ClearPreparseData();
371 :
372 : // The inferred_name is inferred from variable or property assignment of this
373 : // function. It is used to facilitate debugging and profiling of JavaScript
374 : // code written in OO style, where almost all functions are anonymous but are
375 : // assigned to object properties.
376 : inline bool HasInferredName();
377 : inline String inferred_name();
378 :
379 : // Get the function literal id associated with this function, for parsing.
380 : V8_EXPORT_PRIVATE int FunctionLiteralId(Isolate* isolate) const;
381 :
382 : // Break infos are contained in DebugInfo, this is a convenience method
383 : // to simplify access.
384 : bool HasBreakInfo() const;
385 : bool BreakAtEntry() const;
386 :
387 : // Coverage infos are contained in DebugInfo, this is a convenience method
388 : // to simplify access.
389 : bool HasCoverageInfo() const;
390 : CoverageInfo GetCoverageInfo() const;
391 :
392 : // The function's name if it is non-empty, otherwise the inferred name.
393 : String DebugName();
394 :
395 : // Used for flags such as --turbo-filter.
396 : bool PassesFilter(const char* raw_filter);
397 :
398 : // [script_or_debug_info]: One of:
399 : // - Script from which the function originates.
400 : // - a DebugInfo which holds the actual script [HasDebugInfo()].
401 : DECL_ACCESSORS(script_or_debug_info, Object)
402 :
403 : inline Object script() const;
404 : inline void set_script(Object script);
405 :
406 : // The function is subject to debugging if a debug info is attached.
407 : inline bool HasDebugInfo() const;
408 : inline DebugInfo GetDebugInfo() const;
409 : inline void SetDebugInfo(DebugInfo debug_info);
410 :
411 : // The offset of the 'function' token in the script source relative to the
412 : // start position. Can return kFunctionTokenOutOfRange if offset doesn't
413 : // fit in 16 bits.
414 : DECL_UINT16_ACCESSORS(raw_function_token_offset)
415 :
416 : // The position of the 'function' token in the script source. Can return
417 : // kNoSourcePosition if raw_function_token_offset() returns
418 : // kFunctionTokenOutOfRange.
419 : inline int function_token_position() const;
420 :
421 : // Returns true if the function has shared name.
422 : inline bool HasSharedName() const;
423 :
424 : // [flags] Bit field containing various flags about the function.
425 : DECL_INT32_ACCESSORS(flags)
426 :
427 : // Is this function a named function expression in the source code.
428 : DECL_BOOLEAN_ACCESSORS(is_named_expression)
429 :
430 : // Is this function a top-level function (scripts, evals).
431 : DECL_BOOLEAN_ACCESSORS(is_toplevel)
432 :
433 : // Indicates if this function can be lazy compiled.
434 : DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
435 :
436 : // Indicates the language mode.
437 : inline LanguageMode language_mode() const;
438 : inline void set_language_mode(LanguageMode language_mode);
439 :
440 : // Indicates whether the source is implicitly wrapped in a function.
441 : DECL_BOOLEAN_ACCESSORS(is_wrapped)
442 :
443 : // True if the function has any duplicated parameter names.
444 : DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
445 :
446 : // Indicates whether the function is a native function.
447 : // These needs special treatment in .call and .apply since
448 : // null passed as the receiver should not be translated to the
449 : // global object.
450 : DECL_BOOLEAN_ACCESSORS(native)
451 :
452 : // Whether this function was created from a FunctionDeclaration.
453 : DECL_BOOLEAN_ACCESSORS(is_declaration)
454 :
455 : // Indicates that asm->wasm conversion failed and should not be re-attempted.
456 : DECL_BOOLEAN_ACCESSORS(is_asm_wasm_broken)
457 :
458 : // Indicates that the function was created by the Function function.
459 : // Though it's anonymous, toString should treat it as if it had the name
460 : // "anonymous". We don't set the name itself so that the system does not
461 : // see a binding for it.
462 : DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
463 :
464 : // Indicates that the function is either an anonymous expression
465 : // or an arrow function (the name field can be set through the API,
466 : // which does not change this flag).
467 : DECL_BOOLEAN_ACCESSORS(is_anonymous_expression)
468 :
469 : // Indicates that the function represented by the shared function info was
470 : // classed as an immediately invoked function execution (IIFE) function and
471 : // is only executed once.
472 : DECL_BOOLEAN_ACCESSORS(is_oneshot_iife)
473 :
474 : // Indicates that the function represented by the shared function info
475 : // cannot observe the actual parameters passed at a call site, which
476 : // means the function doesn't use the arguments object, doesn't use
477 : // rest parameters, and is also in strict mode (meaning that there's
478 : // no way to get to the actual arguments via the non-standard "arguments"
479 : // accessor on sloppy mode functions). This can be used to speed up calls
480 : // to this function even in the presence of arguments mismatch.
481 : // See http://bit.ly/v8-faster-calls-with-arguments-mismatch for more
482 : // information on this.
483 : DECL_BOOLEAN_ACCESSORS(is_safe_to_skip_arguments_adaptor)
484 :
485 : // Indicates that the function has been reported for binary code coverage.
486 : DECL_BOOLEAN_ACCESSORS(has_reported_binary_coverage)
487 :
488 : inline FunctionKind kind() const;
489 :
490 : // Defines the index in a native context of closure's map instantiated using
491 : // this shared function info.
492 : DECL_INT_ACCESSORS(function_map_index)
493 :
494 : // Clear uninitialized padding space. This ensures that the snapshot content
495 : // is deterministic.
496 : inline void clear_padding();
497 :
498 : // Recalculates the |map_index| value after modifications of this shared info.
499 : inline void UpdateFunctionMapIndex();
500 :
501 : // Indicates whether optimizations have been disabled for this shared function
502 : // info. If we cannot optimize the function we disable optimization to avoid
503 : // spending time attempting to optimize it again.
504 : inline bool optimization_disabled() const;
505 :
506 : // The reason why optimization was disabled.
507 : inline BailoutReason disable_optimization_reason() const;
508 :
509 : // Disable (further) attempted optimization of all functions sharing this
510 : // shared function info.
511 : void DisableOptimization(BailoutReason reason);
512 :
513 : // This class constructor needs to call out to an instance fields
514 : // initializer. This flag is set when creating the
515 : // SharedFunctionInfo as a reminder to emit the initializer call
516 : // when generating code later.
517 : DECL_BOOLEAN_ACCESSORS(requires_instance_members_initializer)
518 :
519 : // [source code]: Source code for the function.
520 : bool HasSourceCode() const;
521 : static Handle<Object> GetSourceCode(Handle<SharedFunctionInfo> shared);
522 : static Handle<Object> GetSourceCodeHarmony(Handle<SharedFunctionInfo> shared);
523 :
524 : // Tells whether this function should be subject to debugging, e.g. for
525 : // - scope inspection
526 : // - internal break points
527 : // - coverage and type profile
528 : // - error stack trace
529 : inline bool IsSubjectToDebugging();
530 :
531 : // Whether this function is defined in user-provided JavaScript code.
532 : inline bool IsUserJavaScript();
533 :
534 : // True if one can flush compiled code from this function, in such a way that
535 : // it can later be re-compiled.
536 : inline bool CanDiscardCompiled() const;
537 :
538 : // Flush compiled data from this function, setting it back to CompileLazy and
539 : // clearing any compiled metadata.
540 : static void DiscardCompiled(Isolate* isolate,
541 : Handle<SharedFunctionInfo> shared_info);
542 :
543 : // Discard the compiled metadata. If called during GC then
544 : // |gc_notify_updated_slot| should be used to record any slot updates.
545 : void DiscardCompiledMetadata(
546 : Isolate* isolate,
547 : std::function<void(HeapObject object, ObjectSlot slot, HeapObject target)>
548 : gc_notify_updated_slot =
549 : [](HeapObject object, ObjectSlot slot, HeapObject target) {});
550 :
551 : // Returns true if the function has old bytecode that could be flushed.
552 : inline bool ShouldFlushBytecode();
553 :
554 : // Check whether or not this function is inlineable.
555 : bool IsInlineable();
556 :
557 : // Source size of this function.
558 : int SourceSize();
559 :
560 : // Returns `false` if formal parameters include rest parameters, optional
561 : // parameters, or destructuring parameters.
562 : // TODO(caitp): make this a flag set during parsing
563 : inline bool has_simple_parameters();
564 :
565 : // Initialize a SharedFunctionInfo from a parsed function literal.
566 : static void InitFromFunctionLiteral(Handle<SharedFunctionInfo> shared_info,
567 : FunctionLiteral* lit, bool is_toplevel);
568 :
569 : // Sets the expected number of properties based on estimate from parser.
570 : void SetExpectedNofPropertiesFromEstimate(FunctionLiteral* literal);
571 :
572 : // Sets the FunctionTokenOffset field based on the given token position and
573 : // start position.
574 : void SetFunctionTokenPosition(int function_token_position,
575 : int start_position);
576 :
577 : static void EnsureSourcePositionsAvailable(
578 : Isolate* isolate, Handle<SharedFunctionInfo> shared_info);
579 :
580 : // Hash based on function literal id and script id.
581 : uint32_t Hash();
582 :
583 : inline bool construct_as_builtin() const;
584 :
585 : // Determines and sets the ConstructAsBuiltinBit in |flags|, based on the
586 : // |function_data|. Must be called when creating the SFI after other fields
587 : // are initialized. The ConstructAsBuiltinBit determines whether
588 : // JSBuiltinsConstructStub or JSConstructStubGeneric should be called to
589 : // construct this function.
590 : inline void CalculateConstructAsBuiltin();
591 :
592 : // Dispatched behavior.
593 : DECL_PRINTER(SharedFunctionInfo)
594 : DECL_VERIFIER(SharedFunctionInfo)
595 : #ifdef OBJECT_PRINT
596 : void PrintSourceCode(std::ostream& os);
597 : #endif
598 :
599 : // Iterate over all shared function infos in a given script.
600 : class ScriptIterator {
601 : public:
602 : ScriptIterator(Isolate* isolate, Script script);
603 : ScriptIterator(Isolate* isolate,
604 : Handle<WeakFixedArray> shared_function_infos);
605 : SharedFunctionInfo Next();
606 113085 : int CurrentIndex() const { return index_ - 1; }
607 :
608 : // Reset the iterator to run on |script|.
609 : void Reset(Script script);
610 :
611 : private:
612 : Isolate* isolate_;
613 : Handle<WeakFixedArray> shared_function_infos_;
614 : int index_;
615 : DISALLOW_COPY_AND_ASSIGN(ScriptIterator);
616 : };
617 :
618 : // Iterate over all shared function infos on the heap.
619 : class GlobalIterator {
620 : public:
621 : explicit GlobalIterator(Isolate* isolate);
622 : SharedFunctionInfo Next();
623 :
624 : private:
625 : Script::Iterator script_iterator_;
626 : WeakArrayList::Iterator noscript_sfi_iterator_;
627 : SharedFunctionInfo::ScriptIterator sfi_iterator_;
628 : DISALLOW_HEAP_ALLOCATION(no_gc_)
629 : DISALLOW_COPY_AND_ASSIGN(GlobalIterator);
630 : };
631 :
632 : DECL_CAST(SharedFunctionInfo)
633 :
634 : // Constants.
635 : static const uint16_t kDontAdaptArgumentsSentinel = static_cast<uint16_t>(-1);
636 :
637 : static const int kMaximumFunctionTokenOffset = kMaxUInt16 - 1;
638 : static const uint16_t kFunctionTokenOutOfRange = static_cast<uint16_t>(-1);
639 : STATIC_ASSERT(kMaximumFunctionTokenOffset + 1 == kFunctionTokenOutOfRange);
640 :
641 : DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
642 : TORQUE_GENERATED_SHARED_FUNCTION_INFO_FIELDS)
643 :
644 : static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
645 :
646 : class BodyDescriptor;
647 :
648 : // Bit positions in |flags|.
649 : #define FLAGS_BIT_FIELDS(V, _) \
650 : /* Have FunctionKind first to make it cheaper to access */ \
651 : V(FunctionKindBits, FunctionKind, 5, _) \
652 : V(IsNativeBit, bool, 1, _) \
653 : V(IsStrictBit, bool, 1, _) \
654 : V(IsWrappedBit, bool, 1, _) \
655 : V(IsClassConstructorBit, bool, 1, _) \
656 : V(HasDuplicateParametersBit, bool, 1, _) \
657 : V(AllowLazyCompilationBit, bool, 1, _) \
658 : V(NeedsHomeObjectBit, bool, 1, _) \
659 : V(IsDeclarationBit, bool, 1, _) \
660 : V(IsAsmWasmBrokenBit, bool, 1, _) \
661 : V(FunctionMapIndexBits, int, 5, _) \
662 : V(DisabledOptimizationReasonBits, BailoutReason, 4, _) \
663 : V(RequiresInstanceMembersInitializer, bool, 1, _) \
664 : V(ConstructAsBuiltinBit, bool, 1, _) \
665 : V(IsAnonymousExpressionBit, bool, 1, _) \
666 : V(NameShouldPrintAsAnonymousBit, bool, 1, _) \
667 : V(HasReportedBinaryCoverageBit, bool, 1, _) \
668 : V(IsNamedExpressionBit, bool, 1, _) \
669 : V(IsTopLevelBit, bool, 1, _) \
670 : V(IsOneshotIIFEBit, bool, 1, _) \
671 : V(IsSafeToSkipArgumentsAdaptorBit, bool, 1, _)
672 : DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
673 : #undef FLAGS_BIT_FIELDS
674 :
675 : // Bailout reasons must fit in the DisabledOptimizationReason bitfield.
676 : STATIC_ASSERT(BailoutReason::kLastErrorMessage <=
677 : DisabledOptimizationReasonBits::kMax);
678 :
679 : STATIC_ASSERT(kLastFunctionKind <= FunctionKindBits::kMax);
680 :
681 : // Indicates that this function uses a super property (or an eval that may
682 : // use a super property).
683 : // This is needed to set up the [[HomeObject]] on the function instance.
684 : inline bool needs_home_object() const;
685 :
686 : V8_INLINE bool IsSharedFunctionInfoWithID() const {
687 : #if V8_SFI_HAS_UNIQUE_ID
688 : return true;
689 : #else
690 : return false;
691 : #endif
692 : }
693 :
694 : private:
695 : // [name_or_scope_info]: Function name string, kNoSharedNameSentinel or
696 : // ScopeInfo.
697 : DECL_ACCESSORS(name_or_scope_info, Object)
698 :
699 : // [outer scope info] The outer scope info, needed to lazily parse this
700 : // function.
701 : DECL_ACCESSORS(outer_scope_info, HeapObject)
702 :
703 : inline void set_kind(FunctionKind kind);
704 :
705 : inline void set_needs_home_object(bool value);
706 :
707 : friend class Factory;
708 : friend class V8HeapExplorer;
709 : FRIEND_TEST(PreParserTest, LazyFunctionLength);
710 :
711 : inline uint16_t length() const;
712 :
713 : // Find the index of this function in the parent script. Slow path of
714 : // FunctionLiteralId.
715 : int FindIndexInScript(Isolate* isolate) const;
716 :
717 : OBJECT_CONSTRUCTORS(SharedFunctionInfo, HeapObject);
718 : };
719 :
720 : class SharedFunctionInfoWithID : public SharedFunctionInfo {
721 : public:
722 : // [unique_id] - For --trace-maps purposes, an identifier that's persistent
723 : // even if the GC moves this SharedFunctionInfo.
724 : DECL_INT_ACCESSORS(unique_id)
725 :
726 : DECL_CAST(SharedFunctionInfoWithID)
727 :
728 : DEFINE_FIELD_OFFSET_CONSTANTS(
729 : SharedFunctionInfo::kSize,
730 : TORQUE_GENERATED_SHARED_FUNCTION_INFO_WITH_ID_FIELDS)
731 :
732 : static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
733 :
734 : OBJECT_CONSTRUCTORS(SharedFunctionInfoWithID, SharedFunctionInfo);
735 : };
736 :
737 : // Printing support.
738 : struct SourceCodeOf {
739 : explicit SourceCodeOf(SharedFunctionInfo v, int max = -1)
740 24 : : value(v), max_length(max) {}
741 : const SharedFunctionInfo value;
742 : int max_length;
743 : };
744 :
745 : // IsCompiledScope enables a caller to check if a function is compiled, and
746 : // ensure it remains compiled (i.e., doesn't have it's bytecode flushed) while
747 : // the scope is retained.
748 : class IsCompiledScope {
749 : public:
750 : inline IsCompiledScope(const SharedFunctionInfo shared, Isolate* isolate);
751 11563674 : inline IsCompiledScope() : retain_bytecode_(), is_compiled_(false) {}
752 :
753 : inline bool is_compiled() const { return is_compiled_; }
754 :
755 : private:
756 : MaybeHandle<BytecodeArray> retain_bytecode_;
757 : bool is_compiled_;
758 : };
759 :
760 : std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v);
761 :
762 : } // namespace internal
763 : } // namespace v8
764 :
765 : #include "src/objects/object-macros-undef.h"
766 :
767 : #endif // V8_OBJECTS_SHARED_FUNCTION_INFO_H_
|