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

11 12
namespace v8 {
namespace internal {
13

14
class FeedbackVectorSpec;
15

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

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

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

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

37
  // Allocate a new uninitialized fixed double array.
38 39 40
  // 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(
41 42 43
      int size,
      PretenureFlag pretenure = NOT_TENURED);

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

49
  Handle<ConstantPoolArray> NewConstantPoolArray(
50 51 52 53 54
      const ConstantPoolArray::NumberOfEntries& small);

  Handle<ConstantPoolArray> NewExtendedConstantPoolArray(
      const ConstantPoolArray::NumberOfEntries& small,
      const ConstantPoolArray::NumberOfEntries& extended);
55

56 57 58
  Handle<OrderedHashSet> NewOrderedHashSet();
  Handle<OrderedHashMap> NewOrderedHashMap();

59 60 61
  // Create a new boxed value.
  Handle<Box> NewBox(Handle<Object> value);

62 63 64
  // Create a new PrototypeInfo struct.
  Handle<PrototypeInfo> NewPrototypeInfo();

65
  // Create a pre-tenured empty AccessorPair.
66
  Handle<AccessorPair> NewAccessorPair();
67

68
  // Create an empty TypeFeedbackInfo.
69 70
  Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();

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

82
  Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
83

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

87 88 89 90 91 92

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

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

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

127

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

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

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

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

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

  MUST_USE_RESULT Handle<String> NewOneByteInternalizedString(
168 169 170 171 172
      Vector<const uint8_t> str, uint32_t hash_field);

  MUST_USE_RESULT Handle<String> NewOneByteInternalizedSubString(
      Handle<SeqOneByteString> string, int offset, int length,
      uint32_t hash_field);
173 174 175 176 177 178

  MUST_USE_RESULT Handle<String> NewTwoByteInternalizedString(
        Vector<const uc16> str,
        uint32_t hash_field);

  MUST_USE_RESULT Handle<String> NewInternalizedStringImpl(
179
      Handle<String> string, int chars, uint32_t hash_field);
180 181 182 183 184 185

  // 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);

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

196
  // Creates a single character string where the character has given code.
197
  // A cache is used for Latin1 codes.
198
  Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
199

200
  // Create a new cons string object which consists of a pair of strings.
201 202
  MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
                                                    Handle<String> right);
203 204 205 206 207 208 209 210
  MUST_USE_RESULT MaybeHandle<String> NewOneByteConsString(
      int length, Handle<String> left, Handle<String> right);
  MUST_USE_RESULT MaybeHandle<String> NewTwoByteConsString(
      int length, Handle<String> left, Handle<String> right);
  MUST_USE_RESULT MaybeHandle<String> NewRawConsString(Handle<Map> map,
                                                       int length,
                                                       Handle<String> left,
                                                       Handle<String> right);
211

212 213
  // Create a new string object which holds a proper substring of a string.
  Handle<String> NewProperSubString(Handle<String> str,
214 215 216
                                    int begin,
                                    int end);

217 218 219 220 221 222
  // 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);
  }

223
  // Creates a new external String object.  There are two String encodings
224
  // in the system: one-byte and two-byte.  Unlike other String types, it does
225
  // not make sense to have a UTF-8 factory function for external strings,
226 227
  // because we cannot change the underlying buffer.  Note that these strings
  // are backed by a string resource that resides outside the V8 heap.
228 229
  MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte(
      const ExternalOneByteString::Resource* resource);
230
  MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
231
      const ExternalTwoByteString::Resource* resource);
232

233 234
  // Create a symbol.
  Handle<Symbol> NewSymbol();
235
  Handle<Symbol> NewPrivateSymbol();
236
  Handle<Symbol> NewPrivateOwnSymbol();
237

238
  // Create a global (but otherwise uninitialized) context.
239
  Handle<Context> NewNativeContext();
240

241 242
  // Create a script context.
  Handle<Context> NewScriptContext(Handle<JSFunction> function,
243 244
                                   Handle<ScopeInfo> scope_info);

245 246
  // Create an empty script context table.
  Handle<ScriptContextTable> NewScriptContextTable();
247

248
  // Create a module context.
249
  Handle<Context> NewModuleContext(Handle<ScopeInfo> scope_info);
250

251
  // Create a function context.
252
  Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function);
253

254
  // Create a catch context.
255 256
  Handle<Context> NewCatchContext(Handle<JSFunction> function,
                                  Handle<Context> previous,
257 258
                                  Handle<String> name,
                                  Handle<Object> thrown_object);
259

260
  // Create a 'with' context.
261 262
  Handle<Context> NewWithContext(Handle<JSFunction> function,
                                 Handle<Context> previous,
263
                                 Handle<JSReceiver> extension);
