factory.h 46.3 KB
Newer Older
1 2 3
// Copyright 2014 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.
4

5 6
#ifndef V8_HEAP_FACTORY_H_
#define V8_HEAP_FACTORY_H_
7

8 9
// Clients of this interface shouldn't depend on lots of heap internals.
// Do not include anything from src/heap here!
10
#include "src/builtins/builtins.h"
11
#include "src/globals.h"
12 13
#include "src/handles.h"
#include "src/heap/heap.h"
14
#include "src/maybe-handles.h"
15
#include "src/messages.h"
16
#include "src/objects/code.h"
17
#include "src/objects/dictionary.h"
18
#include "src/objects/hash-table.h"
19
#include "src/objects/js-array-buffer.h"
20 21
#include "src/objects/js-array.h"
#include "src/objects/js-regexp.h"
22
#include "src/objects/ordered-hash-table.h"
23
#include "src/objects/string.h"
24

25 26
namespace v8 {
namespace internal {
27

28
// Forward declarations.
29
class AliasedArgumentsEntry;
30
class ObjectBoilerplateDescription;
31 32
class BreakPoint;
class BreakPointInfo;
33 34
class CallableTask;
class CallbackTask;
35
class CallHandlerInfo;
36
class Expression;
37
class ArrayBoilerplateDescription;
38
class CoverageInfo;
39
class DebugInfo;
40
class EnumCache;
41
class FreshlyAllocatedBigInt;
42
class Isolate;
43
class JSGeneratorObject;
44 45
class JSMap;
class JSMapIterator;
46
class JSModuleNamespace;
47
class JSProxy;
48 49
class JSSet;
class JSSetIterator;
50
class JSWeakMap;
51 52
class LoadHandler;
class ModuleInfo;
53
class NativeContext;
54
class NewFunctionArgs;
55
class PreParsedScopeData;
56
class PromiseResolveThenableJobTask;
57 58 59
class RegExpMatchInfo;
class ScriptContextTable;
class StoreHandler;
60
class TemplateObjectDescription;
61 62
class UncompiledDataWithoutPreParsedScope;
class UncompiledDataWithPreParsedScope;
63
class WasmExportedFunctionData;
64 65 66
struct SourceRange;
template <typename T>
class ZoneVector;
67

68
enum FunctionMode {
69 70 71 72 73 74
  kWithNameBit = 1 << 0,
  kWithHomeObjectBit = 1 << 1,
  kWithWritablePrototypeBit = 1 << 2,
  kWithReadonlyPrototypeBit = 1 << 3,
  kWithPrototypeBits = kWithWritablePrototypeBit | kWithReadonlyPrototypeBit,

75
  // Without prototype.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  FUNCTION_WITHOUT_PROTOTYPE = 0,
  METHOD_WITH_NAME = kWithNameBit,
  METHOD_WITH_HOME_OBJECT = kWithHomeObjectBit,
  METHOD_WITH_NAME_AND_HOME_OBJECT = kWithNameBit | kWithHomeObjectBit,

  // With writable prototype.
  FUNCTION_WITH_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit,
  FUNCTION_WITH_NAME_AND_WRITEABLE_PROTOTYPE =
      kWithWritablePrototypeBit | kWithNameBit,
  FUNCTION_WITH_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE =
      kWithWritablePrototypeBit | kWithHomeObjectBit,
  FUNCTION_WITH_NAME_AND_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE =
      kWithWritablePrototypeBit | kWithNameBit | kWithHomeObjectBit,

  // With readonly prototype.
  FUNCTION_WITH_READONLY_PROTOTYPE = kWithReadonlyPrototypeBit,
  FUNCTION_WITH_NAME_AND_READONLY_PROTOTYPE =
      kWithReadonlyPrototypeBit | kWithNameBit,
94 95
};

96
// Interface for handle based allocation.
97
class V8_EXPORT_PRIVATE Factory {
98
 public:
99
  Handle<Oddball> NewOddball(Handle<Map> map, const char* to_string,
100
                             Handle<Object> to_number, const char* type_of,
101 102
                             byte kind,
                             PretenureFlag pretenure = TENURED_READ_ONLY);
103

104
  // Marks self references within code generation.
105
  Handle<Oddball> NewSelfReferenceMarker(PretenureFlag pretenure = TENURED);
106

107 108
  // Allocates a fixed array-like object with given map and initialized with
  // undefined values.
109 110 111
  template <typename T = FixedArray>
  Handle<T> NewFixedArrayWithMap(Heap::RootListIndex map_root_index, int length,
                                 PretenureFlag pretenure = NOT_TENURED);
112

113 114 115 116 117 118 119
  // Allocates a weak fixed array-like object with given map and initialized
  // with undefined values.
  template <typename T = WeakFixedArray>
  Handle<T> NewWeakFixedArrayWithMap(Heap::RootListIndex map_root_index,
                                     int length,
                                     PretenureFlag pretenure = NOT_TENURED);

120
  // Allocates a fixed array initialized with undefined values.
121
  Handle<FixedArray> NewFixedArray(int length,
122
                                   PretenureFlag pretenure = NOT_TENURED);
123 124 125 126 127 128

  // Allocates a fixed array which may contain in-place weak references. The
  // array is initialized with undefined values
  Handle<WeakFixedArray> NewWeakFixedArray(
      int length, PretenureFlag pretenure = NOT_TENURED);

129
  // Allocates a property array initialized with undefined values.
130
  Handle<PropertyArray> NewPropertyArray(int length,
131
                                         PretenureFlag pretenure = NOT_TENURED);
132 133 134 135 136
  // Tries allocating a fixed array initialized with undefined values.
  // In case of an allocation failure (OOM) an empty handle is returned.
  // The caller has to manually signal an
  // v8::internal::Heap::FatalProcessOutOfMemory typically by calling
  // NewFixedArray as a fallback.
137
  V8_WARN_UNUSED_RESULT
138
  MaybeHandle<FixedArray> TryNewFixedArray(
139
      int length, PretenureFlag pretenure = NOT_TENURED);
140 141

