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

10 11
namespace v8 {
namespace internal {
12

13
class FeedbackVectorSpec;
14

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

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

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

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

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

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

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

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

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

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

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 85 86 87 88

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

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

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

123

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

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

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

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

156 157 158 159 160 161 162 163
  // 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(
164 165 166 167 168
      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);
169 170 171 172 173 174

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

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

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

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

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

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

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

205 206 207 208 209 210
  // 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);
  }

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

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

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

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

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

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

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

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

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

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

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

262 263
  Handle<CodeCache> NewCodeCache();

264 265 266
  Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
      int aliased_context_slot);

267
  Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
268

269
  Handle<Script> NewScript(Handle<String> source);
270

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

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

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

282
  Handle<ExternalArray> NewExternalArray(
283 284 285 286
      int length,
      ExternalArrayType array_type,
      void* external_pointer,
      PretenureFlag pretenure = NOT_TENURED);
287 288 289 290 291

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

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

295 296
  Handle<PropertyCell> NewPropertyCellWithHole();

297
  Handle<PropertyCell> NewPropertyCell(Handle<Object> value);
298

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

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

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

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

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

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

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

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

323
  Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
324

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

329 330 331
  Handle<FixedDoubleArray> CopyFixedDoubleArray(
      Handle<FixedDoubleArray> array);

332 333 334
  Handle<ConstantPoolArray> CopyConstantPoolArray(
      Handle<ConstantPoolArray> 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 355
                                   PretenureFlag pretenure = NOT_TENURED);

356 357
  // These objects are used by the api to create env-independent data
  // structures in the heap.
358 359 360
  inline Handle<JSObject> NewNeanderObject() {
    return NewJSObjectFromMap(neander_map());
  }
361

yurys's avatar
yurys committed
362 363
  Handle<JSWeakMap> NewJSWeakMap();

364
  Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length);
365 366 367

  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
368 369
  Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
                               PretenureFlag pretenure = NOT_TENURED);
370 371 372
  // JSObject that should have a memento pointing to the allocation site.
  Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
                                          Handle<AllocationSite> site);
373

374
  // Global objects are pretenured and initialized based on a constructor.
375
  Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
376

377 378
  // JS objects are pretenured when allocated by the bootstrapper and
  // runtime.
379 380 381 382 383
  Handle<JSObject> NewJSObjectFromMap(
      Handle<Map> map,
      PretenureFlag pretenure = NOT_TENURED,
      bool allocate_properties = true,
      Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
384

385
  // JS modules are pretenured.
386 387
  Handle<JSModule> NewJSModule(Handle<Context> context,
                               Handle<ScopeInfo> scope_info);
388

389
  // JS arrays are pretenured when allocated by the parser.
390

391
  // Create a JSArray with no elements.
392 393 394 395
  Handle<JSArray> NewJSArray(
      ElementsKind elements_kind,
      PretenureFlag pretenure = NOT_TENURED);

396 397
  // Create a JSArray with a specified length and elements initialized
  // according to the specified mode.
398
  Handle<JSArray> NewJSArray(
399 400
      ElementsKind elements_kind, int length, int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
401 402
      PretenureFlag pretenure = NOT_TENURED);

403 404 405
  Handle<JSArray> NewJSArray(
      int capacity,
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
406
      PretenureFlag pretenure = NOT_TENURED) {
407 408 409
    if (capacity != 0) {
      elements_kind = GetHoleyElementsKind(elements_kind);
    }
410 411
    return NewJSArray(elements_kind, 0, capacity,
                      INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
412 413
  }

414
  // Create a JSArray with the given elements.
415 416 417 418
  Handle<JSArray> NewJSArrayWithElements(
      Handle<FixedArrayBase> elements,
      ElementsKind elements_kind,
      int length,
419
      PretenureFlag pretenure = NOT_TENURED);
420

421
  Handle<JSArray> NewJSArrayWithElements(
422
      Handle<FixedArrayBase> elements,
423
      ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
424 425 426 427
      PretenureFlag pretenure = NOT_TENURED) {
    return NewJSArrayWithElements(
        elements, elements_kind, elements->length(), pretenure);
  }
428

429 430 431 432 433 434
  void NewJSArrayStorage(
      Handle<JSArray> array,
      int length,
      int capacity,
      ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);

435 436
  Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);

437 438
  Handle<JSArrayBuffer> NewJSArrayBuffer();

439 440
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);

441 442 443
  // Creates a new JSTypedArray with the specified buffer.
  Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type,
                                       Handle<JSArrayBuffer> buffer,
444
                                       size_t byte_offset, size_t length);
445

446
  Handle<JSDataView> NewJSDataView();
447 448
  Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer,
                                   size_t byte_offset, size_t byte_length);
449

450 451 452 453
  // TODO(aandrey): Maybe these should take table, index and kind arguments.
  Handle<JSMapIterator> NewJSMapIterator();
  Handle<JSSetIterator> NewJSSetIterator();

454
  // Allocates a Harmony proxy.
455 456
  Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);

457
  // Allocates a Harmony function proxy.
458 459 460 461 462
  Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
                                     Handle<Object> call_trap,
                                     Handle<Object> construct_trap,
                                     Handle<Object> prototype);

463 464 465 466 467 468 469
  // 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);

470 471
  Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy();

472
  // Change the type of the argument into a JS object/function and reinitialize.
473 474
  void BecomeJSObject(Handle<JSProxy> object);
  void BecomeJSFunction(Handle<JSProxy> object);
475

