v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
factory-base.h
Go to the documentation of this file.
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 <string_view>
9
11#include "src/base/strings.h"
12#include "src/common/globals.h"
17#include "src/roots/roots.h"
18#include "torque-generated/class-forward-declarations.h"
19
20namespace v8 {
21namespace internal {
22
24class BytecodeArray;
25class ClassPositions;
26class CoverageInfo;
29class FixedArray;
30template <typename T, typename Base>
33class FunctionLiteral;
34class HeapObject;
36template <typename T>
37class PodArray;
38class PreparseData;
47struct SourceRange;
48enum class Builtin : int32_t;
49template <typename T>
50class ZoneVector;
51
52namespace wasm {
53class ValueType;
54} // namespace wasm
55
56template <typename Impl>
57class FactoryBase;
58
60
63
64// Putting Torque-generated definitions in a superclass allows to shadow them
65// easily when they shouldn't be used and to reference them when they happen to
66// have the same signature.
67template <typename Impl>
68class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) TorqueGeneratedFactory {
69 private:
70 FactoryBase<Impl>* factory() { return static_cast<FactoryBase<Impl>*>(this); }
71
72 public:
73#include "torque-generated/factory.inc"
74};
75
100
101template <typename Impl>
102class FactoryBase : public TorqueGeneratedFactory<Impl> {
103 public:
104 Handle<Code> NewCode(const NewCodeOptions& options);
105
106 DirectHandle<CodeWrapper> NewCodeWrapper();
107
108 // Converts the given boolean condition to JavaScript boolean value.
109 inline Handle<Boolean> ToBoolean(bool value);
110
111#define ROOT_ACCESSOR(Type, name, CamelName) inline Handle<Type> name();
114#undef ROOT_ACCESSOR
115
116 // Numbers (e.g. literals) are pretenured by the parser.
117 // The return value may be a smi or a heap number.
118 template <AllocationType allocation = AllocationType::kYoung>
119 inline Handle<Number> NewNumber(double value);
120 template <AllocationType allocation = AllocationType::kYoung>
121 inline Handle<Number> NewNumberFromInt(int32_t value);
122 template <AllocationType allocation = AllocationType::kYoung>
123 inline Handle<Number> NewNumberFromUint(uint32_t value);
124 template <AllocationType allocation = AllocationType::kYoung>
125 inline DirectHandle<Number> NewNumberFromSize(size_t value);
126 template <AllocationType allocation = AllocationType::kYoung>
127 inline DirectHandle<Number> NewNumberFromInt64(int64_t value);
128 template <AllocationType allocation = AllocationType::kYoung>
129 inline Handle<HeapNumber> NewHeapNumber(double value);
130 template <AllocationType allocation = AllocationType::kYoung>
131 inline Handle<HeapNumber> NewHeapNumberFromBits(uint64_t bits);
132 template <AllocationType allocation = AllocationType::kYoung>
133 inline Handle<HeapNumber> NewHeapNumberWithHoleNaN();
134
135 template <AllocationType allocation = AllocationType::kYoung>
136 inline Handle<HeapNumber> NewHeapInt32(int32_t value);
137
138 template <AllocationType allocation>
139 Handle<HeapNumber> NewHeapNumber();
140
141 Handle<Struct> NewStruct(InstanceType type,
142 AllocationType allocation = AllocationType::kYoung);
143
144 // Create a pre-tenured empty AccessorPair.
145 Handle<AccessorPair> NewAccessorPair();
146
147 // Allocates a fixed array initialized with undefined values.
148 Handle<FixedArray> NewFixedArray(
149 int length, AllocationType allocation = AllocationType::kYoung);
150
151 // Allocates a trusted fixed array in trusted space, initialized with zeros.
152 Handle<TrustedFixedArray> NewTrustedFixedArray(
153 int length, AllocationType allocation = AllocationType::kTrusted);
154
155 // Allocates a protected fixed array in trusted space, initialized with zeros.
156 Handle<ProtectedFixedArray> NewProtectedFixedArray(int length);
157
158 // Allocates a fixed array-like object with given map and initialized with
159 // undefined values.
160 Handle<FixedArray> NewFixedArrayWithMap(
161 DirectHandle<Map> map, int length,
162 AllocationType allocation = AllocationType::kYoung);
163
164 // Allocate a new fixed array with non-existing entries (the hole).
165 Handle<FixedArray> NewFixedArrayWithHoles(
166 int length, AllocationType allocation = AllocationType::kYoung);
167
168 // Allocate a new fixed array with Tagged<Smi>(0) entries.
169 DirectHandle<FixedArray> NewFixedArrayWithZeroes(
170 int length, AllocationType allocation = AllocationType::kYoung);
171
172 // Allocate a new uninitialized fixed double array.
173 // The function returns a pre-allocated empty fixed array for length = 0,
174 // so the return type must be the general fixed array class.
175 Handle<FixedArrayBase> NewFixedDoubleArray(
176 int length, AllocationType allocation = AllocationType::kYoung);
177
178 // Allocates a weak fixed array-like object with given map and initialized
179 // with undefined values. Length must be > 0.
180 Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
181 Tagged<Map> map, int length,
182 AllocationType allocation = AllocationType::kYoung);
183
184 // Allocates a fixed array which may contain in-place weak references. The
185 // array is initialized with undefined values
186 // The function returns a pre-allocated empty weak fixed array for length = 0.
187 Handle<WeakFixedArray> NewWeakFixedArray(
188 int length, AllocationType allocation = AllocationType::kYoung);
189
190 // Allocates a trusted weak fixed array in trusted space, initialized with
191 // zeros.
192 Handle<TrustedWeakFixedArray> NewTrustedWeakFixedArray(int length);
193
194 // Allocates a protected weak fixed array in trusted space, initialized with
195 // zeros.
196 Handle<ProtectedWeakFixedArray> NewProtectedWeakFixedArray(int length);
197
198 // The function returns a pre-allocated empty byte array for length = 0.
199 Handle<ByteArray> NewByteArray(
200 int length, AllocationType allocation = AllocationType::kYoung);
201
202 // Allocates a trusted byte array in trusted space, initialized with zeros.
203 Handle<TrustedByteArray> NewTrustedByteArray(
204 int length, AllocationType allocation_type = AllocationType::kTrusted);
205
206 DirectHandle<DeoptimizationLiteralArray> NewDeoptimizationLiteralArray(
207 int length);
208 DirectHandle<DeoptimizationFrameTranslation>
209 NewDeoptimizationFrameTranslation(int length);
210
211 Handle<BytecodeArray> NewBytecodeArray(
212 int length, const uint8_t* raw_bytecodes, int frame_size,
213 uint16_t parameter_count, uint16_t max_arguments,
214 DirectHandle<TrustedFixedArray> constant_pool,
215 DirectHandle<TrustedByteArray> handler_table,
216 AllocationType allocation = AllocationType::kTrusted);
217
218 DirectHandle<BytecodeWrapper> NewBytecodeWrapper(
219 AllocationType allocation = AllocationType::kOld);
220
221 // Allocates a fixed array for name-value pairs of boilerplate properties and
222 // calculates the number of properties we need to store in the backing store.
223 Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
224 int boilerplate, int all_properties, int index_keys, bool has_seen_proto);
225
226 // Create a new ArrayBoilerplateDescription struct.
227 Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
228 ElementsKind elements_kind, DirectHandle<FixedArrayBase> constant_values);
229
230 DirectHandle<RegExpDataWrapper> NewRegExpDataWrapper();
231
232 DirectHandle<RegExpBoilerplateDescription> NewRegExpBoilerplateDescription(
233 DirectHandle<RegExpData> data, DirectHandle<String> source,
234 Tagged<Smi> flags);
235
236 // Create a new TemplateObjectDescription struct.
237 Handle<TemplateObjectDescription> NewTemplateObjectDescription(
238 DirectHandle<FixedArray> raw_strings,
239 DirectHandle<FixedArray> cooked_strings);
240
241 Handle<Script> NewScript(
242 DirectHandle<UnionOf<String, Undefined>> source,
243 ScriptEventType event_type = ScriptEventType::kCreate);
244 Handle<Script> NewScriptWithId(
245 DirectHandle<UnionOf<String, Undefined>> source, int script_id,
246 ScriptEventType event_type = ScriptEventType::kCreate);
247
248 DirectHandle<SloppyArgumentsElements> NewSloppyArgumentsElements(
249 int length, DirectHandle<Context> context,
250 DirectHandle<FixedArray> arguments,
251 AllocationType allocation = AllocationType::kYoung);
252 DirectHandle<ArrayList> NewArrayList(
253 int size, AllocationType allocation = AllocationType::kYoung);
254
255 Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
256 FunctionLiteral* literal, DirectHandle<Script> script, bool is_toplevel);
257
258 // Create a copy of a given SharedFunctionInfo for use as a placeholder in
259 // off-thread compilation
260 Handle<SharedFunctionInfo> CloneSharedFunctionInfo(
261 DirectHandle<SharedFunctionInfo> other);
262
263 DirectHandle<SharedFunctionInfoWrapper> NewSharedFunctionInfoWrapper(
264 DirectHandle<SharedFunctionInfo> sfi);
265
266 Handle<PreparseData> NewPreparseData(int data_length, int children_length);
267
268 DirectHandle<UncompiledDataWithoutPreparseData>
269 NewUncompiledDataWithoutPreparseData(Handle<String> inferred_name,
270 int32_t start_position,
271 int32_t end_position);
272
273 DirectHandle<UncompiledDataWithPreparseData>
274 NewUncompiledDataWithPreparseData(Handle<String> inferred_name,
275 int32_t start_position,
276 int32_t end_position, Handle<PreparseData>);
277
278 DirectHandle<UncompiledDataWithoutPreparseDataWithJob>
279 NewUncompiledDataWithoutPreparseDataWithJob(Handle<String> inferred_name,
280 int32_t start_position,
281 int32_t end_position);
282
283 DirectHandle<UncompiledDataWithPreparseDataAndJob>
284 NewUncompiledDataWithPreparseDataAndJob(Handle<String> inferred_name,
285 int32_t start_position,
286 int32_t end_position,
287 Handle<PreparseData>);
288
289 // Allocates a FeedbackMetadata object and zeroes the data section.
290 Handle<FeedbackMetadata> NewFeedbackMetadata(
291 int slot_count, int create_closure_slot_count,
292 AllocationType allocation = AllocationType::kOld);
293
294 Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
295
297 bool convert_encoding = false);
299 bool convert_encoding = false);
300
301 template <class StringTableKey>
302 Handle<String> InternalizeStringWithKey(StringTableKey* key);
303
304 Handle<SeqOneByteString> NewOneByteInternalizedString(
305 base::Vector<const uint8_t> str, uint32_t raw_hash_field);
306 Handle<SeqTwoByteString> NewTwoByteInternalizedString(
307 base::Vector<const base::uc16> str, uint32_t raw_hash_field);
308 DirectHandle<SeqOneByteString> NewOneByteInternalizedStringFromTwoByte(
309 base::Vector<const base::uc16> str, uint32_t raw_hash_field);
310
311 Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
312 int length, uint32_t raw_hash_field);
313 Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
314 int length, uint32_t raw_hash_field);
315
316 // Creates a single character string where the character has given code.
317 // A cache is used for Latin1 codes.
318 Handle<String> LookupSingleCharacterStringFromCode(uint16_t code);
319
320 MaybeHandle<String> NewStringFromOneByte(
322 AllocationType allocation = AllocationType::kYoung);
323
325 const char* str, AllocationType allocation = AllocationType::kYoung) {
326 return NewStringFromOneByte(base::OneByteVector(str), allocation)
327 .ToHandleChecked();
328 }
329
331 std::string_view str,
332 AllocationType allocation = AllocationType::kYoung) {
333 return NewStringFromOneByte(base::OneByteVector(str.data(), str.length()),
334 allocation)
335 .ToHandleChecked();
336 }
337
338 // Allocates and partially initializes an one-byte or two-byte String. The
339 // characters of the string are uninitialized. Currently used in regexp code
340 // only, where they are pretenured.
342 int length, AllocationType allocation = AllocationType::kYoung);
344 int length, AllocationType allocation = AllocationType::kYoung);
345 // Create a new cons string object which consists of a pair of strings.
346 template <template <typename> typename HandleType>
347 requires(std::is_convertible_v<HandleType<String>, DirectHandle<String>>)
348 V8_WARN_UNUSED_RESULT HandleType<String>::MaybeType NewConsString(
349 HandleType<String> left, HandleType<String> right,
350 AllocationType allocation = AllocationType::kYoung);
351
353 DirectHandle<String> left, DirectHandle<String> right, int length,
354 bool one_byte, AllocationType allocation = AllocationType::kYoung);
355
358 NumberCacheMode mode = NumberCacheMode::kBoth);
359 V8_WARN_UNUSED_RESULT Handle<String> HeapNumberToString(
360 DirectHandle<HeapNumber> number, double value,
361 NumberCacheMode mode = NumberCacheMode::kBoth);
363 Tagged<Smi> number, NumberCacheMode mode = NumberCacheMode::kBoth);
364
366 int length);
368 int length);
369
370 // Allocates a new BigInt with {length} digits. Only to be used by
371 // MutableBigInt::New*.
373 uint32_t length, AllocationType allocation = AllocationType::kYoung);
374
375 // Create a serialized scope info.
376 Handle<ScopeInfo> NewScopeInfo(int length,
377 AllocationType type = AllocationType::kOld);
378
379 DirectHandle<SourceTextModuleInfo> NewSourceTextModuleInfo();
380
381 Handle<DescriptorArray> NewDescriptorArray(
382 int number_of_descriptors, int slack = 0,
383 AllocationType allocation = AllocationType::kYoung);
384
385 Handle<ClassPositions> NewClassPositions(int start, int end);
386
387 Handle<SwissNameDictionary> NewSwissNameDictionary(
388 int at_least_space_for = kSwissNameDictionaryInitialCapacity,
389 AllocationType allocation = AllocationType::kYoung);
390
391 Handle<SwissNameDictionary> NewSwissNameDictionaryWithCapacity(
392 int capacity, AllocationType allocation);
393
394 DirectHandle<FunctionTemplateRareData> NewFunctionTemplateRareData();
395
396 MaybeDirectHandle<Map> GetInPlaceInternalizedStringMap(
397 Tagged<Map> from_string_map);
398
399 AllocationType RefineAllocationTypeForInPlaceInternalizableString(
400 AllocationType allocation, Tagged<Map> string_map);
401
402#ifdef V8_ENABLE_LEAPTIERING
403 JSDispatchHandle NewJSDispatchHandle(uint16_t parameter_count,
405 JSDispatchTable::Space* space);
406#endif
407
408 protected:
409 // Must be large enough to fit any double, int, or size_t.
410 static constexpr int kNumberToStringBufferSize = 32;
411
412 // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
413 Tagged<HeapObject> AllocateRawArray(int size, AllocationType allocation);
414 Tagged<HeapObject> AllocateRawFixedArray(int length,
415 AllocationType allocation);
416 Tagged<HeapObject> AllocateRawWeakArrayList(int length,
417 AllocationType allocation);
418
419 template <typename StructType>
420 inline Tagged<StructType> NewStructInternal(InstanceType type,
421 AllocationType allocation);
422 Tagged<Struct> NewStructInternal(ReadOnlyRoots roots, Tagged<Map> map,
423 int size, AllocationType allocation);
424
425 Tagged<HeapObject> AllocateRawWithImmortalMap(
426 int size, AllocationType allocation, Tagged<Map> map,
427 AllocationAlignment alignment = kTaggedAligned);
428 Tagged<HeapObject> NewWithImmortalMap(Tagged<Map> map,
429 AllocationType allocation);
430
431 Handle<FixedArray> NewFixedArrayWithFiller(DirectHandle<Map> map, int length,
433 AllocationType allocation);
434
435 Handle<SharedFunctionInfo> NewSharedFunctionInfo(AllocationType allocation);
436 Handle<SharedFunctionInfo> NewSharedFunctionInfo(
437 MaybeDirectHandle<String> maybe_name,
438 MaybeDirectHandle<HeapObject> maybe_function_data, Builtin builtin,
439 int len, AdaptArguments adapt,
440 FunctionKind kind = FunctionKind::kNormalFunction);
441
442 Handle<String> MakeOrFindTwoCharacterString(uint16_t c1, uint16_t c2);
443
444 template <typename SeqStringT>
445 MaybeHandle<SeqStringT> NewRawStringWithMap(int length, Tagged<Map> map,
446 AllocationType allocation);
447
448 private:
449 Impl* impl() { return static_cast<Impl*>(this); }
450 auto isolate() { return impl()->isolate(); }
451 ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }
452
453 Tagged<HeapObject> AllocateRaw(
454 int size, AllocationType allocation,
455 AllocationAlignment alignment = kTaggedAligned);
456
457 friend TorqueGeneratedFactory<Impl>;
458 template <class Derived, class Shape, class Super>
459 friend class TaggedArrayBase;
460 template <class Derived, class Shape, class Super>
461 friend class PrimitiveArrayBase;
462};
463
464extern template class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
466extern template class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
468
469} // namespace internal
470} // namespace v8
471
472#endif // V8_HEAP_FACTORY_BASE_H_
int16_t parameter_count
Definition builtins.cc:67
Builtins::Kind kind
Definition builtins.cc:40
V8_WARN_UNUSED_RESULT HandleType< String >::MaybeType NewConsString(HandleType< String > left, HandleType< String > right, AllocationType allocation=AllocationType::kYoung)
Handle< String > NewStringFromAsciiChecked(const char *str, AllocationType allocation=AllocationType::kYoung)
Handle< String > NewStringFromAsciiChecked(std::string_view str, AllocationType allocation=AllocationType::kYoung)
Handle< String > InternalizeString(base::Vector< const uint8_t > string, bool convert_encoding=false)
ReadOnlyRoots read_only_roots()
int start
int end
#define EXPORT_TEMPLATE_DECLARE(export)
#define ROOT_ACCESSOR(Type, name, CamelName)
FunctionLiteral * literal
Definition liveedit.cc:294
Definition c-api.cc:87
#define MUTABLE_ROOT_LIST(V)
Definition roots.h:483
#define READ_ONLY_ROOT_LIST(V)
Definition roots.h:468
#define V8_EXPORT_PRIVATE
Definition macros.h:460
MaybeHandle< InstructionStream > instruction_stream
MaybeHandle< DeoptimizationData > deoptimization_data
MaybeHandle< TrustedByteArray > bytecode_offset_table
MaybeHandle< TrustedObject > bytecode_or_interpreter_data
MaybeHandle< TrustedByteArray > source_position_table
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
std::unique_ptr< ValueMirror > key