  // Allocate a new fixed array with non-existing entries (the hole).
142
  Handle<FixedArray> NewFixedArrayWithHoles(
143
      int length, PretenureFlag pretenure = NOT_TENURED);
144

145
  // Allocates an uninitialized fixed array. It must be filled by the caller.
146 147
  Handle<FixedArray> NewUninitializedFixedArray(
      int length, PretenureFlag pretenure = NOT_TENURED);
148

149 150 151 152 153
  // Allocates a feedback vector whose slots are initialized with undefined
  // values.
  Handle<FeedbackVector> NewFeedbackVector(
      Handle<SharedFunctionInfo> shared, PretenureFlag pretenure = NOT_TENURED);

154 155
  // 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.
156 157
  Handle<ObjectBoilerplateDescription> NewObjectBoilerplateDescription(
      int boilerplate, int all_properties, int index_keys, bool has_seen_proto);
158

159
  // Allocate a new uninitialized fixed double array.
160
  // The function returns a pre-allocated empty fixed array for length = 0,
161 162
  // so the return type must be the general fixed array class.
  Handle<FixedArrayBase> NewFixedDoubleArray(
163
      int length, PretenureFlag pretenure = NOT_TENURED);
164

165
  // Allocate a new fixed double array with hole values.
166
  Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
167
      int size, PretenureFlag pretenure = NOT_TENURED);
168

169
  // Allocates a FeedbackMedata object and zeroes the data section.
170 171
  Handle<FeedbackMetadata> NewFeedbackMetadata(int slot_count,
                                               PretenureFlag tenure = TENURED);
172

jgruber's avatar
jgruber committed
173 174 175
  Handle<FrameArray> NewFrameArray(int number_of_frames,
                                   PretenureFlag pretenure = NOT_TENURED);

176 177 178
  Handle<OrderedHashSet> NewOrderedHashSet();
  Handle<OrderedHashMap> NewOrderedHashMap();

179
  Handle<SmallOrderedHashSet> NewSmallOrderedHashSet(
180
      int capacity = SmallOrderedHashSet::kMinCapacity,
181
      PretenureFlag pretenure = NOT_TENURED);
182
  Handle<SmallOrderedHashMap> NewSmallOrderedHashMap(
183
      int capacity = SmallOrderedHashMap::kMinCapacity,
184
      PretenureFlag pretenure = NOT_TENURED);
185

186 187 188
  // Create a new PrototypeInfo struct.
  Handle<PrototypeInfo> NewPrototypeInfo();

189 190 191 192
  // Create a new EnumCache struct.
  Handle<EnumCache> NewEnumCache(Handle<FixedArray> keys,
                                 Handle<FixedArray> indices);

193
  // Create a new Tuple2 struct.
194 195
  Handle<Tuple2> NewTuple2(Handle<Object> value1, Handle<Object> value2,
                           PretenureFlag pretenure);
196

197 198
  // Create a new Tuple3 struct.
  Handle<Tuple3> NewTuple3(Handle<Object> value1, Handle<Object> value2,
199
                           Handle<Object> value3, PretenureFlag pretenure);
200

201 202
  // Create a new ArrayBoilerplateDescription struct.
  Handle<ArrayBoilerplateDescription> NewArrayBoilerplateDescription(
203 204
      ElementsKind elements_kind, Handle<FixedArrayBase> constant_values);

205 206
  // Create a new TemplateObjectDescription struct.
  Handle<TemplateObjectDescription> NewTemplateObjectDescription(
207
      Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings);
208

209
  // Create a pre-tenured empty AccessorPair.
210
  Handle<AccessorPair> NewAccessorPair();
211

212 213
  // Finds the internalized copy for string in the string table.
  // If not found, a new string is added to the table and returned.
214
  Handle<String> InternalizeUtf8String(Vector<const char> str);
215 216
  Handle<String> InternalizeUtf8String(const char* str) {
    return InternalizeUtf8String(CStrVector(str));
217
  }
218

219
  Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
220 221
  Handle<String> InternalizeOneByteString(Handle<SeqOneByteString>, int from,
                                          int length);
222

223
  Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
224

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

228
  // Internalized strings are created in the old generation (data space).
229
  inline Handle<String> InternalizeString(Handle<String> string);
230

231
  inline Handle<Name> InternalizeName(Handle<Name> name);
232 233 234 235 236 237

  // String creation functions.  Most of the string creation functions take
  // a Heap::PretenureFlag argument to optionally request that they be
  // allocated in the old generation.  The pretenure flag defaults to
  // DONT_TENURE.
  //
238 239
  // Creates a new String object.  There are two String encodings: one-byte and
  // two-byte.  One should choose between the three string factory functions
240 241
  // based on the encoding of the string buffer that the string is
  // initialized from.
242 243 244
  //   - ...FromOneByte initializes the string from a buffer that is Latin1
  //     encoded (it does not check that the buffer is Latin1 encoded) and
  //     the result will be Latin1 encoded.
245
  //   - ...FromUtf8 initializes the string from a buffer that is UTF-8
246 247 248 249 250
  //     encoded.  If the characters are all ASCII characters, the result
  //     will be Latin1 encoded, otherwise it will converted to two-byte.
  //   - ...FromTwoByte initializes the string from a buffer that is two-byte
  //     encoded.  If the characters are all Latin1 characters, the result
  //     will be converted to Latin1, otherwise it will be left as two-byte.
251
  //
252
  // One-byte strings are pretenured when used as keys in the SourceCodeCache.
253
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromOneByte(
254
      Vector<const uint8_t> str, PretenureFlag pretenure = NOT_TENURED);
255

256 257 258
  template <size_t N>
  inline Handle<String> NewStringFromStaticChars(
      const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) {
259
    DCHECK(N == StrLength(str) + 1);
260 261
    return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure)
        .ToHandleChecked();
262 263 264
  }