264

265
  // Create a block context.
266 267
  Handle<Context> NewBlockContext(Handle<JSFunction> function,
                                  Handle<Context> previous,
268
                                  Handle<ScopeInfo> scope_info);
269

270 271
  // Allocate a new struct.  The struct is pretenured (allocated directly in
  // the old generation).
272
  Handle<Struct> NewStruct(InstanceType type);
273

274 275
  Handle<CodeCache> NewCodeCache();

276 277 278
  Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
      int aliased_context_slot);

279
  Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
280

281
  Handle<Script> NewScript(Handle<String> source);
282

283 284 285
  // Foreign objects are pretenured when allocated by the bootstrapper.
  Handle<Foreign> NewForeign(Address addr,
                             PretenureFlag pretenure = NOT_TENURED);
286

287 288 289
  // Allocate a new foreign object.  The foreign is pretenured (allocated
  // directly in the old generation).
  Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
290

291 292
  Handle<ByteArray> NewByteArray(int length,
                                 PretenureFlag pretenure = NOT_TENURED);
293

294
  Handle<ExternalArray> NewExternalArray(
295 296 297 298
      int length,
      ExternalArrayType array_type,
      void* external_pointer,
      PretenureFlag pretenure = NOT_TENURED);
299 300 301 302 303

  Handle<FixedTypedArrayBase> NewFixedTypedArray(
      int length,
      ExternalArrayType array_type,
      PretenureFlag pretenure = NOT_TENURED);
304

305 306
  Handle<Cell> NewCell(Handle<Object> value);

307
  Handle<PropertyCell> NewPropertyCell();
308

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

311
  // Allocate a tenured AllocationSite. It's payload is null.
312 313
  Handle<AllocationSite> NewAllocationSite();

314 315 316 317
  Handle<Map> NewMap(
      InstanceType type,
      int instance_size,
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
318

319 320 321 322
  Handle<HeapObject> NewFillerObject(int size,
                                     bool double_align,
                                     AllocationSpace space);

323
  Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
324

325 326 327 328 329
  Handle<JSObject> CopyJSObject(Handle<JSObject> object);

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

330 331 332
  Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
                                           Handle<Map> map);

333
  Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
334

335 336 337 338
  // This method expects a COW array in new space, and creates a copy
  // of it in old space.
  Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);

339 340 341
  Handle<FixedDoubleArray> CopyFixedDoubleArray(
      Handle<FixedDoubleArray> array);

342 343 344
  Handle<ConstantPoolArray> CopyConstantPoolArray(
      Handle<ConstantPoolArray> array);

345
  // Numbers (e.g. literals) are pretenured by the parser.
346
  // The return value may be a smi or a heap number.
347 348
  Handle<Object> NewNumber(double value,
                           PretenureFlag pretenure = NOT_TENURED);
349

350 351 352 353
  Handle<Object> NewNumberFromInt(int32_t value,
                                  PretenureFlag pretenure = NOT_TENURED);
  Handle<Object> NewNumberFromUint(uint32_t value,
                                  PretenureFlag pretenure = NOT_TENURED);
354 355 356 357 358 359 360 361
  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);
  }
362
  Handle<HeapNumber> NewHeapNumber(double value,
363
                                   MutableMode mode = IMMUTABLE,
364 365
                                   PretenureFlag pretenure = NOT_TENURED);

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

yurys's avatar
yurys committed
372 373
  Handle<JSWeakMap> NewJSWeakMap();

374
  Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
375 376 377

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

384
  // Global objects are pretenured and initialized based on a constructor.
385
  Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
386

387 388
  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
389 390 391 392 393
  Handle<JSObject> NewJSObjectFromMap(
      Handle<Map> map,
      PretenureFlag pretenure = NOT_TENURED,
      bool allocate_properties = true,
      Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
394

395
  // JS modules are pretenured.
396 397
  Handle<JSModule> NewJSModule(Handle<Context> context,
                               Handle<ScopeInfo> scope_info);
398

399
  // JS arrays are pretenured when allocated by the parser.
400

401
  // Create a JSArray with no elements.
402 403 404 405
  Handle<JSArray> NewJSArray(
      ElementsKind elements_kind,
      PretenureFlag pretenure = NOT_TENURED);

406 407
  // Create a JSArray with a specified length and elements initialized
  // according to the specified mode.
408
  Handle<JSArray> NewJSArray(
409 410
      ElementsKind elements_kind, int length, int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
411 412
      PretenureFlag pretenure = NOT_TENURED);

413 414 415
  Handle<JSArray> NewJSArray(
      int capacity,
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
416
      PretenureFlag pretenure = NOT_TENURED) {
417 418 419
    if (capacity != 0) {
      elements_kind = GetHoleyElementsKind(elements_kind);
    }
420 421
    return NewJSArray(elements_kind, 0, capacity,
                      INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
422 423
  }

424
  // Create a JSArray with the given elements.
425 426 427 428
  Handle<JSArray> NewJSArrayWithElements(
      Handle<FixedArrayBase> elements,
      ElementsKind elements_kind,
      int length,
429
      PretenureFlag pretenure = NOT_TENURED);
430

431
  Handle<JSArray> NewJSArrayWithElements(
432
      Handle<FixedArrayBase> elements,
433
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
434 435 436 437
      PretenureFlag pretenure = NOT_TENURED) {
    return NewJSArrayWithElements(
        elements, elements_kind, elements->length(), pretenure);
  }
438

439 440 441 442 443 444
  void NewJSArrayStorage(
      Handle<JSArray> array,
      int length,
      int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);

445 446
  Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);

447 448
  Handle<JSArrayBuffer> NewJSArrayBuffer();

449 450
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);

