factory-base.h 9.69 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_HEAP_FACTORY_BASE_H_
#define V8_HEAP_FACTORY_BASE_H_

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 13 14 15 16 17
#include "src/roots/roots.h"

namespace v8 {
namespace internal {

class HeapObject;
18 19
class SharedFunctionInfo;
class FunctionLiteral;
20 21
class SeqOneByteString;
class SeqTwoByteString;
22
class FreshlyAllocatedBigInt;
23 24 25
class ObjectBoilerplateDescription;
class ArrayBoilerplateDescription;
class TemplateObjectDescription;
26
class SourceTextModuleInfo;
27 28 29
class PreparseData;
class UncompiledDataWithoutPreparseData;
class UncompiledDataWithPreparseData;
30 31
class BytecodeArray;
class CoverageInfo;
32
class ClassPositions;
33 34 35
struct SourceRange;
template <typename T>
class ZoneVector;
36 37

template <typename Impl>
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 44 45

  // Numbers (e.g. literals) are pretenured by the parser.
  // The return value may be a smi or a heap number.
  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 66
  Handle<Struct> NewStruct(InstanceType type,
                           AllocationType allocation = AllocationType::kYoung);
67

68 69 70
  // Create a pre-tenured empty AccessorPair.
  Handle<AccessorPair> NewAccessorPair();

71
  // Allocates a fixed array initialized with undefined values.
72
  Handle<FixedArray> NewFixedArray(
73 74 75 76
      int length, AllocationType allocation = AllocationType::kYoung);

  // Allocates a fixed array-like object with given map and initialized with
  // undefined values.
77
  Handle<FixedArray> NewFixedArrayWithMap(
78 79
      Handle<Map> map, int length,
      AllocationType allocation = AllocationType::kYoung);
80 81

  // Allocate a new fixed array with non-existing entries (the hole).
82
  Handle<FixedArray> NewFixedArrayWithHoles(
83 84 85 86 87
      int length, AllocationType allocation = AllocationType::kYoung);

  // Allocate a new uninitialized fixed double array.
  // The function returns a pre-allocated empty fixed array for length = 0,
  // so the return type must be the general fixed array class.
88
  Handle<FixedArrayBase> NewFixedDoubleArray(
89 90
      int length, AllocationType allocation = AllocationType::kYoung);

91 92
  // Allocates a weak fixed array-like object with given map and initialized
  // with undefined values.
93
  Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
94 95 96 97
      Map map, int length, AllocationType allocation = AllocationType::kYoung);

  // Allocates a fixed array which may contain in-place weak references. The
  // array is initialized with undefined values
98
  Handle<WeakFixedArray> NewWeakFixedArray(
99 100
      int length, AllocationType allocation = AllocationType::kYoung);

101
  Handle<ByteArray> NewByteArray(
102 103
      int length, AllocationType allocation = AllocationType::kYoung);

104 105 106
  Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
                                         int frame_size, int parameter_count,
                                         Handle<FixedArray> constant_pool);
107

108 109
  // Allocates a fixed array for name-value pairs of boilerplate properties and
  // calculates the number of properties we need to store in the backing store.
110
  Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
111 112 113
      int boilerplate, int all_properties, int index_keys, bool has_seen_proto);

  // Create a new ArrayBoilerplateDescription struct.
114 115
  Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
      ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
116 117

  // Create a new TemplateObjectDescription struct.
118 119
  Handle<TemplateObjectDescription> NewTemplateObjectDescription(
      Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings);
120

121 122
  Handle<Script> NewScript(Handle<String> source);
  Handle<Script> NewScriptWithId(Handle<String> source, int script_id);
123

124 125
  Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
      FunctionLiteral* literal, Handle<Script> script, bool is_toplevel);
126

127
  Handle<PreparseData> NewPreparseData(int data_length, int children_length);
128

129 130
  Handle<UncompiledDataWithoutPreparseData>
  NewUncompiledDataWithoutPreparseData(Handle<String> inferred_name,
131 132 133
                                       int32_t start_position,
                                       int32_t end_position);

134 135 136
  Handle<UncompiledDataWithPreparseData> NewUncompiledDataWithPreparseData(
      Handle<String> inferred_name, int32_t start_position,
      int32_t end_position, Handle<PreparseData>);