  inline Handle<String> NewStringFromAsciiChecked(
265 266 267
      const char* str, PretenureFlag pretenure = NOT_TENURED) {
    return NewStringFromOneByte(OneByteVector(str), pretenure)
        .ToHandleChecked();
268 269
  }

270 271
  // UTF8 strings are pretenured when used for regexp literal patterns and
  // flags in the parser.
272
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromUtf8(
273
      Vector<const char> str, PretenureFlag pretenure = NOT_TENURED);
274

275
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromUtf8SubString(
276 277
      Handle<SeqOneByteString> str, int begin, int end,
      PretenureFlag pretenure = NOT_TENURED);
278

279
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromTwoByte(
280
      Vector<const uc16> str, PretenureFlag pretenure = NOT_TENURED);
281

282
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewStringFromTwoByte(
283 284
      const ZoneVector<uc16>* str, PretenureFlag pretenure = NOT_TENURED);

285 286
  Handle<JSStringIterator> NewJSStringIterator(Handle<String> string);

287 288
  // Allocates an internalized string in old space based on the character
  // stream.
289 290
  Handle<String> NewInternalizedStringFromUtf8(Vector<const char> str,
                                               int chars, uint32_t hash_field);
291

292 293
  Handle<String> NewOneByteInternalizedString(Vector<const uint8_t> str,
                                              uint32_t hash_field);
294

295 296 297
  Handle<String> NewOneByteInternalizedSubString(
      Handle<SeqOneByteString> string, int offset, int length,
      uint32_t hash_field);
298

299 300
  Handle<String> NewTwoByteInternalizedString(Vector<const uc16> str,
                                              uint32_t hash_field);
301

302 303
  Handle<String> NewInternalizedStringImpl(Handle<String> string, int chars,
                                           uint32_t hash_field);
304 305 306

  // Compute the matching internalized string map for a string if possible.
  // Empty handle is returned if string is in new space or not flattened.
307
  V8_WARN_UNUSED_RESULT MaybeHandle<Map> InternalizedStringMapForString(
308 309
      Handle<String> string);

310 311 312 313 314
  // Creates an internalized copy of an external string. |string| must be
  // of type StringClass.
  template <class StringClass>
  Handle<StringClass> InternalizeExternalString(Handle<String> string);

315
  // Allocates and partially initializes an one-byte or two-byte String. The
316 317
  // characters of the string are uninitialized. Currently used in regexp code
  // only, where they are pretenured.
318 319 320 321
  V8_WARN_UNUSED_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
      int length, PretenureFlag pretenure = NOT_TENURED);
  V8_WARN_UNUSED_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
      int length, PretenureFlag pretenure = NOT_TENURED);
322

323
  // Creates a single character string where the character has given code.
324
  // A cache is used for Latin1 codes.
325
  Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
326

327
  // Create a new cons string object which consists of a pair of strings.
328 329
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
                                                          Handle<String> right);
330

331 332 333
  V8_WARN_UNUSED_RESULT Handle<String> NewConsString(Handle<String> left,
                                                     Handle<String> right,
                                                     int length, bool one_byte);
334

335 336 337 338
  // Create or lookup a single characters tring made up of a utf16 surrogate
  // pair.
  Handle<String> NewSurrogatePairString(uint16_t lead, uint16_t trail);

339
  // Create a new string object which holds a proper substring of a string.
340
  Handle<String> NewProperSubString(Handle<String> str, int begin, int end);
341

342
  // Create a new string object which holds a substring of a string.
343
  inline Handle<String> NewSubString(Handle<String> str, int begin, int end);
344

345
  // Creates a new external String object.  There are two String encodings
346
  // in the system: one-byte and two-byte.  Unlike other String types, it does
347
  // not make sense to have a UTF-8 factory function for external strings,
348 349
  // because we cannot change the underlying buffer.  Note that these strings
  // are backed by a string resource that resides outside the V8 heap.
350
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
351
      const ExternalOneByteString::Resource* resource);
352
  V8_WARN_UNUSED_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
353
      const ExternalTwoByteString::Resource* resource);
354 355 356 357
  // Create a new external string object for one-byte encoded native script.
  // It does not cache the resource data pointer.
  Handle<ExternalOneByteString> NewNativeSourceString(
      const ExternalOneByteString::Resource* resource);
358

359 360 361
  // Create a symbol in old or read-only space.
  Handle<Symbol> NewSymbol(PretenureFlag pretenure = TENURED);
  Handle<Symbol> NewPrivateSymbol(PretenureFlag pretenure = TENURED);
362
  Handle<Symbol> NewPrivateFieldSymbol();
363

364
  // Create a global (but otherwise uninitialized) context.
365
  Handle<NativeContext> NewNativeContext();
366

367
  // Create a script context.
368
  Handle<Context> NewScriptContext(Handle<NativeContext> outer,
369 370
                                   Handle<ScopeInfo> scope_info);

371 372
  // Create an empty script context table.
  Handle<ScriptContextTable> NewScriptContextTable();
373

374
  // Create a module context.
375 376
  Handle<Context> NewModuleContext(Handle<Module> module,
                                   Handle<NativeContext> outer,
377
                                   Handle<ScopeInfo> scope_info);
378

379
  // Create a function or eval context.
380 381
  Handle<Context> NewFunctionContext(Handle<Context> outer,
                                     Handle<ScopeInfo> scope_info);
382

383
  // Create a catch context.
384
  Handle<Context> NewCatchContext(Handle<Context> previous,
385
                                  Handle<ScopeInfo> scope_info,
386
                                  Handle<Object> thrown_object);
387

388
  // Create a 'with' context.
389
  Handle<Context> NewWithContext(Handle<Context> previous,
390
                                 Handle<ScopeInfo> scope_info,
391
                                 Handle<JSReceiver> extension);
392

393
  Handle<Context> NewDebugEvaluateContext(Handle<Context> previous,
394
                                          Handle<ScopeInfo> scope_info,
395 396 397 398
                                          Handle<JSReceiver> extension,
                                          Handle<Context> wrapped,
                                          Handle<StringSet> whitelist);

399
  // Create a block context.
400
  Handle<Context> NewBlockContext(Handle<Context> previous,
401
                                  Handle<ScopeInfo> scope_info);
402

403 404 405 406 407
  // Create a context that's used by builtin functions.
  //
  // These are similar to function context but don't have a previous
  // context or any scope info. These are used to store spec defined
  // context values.
408 409
  Handle<Context> NewBuiltinContext(Handle<NativeContext> native_context,
                                    int length);
410

411 412
  Handle<Struct> NewStruct(InstanceType type,
                           PretenureFlag pretenure = NOT_TENURED);
413

414 415 416
  Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
      int aliased_context_slot);

417
  Handle<AccessorInfo> NewAccessorInfo();
418

419 420
  Handle<Script> NewScript(Handle<String> source,
                           PretenureFlag tenure = TENURED);