476
  Handle<JSFunction> NewFunction(Handle<String> name,
477
                                 Handle<Code> code,
478 479
                                 Handle<Object> prototype,
                                 bool read_only_prototype = false);
480
  Handle<JSFunction> NewFunction(Handle<String> name);
481 482
  Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
                                                 Handle<Code> code);
483

484
  Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
485
      Handle<SharedFunctionInfo> function_info,
486 487
      Handle<Context> context,
      PretenureFlag pretenure = TENURED);
488

489 490
  Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code,
                                 Handle<Object> prototype, InstanceType type,
491
                                 int instance_size,
492 493
                                 bool read_only_prototype = false,
                                 bool install_constructor = false);
494
  Handle<JSFunction> NewFunction(Handle<String> name,
495
                                 Handle<Code> code,
496
                                 InstanceType type,
497
                                 int instance_size);
498

499
  // Create a serialized scope info.
500
  Handle<ScopeInfo> NewScopeInfo(int length);
501

502
  // Create an External object for V8's external API.
503 504
  Handle<JSObject> NewExternal(void* value);

505 506 507
  // 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.
508 509 510
  Handle<Code> NewCode(const CodeDesc& desc,
                       Code::Flags flags,
                       Handle<Object> self_reference,
511
                       bool immovable = false,
512
                       bool crankshafted = false,
513 514
                       int prologue_offset = Code::kPrologueOffsetNotSet,
                       bool is_debug = false);
515

516
  Handle<Code> CopyCode(Handle<Code> code);
517

518
  Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
519

520 521
  // Interface for creating error objects.

522 523
  Handle<Object> NewError(const char* maker, const char* message,
                          Handle<JSArray> args);
524
  Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
525 526 527 528 529
  Handle<Object> NewError(const char* maker, const char* message,
                          Vector<Handle<Object> > args);
  Handle<Object> NewError(const char* message, Vector<Handle<Object> > args);
  Handle<Object> NewError(Handle<String> message);
  Handle<Object> NewError(const char* constructor, Handle<String> message);
530

531 532 533
  Handle<Object> NewTypeError(const char* message,
                              Vector<Handle<Object> > args);
  Handle<Object> NewTypeError(Handle<String> message);
534

535 536 537
  Handle<Object> NewRangeError(const char* message,
                               Vector<Handle<Object> > args);
  Handle<Object> NewRangeError(Handle<String> message);
538

539
  Handle<Object> NewInvalidStringLengthError() {
540 541 542 543
    return NewRangeError("invalid_string_length",
                         HandleVector<Object>(NULL, 0));
  }

544 545
  Handle<Object> NewSyntaxError(const char* message, Handle<JSArray> args);
  Handle<Object> NewSyntaxError(Handle<String> message);
546

547
  Handle<Object> NewReferenceError(const char* message,
548
                                   Vector<Handle<Object> > args);
549 550 551 552 553
  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);
554

555 556
  Handle<String> NumberToString(Handle<Object> number,
                                bool check_number_string_cache = true);
557 558 559 560

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

562
  Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
563

564 565 566 567
#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])); \
568
  }
569
  ROOT_LIST(ROOT_ACCESSOR)
570 571
#undef ROOT_ACCESSOR

572 573 574 575 576
#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])); \
  }
577 578
  STRUCT_LIST(STRUCT_MAP_ACCESSOR)
#undef STRUCT_MAP_ACCESSOR
579

580 581 582 583
#define STRING_ACCESSOR(name, str)                              \
  inline Handle<String> name() {                                \
    return Handle<String>(bit_cast<String**>(                   \
        &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
584
  }
585 586
  INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
#undef STRING_ACCESSOR
ager@chromium.org's avatar
ager@chromium.org committed
587

588 589 590 591 592 593 594 595
#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

596 597 598 599 600 601 602 603
#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

604 605 606 607
  inline void set_string_table(Handle<StringTable> table) {
    isolate()->heap()->set_string_table(*table);
  }

608 609
  Handle<String> hidden_string() {
    return Handle<String>(&isolate()->heap()->hidden_string_);
610
  }
611

612
  // Allocates a new SharedFunctionInfo object.
613
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(
614 615
      Handle<String> name, int number_of_literals, FunctionKind kind,
      Handle<Code> code, Handle<ScopeInfo> scope_info,
616
      Handle<TypeFeedbackVector> feedback_vector);
617 618
  Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
                                                   MaybeHandle<Code> code);
619

620
  // Allocate a new type feedback vector
621 622
  Handle<TypeFeedbackVector> NewTypeFeedbackVector(
      const FeedbackVectorSpec& spec);
623

624
  // Allocates a new JSMessageObject object.
625
  Handle<JSMessageObject> NewJSMessageObject(
626 627 628 629 630 631 632
      Handle<String> type,
      Handle<JSArray> arguments,
      int start_position,
      int end_position,
      Handle<Object> script,
      Handle<Object> stack_frames);

633
  Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
634

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

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

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

657 658 659 660 661
  // 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.
  Handle<Object> GlobalConstantFor(Handle<String> name);

662 663 664
  // Converts the given boolean condition to JavaScript boolean value.
  Handle<Object> ToBoolean(bool value);

665
 private:
666
  Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
667

668 669 670 671 672 673 674 675 676 677 678
  // 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);

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

682 683 684 685 686 687
  // 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);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

  // 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);
706 707 708 709 710 711

  // 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);
712 713 714 715 716
};

} }  // namespace v8::internal

#endif  // V8_FACTORY_H_