factory.h 29.5 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 7

#ifndef V8_FACTORY_H_
#define V8_FACTORY_H_

8
#include "src/isolate.h"
9
#include "src/messages.h"
10
#include "src/type-feedback-vector.h"
11

12 13
namespace v8 {
namespace internal {
14

15
// Interface for handle based allocation.
16
class Factory final {
17
 public:
18 19
  Handle<Oddball> NewOddball(Handle<Map> map, const char* to_string,
                             Handle<Object> to_number, const char* type_of,
20 21
                             byte kind);

22
  // Allocates a fixed array initialized with undefined values.
23
  Handle<FixedArray> NewFixedArray(
24 25
      int size,
      PretenureFlag pretenure = NOT_TENURED);
26 27

  // Allocate a new fixed array with non-existing entries (the hole).
28
  Handle<FixedArray> NewFixedArrayWithHoles(
29 30
      int size,
      PretenureFlag pretenure = NOT_TENURED);
31

32 33 34
  // Allocates an uninitialized fixed array. It must be filled by the caller.
  Handle<FixedArray> NewUninitializedFixedArray(int size);

35
  // Allocate a new uninitialized fixed double array.
36 37 38
  // The function returns a pre-allocated empty fixed array for capacity = 0,
  // so the return type must be the general fixed array class.
  Handle<FixedArrayBase> NewFixedDoubleArray(
39 40 41
      int size,
      PretenureFlag pretenure = NOT_TENURED);

42
  // Allocate a new fixed double array with hole values.
43
  Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
44 45 46
      int size,
      PretenureFlag pretenure = NOT_TENURED);

47 48 49
  Handle<OrderedHashSet> NewOrderedHashSet();
  Handle<OrderedHashMap> NewOrderedHashMap();

50 51 52
  // Create a new boxed value.
  Handle<Box> NewBox(Handle<Object> value);

53 54 55
  // Create a new PrototypeInfo struct.
  Handle<PrototypeInfo> NewPrototypeInfo();

56 57 58 59 60
  // Create a new SloppyBlockWithEvalContextExtension struct.
  Handle<SloppyBlockWithEvalContextExtension>
  NewSloppyBlockWithEvalContextExtension(Handle<ScopeInfo> scope_info,
                                         Handle<JSObject> extension);

61
  // Create a pre-tenured empty AccessorPair.
62
  Handle<AccessorPair> NewAccessorPair();
63

64
  // Create an empty TypeFeedbackInfo.
65 66
  Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();

67 68
  // Finds the internalized copy for string in the string table.
  // If not found, a new string is added to the table and returned.
69 70 71
  Handle<String> InternalizeUtf8String(Vector<const char> str);
  Handle<String> InternalizeUtf8String(const char* str) {
    return InternalizeUtf8String(CStrVector(str));
72
  }
73 74
  Handle<String> InternalizeString(Handle<String> str);
  Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
75 76 77
  Handle<String> InternalizeOneByteString(
      Handle<SeqOneByteString>, int from, int length);

78
  Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
79

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

83 84
  Handle<Name> InternalizeName(Handle<Name> name);

85 86 87 88 89 90

  // 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.
  //
91 92
  // Creates a new String object.  There are two String encodings: one-byte and
  // two-byte.  One should choose between the three string factory functions
93 94
  // based on the encoding of the string buffer that the string is
  // initialized from.
95 96 97
  //   - ...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.
98
  //   - ...FromUtf8 initializes the string from a buffer that is UTF-8
99 100 101 102 103
  //     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.
104
  //
105
  // One-byte strings are pretenured when used as keys in the SourceCodeCache.
106
  MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte(
107
      Vector<const uint8_t> str,
108
      PretenureFlag pretenure = NOT_TENURED);
109

110 111 112
  template <size_t N>
  inline Handle<String> NewStringFromStaticChars(
      const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) {
113
    DCHECK(N == StrLength(str) + 1);
114 115
    return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure)
        .ToHandleChecked();
116 117 118 119 120 121 122 123 124
  }

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

125

126 127
  // Allocates and fully initializes a String.  There are two String encodings:
  // one-byte and two-byte. One should choose between the threestring
128 129
  // allocation functions based on the encoding of the string buffer used to
  // initialized the string.
130 131 132
  //   - ...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.
133
  //   - ...FromUTF8 initializes the string from a buffer that is UTF-8
134 135
  //     encoded.  If the characters are all ASCII characters, the result
  //     will be Latin1 encoded, otherwise it will converted to two-byte.
136
  //   - ...FromTwoByte initializes the string from a buffer that is two-byte
137 138
  //     encoded.  If the characters are all Latin1 characters, the
  //     result will be converted to Latin1, otherwise it will be left as
139 140
  //     two-byte.

141
  // TODO(dcarney): remove this function.
142
  MUST_USE_RESULT inline MaybeHandle<String> NewStringFromAscii(
143 144 145 146
      Vector<const char> str,
      PretenureFlag pretenure = NOT_TENURED) {
    return NewStringFromOneByte(Vector<const uint8_t>::cast(str), pretenure);
  }
147 148 149