421 422
  Handle<Script> NewScriptWithId(Handle<String> source, int script_id,
                                 PretenureFlag tenure = TENURED);
423
  Handle<Script> CloneScript(Handle<Script> script);
424

425
  Handle<BreakPointInfo> NewBreakPointInfo(int source_position);
426
  Handle<BreakPoint> NewBreakPoint(int id, Handle<String> condition);
427
  Handle<StackFrameInfo> NewStackFrameInfo();
428 429 430
  Handle<SourcePositionTableWithFrameCache>
  NewSourcePositionTableWithFrameCache(
      Handle<ByteArray> source_position_table,
431
      Handle<SimpleNumberDictionary> stack_frame_cache);
432

433 434 435 436 437
  // Allocate various microtasks.
  Handle<CallableTask> NewCallableTask(Handle<JSReceiver> callable,
                                       Handle<Context> context);
  Handle<CallbackTask> NewCallbackTask(Handle<Foreign> callback,
                                       Handle<Foreign> data);
438 439 440
  Handle<PromiseResolveThenableJobTask> NewPromiseResolveThenableJobTask(
      Handle<JSPromise> promise_to_resolve, Handle<JSReceiver> then,
      Handle<JSReceiver> thenable, Handle<Context> context);
441

442 443 444
  // Foreign objects are pretenured when allocated by the bootstrapper.
  Handle<Foreign> NewForeign(Address addr,
                             PretenureFlag pretenure = NOT_TENURED);
445

446 447
  Handle<ByteArray> NewByteArray(int length,
                                 PretenureFlag pretenure = NOT_TENURED);
448

449
  Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
450 451
                                         int frame_size, int parameter_count,
                                         Handle<FixedArray> constant_pool);
452

453 454
  Handle<FixedTypedArrayBase> NewFixedTypedArrayWithExternalPointer(
      int length, ExternalArrayType array_type, void* external_pointer,
455
      PretenureFlag pretenure = NOT_TENURED);
456 457

  Handle<FixedTypedArrayBase> NewFixedTypedArray(
458 459
      size_t length, size_t byte_length, ExternalArrayType array_type,
      bool initialize, PretenureFlag pretenure = NOT_TENURED);
460

461 462
  Handle<Cell> NewCell(Handle<Object> value);

463 464
  Handle<PropertyCell> NewPropertyCell(Handle<Name> name,
                                       PretenureFlag pretenure = TENURED);
465

466 467 468
  Handle<FeedbackCell> NewNoClosuresCell(Handle<HeapObject> value);
  Handle<FeedbackCell> NewOneClosureCell(Handle<HeapObject> value);
  Handle<FeedbackCell> NewManyClosuresCell(Handle<HeapObject> value);
469

470 471
  Handle<TransitionArray> NewTransitionArray(int number_of_transitions,
                                             int slack = 0);
472

473
  // Allocate a tenured AllocationSite. Its payload is null.
474
  Handle<AllocationSite> NewAllocationSite(bool with_weak_next);
475

476
  // Allocates and initializes a new Map.
477
  Handle<Map> NewMap(InstanceType type, int instance_size,
478 479
                     ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
                     int inobject_properties = 0);
480 481 482 483 484 485 486 487
  // Initializes the fields of a newly created Map. Exposed for tests and
  // heap setup; other code should just call NewMap which takes care of it.
  Map* InitializeMap(Map* map, InstanceType type, int instance_size,
                     ElementsKind elements_kind, int inobject_properties);

  // Allocate a block of memory in the given space (filled with a filler).
  // Used as a fall-back for generated code when the space is full.
  Handle<HeapObject> NewFillerObject(int size, bool double_align,
488 489
                                     AllocationSpace space);

490
  Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
491

492 493
  // Returns a deep copy of the JavaScript object.
  // Properties and elements are copied too.
494
  Handle<JSObject> CopyJSObject(Handle<JSObject> object);
495 496
  // Same as above, but also takes an AllocationSite to be appended in an
  // AllocationMemento.
497 498 499
  Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
                                                  Handle<AllocationSite> site);

500 501 502
  Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
                                           Handle<Map> map);

503 504 505
  Handle<FixedArray> CopyFixedArrayAndGrow(
      Handle<FixedArray> array, int grow_by,
      PretenureFlag pretenure = NOT_TENURED);
506

507 508 509 510
  Handle<WeakFixedArray> CopyWeakFixedArrayAndGrow(
      Handle<WeakFixedArray> array, int grow_by,
      PretenureFlag pretenure = NOT_TENURED);

511 512 513 514
  Handle<WeakArrayList> CopyWeakArrayListAndGrow(
      Handle<WeakArrayList> array, int grow_by,
      PretenureFlag pretenure = NOT_TENURED);

515 516 517 518
  Handle<PropertyArray> CopyPropertyArrayAndGrow(
      Handle<PropertyArray> array, int grow_by,
      PretenureFlag pretenure = NOT_TENURED);

519 520 521
  Handle<FixedArray> CopyFixedArrayUpTo(Handle<FixedArray> array, int new_len,
                                        PretenureFlag pretenure = NOT_TENURED);

522
  Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
523

524 525 526 527
  // This method expects a COW array in new space, and creates a copy
  // of it in old space.
  Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);

528
  Handle<FixedDoubleArray> CopyFixedDoubleArray(Handle<FixedDoubleArray> array);
529

530 531
  Handle<FeedbackVector> CopyFeedbackVector(Handle<FeedbackVector> array);

532
  // Numbers (e.g. literals) are pretenured by the parser.
533
  // The return value may be a smi or a heap number.
534
  Handle<Object> NewNumber(double value, PretenureFlag pretenure = NOT_TENURED);
535

536 537 538
  Handle<Object> NewNumberFromInt(int32_t value,
                                  PretenureFlag pretenure = NOT_TENURED);
  Handle<Object> NewNumberFromUint(uint32_t value,
539
                                   PretenureFlag pretenure = NOT_TENURED);
540 541 542 543 544
  inline Handle<Object> NewNumberFromSize(
      size_t value, PretenureFlag pretenure = NOT_TENURED);
  inline Handle<Object> NewNumberFromInt64(
      int64_t value, PretenureFlag pretenure = NOT_TENURED);
  inline Handle<HeapNumber> NewHeapNumber(
545
      double value, PretenureFlag pretenure = NOT_TENURED);
