• 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/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 create_closure_slot_count,
141       AllocationType allocation = AllocationType::kOld);
142 
143   Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
144 
145   Handle<String> InternalizeString(const Vector<const uint8_t>& string,
146                                    bool convert_encoding = false);
147   Handle<String> InternalizeString(const Vector<const uint16_t>& string,
148                                    bool convert_encoding = false);
149 
150   template <class StringTableKey>
151   Handle<String> InternalizeStringWithKey(StringTableKey* key);
152 
153   Handle<SeqOneByteString> NewOneByteInternalizedString(
154       const Vector<const uint8_t>& str, uint32_t hash_field);
155   Handle<SeqTwoByteString> NewTwoByteInternalizedString(
156       const Vector<const uc16>& str, uint32_t hash_field);
157 
158   Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
159       int length, uint32_t hash_field);
160   Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
161       int length, uint32_t hash_field);
162 
163   // Allocates and partially initializes an one-byte or two-byte String. The
164   // characters of the string are uninitialized. Currently used in regexp code
165   // only, where they are pretenured.
166   V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
167       int length, AllocationType allocation = AllocationType::kYoung);
168   V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
169       int length, AllocationType allocation = AllocationType::kYoung);
170   // Create a new cons string object which consists of a pair of strings.
171   V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(
172       Handle<String> left, Handle<String> right,
173       AllocationType allocation = AllocationType::kYoung);
174 
175   V8_WARN_UNUSED_RESULT Handle<String> NewConsString(
176       Handle<String> left, Handle<String> right, int length, bool one_byte,
177       AllocationType allocation = AllocationType::kYoung);
178 
179   // Allocates a new BigInt with {length} digits. Only to be used by
180   // MutableBigInt::New*.
181   Handle<FreshlyAllocatedBigInt> NewBigInt(
182       int length, AllocationType allocation = AllocationType::kYoung);
183 
184   // Create a serialized scope info.
185   Handle<ScopeInfo> NewScopeInfo(int length,
186                                  AllocationType type = AllocationType::kOld);
187 
188   Handle<SourceTextModuleInfo> NewSourceTextModuleInfo();
189 
190   Handle<DescriptorArray> NewDescriptorArray(
191       int number_of_entries, int slack = 0,
192       AllocationType allocation = AllocationType::kYoung);
193 
194   Handle<ClassPositions> NewClassPositions(int start, int end);
195 
196  protected:
197   // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
198   HeapObject AllocateRawArray(int size, AllocationType allocation);
199   HeapObject AllocateRawFixedArray(int length, AllocationType allocation);
200   HeapObject AllocateRawWeakArrayList(int length, AllocationType allocation);
201 
202   HeapObject AllocateRawWithImmortalMap(
203       int size, AllocationType allocation, Map map,
204       AllocationAlignment alignment = kWordAligned);
205   HeapObject NewWithImmortalMap(Map map, AllocationType allocation);
206 
207   Handle<FixedArray> NewFixedArrayWithFiller(Handle<Map> map, int length,
208                                              Handle<Oddball> filler,
209                                              AllocationType allocation);
210 
211   Handle<SharedFunctionInfo> NewSharedFunctionInfo();
212   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
213       MaybeHandle<String> maybe_name,
214       MaybeHandle<HeapObject> maybe_function_data, int maybe_builtin_index,
215       FunctionKind kind = kNormalFunction);
216 
217   Handle<String> MakeOrFindTwoCharacterString(uint16_t c1, uint16_t c2);
218 
219  private:
220   Impl* impl() { return static_cast<Impl*>(this); }
221   auto isolate() { return impl()->isolate(); }
222   ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }
223 
224   HeapObject AllocateRaw(int size, AllocationType allocation,
225                          AllocationAlignment alignment = kWordAligned);
226 };
227 
228 }  // namespace internal
229 }  // namespace v8
230 
231 #endif  // V8_HEAP_FACTORY_BASE_H_
232