  // UTF8 strings are pretenured when used for regexp literal patterns and
  // flags in the parser.
150
  MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
151
      Vector<const char> str,
152
      PretenureFlag pretenure = NOT_TENURED);
153

154
  MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
155
      Vector<const uc16> str,
156
      PretenureFlag pretenure = NOT_TENURED);
157

158 159
  // Allocates an internalized string in old space based on the character
  // stream.
160 161
  Handle<String> NewInternalizedStringFromUtf8(Vector<const char> str,
                                               int chars, uint32_t hash_field);
162

163 164
  Handle<String> NewOneByteInternalizedString(Vector<const uint8_t> str,
                                              uint32_t hash_field);
165

166
  Handle<String> NewOneByteInternalizedSubString(
167 168
      Handle<SeqOneByteString> string, int offset, int length,
      uint32_t hash_field);
169

170 171
  Handle<String> NewTwoByteInternalizedString(Vector<const uc16> str,
                                              uint32_t hash_field);
172

173 174
  Handle<String> NewInternalizedStringImpl(Handle<String> string, int chars,
                                           uint32_t hash_field);
175 176 177 178 179 180

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

181
  // Allocates and partially initializes an one-byte or two-byte String. The
182 183
  // characters of the string are uninitialized. Currently used in regexp code
  // only, where they are pretenured.
184
  MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
185 186
      int length,
      PretenureFlag pretenure = NOT_TENURED);
187
  MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
188 189 190
      int length,
      PretenureFlag pretenure = NOT_TENURED);

191
  // Creates a single character string where the character has given code.
192
  // A cache is used for Latin1 codes.
193
  Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
194

195
  // Create a new cons string object which consists of a pair of strings.
196 197
  MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
                                                    Handle<String> right);
198

199 200
  // Create a new string object which holds a proper substring of a string.
  Handle<String> NewProperSubString(Handle<String> str,
201 202 203
                                    int begin,
                                    int end);

204 205 206 207 208 209
  // Create a new string object which holds a substring of a string.
  Handle<String> NewSubString(Handle<String> str, int begin, int end) {
    if (begin == 0 && end == str->length()) return str;
    return NewProperSubString(str, begin, end);
  }

210
  // Creates a new external String object.  There are two String encodings
211
  // in the system: one-byte and two-byte.  Unlike other String types, it does
212
  // not make sense to have a UTF-8 factory function for external strings,
213 214
  // because we cannot change the underlying buffer.  Note that these strings
  // are backed by a string resource that resides outside the V8 heap.
215 216
  MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
      const ExternalOneByteString::Resource* resource);
217
  MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
218
      const ExternalTwoByteString::Resource* resource);
219

220 221
  // Create a symbol.
  Handle<Symbol> NewSymbol();
222
  Handle<Symbol> NewPrivateSymbol();
223

224
  // Create a global (but otherwise uninitialized) context.
225
  Handle<Context> NewNativeContext();
226

227 228
  // Create a script context.
  Handle<Context> NewScriptContext(Handle<JSFunction> function,
229 230
                                   Handle<ScopeInfo> scope_info);

231 232
  // Create an empty script context table.
  Handle<ScriptContextTable> NewScriptContextTable();
233

234
  // Create a module context.
235
  Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
236

237
  // Create a function context.
238
  Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
239

240
  // Create a catch context.