546
  inline Handle<HeapNumber> NewHeapNumberFromBits(
547
      uint64_t bits, PretenureFlag pretenure = NOT_TENURED);
548

549
  // Creates heap number object with not yet set value field.
550 551 552 553 554 555 556 557 558 559
  Handle<HeapNumber> NewHeapNumber(PretenureFlag pretenure = NOT_TENURED);

  Handle<MutableHeapNumber> NewMutableHeapNumber(
      PretenureFlag pretenure = NOT_TENURED);
  inline Handle<MutableHeapNumber> NewMutableHeapNumber(
      double value, PretenureFlag pretenure = NOT_TENURED);
  inline Handle<MutableHeapNumber> NewMutableHeapNumberFromBits(
      uint64_t bits, PretenureFlag pretenure = NOT_TENURED);
  inline Handle<MutableHeapNumber> NewMutableHeapNumberWithHoleNaN(
      PretenureFlag pretenure = NOT_TENURED);
560

561 562
  // Allocates a new BigInt with {length} digits. Only to be used by
  // MutableBigInt::New*.
563 564
  Handle<FreshlyAllocatedBigInt> NewBigInt(
      int length, PretenureFlag pretenure = NOT_TENURED);
565

566
  Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
567

568 569
  // Allocates and initializes a new JavaScript object based on a
  // constructor.
570 571
  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
572 573
  Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
                               PretenureFlag pretenure = NOT_TENURED);
574
  // JSObject without a prototype.
575 576
  Handle<JSObject> NewJSObjectWithNullProto(
      PretenureFlag pretenure = NOT_TENURED);
577

578
  // Global objects are pretenured and initialized based on a constructor.
579
  Handle<JSGlobalObject> NewJSGlobalObject(Handle<JSFunction> constructor);
580

581 582 583
  // Allocates and initializes a new JavaScript object based on a map.
  // Passing an allocation site means that a memento will be created that
  // points to the site.
584 585
  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
586
  Handle<JSObject> NewJSObjectFromMap(
587
      Handle<Map> map, PretenureFlag pretenure = NOT_TENURED,
588
      Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
589 590 591 592
  Handle<JSObject> NewSlowJSObjectFromMap(
      Handle<Map> map,
      int number_of_slow_properties = NameDictionary::kInitialCapacity,
      PretenureFlag pretenure = NOT_TENURED);
593

594
  // JS arrays are pretenured when allocated by the parser.
595

596 597
  // Create a JSArray with a specified length and elements initialized
  // according to the specified mode.
598
  Handle<JSArray> NewJSArray(
599 600
      ElementsKind elements_kind, int length, int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
601 602
      PretenureFlag pretenure = NOT_TENURED);

603
  Handle<JSArray> NewJSArray(
604
      int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
605
      PretenureFlag pretenure = NOT_TENURED) {
606 607 608
    if (capacity != 0) {
      elements_kind = GetHoleyElementsKind(elements_kind);
    }
609
    return NewJSArray(elements_kind, 0, capacity,
610
                      INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
611 612
  }

613
  // Create a JSArray with the given elements.
614 615 616
  Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements,
                                         ElementsKind elements_kind, int length,
                                         PretenureFlag pretenure = NOT_TENURED);
617

618
  inline Handle<JSArray> NewJSArrayWithElements(
619
      Handle<FixedArrayBase> elements,
620
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
621
      PretenureFlag pretenure = NOT_TENURED);
622

623
  void NewJSArrayStorage(
624
      Handle<JSArray> array, int length, int capacity,
625 626
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);

627 628
  Handle<JSWeakMap> NewJSWeakMap();

629 630
  Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);

631 632
  Handle<JSModuleNamespace> NewJSModuleNamespace();

633
  Handle<Module> NewModule(Handle<SharedFunctionInfo> code);
634

binji's avatar
binji committed
635
  Handle<JSArrayBuffer> NewJSArrayBuffer(
ben's avatar
ben committed
636 637
      SharedFlag shared = SharedFlag::kNotShared,
      PretenureFlag pretenure = NOT_TENURED);
638

639 640 641
  static void TypeAndSizeForElementsKind(ElementsKind kind,
                                         ExternalArrayType* array_type,
                                         size_t* element_size);
642

ben's avatar
ben committed
643 644
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       PretenureFlag pretenure = NOT_TENURED);
645

ben's avatar
ben committed
646 647
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
                                       PretenureFlag pretenure = NOT_TENURED);
648

649 650 651
  // Creates a new JSTypedArray with the specified buffer.
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       Handle<JSArrayBuffer> buffer,
ben's avatar
ben committed
652 653
                                       size_t byte_offset, size_t length,
                                       PretenureFlag pretenure = NOT_TENURED);
654

655 656
  // Creates a new on-heap JSTypedArray.
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
ben's avatar
ben committed
657 658
                                       size_t number_of_elements,
                                       PretenureFlag pretenure = NOT_TENURED);
659

660 661
  Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
                                   size_t byte_offset, size_t byte_length);
662

663
  Handle<JSIteratorResult> NewJSIteratorResult(Handle<Object> value, bool done);
664
  Handle<JSAsyncFromSyncIterator> NewJSAsyncFromSyncIterator(
665
      Handle<JSReceiver> sync_iterator, Handle<Object> next);
666

667 668 669
  Handle<JSMap> NewJSMap();
  Handle<JSSet> NewJSSet();

670 671 672 673 674 675
  Handle<JSMapIterator> NewJSMapIterator(Handle<Map> map,
                                         Handle<OrderedHashMap> table,
                                         int index);
  Handle<JSSetIterator> NewJSSetIterator(Handle<Map> map,
                                         Handle<OrderedHashSet> table,
                                         int index);
676

677 678 679 680 681
  // Allocates a bound function.
  MaybeHandle<JSBoundFunction> NewJSBoundFunction(
      Handle<JSReceiver> target_function, Handle<Object> bound_this,
      Vector<Handle<Object>> bound_args);

682
  // Allocates a Harmony proxy.
683
  Handle<JSProxy> NewJSProxy(Handle<JSReceiver> target,
684
                             Handle<JSReceiver> handler);
685

