v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1// Copyright 2014 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_HEAP_FACTORY_H_
6#define V8_HEAP_FACTORY_H_
7
8// Clients of this interface shouldn't depend on lots of heap internals.
9// Do not include anything from src/heap here!
10#include "src/base/strings.h"
11#include "src/base/vector.h"
14#include "src/common/globals.h"
16#include "src/handles/handles.h"
19#include "src/heap/heap.h"
22// TODO(leszeks): Remove this by forward declaring JSRegExp::Flags.
24
25namespace unibrow {
26enum class Utf8Variant : uint8_t;
27}
28
29namespace v8 {
30namespace internal {
31
32// Forward declarations.
33class AliasedArgumentsEntry;
34class ObjectBoilerplateDescription;
35class BasicBlockProfilerData;
36class BreakPoint;
37class BreakPointInfo;
38class CallableTask;
39class CallbackTask;
40class CallSiteInfo;
41class Expression;
42class EmbedderDataArray;
43class ArrayBoilerplateDescription;
44class CoverageInfo;
45class DebugInfo;
46class DeoptimizationData;
47class DeoptimizationLiteralArray;
48class DictionaryTemplateInfo;
49class EnumCache;
50class FreshlyAllocatedBigInt;
51class FunctionTemplateInfo;
52class Isolate;
53class JSArrayBufferView;
54class JSDataView;
55class JSDisposableStackBase;
56class JSSyncDisposableStack;
57class JSAsyncDisposableStack;
58class JSGeneratorObject;
59class JSMap;
60class JSMapIterator;
61class JSModuleNamespace;
62class JSPromise;
63class JSProxy;
64class JSSet;
65class JSSetIterator;
66class JSTypedArray;
67class JSWeakMap;
68class LoadHandler;
69class NativeContext;
70class PromiseResolveThenableJobTask;
71class RegExpMatchInfo;
72class ScriptContextTable;
73template <typename>
74class Signature;
75class SourceTextModule;
76class StackFrameInfo;
77class StackTraceInfo;
78class StringSet;
79class StoreHandler;
80class SyntheticModule;
81class TemplateObjectDescription;
82class WasmCapiFunctionData;
83class WasmExportedFunctionData;
84class WasmJSFunctionData;
85class WeakCell;
86
87#if V8_ENABLE_WEBASSEMBLY
88namespace wasm {
89#if V8_ENABLE_DRUMBRAKE
90class WasmInterpreterRuntime;
91#endif // V8_ENABLE_DRUMBRAKE
92
93class ArrayType;
94class StructType;
95class ContType;
96struct WasmElemSegment;
97class WasmValue;
98enum class OnResume : int;
99enum Suspend : int;
100enum Promise : int;
101struct CanonicalTypeIndex;
102class CanonicalValueType;
103class ValueType;
104class CanonicalSig;
105struct ModuleTypeIndex;
106class StackMemory;
107} // namespace wasm
108#endif
109
110enum class SharedFlag : uint8_t;
111enum class InitializedFlag : uint8_t;
112
133
138
139// Interface for handle based allocation.
140class V8_EXPORT_PRIVATE Factory : public FactoryBase<Factory> {
141 public:
142 inline ReadOnlyRoots read_only_roots() const;
143
144 DirectHandle<Hole> NewHole();
145
146 // Allocates a property array initialized with undefined values.
147 DirectHandle<PropertyArray> NewPropertyArray(
148 int length, AllocationType allocation = AllocationType::kYoung);
149 // Tries allocating a fixed array initialized with undefined values.
150 // In case of an allocation failure (OOM) an empty handle is returned.
151 // The caller has to manually signal an
152 // v8::internal::Heap::FatalProcessOutOfMemory typically by calling
153 // NewFixedArray as a fallback.
155 MaybeHandle<FixedArray> TryNewFixedArray(
156 int length, AllocationType allocation = AllocationType::kYoung);
157
158 // Allocates a feedback vector whose slots are initialized with undefined
159 // values.
160 Handle<FeedbackVector> NewFeedbackVector(
162 DirectHandle<ClosureFeedbackCellArray> closure_feedback_cell_array,
163 DirectHandle<FeedbackCell> parent_feedback_cell);
164
165 // Allocates a clean embedder data array with given capacity.
166 DirectHandle<EmbedderDataArray> NewEmbedderDataArray(int length);
167
168 // Allocate a new fixed double array with hole values.
169 DirectHandle<FixedArrayBase> NewFixedDoubleArrayWithHoles(int size);
170
171 // Allocates a NameDictionary with an internal capacity calculated such that
172 // |at_least_space_for| entries can be added without reallocating.
173 DirectHandle<NameDictionary> NewNameDictionary(int at_least_space_for);
174
175 Handle<OrderedHashSet> NewOrderedHashSet();
176 Handle<OrderedHashMap> NewOrderedHashMap();
177 Handle<SmallOrderedHashSet> NewSmallOrderedHashSet(
178 int capacity = kSmallOrderedHashSetMinCapacity,
179 AllocationType allocation = AllocationType::kYoung);
180 Handle<SmallOrderedHashMap> NewSmallOrderedHashMap(
181 int capacity = kSmallOrderedHashMapMinCapacity,
182 AllocationType allocation = AllocationType::kYoung);
183 Handle<SmallOrderedNameDictionary> NewSmallOrderedNameDictionary(
184 int capacity = kSmallOrderedHashMapMinCapacity,
185 AllocationType allocation = AllocationType::kYoung);
186
187 DirectHandle<SwissNameDictionary> CreateCanonicalEmptySwissNameDictionary();
188
189 // Create a new PrototypeInfo struct.
190 DirectHandle<PrototypeInfo> NewPrototypeInfo();
191
192 // Create a new EnumCache struct.
193 DirectHandle<EnumCache> NewEnumCache(
195 AllocationType allocation = AllocationType::kOld);
196
197 // Create a new Tuple2 struct.
198 DirectHandle<Tuple2> NewTuple2Uninitialized(AllocationType allocation);
201 AllocationType allocation);
202
203 // Create a new PropertyDescriptorObject struct.
204 DirectHandle<PropertyDescriptorObject> NewPropertyDescriptorObject();
205
206 // Finds the internalized copy for string in the string table.
207 // If not found, a new string is added to the table and returned.
208 Handle<String> InternalizeUtf8String(base::Vector<const char> str);
210 return InternalizeUtf8String(base::CStrVector(str));
211 }
212
213 // Import InternalizeString overloads from base class.
214 using FactoryBase::InternalizeString;
215
217 bool convert_encoding = false) {
219 convert_encoding);
220 }
221
223 bool convert_encoding = false) {
224 return InternalizeString(base::OneByteVector(str), convert_encoding);
225 }
226
227 template <typename SeqString, template <typename> typename HandleType>
228 requires(
229 std::is_convertible_v<HandleType<SeqString>, DirectHandle<SeqString>>)
230 Handle<String> InternalizeSubString(HandleType<SeqString>, uint32_t from,
231 uint32_t length,
232 bool convert_encoding = false);
233
234 // Internalized strings are created in the old generation (data space).
235 // TODO(b/42203211): InternalizeString and InternalizeName are templatized so
236 // that passing a Handle<T> is not ambiguous when T is a subtype of String or
237 // Name (it could be implicitly converted both to Handle<String> and to
238 // DirectHandle<String>). Here, T should be a subtype of String, which is
239 // enforced by the second template argument and the similar restriction on
240 // Handle's constructor. When the migration to DirectHandle is complete,
241 // these functions can accept simply a DirectHandle<String> or
242 // DirectHandle<Name>.
243 template <typename T>
244 requires(std::is_convertible_v<Handle<T>, Handle<String>>)
246
247 template <typename T>
248 requires(std::is_convertible_v<Handle<T>, Handle<Name>>)
249 inline Handle<Name> InternalizeName(Handle<T> name);
250
251 template <typename T>
252 requires(std::is_convertible_v<DirectHandle<T>, DirectHandle<String>>)
254
255 template <typename T>
256 requires(std::is_convertible_v<DirectHandle<T>, DirectHandle<Name>>)
257 inline DirectHandle<Name> InternalizeName(DirectHandle<T> name);
258
259 // String creation functions. Most of the string creation functions take
260 // an AllocationType argument to optionally request that they be
261 // allocated in the old generation. Otherwise the default is
262 // AllocationType::kYoung.
263 //
264 // Creates a new String object. There are two String encodings: one-byte and
265 // two-byte. One should choose between the three string factory functions
266 // based on the encoding of the string buffer that the string is
267 // initialized from.
268 // - ...FromOneByte (defined in FactoryBase) initializes the string from a
269 // buffer that is Latin1 encoded (it does not check that the buffer is
270 // Latin1 encoded) and the result will be Latin1 encoded.
271 // - ...FromUtf8 initializes the string from a buffer that is UTF-8
272 // encoded. If the characters are all ASCII characters, the result
273 // will be Latin1 encoded, otherwise it will converted to two-byte.
274 // - ...FromTwoByte initializes the string from a buffer that is two-byte
275 // encoded. If the characters are all Latin1 characters, the result
276 // will be converted to Latin1, otherwise it will be left as two-byte.
277 //
278 // One-byte strings are pretenured when used as keys in the SourceCodeCache.
279 template <size_t N>
280 inline Handle<String> NewStringFromStaticChars(
281 const char (&str)[N], AllocationType allocation = AllocationType::kYoung);
282
283 // UTF8 strings are pretenured when used for regexp literal patterns and
284 // flags in the parser.
287 AllocationType allocation = AllocationType::kYoung);
290 AllocationType allocation = AllocationType::kYoung);
291
292#if V8_ENABLE_WEBASSEMBLY
294 DirectHandle<WasmArray> array, uint32_t begin, uint32_t end,
295 unibrow::Utf8Variant utf8_variant,
296 AllocationType allocation = AllocationType::kYoung);
297
299 DirectHandle<ByteArray> array, uint32_t start, uint32_t end,
300 unibrow::Utf8Variant utf8_variant,
301 AllocationType allocation = AllocationType::kYoung);
302
304 DirectHandle<WasmArray> array, uint32_t start, uint32_t end,
305 AllocationType allocation = AllocationType::kYoung);
306#endif // V8_ENABLE_WEBASSEMBLY
307
308 V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromUtf8SubString(
309 Handle<SeqOneByteString> str, int begin, int end,
310 AllocationType allocation = AllocationType::kYoung);
311
312 V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromTwoByte(
314 AllocationType allocation = AllocationType::kYoung);
315
317 const ZoneVector<base::uc16>* str,
318 AllocationType allocation = AllocationType::kYoung);
319
320#if V8_ENABLE_WEBASSEMBLY
321 // Usually the two-byte encodings are in the native endianness, but for
322 // WebAssembly linear memory, they are explicitly little-endian.
324 NewStringFromTwoByteLittleEndian(
326 AllocationType allocation = AllocationType::kYoung);
327#endif // V8_ENABLE_WEBASSEMBLY
328
329 DirectHandle<JSStringIterator> NewJSStringIterator(Handle<String> string);
330
331 DirectHandle<String> NewInternalizedStringImpl(DirectHandle<String> string,
332 int len, uint32_t hash_field);
333
334 // Compute the internalization strategy for the input string.
335 //
336 // Old-generation sequential strings can be internalized by mutating their map
337 // and return kInPlace, along with the matching internalized string map for
338 // string stored in internalized_map.
339 //
340 // Internalized strings return kAlreadyTransitioned.
341 //
342 // All other strings are internalized by flattening and copying and return
343 // kCopy.
345 ComputeInternalizationStrategyForString(
346 DirectHandle<String> string, MaybeDirectHandle<Map>* internalized_map);
347
348 // Creates an internalized copy of an external string. |string| must be
349 // of type StringClass.
350 template <class StringClass>
351 DirectHandle<StringClass> InternalizeExternalString(
352 DirectHandle<String> string);
353
354 // Compute the sharing strategy for the input string.
355 //
356 // Old-generation sequential and thin strings can be shared by mutating their
357 // map and return kInPlace, along with the matching shared string map for the
358 // string stored in shared_map.
359 //
360 // Already-shared strings return kAlreadyTransitioned.
361 //
362 // All other strings are shared by flattening and copying into a sequential
363 // string then sharing that sequential string, and return kCopy.
365 ComputeSharingStrategyForString(DirectHandle<String> string,
366 MaybeDirectHandle<Map>* shared_map);
367
368 // Create or lookup a single character string made up of a utf16 surrogate
369 // pair.
370 DirectHandle<String> NewSurrogatePairString(uint16_t lead, uint16_t trail);
371
372 // Create a new string object which holds a proper substring of a string.
373 Handle<String> NewProperSubString(DirectHandle<String> str, uint32_t begin,
374 uint32_t end);
375 // Same, but always copies (never creates a SlicedString).
376 // {str} must be flat, {length} must be non-zero.
377 Handle<String> NewCopiedSubstring(DirectHandle<String> str, uint32_t begin,
378 uint32_t length);
379
380 // Create a new string object which holds a substring of a string.
381 template <typename T, template <typename> typename HandleType>
382 requires(std::is_convertible_v<HandleType<T>, HandleType<String>>)
383 inline HandleType<String> NewSubString(HandleType<T> str, uint32_t begin,
384 uint32_t end);
385
386 // Creates a new external String object. There are two String encodings
387 // in the system: one-byte and two-byte. Unlike other String types, it does
388 // not make sense to have a UTF-8 factory function for external strings,
389 // because we cannot change the underlying buffer. Note that these strings
390 // are backed by a string resource that resides outside the V8 heap.
391 V8_WARN_UNUSED_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
393 V8_WARN_UNUSED_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
394 const v8::String::ExternalStringResource* resource);
395
396 // Create a symbol in old or read-only space.
397 Handle<Symbol> NewSymbol(AllocationType allocation = AllocationType::kOld);
398 Handle<Symbol> NewPrivateSymbol(
399 AllocationType allocation = AllocationType::kOld);
400 DirectHandle<Symbol> NewPrivateNameSymbol(DirectHandle<String> name);
401
402 // Create a global (but otherwise uninitialized) context.
403 Handle<NativeContext> NewNativeContext();
404
405 // Create a script context.
407 DirectHandle<ScopeInfo> scope_info);
408
409 // Create an empty script context table.
410 Handle<ScriptContextTable> NewScriptContextTable();
411
412 // Create a module context.
415 DirectHandle<ScopeInfo> scope_info);
416
417 // Create a function or eval context.
418 DirectHandle<Context> NewFunctionContext(DirectHandle<Context> outer,
419 DirectHandle<ScopeInfo> scope_info);
420
421 // Create a catch context.
423 DirectHandle<ScopeInfo> scope_info,
424 DirectHandle<Object> thrown_object);
425
426 // Create a 'with' context.
428 DirectHandle<ScopeInfo> scope_info,
430
431 Handle<Context> NewDebugEvaluateContext(DirectHandle<Context> previous,
432 DirectHandle<ScopeInfo> scope_info,
434 DirectHandle<Context> wrapped);
435
436 // Create a block context.
438 DirectHandle<ScopeInfo> scope_info);
439
440 // Create a context that's used by builtin functions.
441 //
442 // These are similar to function context but don't have a previous
443 // context or any scope info. These are used to store spec defined
444 // context values.
445 DirectHandle<Context> NewBuiltinContext(
446 DirectHandle<NativeContext> native_context, int length);
447
448 DirectHandle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
449 int aliased_context_slot);
450
451 DirectHandle<AccessorInfo> NewAccessorInfo();
452
453 DirectHandle<InterceptorInfo> NewInterceptorInfo(
454 AllocationType allocation = AllocationType::kOld);
455
456 DirectHandle<ErrorStackData> NewErrorStackData(
458 call_site_infos_or_formatted_stack,
459 DirectHandle<StackTraceInfo> stack_trace);
460
461 Handle<Script> CloneScript(DirectHandle<Script> script,
462 DirectHandle<String> source);
463
464 DirectHandle<BreakPointInfo> NewBreakPointInfo(int source_position);
466
467 Handle<CallSiteInfo> NewCallSiteInfo(
468 DirectHandle<JSAny> receiver_or_instance,
470 DirectHandle<HeapObject> code_object, int code_offset_or_source_position,
471 int flags, DirectHandle<FixedArray> parameters);
472 DirectHandle<StackFrameInfo> NewStackFrameInfo(
474 int bytecode_offset_or_source_position,
475 DirectHandle<String> function_name, bool is_constructor);
476 Handle<StackTraceInfo> NewStackTraceInfo(DirectHandle<FixedArray> frames);
477
478 // Allocate various microtasks.
480 DirectHandle<Context> context);
483 DirectHandle<PromiseResolveThenableJobTask> NewPromiseResolveThenableJobTask(
484 DirectHandle<JSPromise> promise_to_resolve,
486 DirectHandle<Context> context);
487
488 // Foreign objects are pretenured when allocated by the bootstrapper.
489 template <ExternalPointerTag tag>
490 Handle<Foreign> NewForeign(
491 Address addr, AllocationType allocation_type = AllocationType::kYoung);
492
493 Handle<TrustedForeign> NewTrustedForeign(Address addr);
494
495 Handle<Cell> NewCell(Tagged<Smi> value);
496 Handle<Cell> NewCell();
497
498 Handle<PropertyCell> NewPropertyCell(
501 AllocationType allocation = AllocationType::kOld);
502 DirectHandle<ContextSidePropertyCell> NewContextSidePropertyCell(
504 AllocationType allocation = AllocationType::kOld);
505 DirectHandle<PropertyCell> NewProtector();
506
507 DirectHandle<FeedbackCell> NewNoClosuresCell();
508 DirectHandle<FeedbackCell> NewOneClosureCell(
510 DirectHandle<FeedbackCell> NewManyClosuresCell(
511 AllocationType allocation = AllocationType::kOld);
512
513 DirectHandle<TransitionArray> NewTransitionArray(int number_of_transitions,
514 int slack = 0);
515
516 // Allocate a tenured AllocationSite. Its payload is null.
517 Handle<AllocationSite> NewAllocationSite(bool with_weak_next);
518
519 // Allocates and initializes a new Map.
520 Handle<Map> NewMap(DirectHandle<HeapObject> meta_map_holder,
521 InstanceType type, int instance_size,
522 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
523 int inobject_properties = 0,
524 AllocationType allocation_type = AllocationType::kMap);
525
526 DirectHandle<Map> NewMapWithMetaMap(
527 DirectHandle<Map> meta_map, InstanceType type, int instance_size,
528 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
529 int inobject_properties = 0,
530 AllocationType allocation_type = AllocationType::kMap);
531
532 DirectHandle<Map> NewContextfulMap(
533 DirectHandle<JSReceiver> creation_context_holder, InstanceType type,
534 int instance_size,
535 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
536 int inobject_properties = 0,
537 AllocationType allocation_type = AllocationType::kMap);
538
539 DirectHandle<Map> NewContextfulMap(
540 DirectHandle<NativeContext> native_context, InstanceType type,
541 int instance_size,
542 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
543 int inobject_properties = 0,
544 AllocationType allocation_type = AllocationType::kMap);
545
546 Handle<Map> NewContextfulMapForCurrentContext(
547 InstanceType type, int instance_size,
548 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
549 int inobject_properties = 0,
550 AllocationType allocation_type = AllocationType::kMap);
551
552 Handle<Map> NewContextlessMap(
553 InstanceType type, int instance_size,
554 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
555 int inobject_properties = 0,
556 AllocationType allocation_type = AllocationType::kMap);
557
558 // Initializes the fields of a newly created Map using roots from the
559 // passed-in Heap. Exposed for tests and heap setup; other code should just
560 // call NewMap which takes care of it.
561 Tagged<Map> InitializeMap(Tagged<Map> map, InstanceType type,
562 int instance_size, ElementsKind elements_kind,
563 int inobject_properties, ReadOnlyRoots roots);
564
565 // Allocate a block of memory of the given AllocationType (filled with a
566 // filler). Used as a fall-back for generated code when the space is full.
567 DirectHandle<HeapObject> NewFillerObject(
568 int size, AllocationAlignment alignment, AllocationType allocation,
569 AllocationOrigin origin = AllocationOrigin::kRuntime);
570
571 DirectHandle<JSObject> NewFunctionPrototype(
572 DirectHandle<JSFunction> function);
573
574 // Returns a deep copy of the JavaScript object.
575 // Properties and elements are copied too.
576 Handle<JSObject> CopyJSObject(DirectHandle<JSObject> object);
577 // Same as above, but also takes an AllocationSite to be appended in an
578 // AllocationMemento.
579 Handle<JSObject> CopyJSObjectWithAllocationSite(
581
582 Handle<FixedArray> CopyFixedArrayWithMap(
584 AllocationType allocation = AllocationType::kYoung);
585
586 Handle<FixedArray> CopyFixedArrayAndGrow(
587 DirectHandle<FixedArray> array, int grow_by,
588 AllocationType allocation = AllocationType::kYoung);
589
590 DirectHandle<WeakArrayList> NewWeakArrayList(
591 int capacity, AllocationType allocation = AllocationType::kYoung);
592
593 DirectHandle<WeakFixedArray> CopyWeakFixedArray(
595
596 DirectHandle<WeakFixedArray> CopyWeakFixedArrayAndGrow(
597 DirectHandle<WeakFixedArray> array, int grow_by);
598
599 Handle<WeakArrayList> CopyWeakArrayListAndGrow(
600 DirectHandle<WeakArrayList> array, int grow_by,
601 AllocationType allocation = AllocationType::kYoung);
602
603 DirectHandle<WeakArrayList> CompactWeakArrayList(
604 DirectHandle<WeakArrayList> array, int new_capacity,
605 AllocationType allocation = AllocationType::kYoung);
606
607 DirectHandle<PropertyArray> CopyPropertyArrayAndGrow(
608 DirectHandle<PropertyArray> array, int grow_by);
609
610 Handle<FixedArray> CopyFixedArrayUpTo(
611 DirectHandle<FixedArray> array, int new_len,
612 AllocationType allocation = AllocationType::kYoung);
613
614 Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
615
616 Handle<FixedDoubleArray> CopyFixedDoubleArray(Handle<FixedDoubleArray> array);
617
618 // Creates a new HeapNumber in read-only space if possible otherwise old
619 // space.
620 Handle<HeapNumber> NewHeapNumberForCodeAssembler(double value);
621
622 Handle<JSObject> NewArgumentsObject(DirectHandle<JSFunction> callee,
623 int length);
624
625 // Allocates and initializes a new JavaScript object based on a
626 // constructor.
627 // JS objects are pretenured when allocated by the bootstrapper and
628 // runtime.
629 Handle<JSObject> NewJSObject(
630 DirectHandle<JSFunction> constructor,
631 AllocationType allocation = AllocationType::kYoung,
632 NewJSObjectType = NewJSObjectType::kNoAPIWrapper);
633 // JSObject without a prototype.
634 Handle<JSObject> NewJSObjectWithNullProto();
635 // JSObject without a prototype, in dictionary mode.
636 Handle<JSObject> NewSlowJSObjectWithNullProto();
637
638 // Global objects are pretenured and initialized based on a constructor.
639 Handle<JSGlobalObject> NewJSGlobalObject(
640 DirectHandle<JSFunction> constructor);
641
642 // Allocates and initializes a new JavaScript object based on a map.
643 // Passing an allocation site means that a memento will be created that
644 // points to the site.
645 // JS objects are pretenured when allocated by the bootstrapper and
646 // runtime.
647 Handle<JSObject> NewJSObjectFromMap(
648 DirectHandle<Map> map, AllocationType allocation = AllocationType::kYoung,
649 DirectHandle<AllocationSite> allocation_site =
651 NewJSObjectType = NewJSObjectType::kNoAPIWrapper);
652 // Like NewJSObjectFromMap, but includes allocating a properties dictionary.);
653 Handle<JSObject> NewSlowJSObjectFromMap(
654 DirectHandle<Map> map, int number_of_slow_properties,
655 AllocationType allocation = AllocationType::kYoung,
656 DirectHandle<AllocationSite> allocation_site =
658 NewJSObjectType = NewJSObjectType::kNoAPIWrapper);
659 Handle<JSObject> NewSlowJSObjectFromMap(DirectHandle<Map> map);
660 // Calls NewJSObjectFromMap or NewSlowJSObjectFromMap depending on whether the
661 // map is a dictionary map.
662 inline Handle<JSObject> NewFastOrSlowJSObjectFromMap(
663 DirectHandle<Map> map, int number_of_slow_properties,
664 AllocationType allocation = AllocationType::kYoung,
665 DirectHandle<AllocationSite> allocation_site =
667 NewJSObjectType = NewJSObjectType::kNoAPIWrapper);
668 inline Handle<JSObject> NewFastOrSlowJSObjectFromMap(DirectHandle<Map> map);
669 // Allocates and initializes a new JavaScript object with the given
670 // {prototype} and {properties}. The newly created object will be
671 // in dictionary properties mode. The {elements} can either be the
672 // empty fixed array, in which case the resulting object will have
673 // fast elements, or a NumberDictionary, in which case the resulting
674 // object will have dictionary elements.
675 DirectHandle<JSObject> NewSlowJSObjectWithPropertiesAndElements(
678
679 // JS arrays are pretenured when allocated by the parser.
680
681 // Create a JSArray with a specified length and elements initialized
682 // according to the specified mode.
683 Handle<JSArray> NewJSArray(
684 ElementsKind elements_kind, int length, int capacity,
686 ArrayStorageAllocationMode::DONT_INITIALIZE_ARRAY_ELEMENTS,
687 AllocationType allocation = AllocationType::kYoung);
688
690 int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
691 AllocationType allocation = AllocationType::kYoung) {
692 if (capacity != 0) {
693 elements_kind = GetHoleyElementsKind(elements_kind);
694 }
695 return NewJSArray(
696 elements_kind, 0, capacity,
697 ArrayStorageAllocationMode::INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
698 allocation);
699 }
700
701 // Create a JSArray with the given elements.
702 Handle<JSArray> NewJSArrayWithElements(
703 DirectHandle<FixedArrayBase> elements, ElementsKind elements_kind,
704 int length, AllocationType allocation = AllocationType::kYoung);
705
706 inline Handle<JSArray> NewJSArrayWithElements(
708 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
709 AllocationType allocation = AllocationType::kYoung);
710
711 DirectHandle<JSArray> NewJSArrayForTemplateLiteralArray(
712 DirectHandle<FixedArray> cooked_strings,
713 DirectHandle<FixedArray> raw_strings, int function_literal_id,
714 int slot_id);
715
716 void NewJSArrayStorage(
717 DirectHandle<JSArray> array, int length, int capacity,
719 ArrayStorageAllocationMode::DONT_INITIALIZE_ARRAY_ELEMENTS);
720
721 Handle<JSWeakMap> NewJSWeakMap();
722
723 DirectHandle<JSGeneratorObject> NewJSGeneratorObject(
724 DirectHandle<JSFunction> function);
725
726 DirectHandle<JSModuleNamespace> NewJSModuleNamespace();
727
728 DirectHandle<JSWrappedFunction> NewJSWrappedFunction(
729 DirectHandle<NativeContext> creation_context,
730 DirectHandle<Object> target);
731
732 DirectHandle<JSDisposableStackBase> NewJSDisposableStackBase();
733 DirectHandle<JSSyncDisposableStack> NewJSSyncDisposableStack(
735 DirectHandle<JSAsyncDisposableStack> NewJSAsyncDisposableStack(
737
738#if V8_ENABLE_WEBASSEMBLY
739 DirectHandle<WasmTrustedInstanceData> NewWasmTrustedInstanceData();
740 DirectHandle<WasmDispatchTable> NewWasmDispatchTable(
741 int length, wasm::CanonicalValueType table_type);
742 DirectHandle<WasmTypeInfo> NewWasmTypeInfo(
744 DirectHandle<Map> opt_parent);
745 DirectHandle<WasmInternalFunction> NewWasmInternalFunction(
746 DirectHandle<TrustedObject> ref, int function_index);
747 DirectHandle<WasmFuncRef> NewWasmFuncRef(
748 DirectHandle<WasmInternalFunction> internal_function,
750 DirectHandle<WasmCapiFunctionData> NewWasmCapiFunctionData(
751 Address call_target, DirectHandle<Foreign> embedder_data,
752 DirectHandle<Code> wrapper_code, DirectHandle<Map> rtt,
753 wasm::CanonicalTypeIndex sig_index, const wasm::CanonicalSig* sig);
754 DirectHandle<WasmExportedFunctionData> NewWasmExportedFunctionData(
755 DirectHandle<Code> export_wrapper,
758 DirectHandle<WasmInternalFunction> internal_function,
759 const wasm::CanonicalSig* sig, wasm::CanonicalTypeIndex type_index,
760 int wrapper_budget, wasm::Promise promise);
761 DirectHandle<WasmImportData> NewWasmImportData(
762 DirectHandle<HeapObject> callable, wasm::Suspend suspend,
764 const wasm::CanonicalSig* sig);
765 DirectHandle<WasmImportData> NewWasmImportData(
767
768 DirectHandle<WasmFastApiCallData> NewWasmFastApiCallData(
769 DirectHandle<HeapObject> signature, DirectHandle<Object> callback_data);
770
771 // {opt_call_target} is kNullAddress for JavaScript functions, and
772 // non-null for exported Wasm functions.
773 DirectHandle<WasmJSFunctionData> NewWasmJSFunctionData(
775 DirectHandle<Code> wrapper_code, DirectHandle<Map> rtt,
776 wasm::Suspend suspend, wasm::Promise promise);
777 DirectHandle<WasmResumeData> NewWasmResumeData(
779 DirectHandle<WasmSuspenderObject> NewWasmSuspenderObject();
780 DirectHandle<WasmStruct> NewWasmStruct(const wasm::StructType* type,
783 // The resulting struct will be uninitialized, which means GC might fail for
784 // reference structs until initialization. Follow this up with a
785 // {DisallowGarbageCollection} scope until initialization.
786 Handle<WasmStruct> NewWasmStructUninitialized(
787 const wasm::StructType* type, DirectHandle<Map> map,
788 AllocationType allocation = AllocationType::kYoung);
789
790 DirectHandle<WasmArray> NewWasmArray(wasm::ValueType element_type,
791 uint32_t length,
792 wasm::WasmValue initial_value,
794 DirectHandle<WasmArray> NewWasmArrayFromElements(
797 DirectHandle<WasmArray> NewWasmArrayFromMemory(
798 uint32_t length, DirectHandle<Map> map,
799 wasm::CanonicalValueType element_type, Address source);
800 // Returns a handle to a WasmArray if successful, or a Smi containing a
801 // {MessageTemplate} if computing the array's elements leads to an error.
802 DirectHandle<Object> NewWasmArrayFromElementSegment(
803 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
804 DirectHandle<WasmTrustedInstanceData> shared_trusted_instance_data,
805 uint32_t segment_index, uint32_t start_offset, uint32_t length,
807
808 DirectHandle<SharedFunctionInfo> NewSharedFunctionInfoForWasmExportedFunction(
810 int len, AdaptArguments adapt);
811 DirectHandle<SharedFunctionInfo> NewSharedFunctionInfoForWasmJSFunction(
813 DirectHandle<SharedFunctionInfo> NewSharedFunctionInfoForWasmResume(
815 DirectHandle<SharedFunctionInfo> NewSharedFunctionInfoForWasmCapiFunction(
817#endif // V8_ENABLE_WEBASSEMBLY
818
819 DirectHandle<SourceTextModule> NewSourceTextModule(
821 Handle<SyntheticModule> NewSyntheticModule(
822 DirectHandle<String> module_name, DirectHandle<FixedArray> export_names,
824
825 Handle<JSArrayBuffer> NewJSArrayBuffer(
826 std::shared_ptr<BackingStore> backing_store,
827 AllocationType allocation = AllocationType::kYoung);
828
829 MaybeHandle<JSArrayBuffer> NewJSArrayBufferAndBackingStore(
830 size_t byte_length, InitializedFlag initialized,
831 AllocationType allocation = AllocationType::kYoung);
832
833 MaybeHandle<JSArrayBuffer> NewJSArrayBufferAndBackingStore(
834 size_t byte_length, size_t max_byte_length, InitializedFlag initialized,
835 ResizableFlag resizable = ResizableFlag::kNotResizable,
836 AllocationType allocation = AllocationType::kYoung);
837
838 Handle<JSArrayBuffer> NewJSSharedArrayBuffer(
839 std::shared_ptr<BackingStore> backing_store);
840
841 static void TypeAndSizeForElementsKind(ElementsKind kind,
842 ExternalArrayType* array_type,
843 size_t* element_size);
844
845 // Creates a new JSTypedArray with the specified buffer.
846 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
848 size_t byte_offset, size_t length,
849 bool is_length_tracking = false);
850
851 Handle<JSDataViewOrRabGsabDataView> NewJSDataViewOrRabGsabDataView(
852 DirectHandle<JSArrayBuffer> buffer, size_t byte_offset,
853 size_t byte_length, bool is_length_tracking = false);
854
856 bool done);
857 DirectHandle<JSAsyncFromSyncIterator> NewJSAsyncFromSyncIterator(
859
860 DirectHandle<JSMap> NewJSMap();
861 DirectHandle<JSSet> NewJSSet();
862
863 // Allocates a bound function. If direct handles are enabled, it is the
864 // responsibility of the caller to ensure that the memory pointed to by
865 // `bound_args` is scanned during CSS, e.g., it comes from a
866 // `DirectHandleVector<Object>`.
867 MaybeDirectHandle<JSBoundFunction> NewJSBoundFunction(
868 DirectHandle<JSReceiver> target_function, DirectHandle<JSAny> bound_this,
870 DirectHandle<JSPrototype> prototype);
871
872 // Allocates a Harmony proxy.
875
876 // Reinitialize an JSGlobalProxy based on a constructor. The object
877 // must have the same size as objects allocated using the
878 // constructor. The object is reinitialized and behaves as an
879 // object that has been freshly allocated using the constructor.
880 void ReinitializeJSGlobalProxy(DirectHandle<JSGlobalProxy> global,
881 DirectHandle<JSFunction> constructor);
882
883 Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy(int size);
884
885 // For testing only. Creates a sloppy function without code.
886 Handle<JSFunction> NewFunctionForTesting(DirectHandle<String> name);
887
888 // Create an External object for V8's external API.
889 Handle<JSObject> NewExternal(
890 void* value, AllocationType allocation = AllocationType::kYoung);
891
892 // Allocates a new code object and initializes it to point to the given
893 // off-heap entry point.
894 //
895 // Note it wouldn't be strictly necessary to create new Code objects, instead
896 // Code::instruction_start and instruction_stream could be reset. But creating
897 // a new Code object doesn't hurt much (it only makes mksnapshot slightly more
898 // expensive) and has real benefits:
899 // - it moves all special-casing to mksnapshot-time; at normal runtime, the
900 // system only sees a) non-builtin Code objects (not in RO space, with
901 // instruction_stream set) and b) builtin Code objects (maybe in RO space,
902 // without instruction_stream set).
903 // - it's a convenient bottleneck to make the RO-space allocation decision.
904 DirectHandle<Code> NewCodeObjectForEmbeddedBuiltin(DirectHandle<Code> code,
905 Address off_heap_entry);
906
908
909 // Interface for creating error objects.
910 Handle<JSObject> NewError(DirectHandle<JSFunction> constructor,
911 DirectHandle<String> message,
912 DirectHandle<Object> options = {});
913
914 DirectHandle<Object> NewInvalidStringLengthError();
915
916 inline DirectHandle<Object> NewURIError();
917
918 Handle<JSObject> NewError(DirectHandle<JSFunction> constructor,
919 MessageTemplate template_index,
920 base::Vector<const DirectHandle<Object>> args);
921
922 DirectHandle<JSObject> NewSuppressedErrorAtDisposal(
923 Isolate* isolate, DirectHandle<Object> error,
924 DirectHandle<Object> suppressed_error);
925
926 template <typename... Args>
928 MessageTemplate template_index, Args... args)
929 requires(
930 std::conjunction_v<std::is_convertible<Args, DirectHandle<Object>>...>)
931 {
932 return NewError(constructor, template_index,
933 base::VectorOf<DirectHandle<Object>>({args...}));
934 }
935
936 // https://tc39.es/proposal-shadowrealm/#sec-create-type-error-copy
937 DirectHandle<JSObject> ShadowRealmNewTypeErrorCopy(
938 DirectHandle<Object> original, MessageTemplate template_index,
940
941 template <typename... Args>
943 DirectHandle<Object> original, MessageTemplate template_index,
944 Args... args)
945 requires(
946 std::conjunction_v<std::is_convertible<Args, DirectHandle<Object>>...>)
947 {
948 return ShadowRealmNewTypeErrorCopy(
949 original, template_index,
950 base::VectorOf<DirectHandle<Object>>({args...}));
951 }
952
953#define DECLARE_ERROR(NAME) \
954 Handle<JSObject> New##NAME(MessageTemplate template_index, \
955 base::Vector<const DirectHandle<Object>> args); \
956 \
957 template <typename... Args> \
958 requires(std::is_convertible_v<Args, DirectHandle<Object>> && ...) \
959 Handle<JSObject> New##NAME(MessageTemplate template_index, Args... args) { \
960 return New##NAME(template_index, \
961 base::VectorOf<DirectHandle<Object>>({args...})); \
962 }
963 DECLARE_ERROR(Error)
964 DECLARE_ERROR(EvalError)
965 DECLARE_ERROR(RangeError)
966 DECLARE_ERROR(ReferenceError)
967 DECLARE_ERROR(SuppressedError)
968 DECLARE_ERROR(SyntaxError)
969 DECLARE_ERROR(TypeError)
970 DECLARE_ERROR(WasmCompileError)
971 DECLARE_ERROR(WasmLinkError)
972 DECLARE_ERROR(WasmSuspendError)
973 DECLARE_ERROR(WasmRuntimeError)
974 DECLARE_ERROR(WasmExceptionError)
975#undef DECLARE_ERROR
976
977 Handle<String> SizeToString(size_t value, bool check_cache = true);
979 bool check_cache = true) {
980 return SizeToString(value, check_cache);
981 }
982
983#define ROOT_ACCESSOR(Type, name, CamelName) inline Handle<Type> name();
985#undef ROOT_ACCESSOR
986
987 // Allocates a new SharedFunctionInfo object.
988 Handle<SharedFunctionInfo> NewSharedFunctionInfoForApiFunction(
989 MaybeDirectHandle<String> maybe_name,
990 DirectHandle<FunctionTemplateInfo> function_template_info,
991 FunctionKind kind);
992
993 Handle<SharedFunctionInfo> NewSharedFunctionInfoForBuiltin(
994 MaybeDirectHandle<String> name, Builtin builtin, int len,
995 AdaptArguments adapt, FunctionKind kind = FunctionKind::kNormalFunction);
996
997 DirectHandle<InterpreterData> NewInterpreterData(
998 DirectHandle<BytecodeArray> bytecode_array, DirectHandle<Code> code);
999
1000 static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
1001 return (function_mode & kWithPrototypeBits) != 0;
1002 }
1003
1005 return (function_mode & kWithWritablePrototypeBit) != 0;
1006 }
1007
1008 static bool IsFunctionModeWithName(FunctionMode function_mode) {
1009 return (function_mode & kWithNameBit) != 0;
1010 }
1011
1012 DirectHandle<Map> CreateSloppyFunctionMap(
1013 FunctionMode function_mode,
1014 MaybeDirectHandle<JSFunction> maybe_empty_function);
1015
1016 DirectHandle<Map> CreateStrictFunctionMap(
1017 FunctionMode function_mode, DirectHandle<JSFunction> empty_function);
1018
1019 DirectHandle<Map> CreateClassFunctionMap(
1020 DirectHandle<JSFunction> empty_function);
1021
1022 // Allocates a new JSMessageObject object.
1023 Handle<JSMessageObject> NewJSMessageObject(
1024 MessageTemplate message, DirectHandle<Object> argument,
1025 int start_position, int end_position,
1026 DirectHandle<SharedFunctionInfo> shared_info, int bytecode_offset,
1027 DirectHandle<Script> script,
1028 DirectHandle<StackTraceInfo> stack_trace =
1030
1032
1033 // Return a map for given number of properties using the map cache in the
1034 // native context.
1035 Handle<Map> ObjectLiteralMapFromCache(
1036 DirectHandle<NativeContext> native_context, int number_of_properties);
1037
1038 Handle<LoadHandler> NewLoadHandler(
1039 int data_count, AllocationType allocation = AllocationType::kOld);
1040 Handle<StoreHandler> NewStoreHandler(int data_count);
1041 DirectHandle<MegaDomHandler> NewMegaDomHandler(
1043
1044 // Creates a new FixedArray that holds the data associated with the
1045 // atom regexp and stores it in the regexp.
1046 void SetRegExpAtomData(DirectHandle<JSRegExp> regexp,
1047 DirectHandle<String> source, JSRegExp::Flags flags,
1048 DirectHandle<String> match_pattern);
1049
1050 // Creates a new FixedArray that holds the data associated with the
1051 // irregexp regexp and stores it in the regexp.
1052 void SetRegExpIrregexpData(DirectHandle<JSRegExp> regexp,
1053 DirectHandle<String> source, JSRegExp::Flags flags,
1054 int capture_count, uint32_t backtrack_limit);
1055
1056 // Creates a new FixedArray that holds the data associated with the
1057 // experimental regexp and stores it in the regexp.
1058 void SetRegExpExperimentalData(DirectHandle<JSRegExp> regexp,
1059 DirectHandle<String> source,
1060 JSRegExp::Flags flags, int capture_count);
1061
1062 DirectHandle<RegExpData> NewAtomRegExpData(DirectHandle<String> source,
1063 JSRegExp::Flags flags,
1065 DirectHandle<RegExpData> NewIrRegExpData(DirectHandle<String> source,
1066 JSRegExp::Flags flags,
1067 int capture_count,
1068 uint32_t backtrack_limit);
1069 DirectHandle<RegExpData> NewExperimentalRegExpData(
1070 DirectHandle<String> source, JSRegExp::Flags flags, int capture_count);
1071
1072 // Returns the value for a known global constant (a property of the global
1073 // object which is neither configurable nor writable) like 'undefined'.
1074 // Returns a null handle when the given name is unknown.
1075 DirectHandle<Object> GlobalConstantFor(DirectHandle<Name> name);
1076
1077 // Converts the given ToPrimitive hint to its string representation.
1078 DirectHandle<String> ToPrimitiveHintString(ToPrimitiveHint hint);
1079
1080 Handle<JSPromise> NewJSPromiseWithoutHook();
1081 Handle<JSPromise> NewJSPromise();
1082
1084 AllocationType allocation) {
1085 return New(map, allocation);
1086 }
1087
1088 DirectHandle<JSSharedStruct> NewJSSharedStruct(
1089 DirectHandle<JSFunction> constructor,
1090 MaybeDirectHandle<NumberDictionary> maybe_elements_template);
1091
1092 DirectHandle<JSSharedArray> NewJSSharedArray(
1093 DirectHandle<JSFunction> constructor, int length);
1094
1095 Handle<JSAtomicsMutex> NewJSAtomicsMutex();
1096
1097 Handle<JSAtomicsCondition> NewJSAtomicsCondition();
1098
1099 DirectHandle<FunctionTemplateInfo> NewFunctionTemplateInfo(int length,
1100 bool do_not_cache);
1101
1102 DirectHandle<ObjectTemplateInfo> NewObjectTemplateInfo(
1103 DirectHandle<FunctionTemplateInfo> constructor, bool do_not_cache);
1104
1105 DirectHandle<DictionaryTemplateInfo> NewDictionaryTemplateInfo(
1106 DirectHandle<FixedArray> property_names);
1107
1108 // Helper class for creating JSFunction objects.
1110 public:
1112 DirectHandle<Context> context);
1113
1115
1117 maybe_map_ = v;
1118 return *this;
1119 }
1121 allocation_type_ = v;
1122 return *this;
1123 }
1125 maybe_feedback_cell_ = v;
1126 return *this;
1127 }
1128
1129 private:
1131
1137 AllocationType allocation_type_ = AllocationType::kOld;
1138
1139 friend class Factory;
1140 };
1141
1142 // Allows creation of InstructionStream objects. It provides two build
1143 // methods, one of which tries to gracefully handle allocation failure.
1145 public:
1146 CodeBuilder(Isolate* isolate, const CodeDesc& desc, CodeKind kind);
1147
1148 // TODO(victorgomes): Remove Isolate dependency from CodeBuilder.
1149 CodeBuilder(LocalIsolate* local_isolate, const CodeDesc& desc,
1150 CodeKind kind);
1151
1152 // Builds a new code object (fully initialized). All header fields of the
1153 // associated InstructionStream are immutable and the InstructionStream
1154 // object is write protected.
1156 // Like Build, builds a new code object. May return an empty handle if the
1157 // allocation fails.
1159
1160 // Sets the self-reference object in which a reference to the code object is
1161 // stored. This allows generated code to reference its own InstructionStream
1162 // object by using this handle.
1164 DCHECK(!self_reference.is_null());
1165 self_reference_ = self_reference;
1166 return *this;
1167 }
1168
1170 DCHECK_IMPLIES(builtin != Builtin::kNoBuiltinId,
1172 builtin_ = builtin;
1173 return *this;
1174 }
1175
1178 inlined_bytecode_size_ = size;
1179 return *this;
1180 }
1181
1184 osr_offset_ = offset;
1185 return *this;
1186 }
1187
1189 DCHECK_NE(kind_, CodeKind::BASELINE);
1190 DCHECK(!table.is_null());
1191 source_position_table_ = table;
1192 return *this;
1193 }
1194
1195 inline CodeBuilder& set_empty_source_position_table();
1196
1198 DCHECK_EQ(kind_, CodeKind::BASELINE);
1199 DCHECK(!table.is_null());
1200 bytecode_offset_table_ = table;
1201 return *this;
1202 }
1203
1205 Handle<DeoptimizationData> deopt_data) {
1206 DCHECK_NE(kind_, CodeKind::BASELINE);
1207 DCHECK(!deopt_data.is_null());
1208 deoptimization_data_ = deopt_data;
1209 return *this;
1210 }
1211
1212 inline CodeBuilder& set_interpreter_data(
1213 Handle<TrustedObject> interpreter_data);
1214
1217 is_context_specialized_ = true;
1218 return *this;
1219 }
1220
1223 is_turbofanned_ = true;
1224 return *this;
1225 }
1226
1228 stack_slots_ = stack_slots;
1229 return *this;
1230 }
1231
1233 parameter_count_ = parameter_count;
1234 return *this;
1235 }
1236
1238 profiler_data_ = profiler_data;
1239 return *this;
1240 }
1241
1242 private:
1243 MaybeHandle<Code> BuildInternal(bool retry_allocation_or_fail);
1244
1245 DirectHandle<TrustedByteArray> NewTrustedByteArray(int length);
1246 // Return an allocation suitable for InstructionStreams but without writing
1247 // the map.
1248 Tagged<HeapObject> AllocateUninitializedInstructionStream(
1249 bool retry_allocation_or_fail);
1250 Handle<Code> NewCode(const NewCodeOptions& options);
1251
1256
1258 Builtin builtin_ = Builtin::kNoBuiltinId;
1259 uint32_t inlined_bytecode_size_ = 0;
1260 BytecodeOffset osr_offset_ = BytecodeOffset::None();
1265 BasicBlockProfilerData* profiler_data_ = nullptr;
1266 bool is_context_specialized_ = false;
1267 bool is_turbofanned_ = false;
1268 uint32_t stack_slots_ = 0;
1269 uint16_t parameter_count_ = 0;
1270 };
1271
1272 private:
1273 friend class FactoryBase<Factory>;
1274
1275 // ------
1276 // Customization points for FactoryBase
1277 Tagged<HeapObject> AllocateRaw(
1278 int size, AllocationType allocation,
1279 AllocationAlignment alignment = kTaggedAligned);
1280
1281 Isolate* isolate() const {
1282 // Downcast to the privately inherited sub-class using c-style casts to
1283 // avoid undefined behavior (as static_cast cannot cast across private
1284 // bases).
1285 // NOLINTNEXTLINE (google-readability-casting)
1286 return (Isolate*)this; // NOLINT(readability/casting)
1287 }
1288
1289 V8_INLINE HeapAllocator* allocator() const;
1290
1291 bool CanAllocateInReadOnlySpace();
1292 bool EmptyStringRootIsInitialized();
1293 AllocationType AllocationTypeForInPlaceInternalizableString();
1294
1295 void ProcessNewScript(DirectHandle<Script> shared,
1296 ScriptEventType script_event_type);
1297 // ------
1298
1299 // MetaMapProviderFunc is supposed to be a function returning Tagged<Map>.
1300 // For example, std::function<Tagged<Map>()>.
1301 template <typename MetaMapProviderFunc>
1303 MetaMapProviderFunc&& meta_map_provider, InstanceType type,
1304 int instance_size,
1305 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
1306 int inobject_properties = 0,
1307 AllocationType allocation_type = AllocationType::kMap);
1308
1309 Tagged<HeapObject> AllocateRawWithAllocationSite(
1310 DirectHandle<Map> map, AllocationType allocation,
1311 DirectHandle<AllocationSite> allocation_site);
1312
1313 Handle<JSArrayBufferView> NewJSArrayBufferView(
1315 DirectHandle<JSArrayBuffer> buffer, size_t byte_offset,
1316 size_t byte_length);
1317
1318 Tagged<Symbol> NewSymbolInternal(
1319 AllocationType allocation = AllocationType::kOld);
1320
1321 // Allocates new context with given map, sets length and initializes the
1322 // after-header part with uninitialized values and leaves the context header
1323 // uninitialized.
1324 Tagged<Context> NewContextInternal(DirectHandle<Map> map, int size,
1325 int variadic_part_length,
1326 AllocationType allocation);
1327
1328 template <typename T>
1329 Handle<T> AllocateSmallOrderedHashTable(DirectHandle<Map> map, int capacity,
1330 AllocationType allocation);
1331
1332 // Creates a heap object based on the map. The fields of the heap object are
1333 // not initialized, it's the responsibility of the caller to do that.
1335
1336 template <typename T>
1337 Handle<T> CopyArrayWithMap(
1339 AllocationType allocation = AllocationType::kYoung);
1340 template <typename T>
1341 Handle<T> CopyArrayAndGrow(DirectHandle<T> src, int grow_by,
1342 AllocationType allocation);
1343
1344 MaybeHandle<String> NewStringFromTwoByte(const base::uc16* string, int length,
1345 AllocationType allocation);
1346
1347 // Functions to get the hash of a number for the number_string_cache.
1348 int NumberToStringCacheHash(Tagged<Smi> number);
1349 int NumberToStringCacheHash(double number);
1350
1351 // Attempt to find the number in a small cache. If we finds it, return
1352 // the string representation of the number. Otherwise return undefined.
1353 V8_INLINE Handle<Object> NumberToStringCacheGet(Tagged<Object> number,
1354 int hash);
1355
1356 // Update the cache with a new number-string pair.
1357 V8_INLINE void NumberToStringCacheSet(DirectHandle<Object> number, int hash,
1358 DirectHandle<String> js_string);
1359
1360 // Creates a new JSArray with the given backing storage. Performs no
1361 // verification of the backing storage because it may not yet be filled.
1362 Handle<JSArray> NewJSArrayWithUnverifiedElements(
1363 DirectHandle<FixedArrayBase> elements, ElementsKind elements_kind,
1364 int length, AllocationType allocation = AllocationType::kYoung);
1365 Handle<JSArray> NewJSArrayWithUnverifiedElements(
1366 DirectHandle<Map> map, DirectHandle<FixedArrayBase> elements, int length,
1367 AllocationType allocation = AllocationType::kYoung);
1368
1369 // Creates the backing storage for a JSArray. This handle must be discarded
1370 // before returning the JSArray reference to code outside Factory, which might
1371 // decide to left-trim the backing store. To avoid unnecessary HandleScopes,
1372 // this method requires capacity greater than zero.
1373 DirectHandle<FixedArrayBase> NewJSArrayStorage(
1374 ElementsKind elements_kind, int capacity,
1376 ArrayStorageAllocationMode::DONT_INITIALIZE_ARRAY_ELEMENTS);
1377
1378 void InitializeAllocationMemento(Tagged<AllocationMemento> memento,
1379 Tagged<AllocationSite> allocation_site);
1380
1381 // Initializes a JSObject based on its map.
1382 void InitializeJSObjectFromMap(
1383 Tagged<JSObject> obj, Tagged<Object> properties, Tagged<Map> map,
1384 NewJSObjectType = NewJSObjectType::kNoAPIWrapper);
1385 // Initializes JSObject body starting at given offset.
1386 void InitializeJSObjectBody(Tagged<JSObject> obj, Tagged<Map> map,
1387 int start_offset);
1388 void InitializeCppHeapWrapper(Tagged<JSObject> obj);
1389
1390 Handle<WeakArrayList> NewUninitializedWeakArrayList(
1391 int capacity, AllocationType allocation = AllocationType::kYoung);
1392
1393#if V8_ENABLE_WEBASSEMBLY
1394 // The resulting array will be uninitialized, which means GC might fail for
1395 // reference arrays until initialization. Follow this up with a
1396 // {DisallowGarbageCollection} scope until initialization.
1397 Tagged<WasmArray> NewWasmArrayUninitialized(uint32_t length,
1398 DirectHandle<Map> map);
1399
1400#if V8_ENABLE_DRUMBRAKE
1401 // WasmInterpreterRuntime needs to call NewWasmArrayUninitialized.
1402 friend class wasm::WasmInterpreterRuntime;
1403#endif // V8_ENABLE_DRUMBRAKE
1404#endif // V8_ENABLE_WEBASSEMBLY
1405};
1406
1407} // namespace internal
1408} // namespace v8
1409
1410#endif // V8_HEAP_FACTORY_H_
int16_t parameter_count
Definition builtins.cc:67
Builtins::Kind kind
Definition builtins.cc:40
MaybeHandle< TrustedObject > interpreter_data_
Definition factory.h:1264
MaybeHandle< TrustedByteArray > bytecode_offset_table_
Definition factory.h:1261
CodeBuilder & set_source_position_table(Handle< TrustedByteArray > table)
Definition factory.h:1188
CodeBuilder & set_bytecode_offset_table(Handle< TrustedByteArray > table)
Definition factory.h:1197
CodeBuilder & set_builtin(Builtin builtin)
Definition factory.h:1169
MaybeHandle< Object > self_reference_
Definition factory.h:1257
CodeBuilder & set_stack_slots(int stack_slots)
Definition factory.h:1227
CodeBuilder & set_deoptimization_data(Handle< DeoptimizationData > deopt_data)
Definition factory.h:1204
CodeBuilder & set_parameter_count(uint16_t parameter_count)
Definition factory.h:1232
MaybeHandle< DeoptimizationData > deoptimization_data_
Definition factory.h:1263
CodeBuilder & set_osr_offset(BytecodeOffset offset)
Definition factory.h:1182
CodeBuilder & set_self_reference(Handle< Object > self_reference)
Definition factory.h:1163
CodeBuilder & set_inlined_bytecode_size(uint32_t size)
Definition factory.h:1176
CodeBuilder & set_is_context_specialized()
Definition factory.h:1215
CodeBuilder & set_profiler_data(BasicBlockProfilerData *profiler_data)
Definition factory.h:1237
MaybeHandle< TrustedByteArray > source_position_table_
Definition factory.h:1262
MaybeDirectHandle< Map > maybe_map_
Definition factory.h:1135
JSFunctionBuilder & set_map(DirectHandle< Map > v)
Definition factory.h:1116
DirectHandle< SharedFunctionInfo > sfi_
Definition factory.h:1133
DirectHandle< Context > context_
Definition factory.h:1134
JSFunctionBuilder & set_feedback_cell(DirectHandle< FeedbackCell > v)
Definition factory.h:1124
JSFunctionBuilder & set_allocation_type(AllocationType v)
Definition factory.h:1120
MaybeDirectHandle< FeedbackCell > maybe_feedback_cell_
Definition factory.h:1136
Isolate * isolate() const
Definition factory.h:1281
static bool IsFunctionModeWithName(FunctionMode function_mode)
Definition factory.h:1008
static bool IsFunctionModeWithWritablePrototype(FunctionMode function_mode)
Definition factory.h:1004
DirectHandle< String > Uint32ToString(uint32_t value, bool check_cache=true)
Definition factory.h:978
Handle< String > InternalizeString(const char *str, bool convert_encoding=false)
Definition factory.h:222
V8_INLINE Handle< Map > NewMapImpl(MetaMapProviderFunc &&meta_map_provider, InstanceType type, int instance_size, ElementsKind elements_kind=TERMINAL_FAST_ELEMENTS_KIND, int inobject_properties=0, AllocationType allocation_type=AllocationType::kMap)
Handle< String > InternalizeUtf8String(const char *str)
Definition factory.h:209
DirectHandle< JSObject > NewError(DirectHandle< JSFunction > constructor, MessageTemplate template_index, Args... args)
Definition factory.h:927
DirectHandle< JSObject > ShadowRealmNewTypeErrorCopy(DirectHandle< Object > original, MessageTemplate template_index, Args... args)
Definition factory.h:942
Tagged< HeapObject > NewForTest(DirectHandle< Map > map, AllocationType allocation)
Definition factory.h:1083
Handle< JSArray > NewJSArray(int capacity, ElementsKind elements_kind=TERMINAL_FAST_ELEMENTS_KIND, AllocationType allocation=AllocationType::kYoung)
Definition factory.h:689
Handle< String > InternalizeString(base::Vector< const char > str, bool convert_encoding=false)
Definition factory.h:216
static bool IsFunctionModeWithPrototype(FunctionMode function_mode)
Definition factory.h:1000
V8_INLINE bool is_null() const
Definition handles.h:69
const PropertyKind kind_
int start
int end
LineAndColumn previous
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
#define ROOT_ACCESSOR(Type, name, CamelName)
#define DECLARE_ERROR(NAME)
Definition factory.h:953
int32_t offset
std::string extension
TNode< Object > callback
std::string pattern
Builtin builtin
base::SmallVector< int32_t, 1 > stack_slots
Utf8Variant
Definition unicode.h:145
uint16_t uc16
Definition strings.h:18
constexpr bool CodeKindCanOSR(CodeKind kind)
Definition code-kind.h:91
constexpr int kSmallOrderedHashMapMinCapacity
Definition globals.h:2772
constexpr bool CodeKindIsOptimizedJSFunction(CodeKind kind)
Definition code-kind.h:66
constexpr int kSmallOrderedHashSetMinCapacity
Definition globals.h:2771
StringTransitionStrategy
Definition globals.h:2864
ElementsKind GetHoleyElementsKind(ElementsKind packed_kind)
typename detail::FlattenUnionHelper< Union<>, Ts... >::type UnionOf
Definition union.h:123
constexpr bool CodeKindIsUnoptimizedJSFunction(CodeKind kind)
Definition code-kind.h:59
constexpr bool CodeKindIsJSFunction(CodeKind kind)
Definition code-kind.h:72
@ kWithPrototypeBits
Definition factory.h:117
@ FUNCTION_WITH_NAME_AND_READONLY_PROTOTYPE
Definition factory.h:130
@ FUNCTION_WITH_READONLY_PROTOTYPE
Definition factory.h:129
@ FUNCTION_WITH_NAME_AND_WRITEABLE_PROTOTYPE
Definition factory.h:125
@ kWithWritablePrototypeBit
Definition factory.h:115
@ METHOD_WITH_NAME
Definition factory.h:121
@ FUNCTION_WITHOUT_PROTOTYPE
Definition factory.h:120
@ FUNCTION_WITH_WRITEABLE_PROTOTYPE
Definition factory.h:124
@ kWithReadonlyPrototypeBit
Definition factory.h:116
ArrayStorageAllocationMode
Definition factory.h:134
Definition c-api.cc:87
#define MUTABLE_ROOT_LIST(V)
Definition roots.h:483
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
SourcePositionTable * source_position_table_