451 452
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind);

453 454 455
  // Creates a new JSTypedArray with the specified buffer.
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       Handle<JSArrayBuffer> buffer,
456
                                       size_t byte_offset, size_t length);
457

458 459 460 461
  // Creates a new on-heap JSTypedArray.
  Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind,
                                       size_t number_of_elements);

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

466 467 468 469
  // TODO(aandrey): Maybe these should take table, index and kind arguments.
  Handle<JSMapIterator> NewJSMapIterator();
  Handle<JSSetIterator> NewJSSetIterator();

470
  // Allocates a Harmony proxy.
471 472
  Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);

473
  // Allocates a Harmony function proxy.
474 475 476 477 478
  Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
                                     Handle<Object> call_trap,
                                     Handle<Object> construct_trap,
                                     Handle<Object> prototype);

479 480 481 482 483 484 485
  // 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);

486 487
  Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy();

488
  // Change the type of the argument into a JS object/function and reinitialize.
489 490
  void BecomeJSObject(Handle<JSProxy> object);
  void BecomeJSFunction(Handle<JSProxy> object);
491

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

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

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

517
  // Create a serialized scope info.
518
  Handle<ScopeInfo> NewScopeInfo(int length);
519

520
  // Create an External object for V8's external API.
521 522
  Handle<JSObject> NewExternal(void* value);

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

534
  Handle<Code> CopyCode(Handle<Code> code);
535

536
  Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
537

538 539
  // Interface for creating error objects.

540 541
  Handle<Object> NewError(const char* maker, const char* message,
                          Handle<JSArray> args);
542
  Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
543 544 545
  Handle<Object> NewError(const char* maker, const char* message,
                          Vector<Handle<Object> > args);
  Handle<Object> NewError(const char* message, Vector<Handle<Object> > args);
546

547 548
  Handle<Object> NewError(Handle<String> message);
  Handle<Object> NewError(const char* constructor, Handle<String> message);
549

550 551 552
  Handle<Object> NewTypeError(const char* message,
                              Vector<Handle<Object> > args);
  Handle<Object> NewTypeError(Handle<String> message);
553

554 555 556
  Handle<Object> NewRangeError(const char* message,
                               Vector<Handle<Object> > args);
  Handle<Object> NewRangeError(Handle<String> message);
557

558
  Handle<Object> NewInvalidStringLengthError() {
559 560 561 562
    return NewRangeError("invalid_string_length",
                         HandleVector<Object>(NULL, 0));
  }

563 564
  Handle<Object> NewSyntaxError(const char* message, Handle<JSArray> args);
  Handle<Object> NewSyntaxError(Handle<String> message);
565

566
  Handle<Object> NewReferenceError(const char* message,
567
                                   Vector<Handle<Object> > args);
568 569 570 571 572
  Handle<Object> NewReferenceError(const char* message, Handle<JSArray> args);
  Handle<Object> NewReferenceError(Handle<String> message);

  Handle<Object> NewEvalError(const char* message,
                              Vector<Handle<Object> > args);
573

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  Handle<Object> NewError(const char* maker,
                          MessageTemplate::Template template_index,
                          Handle<Object> arg0, Handle<Object> arg1,
                          Handle<Object> arg2);

  Handle<Object> NewError(MessageTemplate::Template template_index,
                          Handle<Object> arg0 = Handle<Object>(),
                          Handle<Object> arg1 = Handle<Object>(),
                          Handle<Object> arg2 = Handle<Object>());

  Handle<Object> NewTypeError(MessageTemplate::Template template_index,
                              Handle<Object> arg0 = Handle<Object>(),
                              Handle<Object> arg1 = Handle<Object>(),
                              Handle<Object> arg2 = Handle<Object>());

  Handle<Object> NewEvalError(MessageTemplate::Template template_index,
                              Handle<Object> arg0 = Handle<Object>(),
                              Handle<Object> arg1 = Handle<Object>(),
                              Handle<Object> arg2 = Handle<Object>());