686 687 688 689 690 691 692
  // Reinitialize an JSGlobalProxy based on a constructor.  The object
  // must have the same size as objects allocated using the
  // constructor.  The object is reinitialized and behaves as an
  // object that has been freshly allocated using the constructor.
  void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
                                 Handle<JSFunction> constructor);

693
  Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy(int size);
694

695 696 697 698 699 700 701 702
  // Creates a new JSFunction according to the given args. This is the function
  // you'll probably want to use when creating a JSFunction from the runtime.
  Handle<JSFunction> NewFunction(const NewFunctionArgs& args);

  // For testing only. Creates a sloppy function without code.
  Handle<JSFunction> NewFunctionForTest(Handle<String> name);

  // Function creation from SharedFunctionInfo.
703

704 705
  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
      Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
706
      Handle<Context> context, Handle<FeedbackCell> feedback_cell,
707 708 709 710
      PretenureFlag pretenure = TENURED);

  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
      Handle<SharedFunctionInfo> function_info, Handle<Context> context,
711
      Handle<FeedbackCell> feedback_cell, PretenureFlag pretenure = TENURED);
712

713
  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
714
      Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
715
      Handle<Context> context, PretenureFlag pretenure = TENURED);
716 717 718

  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
      Handle<SharedFunctionInfo> function_info, Handle<Context> context,
719
      PretenureFlag pretenure = TENURED);
720

721 722 723 724
  // The choke-point for JSFunction creation. Handles allocation and
  // initialization. All other utility methods call into this.
  Handle<JSFunction> NewFunction(Handle<Map> map,
                                 Handle<SharedFunctionInfo> info,
725
                                 Handle<Context> context,
726
                                 PretenureFlag pretenure = TENURED);
727

728
  // Create a serialized scope info.
729
  Handle<ScopeInfo> NewScopeInfo(int length);
730

731 732
  Handle<ModuleInfo> NewModuleInfo();

733
  Handle<PreParsedScopeData> NewPreParsedScopeData(int length);
734

735
  Handle<UncompiledDataWithoutPreParsedScope>
736 737
  NewUncompiledDataWithoutPreParsedScope(Handle<String> inferred_name,
                                         int32_t start_position,
738 739
                                         int32_t end_position,
                                         int32_t function_literal_id);
740 741

  Handle<UncompiledDataWithPreParsedScope> NewUncompiledDataWithPreParsedScope(
742 743
      Handle<String> inferred_name, int32_t start_position,
      int32_t end_position, int32_t function_literal_id,
744
      Handle<PreParsedScopeData>);
745

746
  // Create an External object for V8's external API.
747 748
  Handle<JSObject> NewExternal(void* value);

749 750 751
  // Creates a new CodeDataContainer for a Code object.
  Handle<CodeDataContainer> NewCodeDataContainer(int flags);

752 753
  // Allocates a new code object (fully initialized). All header fields of the
  // returned object are immutable and the code object is write protected.
754 755 756
  // The reference to the Code object is stored in self_reference.
  // This allows generated code to reference its own Code object
  // by containing this handle.
757
  Handle<Code> NewCode(const CodeDesc& desc, Code::Kind kind,
758
                       Handle<Object> self_reference,
759
                       int32_t builtin_index = Builtins::kNoBuiltinId,
760 761 762
                       MaybeHandle<ByteArray> maybe_source_position_table =
                           MaybeHandle<ByteArray>(),
                       MaybeHandle<DeoptimizationData> maybe_deopt_data =
763
                           MaybeHandle<DeoptimizationData>(),
764
                       Movability movability = kMovable, uint32_t stub_key = 0,
765
                       bool is_turbofanned = false, int stack_slots = 0,
766 767
                       int safepoint_table_offset = 0,
                       int handler_table_offset = 0);
768

769 770 771 772 773 774 775 776 777 778 779 780 781 782
  // Like NewCode, this function allocates a new code object (fully
  // initialized). It may return an empty handle if the allocation does not
  // succeed.
  V8_WARN_UNUSED_RESULT MaybeHandle<Code> TryNewCode(
      const CodeDesc& desc, Code::Kind kind, Handle<Object> self_reference,
      int32_t builtin_index = Builtins::kNoBuiltinId,
      MaybeHandle<ByteArray> maybe_source_position_table =
          MaybeHandle<ByteArray>(),
      MaybeHandle<DeoptimizationData> maybe_deopt_data =
          MaybeHandle<DeoptimizationData>(),
      Movability movability = kMovable, uint32_t stub_key = 0,
      bool is_turbofanned = false, int stack_slots = 0,
      int safepoint_table_offset = 0, int handler_table_offset = 0);

783 784
  // Allocates a new, empty code object for use by builtin deserialization. The
  // given {size} argument specifies the size of the entire code object.
785 786
  // Can only be used when code space is unprotected and requires manual
  // initialization by the caller.
787 788
  Handle<Code> NewCodeForDeserialization(uint32_t size);

789 790 791 792 793
  // Allocates a new code object and initializes it as the trampoline to the
  // given off-heap entry point.
  Handle<Code> NewOffHeapTrampolineFor(Handle<Code> code,
                                       Address off_heap_entry);

794
  Handle<Code> CopyCode(Handle<Code> code);
795

796 797
  Handle<BytecodeArray> CopyBytecodeArray(Handle<BytecodeArray>);

798
  // Interface for creating error objects.
799 800
  Handle<Object> NewError(Handle<JSFunction> constructor,
                          Handle<String> message);
801

802
  Handle<Object> NewInvalidStringLengthError();
803

804
  inline Handle<Object> NewURIError();
805

806
  Handle<Object> NewError(Handle<JSFunction> constructor,
807
                          MessageTemplate::Template template_index,
808 809 810
                          Handle<Object> arg0 = Handle<Object>(),
                          Handle<Object> arg1 = Handle<Object>(),
                          Handle<Object> arg2 = Handle<Object>());
811

812 813 814 815 816 817 818 819 820 821 822
#define DECLARE_ERROR(NAME)                                          \
  Handle<Object> New##NAME(MessageTemplate::Template template_index, \
                           Handle<Object> arg0 = Handle<Object>(),   \
                           Handle<Object> arg1 = Handle<Object>(),   \
                           Handle<Object> arg2 = Handle<Object>());
  DECLARE_ERROR(Error)
  DECLARE_ERROR(EvalError)
  DECLARE_ERROR(RangeError)
  DECLARE_ERROR(ReferenceError)
  DECLARE_ERROR(SyntaxError)
  DECLARE_ERROR(TypeError)
