factory-base.h 10.4 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
#include "src/roots/roots.h"
13
#include "torque-generated/class-forward-declarations.h"
14 15 16 17 18

namespace v8 {
namespace internal {

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
namespace wasm {
class ValueType;
}  // namespace wasm

template <typename Impl>
class FactoryBase;

// Putting Torque-generated definitions in a superclass allows to shadow them
// easily when they shouldn't be used and to reference them when they happen to
// have the same signature.
template <typename Impl>
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) TorqueGeneratedFactory {
 private:
  FactoryBase<Impl>* factory() { return static_cast<FactoryBase<Impl>*>(this); }

 public:
#include "torque-generated/factory.inc"
};

59
template <typename Impl>
60 61
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) FactoryBase
    : public TorqueGeneratedFactory<Impl> {
62
 public:
63
  // Converts the given boolean condition to JavaScript boolean value.
64
  inline Handle<Oddball> ToBoolean(bool value);
65 66 67 68

  // 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>
69
  inline Handle<Object> NewNumber(double value);
70
  template <AllocationType allocation = AllocationType::kYoung>
71
  inline Handle<Object> NewNumberFromInt(int32_t value);
72
  template <AllocationType allocation = AllocationType::kYoung>
73
  inline Handle<Object> NewNumberFromUint(uint32_t value);
74
  template <AllocationType allocation = AllocationType::kYoung>
75
  inline Handle<Object> NewNumberFromSize(size_t value);
76
  template <AllocationType allocation = AllocationType::kYoung>
77
  inline Handle<Object> NewNumberFromInt64(int64_t value);
78
  template <AllocationType allocation = AllocationType::kYoung>
79
  inline Handle<HeapNumber> NewHeapNumber(double value);
80
  template <AllocationType allocation = AllocationType::kYoung>
81
  inline Handle<HeapNumber> NewHeapNumberFromBits(uint64_t bits);
82
  template <AllocationType allocation = AllocationType::kYoung>
83
  inline Handle<HeapNumber> NewHeapNumberWithHoleNaN();
84 85

  template <AllocationType allocation>
86
  Handle<HeapNumber> NewHeapNumber();
87

88 89
  Handle<Struct> NewStruct(InstanceType type,
                           AllocationType allocation = AllocationType::kYoung);
90

91 92 93
  // Create a pre-tenured empty AccessorPair.
  Handle<AccessorPair> NewAccessorPair();

94
  // Allocates a fixed array initialized with undefined values.
95
  Handle<FixedArray> NewFixedArray(
96 97 98 99
      int length, AllocationType allocation = AllocationType::kYoung);

  // Allocates a fixed array-like object with given map and initialized with
  // undefined values.
100
  Handle<FixedArray> NewFixedArrayWithMap(
101 102
      Handle<Map> map, int length,
      AllocationType allocation = AllocationType::kYoung);
103 104

  // Allocate a new fixed array with non-existing entries (the hole).
105
  Handle<FixedArray> NewFixedArrayWithHoles(
106 107 108 109 110
      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.
111
  Handle<FixedArrayBase> NewFixedDoubleArray(
112 113
      int length, AllocationType allocation = AllocationType::kYoung);

114 115
  // Allocates a weak fixed array-like object with given map and initialized
  // with undefined values.
116
  Handle<WeakFixedArray> NewWeakFixedArrayWithMap(
117 118 119 120
      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
121
  Handle<WeakFixedArray> NewWeakFixedArray(
122 123
      int length, AllocationType allocation = AllocationType::kYoung);

124
  Handle<ByteArray> NewByteArray(
125 126
      int length, AllocationType allocation = AllocationType::kYoung);

127 128 129
  Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
                                         int frame_size, int parameter_count,
                                         Handle<FixedArray> constant_pool);
130

131 132
  // 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.
133
  Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
134 135 136
      int boilerplate, int all_properties, int index_keys, bool has_seen_proto);

  // Create a new ArrayBoilerplateDescription struct.
137 138
  Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
      ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);
139 140

  // Create a new TemplateObjectDescription struct.
141 142
  Handle<TemplateObjectDescription> NewTemplateObjectDescription(
      Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings);