241 242
  Handle<Context> NewCatchContext(Handle<JSFunction> function,
                                  Handle<Context> previous,
243 244
                                  Handle<String> name,
                                  Handle<Object> thrown_object);
245

246
  // Create a 'with' context.
247 248
  Handle<Context> NewWithContext(Handle<JSFunction> function,
                                 Handle<Context> previous,
249
                                 Handle<JSReceiver> extension);
250

251
  // Create a block context.
252 253
  Handle<Context> NewBlockContext(Handle<JSFunction> function,
                                  Handle<Context> previous,
254
                                  Handle<ScopeInfo> scope_info);
255

256 257
  // Allocate a new struct.  The struct is pretenured (allocated directly in
  // the old generation).
258
  Handle<Struct> NewStruct(InstanceType type);
259

260 261
  Handle<CodeCache> NewCodeCache();

262 263 264
  Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
      int aliased_context_slot);

265
  Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
266

267
  Handle<Script> NewScript(Handle<String> source);
268

269 270 271
  // Foreign objects are pretenured when allocated by the bootstrapper.
  Handle<Foreign> NewForeign(Address addr,
                             PretenureFlag pretenure = NOT_TENURED);
272

273 274 275
  // Allocate a new foreign object.  The foreign is pretenured (allocated
  // directly in the old generation).
  Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
276

277 278
  Handle<ByteArray> NewByteArray(int length,
                                 PretenureFlag pretenure = NOT_TENURED);
279

280
  Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes,
281 282
                                         int frame_size, int parameter_count,
                                         Handle<FixedArray> constant_pool);
283

284 285
  Handle<FixedTypedArrayBase> NewFixedTypedArrayWithExternalPointer(
      int length, ExternalArrayType array_type, void* external_pointer,
286
      PretenureFlag pretenure = NOT_TENURED);
287 288

  Handle<FixedTypedArrayBase> NewFixedTypedArray(
289
      int length, ExternalArrayType array_type, bool initialize,
290
      PretenureFlag pretenure = NOT_TENURED);
291

292 293
  Handle<Cell> NewCell(Handle<Object> value);

294
  Handle<PropertyCell> NewPropertyCell();
295

ulan@chromium.org's avatar
ulan@chromium.org committed
296 297
  Handle<WeakCell> NewWeakCell(Handle<HeapObject> value);

298 299
  Handle<TransitionArray> NewTransitionArray(int capacity);

300
  // Allocate a tenured AllocationSite. It's payload is null.
301 302
  Handle<AllocationSite> NewAllocationSite();

303 304 305 306
  Handle<Map> NewMap(
      InstanceType type,
      int instance_size,
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
307

308 309 310 311
  Handle<HeapObject> NewFillerObject(int size,
                                     bool double_align,
                                     AllocationSpace space);

312
  Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
313

314 315 316 317 318
  Handle<JSObject> CopyJSObject(Handle<JSObject> object);

  Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
                                                  Handle<AllocationSite> site);

319 320 321
  Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
                                           Handle<Map> map);

322 323 324
  Handle<FixedArray> CopyFixedArrayAndGrow(
      Handle<FixedArray> array, int grow_by,
      PretenureFlag pretenure = NOT_TENURED);
325

326
  Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
327

328 329 330 331
  // This method expects a COW array in new space, and creates a copy
  // of it in old space.
  Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);

332 333 334
  Handle<FixedDoubleArray> CopyFixedDoubleArray(
      Handle<FixedDoubleArray> array);

335
  // Numbers (e.g. literals) are pretenured by the parser.
336
  // The return value may be a smi or a heap number.
337 338
  Handle<Object> NewNumber(double value,
                           PretenureFlag pretenure = NOT_TENURED);
339

340 341 342 343
  Handle<Object> NewNumberFromInt(int32_t value,
                                  PretenureFlag pretenure = NOT_TENURED);
  Handle<Object> NewNumberFromUint(uint32_t value,
                                  PretenureFlag pretenure = NOT_TENURED);
344 345 346 347 348 349 350 351
  Handle<Object> NewNumberFromSize(size_t value,
                                   PretenureFlag pretenure = NOT_TENURED) {
    if (Smi::IsValid(static_cast<intptr_t>(value))) {
      return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
                            isolate());
    }
    return NewNumber(static_cast<double>(value), pretenure);
  }