823
  DECLARE_ERROR(WasmCompileError)
824
  DECLARE_ERROR(WasmLinkError)
825
  DECLARE_ERROR(WasmRuntimeError)
826
#undef DECLARE_ERROR
827

828 829
  Handle<String> NumberToString(Handle<Object> number, bool check_cache = true);
  Handle<String> NumberToString(Smi* number, bool check_cache = true);
830

831
  inline Handle<String> Uint32ToString(uint32_t value, bool check_cache = true);
832

833
#define ROOT_ACCESSOR(type, name, camel_name) inline Handle<type> name();
834
  ROOT_LIST(ROOT_ACCESSOR)
835 836
#undef ROOT_ACCESSOR

837
#define STRUCT_MAP_ACCESSOR(NAME, Name, name) inline Handle<Map> name##_map();
838 839
  STRUCT_LIST(STRUCT_MAP_ACCESSOR)
#undef STRUCT_MAP_ACCESSOR
840

841 842 843 844 845
#define ALLOCATION_SITE_MAP_ACCESSOR(NAME, Name, Size, name) \
  inline Handle<Map> name##_map();
  ALLOCATION_SITE_LIST(ALLOCATION_SITE_MAP_ACCESSOR)
#undef ALLOCATION_SITE_MAP_ACCESSOR

846 847 848 849 850
#define DATA_HANDLER_MAP_ACCESSOR(NAME, Name, Size, name) \
  inline Handle<Map> name##_map();
  DATA_HANDLER_LIST(DATA_HANDLER_MAP_ACCESSOR)
#undef DATA_HANDLER_MAP_ACCESSOR

851
#define STRING_ACCESSOR(name, str) inline Handle<String> name();
852 853
  INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
#undef STRING_ACCESSOR
ager@chromium.org's avatar
ager@chromium.org committed
854

855
#define SYMBOL_ACCESSOR(name) inline Handle<Symbol> name();
856 857 858
  PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
#undef SYMBOL_ACCESSOR

859
#define SYMBOL_ACCESSOR(name, description) inline Handle<Symbol> name();
860
  PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
861
  WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
862 863
#undef SYMBOL_ACCESSOR

864
#define ACCESSOR_INFO_ACCESSOR(accessor_name, ...) \
865 866 867 868
  inline Handle<AccessorInfo> accessor_name##_accessor();
  ACCESSOR_INFO_LIST(ACCESSOR_INFO_ACCESSOR)
#undef ACCESSOR_INFO_ACCESSOR

869
  // Allocates a new SharedFunctionInfo object.
870 871
  Handle<SharedFunctionInfo> NewSharedFunctionInfoForApiFunction(
      MaybeHandle<String> maybe_name,
872
      Handle<FunctionTemplateInfo> function_template_info, FunctionKind kind);
873 874

  Handle<SharedFunctionInfo> NewSharedFunctionInfoForBuiltin(
875
      MaybeHandle<String> name, int builtin_index,
876
      FunctionKind kind = kNormalFunction);
877

878
  Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
879
      FunctionLiteral* literal, Handle<Script> script, bool is_toplevel);
880

881
  static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
882 883 884 885 886 887 888 889 890 891 892 893 894
    return (function_mode & kWithPrototypeBits) != 0;
  }

  static bool IsFunctionModeWithWritablePrototype(FunctionMode function_mode) {
    return (function_mode & kWithWritablePrototypeBit) != 0;
  }

  static bool IsFunctionModeWithName(FunctionMode function_mode) {
    return (function_mode & kWithNameBit) != 0;
  }

  static bool IsFunctionModeWithHomeObject(FunctionMode function_mode) {
    return (function_mode & kWithHomeObjectBit) != 0;
895 896
  }

897 898
  Handle<Map> CreateSloppyFunctionMap(
      FunctionMode function_mode, MaybeHandle<JSFunction> maybe_empty_function);
899 900 901 902

  Handle<Map> CreateStrictFunctionMap(FunctionMode function_mode,
                                      Handle<JSFunction> empty_function);

903 904
  Handle<Map> CreateClassFunctionMap(Handle<JSFunction> empty_function);

905
  // Allocates a new JSMessageObject object.
906 907 908 909
  Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
                                             Handle<Object> argument,
                                             int start_position,
                                             int end_position,
910
                                             Handle<Script> script,
911
                                             Handle<Object> stack_frames);
912

913
  Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
914

915 916
  Handle<CoverageInfo> NewCoverageInfo(const ZoneVector<SourceRange>& slots);

917 918
  // Return a map for given number of properties using the map cache in the
  // native context.
919
  Handle<Map> ObjectLiteralMapFromCache(Handle<NativeContext> native_context,
920
                                        int number_of_properties);
921

922 923 924
  Handle<LoadHandler> NewLoadHandler(int data_count);
  Handle<StoreHandler> NewStoreHandler(int data_count);

925
  Handle<RegExpMatchInfo> NewRegExpMatchInfo();
926

927
  // Creates a new FixedArray that holds the data associated with the
928
  // atom regexp and stores it in the regexp.
929 930
  void SetRegExpAtomData(Handle<JSRegExp> regexp, JSRegExp::Type type,
                         Handle<String> source, JSRegExp::Flags flags,
931
                         Handle<Object> match_pattern);
932 933 934

  // Creates a new FixedArray that holds the data associated with the
  // irregexp regexp and stores it in the regexp.
935 936
  void SetRegExpIrregexpData(Handle<JSRegExp> regexp, JSRegExp::Type type,
                             Handle<String> source, JSRegExp::Flags flags,
937
                             int capture_count);
938

939 940 941
  // Returns the value for a known global constant (a property of the global
  // object which is neither configurable nor writable) like 'undefined'.
  // Returns a null handle when the given name is unknown.
942
  Handle<Object> GlobalConstantFor(Handle<Name> name);
943

944 945 946
  // Converts the given boolean condition to JavaScript boolean value.
  Handle<Object> ToBoolean(bool value);