143

144 145
  Handle<Script> NewScript(Handle<String> source);
  Handle<Script> NewScriptWithId(Handle<String> source, int script_id);
146

147 148
  Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
      FunctionLiteral* literal, Handle<Script> script, bool is_toplevel);
149

150
  Handle<PreparseData> NewPreparseData(int data_length, int children_length);
151

152 153
  Handle<UncompiledDataWithoutPreparseData>
  NewUncompiledDataWithoutPreparseData(Handle<String> inferred_name,
154 155 156
                                       int32_t start_position,
                                       int32_t end_position);

157 158 159
  Handle<UncompiledDataWithPreparseData> NewUncompiledDataWithPreparseData(
      Handle<String> inferred_name, int32_t start_position,
      int32_t end_position, Handle<PreparseData>);
160

161 162
  // Allocates a FeedbackMedata object and zeroes the data section.
  Handle<FeedbackMetadata> NewFeedbackMetadata(
163
      int slot_count, int create_closure_slot_count,
164 165
      AllocationType allocation = AllocationType::kOld);

166
  Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);
167

168 169 170 171 172 173 174 175
  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);

176
  Handle<SeqOneByteString> NewOneByteInternalizedString(
177
      const Vector<const uint8_t>& str, uint32_t raw_hash_field);
178
  Handle<SeqTwoByteString> NewTwoByteInternalizedString(
179
      const Vector<const uc16>& str, uint32_t raw_hash_field);
180

181
  Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
182
      int length, uint32_t raw_hash_field);
183
  Handle<SeqTwoByteString> AllocateRawTwoByteInternalizedString(
184
      int length, uint32_t raw_hash_field);
185 186 187 188

  // 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.
189 190 191 192
  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);
193
  // Create a new cons string object which consists of a pair of strings.
194 195
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(
      Handle<String> left, Handle<String> right,
196 197
      AllocationType allocation = AllocationType::kYoung);

198 199 200
  V8_WARN_UNUSED_RESULT Handle<String> NewConsString(
      Handle<String> left, Handle<String> right, int length, bool one_byte,
      AllocationType allocation = AllocationType::kYoung);
201

202 203
  // Allocates a new BigInt with {length} digits. Only to be used by
  // MutableBigInt::New*.
204
  Handle<FreshlyAllocatedBigInt> NewBigInt(
205 206
      int length, AllocationType allocation = AllocationType::kYoung);

207
  // Create a serialized scope info.
208 209
  Handle<ScopeInfo> NewScopeInfo(int length,
                                 AllocationType type = AllocationType::kOld);
210

211
  Handle<SourceTextModuleInfo> NewSourceTextModuleInfo();
212

213 214 215 216 217 218
  Handle<DescriptorArray> NewDescriptorArray(
      int number_of_entries, int slack = 0,
      AllocationType allocation = AllocationType::kYoung);

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

219
 protected:
220 221 222 223 224
  // 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);

225 226 227
  HeapObject AllocateRawWithImmortalMap(
      int size, AllocationType allocation, Map map,
      AllocationAlignment alignment = kWordAligned);
228
  HeapObject NewWithImmortalMap(Map map, AllocationType allocation);
229

230 231
  Handle<FixedArray> NewFixedArrayWithFiller(Handle<Map> map, int length,
                                             Handle<Oddball> filler,
232
                                             AllocationType allocation);
233

234 235 236 237 238
  Handle<SharedFunctionInfo> NewSharedFunctionInfo();
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
      MaybeHandle<String> maybe_name,
      MaybeHandle<HeapObject> maybe_function_data, int maybe_builtin_index,
      FunctionKind kind = kNormalFunction);
239

240 241
  Handle<String> MakeOrFindTwoCharacterString(uint16_t c1, uint16_t c2);

242 243
 private:
  Impl* impl() { return static_cast<Impl*>(this); }
244
  auto isolate() { return impl()->isolate(); }
245 246 247 248
  ReadOnlyRoots read_only_roots() { return impl()->read_only_roots(); }

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

  friend TorqueGeneratedFactory<Impl>;
251 252 253 254 255 256
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_FACTORY_BASE_H_