352
  Handle<HeapNumber> NewHeapNumber(double value,
353
                                   MutableMode mode = IMMUTABLE,
354
                                   PretenureFlag pretenure = NOT_TENURED);
355 356 357 358 359 360

#define SIMD128_NEW_DECL(TYPE, Type, type, lane_count, lane_type) \
  Handle<Type> New##Type(lane_type lanes[lane_count],             \
                         PretenureFlag pretenure = NOT_TENURED);
  SIMD128_TYPES(SIMD128_NEW_DECL)
#undef SIMD128_NEW_DECL
361

362 363
  // These objects are used by the api to create env-independent data
  // structures in the heap.
364 365 366
  inline Handle<JSObject> NewNeanderObject() {
    return NewJSObjectFromMap(neander_map());
  }
367

yurys's avatar
yurys committed
368 369
  Handle<JSWeakMap> NewJSWeakMap();

370
  Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
371 372 373

  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
374 375
  Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
                               PretenureFlag pretenure = NOT_TENURED);
376 377 378
  // JSObject that should have a memento pointing to the allocation site.
  Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
                                          Handle<AllocationSite> site);
379

380
  // Global objects are pretenured and initialized based on a constructor.
381
  Handle<JSGlobalObject> NewJSGlobalObject(Handle<JSFunction> constructor);
382

383 384
  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
385 386 387 388
  Handle<JSObject> NewJSObjectFromMap(
      Handle<Map> map,
      PretenureFlag pretenure = NOT_TENURED,
      Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
389

390
  // JS modules are pretenured.
391 392
  Handle<JSModule> NewJSModule(Handle<Context> context,
                               Handle<ScopeInfo> scope_info);
393

394
  // JS arrays are pretenured when allocated by the parser.
395

396
  // Create a JSArray with no elements.
397 398 399
  Handle<JSArray> NewJSArray(ElementsKind elements_kind,
                             Strength strength = Strength::WEAK,
                             PretenureFlag pretenure = NOT_TENURED);
400

401 402
  // Create a JSArray with a specified length and elements initialized
  // according to the specified mode.
403
  Handle<JSArray> NewJSArray(
404
      ElementsKind elements_kind, int length, int capacity,
405
      Strength strength = Strength::WEAK,
406
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
407 408
      PretenureFlag pretenure = NOT_TENURED);

409
  Handle<JSArray> NewJSArray(
410 411
      int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
      Strength strength = Strength::WEAK,
412
      PretenureFlag pretenure = NOT_TENURED) {
413 414 415
    if (capacity != 0) {
      elements_kind = GetHoleyElementsKind(elements_kind);
    }
416
    return NewJSArray(elements_kind, 0, capacity, strength,
417
                      INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
418 419
  }

420
  // Create a JSArray with the given elements.
421 422 423 424
  Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements,
                                         ElementsKind elements_kind, int length,
                                         Strength strength = Strength::WEAK,
                                         PretenureFlag pretenure = NOT_TENURED);
425

426
  Handle<JSArray> NewJSArrayWithElements(
427
      Handle<FixedArrayBase> elements,
428
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
429
      Strength strength = Strength::WEAK,
430
      PretenureFlag pretenure = NOT_TENURED) {
431 432
    return NewJSArrayWithElements(elements, elements_kind, elements->length(),
                                  strength, pretenure);
433
  }
434

435 436 437 438 439 440
  void NewJSArrayStorage(
      Handle<JSArray> array,
      int length,
      int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);

441 442
  Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);

binji's avatar
binji committed
443
  Handle<JSArrayBuffer> NewJSArrayBuffer(
ben's avatar
ben committed
444 445
      SharedFlag shared = SharedFlag::kNotShared,
      PretenureFlag pretenure = NOT_TENURED);
446

ben's avatar
ben committed
447 448
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       PretenureFlag pretenure = NOT_TENURED);
449

ben's avatar
ben committed
450 451
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
                                       PretenureFlag pretenure = NOT_TENURED);
452

453 454 455
  // Creates a new JSTypedArray with the specified buffer.
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       Handle<JSArrayBuffer> buffer,
ben's avatar
ben committed
456 457
                                       size_t byte_offset, size_t length,
                                       PretenureFlag pretenure = NOT_TENURED);
458