947 948 949
  // Converts the given ToPrimitive hint to it's string representation.
  Handle<String> ToPrimitiveHintString(ToPrimitiveHint hint);

950 951
  Handle<JSPromise> NewJSPromise(PretenureFlag pretenure = NOT_TENURED);

952 953
  Handle<CallHandlerInfo> NewCallHandlerInfo(bool has_no_side_effect = false);

954 955 956 957
  HeapObject* NewForTest(Handle<Map> map, PretenureFlag pretenure) {
    return New(map, pretenure);
  }

958
 private:
959 960 961 962 963 964
  Isolate* isolate() {
    // Downcast to the privately inherited sub-class using c-style casts to
    // avoid undefined behavior (as static_cast cannot cast across private
    // bases).
    return (Isolate*)this;  // NOLINT(readability/casting)
  }
965

966 967 968 969 970 971 972 973 974 975
  HeapObject* AllocateRawWithImmortalMap(
      int size, PretenureFlag pretenure, Map* map,
      AllocationAlignment alignment = kWordAligned);
  HeapObject* AllocateRawWithAllocationSite(
      Handle<Map> map, PretenureFlag pretenure,
      Handle<AllocationSite> allocation_site);

  // Allocate memory for an uninitialized array (e.g., a FixedArray or similar).
  HeapObject* AllocateRawArray(int size, PretenureFlag pretenure);
  HeapObject* AllocateRawFixedArray(int length, PretenureFlag pretenure);
976
  HeapObject* AllocateRawWeakArrayList(int length, PretenureFlag pretenure);
977 978 979 980
  Handle<FixedArray> NewFixedArrayWithFiller(Heap::RootListIndex map_root_index,
                                             int length, Object* filler,
                                             PretenureFlag pretenure);

981
  // Creates a heap object based on the map. The fields of the heap object are
982 983
  // not initialized, it's the responsibility of the caller to do that.
  HeapObject* New(Handle<Map> map, PretenureFlag pretenure);
984

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  template <typename T>
  Handle<T> CopyArrayWithMap(Handle<T> src, Handle<Map> map);
  template <typename T>
  Handle<T> CopyArrayAndGrow(Handle<T> src, int grow_by,
                             PretenureFlag pretenure);

  template <bool is_one_byte, typename T>
  Handle<String> AllocateInternalizedStringImpl(T t, int chars,
                                                uint32_t hash_field);

  Handle<SeqOneByteString> AllocateRawOneByteInternalizedString(
      int length, uint32_t hash_field);

  Handle<String> AllocateTwoByteInternalizedString(Vector<const uc16> str,
                                                   uint32_t hash_field);
1000

1001 1002 1003
  MaybeHandle<String> NewStringFromTwoByte(const uc16* string, int length,
                                           PretenureFlag pretenure);

1004 1005
  // Attempt to find the number in a small cache.  If we finds it, return
  // the string representation of the number.  Otherwise return undefined.
1006
  Handle<Object> NumberToStringCacheGet(Object* number, int hash);
1007 1008

  // Update the cache with a new number-string pair.
1009 1010
  Handle<String> NumberToStringCacheSet(Handle<Object> number, int hash,
                                        const char* string, bool check_cache);
1011

1012 1013 1014
  // Create a JSArray with no elements and no length.
  Handle<JSArray> NewJSArray(ElementsKind elements_kind,
                             PretenureFlag pretenure = NOT_TENURED);
1015 1016 1017

  Handle<JSPromise> NewJSPromiseWithoutHook(
      PretenureFlag pretenure = NOT_TENURED);
1018 1019 1020

  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
      MaybeHandle<String> name, MaybeHandle<HeapObject> maybe_function_data,
1021
      int maybe_builtin_index, FunctionKind kind = kNormalFunction);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

  void InitializeAllocationMemento(AllocationMemento* memento,
                                   AllocationSite* allocation_site);

  // Initializes a JSObject based on its map.
  void InitializeJSObjectFromMap(Handle<JSObject> obj,
                                 Handle<Object> properties, Handle<Map> map);
  // Initializes JSObject body starting at given offset.
  void InitializeJSObjectBody(Handle<JSObject> obj, Handle<Map> map,
                              int start_offset);
1032 1033
};

1034 1035 1036
// Utility class to simplify argument handling around JSFunction creation.
class NewFunctionArgs final {
 public:
1037 1038 1039
  static NewFunctionArgs ForWasm(
      Handle<String> name,
      Handle<WasmExportedFunctionData> exported_function_data, Handle<Map> map);
1040 1041
  static NewFunctionArgs ForBuiltin(Handle<String> name, Handle<Map> map,
                                    int builtin_id);
1042 1043 1044 1045
  static NewFunctionArgs ForFunctionWithoutCode(Handle<String> name,
                                                Handle<Map> map,
                                                LanguageMode language_mode);
  static NewFunctionArgs ForBuiltinWithPrototype(
1046 1047 1048
      Handle<String> name, Handle<Object> prototype, InstanceType type,
      int instance_size, int inobject_properties, int builtin_id,
      MutableMode prototype_mutability);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  static NewFunctionArgs ForBuiltinWithoutPrototype(Handle<String> name,
                                                    int builtin_id,
                                                    LanguageMode language_mode);

  Handle<Map> GetMap(Isolate* isolate) const;

 private:
  NewFunctionArgs() {}  // Use the static factory constructors.

  void SetShouldCreateAndSetInitialMap();
  void SetShouldSetPrototype();
  void SetShouldSetLanguageMode();

  // Sentinel value.
  static const int kUninitialized = -1;

  Handle<String> name_;
  MaybeHandle<Map> maybe_map_;
1067
  MaybeHandle<WasmExportedFunctionData> maybe_exported_function_data_;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

  bool should_create_and_set_initial_map_ = false;
  InstanceType type_;
  int instance_size_ = kUninitialized;
  int inobject_properties_ = kUninitialized;

  bool should_set_prototype_ = false;
  MaybeHandle<Object> maybe_prototype_;

  bool should_set_language_mode_ = false;
  LanguageMode language_mode_;

  int maybe_builtin_id_ = kUninitialized;

  MutableMode prototype_mutability_;

  friend class Factory;
};

1087 1088
}  // namespace internal
}  // namespace v8
1089

1090
#endif  // V8_HEAP_FACTORY_H_