• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/strings.h"
10 #include "src/common/globals.h"
11 #include "src/objects/function-kind.h"
12 #include "src/objects/instance-type.h"
13 #include "src/roots/roots.h"
14 #include "torque-generated/class-forward-declarations.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 class HeapObject;
20 class SharedFunctionInfo;
21 class FunctionLiteral;
22 class SeqOneByteString;
23 class SeqTwoByteString;
24 class FreshlyAllocatedBigInt;
25 class ObjectBoilerplateDescription;
26 class ArrayBoilerplateDescription;
27 class RegExpBoilerplateDescription;
28 class TemplateObjectDescription;
29 class SourceTextModuleInfo;
30 class PreparseData;
31 template <class T>
32 class PodArray;
33 class UncompiledDataWithoutPreparseData;
34 class UncompiledDataWithPreparseData;
35 class BytecodeArray;
36 class CoverageInfo;
37 class ClassPositions;
38 struct SourceRange;
39 enum class Builtin : int32_t;
40 template <typename T>
41 class ZoneVector;
42 
43 namespace wasm {
44 class ValueType;
45 }  // namespace wasm
46 
47 template <typename Impl>
48 class FactoryBase;
49 
50 // Putting Torque-generated definitions in a superclass allows to shadow them
51 // easily when they shouldn't be used and to reference them when they happen to
52 // have the same signature.
53 template <typename Impl>
EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)54 class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) TorqueGeneratedFactory {
55  private:
56   FactoryBase<Impl>* factory() { return static_cast<FactoryBase<Impl>*>(this); }
57 
58  public:
59 #include "torque-generated/factory.inc"
60 };
61 
62 template <typename Impl>
EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)63 class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) FactoryBase
64     : public TorqueGeneratedFactory<Impl> {
65  public:
66   // Converts the given boolean condition to JavaScript boolean value.
67   inline Handle<Oddball> ToBoolean(bool value);
68 
69   // Numbers (e.g. literals) are pretenured by the parser.
70   // The return value may be a smi or a heap number.
71   template <AllocationType allocation = AllocationType::kYoung>
72   inline Handle<Object> NewNumber(double value);
73   template <AllocationType allocation = AllocationType::kYoung>
74   inline Handle<Object> NewNumberFromInt(int32_t value);
75   template <AllocationType allocation = AllocationType::kYoung>
76   inline Handle<Object> NewNumberFromUint(uint32_t value);
77   template <AllocationType allocation = AllocationType::kYoung>
78   inline Handle<Object> NewNumberFromSize(size_t value);
79   template <AllocationType allocation = AllocationType::kYoung>
80   inline Handle<Object> NewNumberFromInt64(int64_t value);
81   template <AllocationType allocation = AllocationType::kYoung>
82   inline Handle<HeapNumber> NewHeapNumber(double value);
83   template <AllocationType allocation = AllocationType::kYoung>
84   inline Handle<HeapNumber> NewHeapNumberFromBits(uint64_t bits);
85   template <AllocationType allocation = AllocationType::kYoung>
86   inline Handle<HeapNumber> NewHeapNumberWithHoleNaN();
87 
88   template <AllocationType allocation>
89   Handle<HeapNumber> NewHeapNumber();
90 
91   Handle<Struct> NewStruct(InstanceType type,
92                            AllocationType allocation = AllocationType::kYoung);
93 
94   // Create a pre-tenured empty AccessorPair.
95   Handle<AccessorPair> NewAccessorPair();
96 
97   // Creates a new CodeDataContainer for a Code object.
98   Handle<CodeDataContainer> NewCodeDataContainer(int flags,
99                                                  AllocationType allocation);
100 
101   // Allocates a fixed array initialized with undefined values.
102   Handle<FixedArray> NewFixedArray(
103       int length, AllocationType allocation = AllocationType::kYoung);
104 
105   // Allocates a fixed array-like object with given map and initialized with
106   // undefined values.
107   Handle<FixedArray> NewFixedArrayWithMap(
108       Handle<Map> map, int length,
109       AllocationType allocation = AllocationType::kYoung);
110 
111   // Allocate a new fixed array with non-existing entries (the hole).
112   Handle<FixedArray> NewFixedArrayWithHoles(
113       int length, AllocationType allocation = AllocationType::kYoung);
114 
115   // Allocate a new fixed array with Smi(0) entries.
116   Handle<FixedArray> NewFixedArrayWithZeroes(
117       int length, AllocationType allocation = AllocationType::kYoung);
118 
119   // Allocate a new uninitialized fixed double array.
120   // The function returns a pre-allocated empty fixed array for length = 0,
121   // so the return type must be the general fixed array class.
122   Handle<FixedArrayBase> NewFixedDoubleArray(
123       int length, AllocationType allocation = AllocationType::kYoung);
124 
125   // Allocates a weak fixed array-like object with given map and initialized
126   // with undefined values. Length must be > 0.
127   Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
128       Map map, int length, AllocationType allocation = AllocationType::kYoung);
129 
130   // Allocates a fixed array which may contain in-place weak references. The
131   // array is initialized with undefined values
132   // The function returns a pre-allocated empty weak fixed array for length = 0.
133   Handle<WeakFixedArray> NewWeakFixedArray(
134       int length, AllocationType allocation = AllocationType::kYoung);
135 
136   // The function returns a pre-allocated empty byte array for length = 0.
137   Handle<ByteArray> NewByteArray(
138       int length, AllocationType allocation = AllocationType::kYoung);
139 
140   Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
141                                          int frame_size, int parameter_count,
142                                          Handle<FixedArray> constant_pool);
143 
144   // Allocates a fixed array for name-value pairs of boilerplate properties and
145   // calculates the number of properties we need to store in the backing store.
146   Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
147       int boilerplate, int all_properties, int index_keys, bool has_seen_proto);
148 
149   // Create a new ArrayBoilerplateDescription struct.
150   Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
151       ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
152 
153   Handle<RegExpBoilerplateDescription> NewRegExpBoilerplateDescription(
154       Handle<FixedArray> data, Handle<String> source, Smi flags);
155 
156   // Create a new TemplateObjectDescription struct.
157   Handle<TemplateObjectDescription> NewTemplateObjectDescription(
158       Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings);
159 
160   Handle<Script> NewScript(Handle<PrimitiveHeapObject> source);
161   Handle<Script> NewScriptWithId(Handle<PrimitiveHeapObject> source,
162                                  int script_id);
163 
164   Handle<ArrayList> NewArrayList(
165       int size, AllocationType allocation = AllocationType::kYoung);
166 
167   Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
168       FunctionLiteral* literal, Handle<Script> script, bool is_toplevel);
169 
170   // Create a copy of a given SharedFunctionInfo for use as a placeholder in
171   // off-thread compilation
172   Handle<SharedFunctionInfo> CloneSharedFunctionInfo(
173       Handle<SharedFunctionInfo> other);
174 
175   Handle<PreparseData> NewPreparseData(int data_length, int children_length);
176 
177   Handle<UncompiledDataWithoutPreparseData>
178   NewUncompiledDataWithoutPreparseData(Handle<String> inferred_name,
179                                        int32_t start_position,
180                                        int32_t end_position);
181 
182   Handle<UncompiledDataWithPreparseData> NewUncompiledDataWithPreparseData(
183       Handle<String> inferred_name, int32_t start_position,
184       int32_t end_position, Handle<PreparseData>);
185 
186   Handle<UncompiledDataWithoutPreparseDataWithJob>
187   NewUncompiledDataWithoutPreparseDataWithJob(Handle<String> inferred_name,
188                                               int32_t start_position,
189                                               int32_t end_position);
190 
191   Handle<UncompiledDataWithPreparseDataAndJob>
192   NewUncompiledDataWithPreparseDataAndJob(Handle<String> inferred_name,
193                                           int32_t start_position,
194                                           int32_t end_position,
195                                           Handle<PreparseData>);
196 
197   // Allocates a FeedbackMedata object and zeroes the data section.
198   Handle<FeedbackMetadata> NewFeedbackMetadata(
199       int slot_count, int create_closure_slot_count,
200       AllocationType allocation = AllocationType::kOld);
201 
202   Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
203 
204   Handle<String> InternalizeString(const base::Vector<const uint8_t>& string,
205                                    bool convert_encoding = false);
206   Handle<String> InternalizeString(const base::Vector<const uint16_t>& string,
207                                    bool convert_encoding = false);
208 
209   template <class StringTableKey>
210   Handle<String> InternalizeStringWithKey(StringTableKey* key);
211 
212   Handle<SeqOneByteString> NewOneByteInternalizedString(
213       const base::Vector<const uint8_t>& str, uint32_t raw_hash_field);
214   Handle<SeqTwoByteString> NewTwoByteInternalizedString(
215       const base::Vector<const base::uc16>& str, uint32_t raw_hash_field);
216 
217   Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
218       int length, uint32_t raw_hash_field);
219   Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
220       int length, uint32_t raw_hash_field);
221 
222   // Allocates and partially initializes an one-byte or two-byte String. The
223   // characters of the string are uninitialized. Currently used in regexp code
224   // only, where they are pretenured.
225   V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
226       int length, AllocationType allocation = AllocationType::kYoung);
227   V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
228       int length, AllocationType allocation = AllocationType::kYoung);
229   // Create a new cons string object which consists of a pair of strings.
230   V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(
231       Handle<String> left, Handle<String> right,
232       AllocationType allocation = AllocationType::kYoung);
233 
234   V8_WARN_UNUSED_RESULT Handle<String> NewConsString(
235       Handle<String> left, Handle<String> right, int length, bool one_byte,
236       AllocationType allocation = AllocationType::kYoung);
237 
238   V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawSharedOneByteString(
239       int length);
240   V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawSharedTwoByteString(
241       int length);
242 
243   // Allocates a new BigInt with {length} digits. Only to be used by
244   // MutableBigInt::New*.
245   Handle<FreshlyAllocatedBigInt> NewBigInt(
246       int length, AllocationType allocation = AllocationType::kYoung);
247 
248   // Create a serialized scope info.
249   Handle<ScopeInfo> NewScopeInfo(int length,
250                                  AllocationType type = AllocationType::kOld);
251 
252   Handle<SourceTextModuleInfo> NewSourceTextModuleInfo();
253 
254   Handle<DescriptorArray> NewDescriptorArray(
255       int number_of_entries, int slack = 0,
256       AllocationType allocation = AllocationType::kYoung);
257 
258   Handle<ClassPositions> NewClassPositions(int start, int end);
259 
260   Handle<SwissNameDictionary> NewSwissNameDictionary(
261       int at_least_space_for = kSwissNameDictionaryInitialCapacity,
262       AllocationType allocation = AllocationType::kYoung);
263 
264   Handle<SwissNameDictionary> NewSwissNameDictionaryWithCapacity(
265       int capacity, AllocationType allocation);
266 
267   Handle<FunctionTemplateRareData> NewFunctionTemplateRareData();
268 
269   MaybeHandle<Map> GetInPlaceInternalizedStringMap(Map from_string_map);
270 
271   Handle<Map> GetStringMigrationSentinelMap(InstanceType from_string_type);
272 
273   AllocationType RefineAllocationTypeForInPlaceInternalizableString(
274       AllocationType allocation, Map string_map);
275 
276  protected:
277   // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
278   HeapObject AllocateRawArray(int size, AllocationType allocation);
279   HeapObject AllocateRawFixedArray(int length, AllocationType allocation);
280   HeapObject AllocateRawWeakArrayList(int length, AllocationType allocation);
281 
282   template <typename StructType>
283   inline StructType NewStructInternal(InstanceType type,
284                                       AllocationType allocation);
285   Struct NewStructInternal(ReadOnlyRoots roots, Map map, int size,
286                            AllocationType allocation);
287 
288   HeapObject AllocateRawWithImmortalMap(
289       int size, AllocationType allocation, Map map,
290       AllocationAlignment alignment = kTaggedAligned);
291   HeapObject NewWithImmortalMap(Map map, AllocationType allocation);
292 
293   Handle<FixedArray> NewFixedArrayWithFiller(Handle<Map> map, int length,
294                                              Handle<Oddball> filler,
295                                              AllocationType allocation);
296 
297   Handle<SharedFunctionInfo> NewSharedFunctionInfo();
298   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
299       MaybeHandle<String> maybe_name,
300       MaybeHandle<HeapObject> maybe_function_data, Builtin builtin,
301       FunctionKind kind = FunctionKind::kNormalFunction);
302 
303   Handle<String> MakeOrFindTwoCharacterString(uint16_t c1, uint16_t c2);
304 
305   template <typename SeqStringT>
306   MaybeHandle<SeqStringT> NewRawStringWithMap(int length, Map map,
307                                               AllocationType allocation);
308 
309  private:
310   friend class WebSnapshotDeserializer;
311   Impl* impl() { return static_cast<Impl*>(this); }
312   auto isolate() { return impl()->isolate(); }
313   ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }
314 
315   HeapObject AllocateRaw(int size, AllocationType allocation,
316                          AllocationAlignment alignment = kTaggedAligned);
317 
318   friend TorqueGeneratedFactory<Impl>;
319 };
320 
321 }  // namespace internal
322 }  // namespace v8
323 
324 #endif  // V8_HEAP_FACTORY_BASE_H_
325