459 460
  // Creates a new on-heap JSTypedArray.
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
ben's avatar
ben committed
461 462
                                       size_t number_of_elements,
                                       PretenureFlag pretenure = NOT_TENURED);
463

464
  Handle<JSDataView> NewJSDataView();
465 466
  Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
                                   size_t byte_offset, size_t byte_length);
467

468 469 470
  Handle<JSMap> NewJSMap();
  Handle<JSSet> NewJSSet();

471 472 473 474
  // TODO(aandrey): Maybe these should take table, index and kind arguments.
  Handle<JSMapIterator> NewJSMapIterator();
  Handle<JSSetIterator> NewJSSetIterator();

475 476 477 478 479
  // Creates a new JSIteratorResult object with the arguments {value} and
  // {done}.  Implemented according to ES6 section 7.4.7 CreateIterResultObject.
  Handle<JSIteratorResult> NewJSIteratorResult(Handle<Object> value,
                                               Handle<Object> done);

480
  // Allocates a Harmony proxy.
481
  Handle<JSProxy> NewJSProxy(Handle<JSReceiver> target,
482
                             Handle<JSReceiver> handler);
483

484 485 486 487 488 489 490
  // 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);

491 492
  Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy();

493
  Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
494
                                 Handle<Object> prototype,
495 496
                                 bool read_only_prototype = false,
                                 bool is_strict = false);
497
  Handle<JSFunction> NewFunction(Handle<String> name);
498
  Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
499 500
                                                 Handle<Code> code,
                                                 bool is_strict = false);
501

502
  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
503 504 505 506 507
      Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info,
      Handle<Context> context, PretenureFlag pretenure = TENURED);

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

510 511
  Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
                                 Handle<Object> prototype, InstanceType type,
512
                                 int instance_size,
513
                                 bool read_only_prototype = false,
514 515
                                 bool install_constructor = false,
                                 bool is_strict = false);
516
  Handle<JSFunction> NewFunction(Handle<String> name,
517
                                 Handle<Code> code,
518
                                 InstanceType type,
519
                                 int instance_size);
520

521
  // Create a serialized scope info.
522
  Handle<ScopeInfo> NewScopeInfo(int length);
523

524
  // Create an External object for V8's external API.
525 526
  Handle<JSObject> NewExternal(void* value);

527 528 529
  // 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.
530 531 532
  Handle<Code> NewCode(const CodeDesc& desc,
                       Code::Flags flags,
                       Handle<Object> self_reference,
533
                       bool immovable = false,
534
                       bool crankshafted = false,
535 536
                       int prologue_offset = Code::kPrologueOffsetNotSet,
                       bool is_debug = false);
537

538
  Handle<Code> CopyCode(Handle<Code> code);
539

540
  Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
541

542
  // Interface for creating error objects.
543 544
  Handle<Object> NewError(Handle<JSFunction> constructor,
                          Handle<String> message);
545

546
  Handle<Object> NewInvalidStringLengthError() {
547
    return NewRangeError(MessageTemplate::kInvalidStringLength);
548 549
  }

550
  Handle<Object> NewError(Handle<JSFunction> constructor,
551
                          MessageTemplate::Template template_index,
552 553 554
                          Handle<Object> arg0 = Handle<Object>(),
                          Handle<Object> arg1 = Handle<Object>(),
                          Handle<Object> arg2 = Handle<Object>());
555

556 557 558 559 560 561 562 563 564 565 566
#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)
567
#undef DEFINE_ERROR
568

569 570
  Handle<String> NumberToString(Handle<Object> number,
                                bool check_number_string_cache = true);
571 572 573 574

  Handle<String> Uint32ToString(uint32_t value) {
    return NumberToString(NewNumberFromUint(value));
  }
575

576
  Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
577