137

138 139 140 141 142
  // Allocates a FeedbackMedata object and zeroes the data section.
  Handle<FeedbackMetadata> NewFeedbackMetadata(
      int slot_count, int feedback_cell_count,
      AllocationType allocation = AllocationType::kOld);

143
  Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
144

145 146 147 148 149 150 151 152
  Handle<String> InternalizeString(const Vector<const uint8_t>& string,
                                   bool convert_encoding = false);
  Handle<String> InternalizeString(const Vector<const uint16_t>& string,
                                   bool convert_encoding = false);

  template <class StringTableKey>
  Handle<String> InternalizeStringWithKey(StringTableKey* key);

153
  Handle<SeqOneByteString> NewOneByteInternalizedString(
154
      const Vector<const uint8_t>& str, uint32_t hash_field);
155
  Handle<SeqTwoByteString> NewTwoByteInternalizedString(
156 157
      const Vector<const uc16>& str, uint32_t hash_field);

158
  Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
159
      int length, uint32_t hash_field);
160
  Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
161 162 163 164 165
      int length, uint32_t hash_field);

  // Allocates and partially initializes an one-byte or two-byte String. The
  // characters of the string are uninitialized. Currently used in regexp code
  // only, where they are pretenured.
166 167 168 169
  V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
      int length, AllocationType allocation = AllocationType::kYoung);
  V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
      int length, AllocationType allocation = AllocationType::kYoung);
170
  // Create a new cons string object which consists of a pair of strings.
171 172
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(
      Handle<String> left, Handle<String> right,
173 174
      AllocationType allocation = AllocationType::kYoung);

175 176 177
  V8_WARN_UNUSED_RESULT Handle<String> NewConsString(
      Handle<String> left, Handle<String> right, int length, bool one_byte,
      AllocationType allocation = AllocationType::kYoung);
178

179 180
  // Allocates a new BigInt with {length} digits. Only to be used by
  // MutableBigInt::New*.
181
  Handle<FreshlyAllocatedBigInt> NewBigInt(
182 183
      int length, AllocationType allocation = AllocationType::kYoung);

184
  // Create a serialized scope info.
185 186
  Handle<ScopeInfo> NewScopeInfo(int length,
                                 AllocationType type = AllocationType::kOld);
187

188
  Handle<SourceTextModuleInfo> NewSourceTextModuleInfo();
189

190 191 192 193 194 195
  Handle<DescriptorArray> NewDescriptorArray(
      int number_of_entries, int slack = 0,
      AllocationType allocation = AllocationType::kYoung);

  Handle<ClassPositions> NewClassPositions(int start, int end);

196
 protected:
197 198 199 200 201
  // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
  HeapObject AllocateRawArray(int size, AllocationType allocation);
  HeapObject AllocateRawFixedArray(int length, AllocationType allocation);
  HeapObject AllocateRawWeakArrayList(int length, AllocationType allocation);

202 203 204
  HeapObject AllocateRawWithImmortalMap(
      int size, AllocationType allocation, Map map,
      AllocationAlignment alignment = kWordAligned);
205
  HeapObject NewWithImmortalMap(Map map, AllocationType allocation);
206

207 208
  Handle<FixedArray> NewFixedArrayWithFiller(Handle<Map> map, int length,
                                             Handle<Oddball> filler,
209
                                             AllocationType allocation);
210

211 212 213 214 215
  Handle<SharedFunctionInfo> NewSharedFunctionInfo();
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
      MaybeHandle<String> maybe_name,
      MaybeHandle<HeapObject> maybe_function_data, int maybe_builtin_index,
      FunctionKind kind = kNormalFunction);
216

217 218
  Handle<String> MakeOrFindTwoCharacterString(uint16_t c1, uint16_t c2);

219 220
 private:
  Impl* impl() { return static_cast<Impl*>(this); }
221
  auto isolate() { return impl()->isolate(); }
222 223 224 225 226 227 228 229 230 231
  ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }

  HeapObject AllocateRaw(int size, AllocationType allocation,
                         AllocationAlignment alignment = kWordAligned);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_FACTORY_BASE_H_