1 // Copyright 2020 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_BASE_H_
6 #define V8_HEAP_FACTORY_BASE_H_
7 
8 #include "src/base/export-template.h"
9 #include "src/common/globals.h"
10 #include "src/objects/function-kind.h"
11 #include "src/objects/instance-type.h"
12 #include "src/roots/roots.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class HeapObject;
18 class SharedFunctionInfo;
19 class FunctionLiteral;
20 class SeqOneByteString;
21 class SeqTwoByteString;
22 class FreshlyAllocatedBigInt;
23 class ObjectBoilerplateDescription;
24 class ArrayBoilerplateDescription;
25 class TemplateObjectDescription;
26 class SourceTextModuleInfo;
27 class PreparseData;
28 class UncompiledDataWithoutPreparseData;
29 class UncompiledDataWithPreparseData;
30 class BytecodeArray;
31 class CoverageInfo;
32 class ClassPositions;
33 struct SourceRange;
34 template <typename T>
35 class ZoneVector;
36 
37 template <typename Impl>
EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)38 class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) FactoryBase {
39  public:
40   // Converts the given boolean condition to JavaScript boolean value.
41   inline Handle<Oddball> ToBoolean(bool value);
42 
43   // Numbers (e.g. literals) are pretenured by the parser.
44   // The return value may be a smi or a heap number.
45   template <AllocationType allocation = AllocationType::kYoung>
46   inline Handle<Object> NewNumber(double value);
47   template <AllocationType allocation = AllocationType::kYoung>
48   inline Handle<Object> NewNumberFromInt(int32_t value);
49   template <AllocationType allocation = AllocationType::kYoung>
50   inline Handle<Object> NewNumberFromUint(uint32_t value);
51   template <AllocationType allocation = AllocationType::kYoung>
52   inline Handle<Object> NewNumberFromSize(size_t value);
53   template <AllocationType allocation = AllocationType::kYoung>
54   inline Handle<Object> NewNumberFromInt64(int64_t value);
55   template <AllocationType allocation = AllocationType::kYoung>
56   inline Handle<HeapNumber> NewHeapNumber(double value);
57   template <AllocationType allocation = AllocationType::kYoung>
58   inline Handle<HeapNumber> NewHeapNumberFromBits(uint64_t bits);
59   template <AllocationType allocation = AllocationType::kYoung>
60   inline Handle<HeapNumber> NewHeapNumberWithHoleNaN();
61 
62   template <AllocationType allocation>
63   Handle<HeapNumber> NewHeapNumber();
64 
65   Handle<Struct> NewStruct(InstanceType type,
66                            AllocationType allocation = AllocationType::kYoung);
67 
68   // Create a pre-tenured empty AccessorPair.
69   Handle<AccessorPair> NewAccessorPair();
70 
71   // Allocates a fixed array initialized with undefined values.
72   Handle<FixedArray> NewFixedArray(
73       int length, AllocationType allocation = AllocationType::kYoung);
74 
75   // Allocates a fixed array-like object with given map and initialized with
76   // undefined values.
77   Handle<FixedArray> NewFixedArrayWithMap(
78       Handle<Map> map, int length,
79       AllocationType allocation = AllocationType::kYoung);
80 
81   // Allocate a new fixed array with non-existing entries (the hole).
82   Handle<FixedArray> NewFixedArrayWithHoles(
83       int length, AllocationType allocation = AllocationType::kYoung);
84 
85   // Allocate a new uninitialized fixed double array.
86   // The function returns a pre-allocated empty fixed array for length = 0,
87   // so the return type must be the general fixed array class.
88   Handle<FixedArrayBase> NewFixedDoubleArray(
89       int length, AllocationType allocation = AllocationType::kYoung);
90 
91   // Allocates a weak fixed array-like object with given map and initialized
92   // with undefined values.
93   Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
94       Map map, int length, AllocationType allocation = AllocationType::kYoung);
95 
96   // Allocates a fixed array which may contain in-place weak references. The
97   // array is initialized with undefined values
98   Handle<WeakFixedArray> NewWeakFixedArray(
99       int length, AllocationType allocation = AllocationType::kYoung);
100 
101   Handle<ByteArray> NewByteArray(
102       int length, AllocationType allocation = AllocationType::kYoung);
103 
104   Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
105                                          int frame_size, int parameter_count,
106                                          Handle<FixedArray> constant_pool);
107 
108   // Allocates a fixed array for name-value pairs of boilerplate properties and
109   // calculates the number of properties we need to store in the backing store.
110   Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
111       int boilerplate, int all_properties, int index_keys, bool has_seen_proto);
112 
113   // Create a new ArrayBoilerplateDescription struct.
114   Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
115       ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
116 
117   // Create a new TemplateObjectDescription struct.
118   Handle<TemplateObjectDescription> NewTemplateObjectDescription(
119       Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings);
120 
121   Handle<Script> NewScript(Handle<String> source);
122   Handle<Script> NewScriptWithId(Handle<String> source, int script_id);
123 
124   Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
125       FunctionLiteral* literal, Handle<Script> script, bool is_toplevel);
126 
127   Handle<PreparseData> NewPreparseData(int data_length, int children_length);
128 
129   Handle<UncompiledDataWithoutPreparseData>
130   NewUncompiledDataWithoutPreparseData(Handle<String> inferred_name,
131                                        int32_t start_position,
132                                        int32_t end_position);
133 
134   Handle<UncompiledDataWithPreparseData> NewUncompiledDataWithPreparseData(
135       Handle<String> inferred_name, int32_t start_position,
136       int32_t end_position, Handle<PreparseData>);
137 
138   // Allocates a FeedbackMedata object and zeroes the data section.
139   Handle<FeedbackMetadata> NewFeedbackMetadata(
140       int slot_count, int feedback_cell_count,
141       AllocationType allocation = AllocationType::kOld);
142 
143   Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
144 
145   Handle<SeqOneByteString> NewOneByteInternalizedString(
146       const Vector<const uint8_t>& str, uint32_t hash_field);
147   Handle<SeqTwoByteString> NewTwoByteInternalizedString(
148       const Vector<const uc16>& str, uint32_t hash_field);
149 
150   Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
151       int length, uint32_t hash_field);
152   Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
153       int length, uint32_t hash_field);
154 
155   // Allocates and partially initializes an one-byte or two-byte String. The
156   // characters of the string are uninitialized. Currently used in regexp code
157   // only, where they are pretenured.
158   V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
159       int length, AllocationType allocation = AllocationType::kYoung);
160   V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
161       int length, AllocationType allocation = AllocationType::kYoung);
162   // Create a new cons string object which consists of a pair of strings.
163   V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(
164       Handle<String> left, Handle<String> right,
165       AllocationType allocation = AllocationType::kYoung);
166 
167   V8_WARN_UNUSED_RESULT Handle<String> NewConsString(
168       Handle<String> left, Handle<String> right, int length, bool one_byte,
169       AllocationType allocation = AllocationType::kYoung);
170 
171   // Allocates a new BigInt with {length} digits. Only to be used by
172   // MutableBigInt::New*.
173   Handle<FreshlyAllocatedBigInt> NewBigInt(
174       int length, AllocationType allocation = AllocationType::kYoung);
175 
176   // Create a serialized scope info.
177   Handle<ScopeInfo> NewScopeInfo(int length,
178                                  AllocationType type = AllocationType::kOld);
179 
180   Handle<SourceTextModuleInfo> NewSourceTextModuleInfo();
181 
182   Handle<DescriptorArray> NewDescriptorArray(
183       int number_of_entries, int slack = 0,
184       AllocationType allocation = AllocationType::kYoung);
185 
186   Handle<ClassPositions> NewClassPositions(int start, int end);
187 
188  protected:
189   // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
190   HeapObject AllocateRawArray(int size, AllocationType allocation);
191   HeapObject AllocateRawFixedArray(int length, AllocationType allocation);
192   HeapObject AllocateRawWeakArrayList(int length, AllocationType allocation);
193 
194   HeapObject AllocateRawWithImmortalMap(
195       int size, AllocationType allocation, Map map,
196       AllocationAlignment alignment = kWordAligned);
197   HeapObject NewWithImmortalMap(Map map, AllocationType allocation);
198 
199   Handle<FixedArray> NewFixedArrayWithFiller(Handle<Map> map, int length,
200                                              Handle<Oddball> filler,
201                                              AllocationType allocation);
202 
203   Handle<SharedFunctionInfo> NewSharedFunctionInfo();
204   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
205       MaybeHandle<String> maybe_name,
206       MaybeHandle<HeapObject> maybe_function_data, int maybe_builtin_index,
207       FunctionKind kind = kNormalFunction);
208 
209  private:
210   Impl* impl() { return static_cast<Impl*>(this); }
211   auto isolate() { return impl()->isolate(); }
212   ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }
213 
214   HeapObject AllocateRaw(int size, AllocationType allocation,
215                          AllocationAlignment alignment = kWordAligned);
216 };
217 
218 }  // namespace internal
219 }  // namespace v8
220 
221 #endif  // V8_HEAP_FACTORY_BASE_H_
222