Home | History | Annotate | Download | only in src
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_FACTORY_H_
      6 #define V8_FACTORY_H_
      7 
      8 #include "src/isolate.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 
     13 // Interface for handle based allocation.
     14 
     15 class Factory V8_FINAL {
     16  public:
     17   Handle<Oddball> NewOddball(Handle<Map> map,
     18                              const char* to_string,
     19                              Handle<Object> to_number,
     20                              byte kind);
     21 
     22   // Allocates a fixed array initialized with undefined values.
     23   Handle<FixedArray> NewFixedArray(
     24       int size,
     25       PretenureFlag pretenure = NOT_TENURED);
     26 
     27   // Allocate a new fixed array with non-existing entries (the hole).
     28   Handle<FixedArray> NewFixedArrayWithHoles(
     29       int size,
     30       PretenureFlag pretenure = NOT_TENURED);
     31 
     32   // Allocates an uninitialized fixed array. It must be filled by the caller.
     33   Handle<FixedArray> NewUninitializedFixedArray(int size);
     34 
     35   // Allocate a new uninitialized fixed double array.
     36   // The function returns a pre-allocated empty fixed array for capacity = 0,
     37   // so the return type must be the general fixed array class.
     38   Handle<FixedArrayBase> NewFixedDoubleArray(
     39       int size,
     40       PretenureFlag pretenure = NOT_TENURED);
     41 
     42   // Allocate a new fixed double array with hole values.
     43   Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles(
     44       int size,
     45       PretenureFlag pretenure = NOT_TENURED);
     46 
     47   Handle<ConstantPoolArray> NewConstantPoolArray(
     48       const ConstantPoolArray::NumberOfEntries& small);
     49 
     50   Handle<ConstantPoolArray> NewExtendedConstantPoolArray(
     51       const ConstantPoolArray::NumberOfEntries& small,
     52       const ConstantPoolArray::NumberOfEntries& extended);
     53 
     54   Handle<OrderedHashSet> NewOrderedHashSet();
     55   Handle<OrderedHashMap> NewOrderedHashMap();
     56 
     57   // Create a new boxed value.
     58   Handle<Box> NewBox(Handle<Object> value);
     59 
     60   // Create a pre-tenured empty AccessorPair.
     61   Handle<AccessorPair> NewAccessorPair();
     62 
     63   // Create an empty TypeFeedbackInfo.
     64   Handle<TypeFeedbackInfo> NewTypeFeedbackInfo();
     65 
     66   // Finds the internalized copy for string in the string table.
     67   // If not found, a new string is added to the table and returned.
     68   Handle<String> InternalizeUtf8String(Vector<const char> str);
     69   Handle<String> InternalizeUtf8String(const char* str) {
     70     return InternalizeUtf8String(CStrVector(str));
     71   }
     72   Handle<String> InternalizeString(Handle<String> str);
     73   Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
     74   Handle<String> InternalizeOneByteString(
     75       Handle<SeqOneByteString>, int from, int length);
     76 
     77   Handle<String> InternalizeTwoByteString(Vector<const uc16> str);
     78 
     79   template<class StringTableKey>
     80   Handle<String> InternalizeStringWithKey(StringTableKey* key);
     81 
     82 
     83   // String creation functions.  Most of the string creation functions take
     84   // a Heap::PretenureFlag argument to optionally request that they be
     85   // allocated in the old generation.  The pretenure flag defaults to
     86   // DONT_TENURE.
     87   //
     88   // Creates a new String object.  There are two String encodings: ASCII and
     89   // two byte.  One should choose between the three string factory functions
     90   // based on the encoding of the string buffer that the string is
     91   // initialized from.
     92   //   - ...FromAscii initializes the string from a buffer that is ASCII
     93   //     encoded (it does not check that the buffer is ASCII encoded) and
     94   //     the result will be ASCII encoded.
     95   //   - ...FromUtf8 initializes the string from a buffer that is UTF-8
     96   //     encoded.  If the characters are all single-byte characters, the
     97   //     result will be ASCII encoded, otherwise it will converted to two
     98   //     byte.
     99   //   - ...FromTwoByte initializes the string from a buffer that is two
    100   //     byte encoded.  If the characters are all single-byte characters,
    101   //     the result will be converted to ASCII, otherwise it will be left as
    102   //     two byte.
    103   //
    104   // ASCII strings are pretenured when used as keys in the SourceCodeCache.
    105   MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte(
    106       Vector<const uint8_t> str,
    107       PretenureFlag pretenure = NOT_TENURED);
    108 
    109   template<size_t N>
    110   inline Handle<String> NewStringFromStaticAscii(
    111       const char (&str)[N],
    112       PretenureFlag pretenure = NOT_TENURED) {
    113     ASSERT(N == StrLength(str) + 1);
    114     return NewStringFromOneByte(
    115         STATIC_ASCII_VECTOR(str), pretenure).ToHandleChecked();
    116   }
    117 
    118   inline Handle<String> NewStringFromAsciiChecked(
    119       const char* str,
    120       PretenureFlag pretenure = NOT_TENURED) {
    121     return NewStringFromOneByte(
    122         OneByteVector(str), pretenure).ToHandleChecked();
    123   }
    124 
    125 
    126   // Allocates and fully initializes a String.  There are two String
    127   // encodings: ASCII and two byte. One should choose between the three string
    128   // allocation functions based on the encoding of the string buffer used to
    129   // initialized the string.
    130   //   - ...FromAscii initializes the string from a buffer that is ASCII
    131   //     encoded (it does not check that the buffer is ASCII encoded) and the
    132   //     result will be ASCII encoded.
    133   //   - ...FromUTF8 initializes the string from a buffer that is UTF-8
    134   //     encoded.  If the characters are all single-byte characters, the
    135   //     result will be ASCII encoded, otherwise it will converted to two
    136   //     byte.
    137   //   - ...FromTwoByte initializes the string from a buffer that is two-byte
    138   //     encoded.  If the characters are all single-byte characters, the
    139   //     result will be converted to ASCII, otherwise it will be left as
    140   //     two-byte.
    141 
    142   // TODO(dcarney): remove this function.
    143   MUST_USE_RESULT inline MaybeHandle<String> NewStringFromAscii(
    144       Vector<const char> str,
    145       PretenureFlag pretenure = NOT_TENURED) {
    146     return NewStringFromOneByte(Vector<const uint8_t>::cast(str), pretenure);
    147   }
    148 
    149   // UTF8 strings are pretenured when used for regexp literal patterns and
    150   // flags in the parser.
    151   MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8(
    152       Vector<const char> str,
    153       PretenureFlag pretenure = NOT_TENURED);
    154 
    155   MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte(
    156       Vector<const uc16> str,
    157       PretenureFlag pretenure = NOT_TENURED);
    158 
    159   // Allocates an internalized string in old space based on the character
    160   // stream.
    161   MUST_USE_RESULT Handle<String> NewInternalizedStringFromUtf8(
    162       Vector<const char> str,
    163       int chars,
    164       uint32_t hash_field);
    165 
    166   MUST_USE_RESULT Handle<String> NewOneByteInternalizedString(
    167         Vector<const uint8_t> str,
    168         uint32_t hash_field);
    169 
    170   MUST_USE_RESULT Handle<String> NewTwoByteInternalizedString(
    171         Vector<const uc16> str,
    172         uint32_t hash_field);
    173 
    174   MUST_USE_RESULT Handle<String> NewInternalizedStringImpl(
    175       Handle<String> string, int chars, uint32_t hash_field);
    176 
    177   // Compute the matching internalized string map for a string if possible.
    178   // Empty handle is returned if string is in new space or not flattened.
    179   MUST_USE_RESULT MaybeHandle<Map> InternalizedStringMapForString(
    180       Handle<String> string);
    181 
    182   // Allocates and partially initializes an ASCII or TwoByte String. The
    183   // characters of the string are uninitialized. Currently used in regexp code
    184   // only, where they are pretenured.
    185   MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString(
    186       int length,
    187       PretenureFlag pretenure = NOT_TENURED);
    188   MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString(
    189       int length,
    190       PretenureFlag pretenure = NOT_TENURED);
    191 
    192   // Creates a single character string where the character has given code.
    193   // A cache is used for ASCII codes.
    194   Handle<String> LookupSingleCharacterStringFromCode(uint32_t code);
    195 
    196   // Create a new cons string object which consists of a pair of strings.
    197   MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left,
    198                                                     Handle<String> right);
    199 
    200   // Create a new sequential string containing the concatenation of the inputs.
    201   Handle<String> NewFlatConcatString(Handle<String> first,
    202                                      Handle<String> second);
    203 
    204   // Create a new string object which holds a proper substring of a string.
    205   Handle<String> NewProperSubString(Handle<String> str,
    206                                     int begin,
    207                                     int end);
    208 
    209   // Create a new string object which holds a substring of a string.
    210   Handle<String> NewSubString(Handle<String> str, int begin, int end) {
    211     if (begin == 0 && end == str->length()) return str;
    212     return NewProperSubString(str, begin, end);
    213   }
    214 
    215   // Creates a new external String object.  There are two String encodings
    216   // in the system: ASCII and two byte.  Unlike other String types, it does
    217   // not make sense to have a UTF-8 factory function for external strings,
    218   // because we cannot change the underlying buffer.  Note that these strings
    219   // are backed by a string resource that resides outside the V8 heap.
    220   MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromAscii(
    221       const ExternalAsciiString::Resource* resource);
    222   MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte(
    223       const ExternalTwoByteString::Resource* resource);
    224 
    225   // Create a symbol.
    226   Handle<Symbol> NewSymbol();
    227   Handle<Symbol> NewPrivateSymbol();
    228 
    229   // Create a global (but otherwise uninitialized) context.
    230   Handle<Context> NewNativeContext();
    231 
    232   // Create a global context.
    233   Handle<Context> NewGlobalContext(Handle<JSFunction> function,
    234                                    Handle<ScopeInfo> scope_info);
    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   Handle<Context> NewCatchContext(Handle<JSFunction> function,
    244                                   Handle<Context> previous,
    245                                   Handle<String> name,
    246                                   Handle<Object> thrown_object);
    247 
    248   // Create a 'with' context.
    249   Handle<Context> NewWithContext(Handle<JSFunction> function,
    250                                  Handle<Context> previous,
    251                                  Handle<JSReceiver> extension);
    252 
    253   // Create a block context.
    254   Handle<Context> NewBlockContext(Handle<JSFunction> function,
    255                                   Handle<Context> previous,
    256                                   Handle<ScopeInfo> scope_info);
    257 
    258   // Allocate a new struct.  The struct is pretenured (allocated directly in
    259   // the old generation).
    260   Handle<Struct> NewStruct(InstanceType type);
    261 
    262   Handle<CodeCache> NewCodeCache();
    263 
    264   Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry(
    265       int aliased_context_slot);
    266 
    267   Handle<DeclaredAccessorDescriptor> NewDeclaredAccessorDescriptor();
    268 
    269   Handle<DeclaredAccessorInfo> NewDeclaredAccessorInfo();
    270 
    271   Handle<ExecutableAccessorInfo> NewExecutableAccessorInfo();
    272 
    273   Handle<Script> NewScript(Handle<String> source);
    274 
    275   // Foreign objects are pretenured when allocated by the bootstrapper.
    276   Handle<Foreign> NewForeign(Address addr,
    277                              PretenureFlag pretenure = NOT_TENURED);
    278 
    279   // Allocate a new foreign object.  The foreign is pretenured (allocated
    280   // directly in the old generation).
    281   Handle<Foreign> NewForeign(const AccessorDescriptor* foreign);
    282 
    283   Handle<ByteArray> NewByteArray(int length,
    284                                  PretenureFlag pretenure = NOT_TENURED);
    285 
    286   Handle<ExternalArray> NewExternalArray(
    287       int length,
    288       ExternalArrayType array_type,
    289       void* external_pointer,
    290       PretenureFlag pretenure = NOT_TENURED);
    291 
    292   Handle<FixedTypedArrayBase> NewFixedTypedArray(
    293       int length,
    294       ExternalArrayType array_type,
    295       PretenureFlag pretenure = NOT_TENURED);
    296 
    297   Handle<Cell> NewCell(Handle<Object> value);
    298 
    299   Handle<PropertyCell> NewPropertyCellWithHole();
    300 
    301   Handle<PropertyCell> NewPropertyCell(Handle<Object> value);
    302 
    303   // Allocate a tenured AllocationSite. It's payload is null.
    304   Handle<AllocationSite> NewAllocationSite();
    305 
    306   Handle<Map> NewMap(
    307       InstanceType type,
    308       int instance_size,
    309       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
    310 
    311   Handle<HeapObject> NewFillerObject(int size,
    312                                      bool double_align,
    313                                      AllocationSpace space);
    314 
    315   Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);
    316 
    317   Handle<JSObject> CopyJSObject(Handle<JSObject> object);
    318 
    319   Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object,
    320                                                   Handle<AllocationSite> site);
    321 
    322   Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array,
    323                                            Handle<Map> map);
    324 
    325   Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array);
    326 
    327   // This method expects a COW array in new space, and creates a copy
    328   // of it in old space.
    329   Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array);
    330 
    331   Handle<FixedDoubleArray> CopyFixedDoubleArray(
    332       Handle<FixedDoubleArray> array);
    333 
    334   Handle<ConstantPoolArray> CopyConstantPoolArray(
    335       Handle<ConstantPoolArray> array);
    336 
    337   // Numbers (e.g. literals) are pretenured by the parser.
    338   // The return value may be a smi or a heap number.
    339   Handle<Object> NewNumber(double value,
    340                            PretenureFlag pretenure = NOT_TENURED);
    341 
    342   Handle<Object> NewNumberFromInt(int32_t value,
    343                                   PretenureFlag pretenure = NOT_TENURED);
    344   Handle<Object> NewNumberFromUint(uint32_t value,
    345                                   PretenureFlag pretenure = NOT_TENURED);
    346   Handle<Object> NewNumberFromSize(size_t value,
    347                                    PretenureFlag pretenure = NOT_TENURED) {
    348     if (Smi::IsValid(static_cast<intptr_t>(value))) {
    349       return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
    350                             isolate());
    351     }
    352     return NewNumber(static_cast<double>(value), pretenure);
    353   }
    354   Handle<HeapNumber> NewHeapNumber(double value,
    355                                    PretenureFlag pretenure = NOT_TENURED);
    356 
    357 
    358   // These objects are used by the api to create env-independent data
    359   // structures in the heap.
    360   inline Handle<JSObject> NewNeanderObject() {
    361     return NewJSObjectFromMap(neander_map());
    362   }
    363 
    364   Handle<JSObject> NewArgumentsObject(Handle<Object> callee, int length);
    365 
    366   // JS objects are pretenured when allocated by the bootstrapper and
    367   // runtime.
    368   Handle<JSObject> NewJSObject(Handle<JSFunction> constructor,
    369                                PretenureFlag pretenure = NOT_TENURED);
    370   // JSObject that should have a memento pointing to the allocation site.
    371   Handle<JSObject> NewJSObjectWithMemento(Handle<JSFunction> constructor,
    372                                           Handle<AllocationSite> site);
    373 
    374   // Global objects are pretenured and initialized based on a constructor.
    375   Handle<GlobalObject> NewGlobalObject(Handle<JSFunction> constructor);
    376 
    377   // JS objects are pretenured when allocated by the bootstrapper and
    378   // runtime.
    379   Handle<JSObject> NewJSObjectFromMap(
    380       Handle<Map> map,
    381       PretenureFlag pretenure = NOT_TENURED,
    382       bool allocate_properties = true,
    383       Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null());
    384 
    385   // JS modules are pretenured.
    386   Handle<JSModule> NewJSModule(Handle<Context> context,
    387                                Handle<ScopeInfo> scope_info);
    388 
    389   // JS arrays are pretenured when allocated by the parser.
    390 
    391   // Create a JSArray with no elements.
    392   Handle<JSArray> NewJSArray(
    393       ElementsKind elements_kind,
    394       PretenureFlag pretenure = NOT_TENURED);
    395 
    396   // Create a JSArray with a specified length and elements initialized
    397   // according to the specified mode.
    398   Handle<JSArray> NewJSArray(
    399       ElementsKind elements_kind,
    400       int length,
    401       int capacity,
    402       ArrayStorageAllocationMode mode = INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
    403       PretenureFlag pretenure = NOT_TENURED);
    404 
    405   Handle<JSArray> NewJSArray(
    406       int capacity,
    407       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
    408       PretenureFlag pretenure = NOT_TENURED) {
    409     if (capacity != 0) {
    410       elements_kind = GetHoleyElementsKind(elements_kind);
    411     }
    412     return NewJSArray(elements_kind, 0, capacity,
    413                       INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure);
    414   }
    415 
    416   // Create a JSArray with the given elements.
    417   Handle<JSArray> NewJSArrayWithElements(
    418       Handle<FixedArrayBase> elements,
    419       ElementsKind elements_kind,
    420       int length,
    421       PretenureFlag pretenure = NOT_TENURED);
    422 
    423   Handle<JSArray> NewJSArrayWithElements(
    424       Handle<FixedArrayBase> elements,
    425       ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND,
    426       PretenureFlag pretenure = NOT_TENURED) {
    427     return NewJSArrayWithElements(
    428         elements, elements_kind, elements->length(), pretenure);
    429   }
    430 
    431   void NewJSArrayStorage(
    432       Handle<JSArray> array,
    433       int length,
    434       int capacity,
    435       ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
    436 
    437   Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
    438 
    439   Handle<JSArrayBuffer> NewJSArrayBuffer();
    440 
    441   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
    442 
    443   Handle<JSDataView> NewJSDataView();
    444 
    445   // Allocates a Harmony proxy.
    446   Handle<JSProxy> NewJSProxy(Handle<Object> handler, Handle<Object> prototype);
    447 
    448   // Allocates a Harmony function proxy.
    449   Handle<JSProxy> NewJSFunctionProxy(Handle<Object> handler,
    450                                      Handle<Object> call_trap,
    451                                      Handle<Object> construct_trap,
    452                                      Handle<Object> prototype);
    453 
    454   // Reinitialize a JSReceiver into an (empty) JS object of respective type and
    455   // size, but keeping the original prototype.  The receiver must have at least
    456   // the size of the new object.  The object is reinitialized and behaves as an
    457   // object that has been freshly allocated.
    458   void ReinitializeJSReceiver(
    459       Handle<JSReceiver> object, InstanceType type, int size);
    460 
    461   // Reinitialize an JSGlobalProxy based on a constructor.  The object
    462   // must have the same size as objects allocated using the
    463   // constructor.  The object is reinitialized and behaves as an
    464   // object that has been freshly allocated using the constructor.
    465   void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global,
    466                                  Handle<JSFunction> constructor);
    467 
    468   // Change the type of the argument into a JS object/function and reinitialize.
    469   void BecomeJSObject(Handle<JSReceiver> object);
    470   void BecomeJSFunction(Handle<JSReceiver> object);
    471 
    472   Handle<JSFunction> NewFunction(Handle<String> name,
    473                                  Handle<Code> code,
    474                                  Handle<Object> prototype,
    475                                  bool read_only_prototype = false);
    476   Handle<JSFunction> NewFunction(Handle<String> name);
    477   Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name,
    478                                                  Handle<Code> code);
    479 
    480   Handle<JSFunction> NewFunctionFromSharedFunctionInfo(
    481       Handle<SharedFunctionInfo> function_info,
    482       Handle<Context> context,
    483       PretenureFlag pretenure = TENURED);
    484 
    485   Handle<JSFunction> NewFunction(Handle<String> name,
    486                                  Handle<Code> code,
    487                                  Handle<Object> prototype,
    488                                  InstanceType type,
    489                                  int instance_size,
    490                                  bool read_only_prototype = false);
    491   Handle<JSFunction> NewFunction(Handle<String> name,
    492                                  Handle<Code> code,
    493                                  InstanceType type,
    494                                  int instance_size);
    495 
    496   // Create a serialized scope info.
    497   Handle<ScopeInfo> NewScopeInfo(int length);
    498 
    499   // Create an External object for V8's external API.
    500   Handle<JSObject> NewExternal(void* value);
    501 
    502   // The reference to the Code object is stored in self_reference.
    503   // This allows generated code to reference its own Code object
    504   // by containing this handle.
    505   Handle<Code> NewCode(const CodeDesc& desc,
    506                        Code::Flags flags,
    507                        Handle<Object> self_reference,
    508                        bool immovable = false,
    509                        bool crankshafted = false,
    510                        int prologue_offset = Code::kPrologueOffsetNotSet,
    511                        bool is_debug = false);
    512 
    513   Handle<Code> CopyCode(Handle<Code> code);
    514 
    515   Handle<Code> CopyCode(Handle<Code> code, Vector<byte> reloc_info);
    516 
    517   // Interface for creating error objects.
    518 
    519   Handle<Object> NewError(const char* maker, const char* message,
    520                           Handle<JSArray> args);
    521   Handle<String> EmergencyNewError(const char* message, Handle<JSArray> args);
    522   Handle<Object> NewError(const char* maker, const char* message,
    523                           Vector< Handle<Object> > args);
    524   Handle<Object> NewError(const char* message,
    525                           Vector< Handle<Object> > args);
    526   Handle<Object> NewError(Handle<String> message);
    527   Handle<Object> NewError(const char* constructor,
    528                           Handle<String> message);
    529 
    530   Handle<Object> NewTypeError(const char* message,
    531                               Vector< Handle<Object> > args);
    532   Handle<Object> NewTypeError(Handle<String> message);
    533 
    534   Handle<Object> NewRangeError(const char* message,
    535                                Vector< Handle<Object> > args);
    536   Handle<Object> NewRangeError(Handle<String> message);
    537 
    538   Handle<Object> NewInvalidStringLengthError() {
    539     return NewRangeError("invalid_string_length",
    540                          HandleVector<Object>(NULL, 0));
    541   }
    542 
    543   Handle<Object> NewSyntaxError(const char* message, Handle<JSArray> args);
    544   Handle<Object> NewSyntaxError(Handle<String> message);
    545 
    546   Handle<Object> NewReferenceError(const char* message,
    547                                    Vector< Handle<Object> > args);
    548   Handle<Object> NewReferenceError(const char* message, Handle<JSArray> args);
    549   Handle<Object> NewReferenceError(Handle<String> message);
    550 
    551   Handle<Object> NewEvalError(const char* message,
    552                               Vector< Handle<Object> > args);
    553 
    554   Handle<JSObject> NewIteratorResultObject(Handle<Object> value, bool done);
    555 
    556   Handle<String> NumberToString(Handle<Object> number,
    557                                 bool check_number_string_cache = true);
    558 
    559   Handle<String> Uint32ToString(uint32_t value) {
    560     return NumberToString(NewNumberFromUint(value));
    561   }
    562 
    563   enum ApiInstanceType {
    564     JavaScriptObject,
    565     InnerGlobalObject,
    566     OuterGlobalObject
    567   };
    568 
    569   Handle<JSFunction> CreateApiFunction(
    570       Handle<FunctionTemplateInfo> data,
    571       Handle<Object> prototype,
    572       ApiInstanceType type = JavaScriptObject);
    573 
    574   Handle<JSFunction> InstallMembers(Handle<JSFunction> function);
    575 
    576   // Installs interceptors on the instance.  'desc' is a function template,
    577   // and instance is an object instance created by the function of this
    578   // function template.
    579   MUST_USE_RESULT MaybeHandle<FunctionTemplateInfo> ConfigureInstance(
    580       Handle<FunctionTemplateInfo> desc, Handle<JSObject> instance);
    581 
    582 #define ROOT_ACCESSOR(type, name, camel_name)                                  \
    583   inline Handle<type> name() {                                                 \
    584     return Handle<type>(BitCast<type**>(                                       \
    585         &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex]));          \
    586   }
    587   ROOT_LIST(ROOT_ACCESSOR)
    588 #undef ROOT_ACCESSOR
    589 
    590 #define STRUCT_MAP_ACCESSOR(NAME, Name, name)                                  \
    591   inline Handle<Map> name##_map() {                                            \
    592     return Handle<Map>(BitCast<Map**>(                                         \
    593         &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex]));             \
    594     }
    595   STRUCT_LIST(STRUCT_MAP_ACCESSOR)
    596 #undef STRUCT_MAP_ACCESSOR
    597 
    598 #define STRING_ACCESSOR(name, str)                                             \
    599   inline Handle<String> name() {                                               \
    600     return Handle<String>(BitCast<String**>(                                   \
    601         &isolate()->heap()->roots_[Heap::k##name##RootIndex]));                \
    602   }
    603   INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
    604 #undef STRING_ACCESSOR
    605 
    606   inline void set_string_table(Handle<StringTable> table) {
    607     isolate()->heap()->set_string_table(*table);
    608   }
    609 
    610   Handle<String> hidden_string() {
    611     return Handle<String>(&isolate()->heap()->hidden_string_);
    612   }
    613 
    614   // Allocates a new SharedFunctionInfo object.
    615   Handle<SharedFunctionInfo> NewSharedFunctionInfo(
    616       Handle<String> name,
    617       int number_of_literals,
    618       bool is_generator,
    619       Handle<Code> code,
    620       Handle<ScopeInfo> scope_info,
    621       Handle<FixedArray> feedback_vector);
    622   Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name,
    623                                                    MaybeHandle<Code> code);
    624 
    625   // Allocate a new type feedback vector
    626   Handle<FixedArray> NewTypeFeedbackVector(int slot_count);
    627 
    628   // Allocates a new JSMessageObject object.
    629   Handle<JSMessageObject> NewJSMessageObject(
    630       Handle<String> type,
    631       Handle<JSArray> arguments,
    632       int start_position,
    633       int end_position,
    634       Handle<Object> script,
    635       Handle<Object> stack_frames);
    636 
    637   Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared);
    638 
    639   // Return a map using the map cache in the native context.
    640   // The key the an ordered set of property names.
    641   Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context,
    642                                         Handle<FixedArray> keys);
    643 
    644   // Creates a new FixedArray that holds the data associated with the
    645   // atom regexp and stores it in the regexp.
    646   void SetRegExpAtomData(Handle<JSRegExp> regexp,
    647                          JSRegExp::Type type,
    648                          Handle<String> source,
    649                          JSRegExp::Flags flags,
    650                          Handle<Object> match_pattern);
    651 
    652   // Creates a new FixedArray that holds the data associated with the
    653   // irregexp regexp and stores it in the regexp.
    654   void SetRegExpIrregexpData(Handle<JSRegExp> regexp,
    655                              JSRegExp::Type type,
    656                              Handle<String> source,
    657                              JSRegExp::Flags flags,
    658                              int capture_count);
    659 
    660   // Returns the value for a known global constant (a property of the global
    661   // object which is neither configurable nor writable) like 'undefined'.
    662   // Returns a null handle when the given name is unknown.
    663   Handle<Object> GlobalConstantFor(Handle<String> name);
    664 
    665   // Converts the given boolean condition to JavaScript boolean value.
    666   Handle<Object> ToBoolean(bool value);
    667 
    668  private:
    669   Isolate* isolate() { return reinterpret_cast<Isolate*>(this); }
    670 
    671   // Creates a heap object based on the map. The fields of the heap object are
    672   // not initialized by New<>() functions. It's the responsibility of the caller
    673   // to do that.
    674   template<typename T>
    675   Handle<T> New(Handle<Map> map, AllocationSpace space);
    676 
    677   template<typename T>
    678   Handle<T> New(Handle<Map> map,
    679                 AllocationSpace space,
    680                 Handle<AllocationSite> allocation_site);
    681 
    682   // Creates a code object that is not yet fully initialized yet.
    683   inline Handle<Code> NewCodeRaw(int object_size, bool immovable);
    684 
    685   // Create a new map cache.
    686   Handle<MapCache> NewMapCache(int at_least_space_for);
    687 
    688   // Update the map cache in the native context with (keys, map)
    689   Handle<MapCache> AddToMapCache(Handle<Context> context,
    690                                  Handle<FixedArray> keys,
    691                                  Handle<Map> map);
    692 
    693   // Attempt to find the number in a small cache.  If we finds it, return
    694   // the string representation of the number.  Otherwise return undefined.
    695   Handle<Object> GetNumberStringCache(Handle<Object> number);
    696 
    697   // Update the cache with a new number-string pair.
    698   void SetNumberStringCache(Handle<Object> number, Handle<String> string);
    699 
    700   // Initializes a function with a shared part and prototype.
    701   // Note: this code was factored out of NewFunction such that other parts of
    702   // the VM could use it. Specifically, a function that creates instances of
    703   // type JS_FUNCTION_TYPE benefit from the use of this function.
    704   inline void InitializeFunction(Handle<JSFunction> function,
    705                                  Handle<SharedFunctionInfo> info,
    706                                  Handle<Context> context);
    707 
    708   // Creates a function initialized with a shared part.
    709   Handle<JSFunction> NewFunction(Handle<Map> map,
    710                                  Handle<SharedFunctionInfo> info,
    711                                  Handle<Context> context,
    712                                  PretenureFlag pretenure = TENURED);
    713 
    714   Handle<JSFunction> NewFunction(Handle<Map> map,
    715                                  Handle<String> name,
    716                                  MaybeHandle<Code> maybe_code);
    717 };
    718 
    719 } }  // namespace v8::internal
    720 
    721 #endif  // V8_FACTORY_H_
    722