594 595
  Handle<String> NumberToString(Handle<Object> number,
                                bool check_number_string_cache = true);
596 597 598 599

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

601
  Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
602

603 604 605 606
#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])); \
607
  }
608
  ROOT_LIST(ROOT_ACCESSOR)
609 610
#undef ROOT_ACCESSOR

611 612 613 614 615
#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])); \
  }
616 617
  STRUCT_LIST(STRUCT_MAP_ACCESSOR)
#undef STRUCT_MAP_ACCESSOR
618

619 620 621 622
#define STRING_ACCESSOR(name, str)                              \
  inline Handle<String> name() {                                \
    return Handle<String>(bit_cast<String**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
623
  }
624 625
  INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
#undef STRING_ACCESSOR
ager@chromium.org's avatar
ager@chromium.org committed
626

627 628 629 630 631 632 633 634
#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

635 636 637 638 639 640 641 642
#define SYMBOL_ACCESSOR(name, varname, description)             \
  inline Handle<Symbol> name() {                                \
    return Handle<Symbol>(bit_cast<Symbol**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
  }
  PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
#undef SYMBOL_ACCESSOR

643 644 645 646
  inline void set_string_table(Handle<StringTable> table) {
    isolate()->heap()->set_string_table(*table);
  }

647 648
  Handle<String> hidden_string() {
    return Handle<String>(&isolate()->heap()->hidden_string_);
649
  }
650

651
  // Allocates a new SharedFunctionInfo object.
652
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
653 654
      Handle<String> name, int number_of_literals, FunctionKind kind,
      Handle<Code> code, Handle<ScopeInfo> scope_info,
655
      Handle<TypeFeedbackVector> feedback_vector);
656 657
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
                                                   MaybeHandle<Code> code);
658

659
  // Allocate a new type feedback vector
660 661
  template <typename Spec>
  Handle<TypeFeedbackVector> NewTypeFeedbackVector(const Spec* spec);
662

663
  // Allocates a new JSMessageObject object.
664
  Handle<JSMessageObject> NewJSMessageObject(
665 666 667 668 669 670 671
      Handle<String> type,
      Handle<JSArray> arguments,
      int start_position,
      int end_position,
      Handle<Object> script,
      Handle<Object> stack_frames);

672
  Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
673

674 675
  // Return a map for given number of properties using the map cache in the
  // native context.
676
  Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
677 678
                                        int number_of_properties,
                                        bool* is_result_from_cache);
679

680
  // Creates a new FixedArray that holds the data associated with the
681
  // atom regexp and stores it in the regexp.
682 683 684 685 686
  void SetRegExpAtomData(Handle<JSRegExp> regexp,
                         JSRegExp::Type type,
                         Handle<String> source,
                         JSRegExp::Flags flags,
                         Handle<Object> match_pattern);
687 688 689

  // Creates a new FixedArray that holds the data associated with the
  // irregexp regexp and stores it in the regexp.
690 691 692 693 694
  void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
                             JSRegExp::Type type,
                             Handle<String> source,
                             JSRegExp::Flags flags,
                             int capture_count);
695

696 697 698
  // 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.
699
  Handle<Object> GlobalConstantFor(Handle<Name> name);
700

701 702 703
  // Converts the given boolean condition to JavaScript boolean value.
  Handle<Object> ToBoolean(bool value);

704
 private:
705
  Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
706

707 708 709 710 711 712 713 714 715 716 717
  // 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);

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

721 722 723 724 725 726
  // 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);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

  // Initializes a function with a shared part and prototype.
  // Note: this code was factored out of NewFunction such that other parts of
  // the VM could use it. Specifically, a function that creates instances of
  // type JS_FUNCTION_TYPE benefit from the use of this function.
  inline void InitializeFunction(Handle<JSFunction> function,
                                 Handle<SharedFunctionInfo> info,
                                 Handle<Context> context);

  // 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);
745 746 747 748 749 750

  // Reinitialize a JSProxy into an (empty) JS object of respective type and
  // size, but keeping the original prototype.  The receiver must have at least
  // the size of the new object.  The object is reinitialized and behaves as an
  // object that has been freshly allocated.
  void ReinitializeJSProxy(Handle<JSProxy> proxy, InstanceType type, int size);
751 752 753 754 755
};

} }  // namespace v8::internal

#endif  // V8_FACTORY_H_