578 579 580 581
#define ROOT_ACCESSOR(type, name, camel_name)                         \
  inline Handle<type> name() {                                        \
    return Handle<type>(bit_cast<type**>(                             \
        &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
582
  }
583
  ROOT_LIST(ROOT_ACCESSOR)
584 585
#undef ROOT_ACCESSOR

586 587 588 589 590
#define STRUCT_MAP_ACCESSOR(NAME, Name, name)                      \
  inline Handle<Map> name##_map() {                                \
    return Handle<Map>(bit_cast<Map**>(                            \
        &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
  }
591 592
  STRUCT_LIST(STRUCT_MAP_ACCESSOR)
#undef STRUCT_MAP_ACCESSOR
593

594 595 596 597
#define STRING_ACCESSOR(name, str)                              \
  inline Handle<String> name() {                                \
    return Handle<String>(bit_cast<String**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
598
  }
599 600
  INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
#undef STRING_ACCESSOR
ager@chromium.org's avatar
ager@chromium.org committed
601

602 603 604 605 606 607 608 609
#define SYMBOL_ACCESSOR(name)                                   \
  inline Handle<Symbol> name() {                                \
    return Handle<Symbol>(bit_cast<Symbol**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
  }
  PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
#undef SYMBOL_ACCESSOR

610
#define SYMBOL_ACCESSOR(name, description)                      \
611 612 613 614 615
  inline Handle<Symbol> name() {                                \
    return Handle<Symbol>(bit_cast<Symbol**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
  }
  PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
616
  WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
617 618
#undef SYMBOL_ACCESSOR

619
  // Allocates a new SharedFunctionInfo object.
620
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
621 622
      Handle<String> name, int number_of_literals, FunctionKind kind,
      Handle<Code> code, Handle<ScopeInfo> scope_info,
623
      Handle<TypeFeedbackVector> feedback_vector);
624
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
625 626
                                                   MaybeHandle<Code> code,
                                                   bool is_constructor);
627

628
  // Allocates a new JSMessageObject object.
629 630 631 632 633 634
  Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message,
                                             Handle<Object> argument,
                                             int start_position,
                                             int end_position,
                                             Handle<Object> script,
                                             Handle<Object> stack_frames);
635

636
  Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
637

638 639
  // Return a map for given number of properties using the map cache in the
  // native context.
640
  Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
641
                                        int number_of_properties,
642
                                        bool is_strong,
643
                                        bool* is_result_from_cache);
644

645
  // Creates a new FixedArray that holds the data associated with the
646
  // atom regexp and stores it in the regexp.
647 648 649 650 651
  void SetRegExpAtomData(Handle<JSRegExp> regexp,
                         JSRegExp::Type type,
                         Handle<String> source,
                         JSRegExp::Flags flags,
                         Handle<Object> match_pattern);
652 653 654

  // Creates a new FixedArray that holds the data associated with the
  // irregexp regexp and stores it in the regexp.
655 656 657 658 659
  void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
                             JSRegExp::Type type,
                             Handle<String> source,
                             JSRegExp::Flags flags,
                             int capture_count);
660

661 662 663
  // 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.
664
  Handle<Object> GlobalConstantFor(Handle<Name> name);
665

666 667 668
  // Converts the given boolean condition to JavaScript boolean value.
  Handle<Object> ToBoolean(bool value);

669
 private:
670
  Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
671

672 673 674 675 676 677 678 679 680 681 682
  // Creates a heap object based on the map. The fields of the heap object are
  // not initialized by New<>() functions. It's the responsibility of the caller
  // to do that.
  template<typename T>
  Handle<T> New(Handle<Map> map, AllocationSpace space);

  template<typename T>
  Handle<T> New(Handle<Map> map,
                AllocationSpace space,
                Handle<AllocationSite> allocation_site);

683 684 685
  // Creates a code object that is not yet fully initialized yet.
  inline Handle<Code> NewCodeRaw(int object_size, bool immovable);

686 687 688 689 690 691
  // Attempt to find the number in a small cache.  If we finds it, return
  // the string representation of the number.  Otherwise return undefined.
  Handle<Object> GetNumberStringCache(Handle<Object> number);

  // Update the cache with a new number-string pair.
  void SetNumberStringCache(Handle<Object> number, Handle<String> string);
692 693 694 695 696 697 698 699 700 701

  // Creates a function initialized with a shared part.
  Handle<JSFunction> NewFunction(Handle<Map> map,
                                 Handle<SharedFunctionInfo> info,
                                 Handle<Context> context,
                                 PretenureFlag pretenure = TENURED);

  Handle<JSFunction> NewFunction(Handle<Map> map,
                                 Handle<String> name,
                                 MaybeHandle<Code> maybe_code);
702 703
};

704 705
}  // namespace internal
}  // namespace v8
706 707

#endif  // V8_FACTORY_H_