Home | History | Annotate | Download | only in src
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "v8.h"
     29 
     30 #include "api.h"
     31 #include "debug.h"
     32 #include "execution.h"
     33 #include "factory.h"
     34 #include "isolate-inl.h"
     35 #include "macro-assembler.h"
     36 #include "objects.h"
     37 #include "objects-visiting.h"
     38 #include "platform.h"
     39 #include "scopeinfo.h"
     40 
     41 namespace v8 {
     42 namespace internal {
     43 
     44 
     45 Handle<Box> Factory::NewBox(Handle<Object> value, PretenureFlag pretenure) {
     46   CALL_HEAP_FUNCTION(
     47       isolate(),
     48       isolate()->heap()->AllocateBox(*value, pretenure),
     49       Box);
     50 }
     51 
     52 
     53 Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
     54   ASSERT(0 <= size);
     55   CALL_HEAP_FUNCTION(
     56       isolate(),
     57       isolate()->heap()->AllocateFixedArray(size, pretenure),
     58       FixedArray);
     59 }
     60 
     61 
     62 Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
     63                                                    PretenureFlag pretenure) {
     64   ASSERT(0 <= size);
     65   CALL_HEAP_FUNCTION(
     66       isolate(),
     67       isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
     68       FixedArray);
     69 }
     70 
     71 
     72 Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
     73                                                       PretenureFlag pretenure) {
     74   ASSERT(0 <= size);
     75   CALL_HEAP_FUNCTION(
     76       isolate(),
     77       isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
     78       FixedDoubleArray);
     79 }
     80 
     81 
     82 Handle<NameDictionary> Factory::NewNameDictionary(int at_least_space_for) {
     83   ASSERT(0 <= at_least_space_for);
     84   CALL_HEAP_FUNCTION(isolate(),
     85                      NameDictionary::Allocate(isolate()->heap(),
     86                                               at_least_space_for),
     87                      NameDictionary);
     88 }
     89 
     90 
     91 Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
     92     int at_least_space_for) {
     93   ASSERT(0 <= at_least_space_for);
     94   CALL_HEAP_FUNCTION(isolate(),
     95                      SeededNumberDictionary::Allocate(isolate()->heap(),
     96                                                       at_least_space_for),
     97                      SeededNumberDictionary);
     98 }
     99 
    100 
    101 Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
    102     int at_least_space_for) {
    103   ASSERT(0 <= at_least_space_for);
    104   CALL_HEAP_FUNCTION(isolate(),
    105                      UnseededNumberDictionary::Allocate(isolate()->heap(),
    106                                                         at_least_space_for),
    107                      UnseededNumberDictionary);
    108 }
    109 
    110 
    111 Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
    112   ASSERT(0 <= at_least_space_for);
    113   CALL_HEAP_FUNCTION(isolate(),
    114                      ObjectHashSet::Allocate(isolate()->heap(),
    115                                              at_least_space_for),
    116                      ObjectHashSet);
    117 }
    118 
    119 
    120 Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
    121   ASSERT(0 <= at_least_space_for);
    122   CALL_HEAP_FUNCTION(isolate(),
    123                      ObjectHashTable::Allocate(isolate()->heap(),
    124                                                at_least_space_for),
    125                      ObjectHashTable);
    126 }
    127 
    128 
    129 Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
    130                                                     int slack) {
    131   ASSERT(0 <= number_of_descriptors);
    132   CALL_HEAP_FUNCTION(isolate(),
    133                      DescriptorArray::Allocate(number_of_descriptors, slack),
    134                      DescriptorArray);
    135 }
    136 
    137 
    138 Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
    139     int deopt_entry_count,
    140     PretenureFlag pretenure) {
    141   ASSERT(deopt_entry_count > 0);
    142   CALL_HEAP_FUNCTION(isolate(),
    143                      DeoptimizationInputData::Allocate(deopt_entry_count,
    144                                                        pretenure),
    145                      DeoptimizationInputData);
    146 }
    147 
    148 
    149 Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
    150     int deopt_entry_count,
    151     PretenureFlag pretenure) {
    152   ASSERT(deopt_entry_count > 0);
    153   CALL_HEAP_FUNCTION(isolate(),
    154                      DeoptimizationOutputData::Allocate(deopt_entry_count,
    155                                                         pretenure),
    156                      DeoptimizationOutputData);
    157 }
    158 
    159 
    160 Handle<AccessorPair> Factory::NewAccessorPair() {
    161   CALL_HEAP_FUNCTION(isolate(),
    162                      isolate()->heap()->AllocateAccessorPair(),
    163                      AccessorPair);
    164 }
    165 
    166 
    167 Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
    168   CALL_HEAP_FUNCTION(isolate(),
    169                      isolate()->heap()->AllocateTypeFeedbackInfo(),
    170                      TypeFeedbackInfo);
    171 }
    172 
    173 
    174 // Internalized strings are created in the old generation (data space).
    175 Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
    176   CALL_HEAP_FUNCTION(isolate(),
    177                      isolate()->heap()->InternalizeUtf8String(string),
    178                      String);
    179 }
    180 
    181 
    182 // Internalized strings are created in the old generation (data space).
    183 Handle<String> Factory::InternalizeString(Handle<String> string) {
    184   CALL_HEAP_FUNCTION(isolate(),
    185                      isolate()->heap()->InternalizeString(*string),
    186                      String);
    187 }
    188 
    189 
    190 Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
    191   CALL_HEAP_FUNCTION(isolate(),
    192                      isolate()->heap()->InternalizeOneByteString(string),
    193                      String);
    194 }
    195 
    196 
    197 Handle<String> Factory::InternalizeOneByteString(
    198     Handle<SeqOneByteString> string, int from, int length) {
    199   CALL_HEAP_FUNCTION(isolate(),
    200                      isolate()->heap()->InternalizeOneByteString(
    201                          string, from, length),
    202                      String);
    203 }
    204 
    205 
    206 Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
    207   CALL_HEAP_FUNCTION(isolate(),
    208                      isolate()->heap()->InternalizeTwoByteString(string),
    209                      String);
    210 }
    211 
    212 
    213 Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
    214                                              PretenureFlag pretenure) {
    215   CALL_HEAP_FUNCTION(
    216       isolate(),
    217       isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
    218       String);
    219 }
    220 
    221 Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
    222                                           PretenureFlag pretenure) {
    223   CALL_HEAP_FUNCTION(
    224       isolate(),
    225       isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
    226       String);
    227 }
    228 
    229 
    230 Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
    231                                              PretenureFlag pretenure) {
    232   CALL_HEAP_FUNCTION(
    233       isolate(),
    234       isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
    235       String);
    236 }
    237 
    238 
    239 Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
    240                                                   PretenureFlag pretenure) {
    241   CALL_HEAP_FUNCTION(
    242       isolate(),
    243       isolate()->heap()->AllocateRawOneByteString(length, pretenure),
    244       SeqOneByteString);
    245 }
    246 
    247 
    248 Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
    249                                                       PretenureFlag pretenure) {
    250   CALL_HEAP_FUNCTION(
    251       isolate(),
    252       isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
    253       SeqTwoByteString);
    254 }
    255 
    256 
    257 Handle<String> Factory::NewConsString(Handle<String> first,
    258                                       Handle<String> second) {
    259   CALL_HEAP_FUNCTION(isolate(),
    260                      isolate()->heap()->AllocateConsString(*first, *second),
    261                      String);
    262 }
    263 
    264 
    265 template<typename SinkChar, typename StringType>
    266 Handle<String> ConcatStringContent(Handle<StringType> result,
    267                                    Handle<String> first,
    268                                    Handle<String> second) {
    269   DisallowHeapAllocation pointer_stays_valid;
    270   SinkChar* sink = result->GetChars();
    271   String::WriteToFlat(*first, sink, 0, first->length());
    272   String::WriteToFlat(*second, sink + first->length(), 0, second->length());
    273   return result;
    274 }
    275 
    276 
    277 Handle<String> Factory::NewFlatConcatString(Handle<String> first,
    278                                             Handle<String> second) {
    279   int total_length = first->length() + second->length();
    280   if (first->IsOneByteRepresentationUnderneath() &&
    281       second->IsOneByteRepresentationUnderneath()) {
    282     return ConcatStringContent<uint8_t>(
    283         NewRawOneByteString(total_length), first, second);
    284   } else {
    285     return ConcatStringContent<uc16>(
    286         NewRawTwoByteString(total_length), first, second);
    287   }
    288 }
    289 
    290 
    291 Handle<String> Factory::NewSubString(Handle<String> str,
    292                                      int begin,
    293                                      int end) {
    294   CALL_HEAP_FUNCTION(isolate(),
    295                      str->SubString(begin, end),
    296                      String);
    297 }
    298 
    299 
    300 Handle<String> Factory::NewProperSubString(Handle<String> str,
    301                                            int begin,
    302                                            int end) {
    303   ASSERT(begin > 0 || end < str->length());
    304   CALL_HEAP_FUNCTION(isolate(),
    305                      isolate()->heap()->AllocateSubString(*str, begin, end),
    306                      String);
    307 }
    308 
    309 
    310 Handle<String> Factory::NewExternalStringFromAscii(
    311     const ExternalAsciiString::Resource* resource) {
    312   CALL_HEAP_FUNCTION(
    313       isolate(),
    314       isolate()->heap()->AllocateExternalStringFromAscii(resource),
    315       String);
    316 }
    317 
    318 
    319 Handle<String> Factory::NewExternalStringFromTwoByte(
    320     const ExternalTwoByteString::Resource* resource) {
    321   CALL_HEAP_FUNCTION(
    322       isolate(),
    323       isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
    324       String);
    325 }
    326 
    327 
    328 Handle<Symbol> Factory::NewSymbol() {
    329   CALL_HEAP_FUNCTION(
    330       isolate(),
    331       isolate()->heap()->AllocateSymbol(),
    332       Symbol);
    333 }
    334 
    335 
    336 Handle<Context> Factory::NewNativeContext() {
    337   CALL_HEAP_FUNCTION(
    338       isolate(),
    339       isolate()->heap()->AllocateNativeContext(),
    340       Context);
    341 }
    342 
    343 
    344 Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
    345                                           Handle<ScopeInfo> scope_info) {
    346   CALL_HEAP_FUNCTION(
    347       isolate(),
    348       isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
    349       Context);
    350 }
    351 
    352 
    353 Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
    354   CALL_HEAP_FUNCTION(
    355       isolate(),
    356       isolate()->heap()->AllocateModuleContext(*scope_info),
    357       Context);
    358 }
    359 
    360 
    361 Handle<Context> Factory::NewFunctionContext(int length,
    362                                             Handle<JSFunction> function) {
    363   CALL_HEAP_FUNCTION(
    364       isolate(),
    365       isolate()->heap()->AllocateFunctionContext(length, *function),
    366       Context);
    367 }
    368 
    369 
    370 Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
    371                                          Handle<Context> previous,
    372                                          Handle<String> name,
    373                                          Handle<Object> thrown_object) {
    374   CALL_HEAP_FUNCTION(
    375       isolate(),
    376       isolate()->heap()->AllocateCatchContext(*function,
    377                                               *previous,
    378                                               *name,
    379                                               *thrown_object),
    380       Context);
    381 }
    382 
    383 
    384 Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
    385                                         Handle<Context> previous,
    386                                         Handle<JSObject> extension) {
    387   CALL_HEAP_FUNCTION(
    388       isolate(),
    389       isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
    390       Context);
    391 }
    392 
    393 
    394 Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
    395                                          Handle<Context> previous,
    396                                          Handle<ScopeInfo> scope_info) {
    397   CALL_HEAP_FUNCTION(
    398       isolate(),
    399       isolate()->heap()->AllocateBlockContext(*function,
    400                                               *previous,
    401                                               *scope_info),
    402       Context);
    403 }
    404 
    405 
    406 Handle<Struct> Factory::NewStruct(InstanceType type) {
    407   CALL_HEAP_FUNCTION(
    408       isolate(),
    409       isolate()->heap()->AllocateStruct(type),
    410       Struct);
    411 }
    412 
    413 
    414 Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() {
    415   return Handle<DeclaredAccessorDescriptor>::cast(
    416       NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE));
    417 }
    418 
    419 
    420 Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
    421   Handle<DeclaredAccessorInfo> info =
    422       Handle<DeclaredAccessorInfo>::cast(
    423           NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
    424   info->set_flag(0);  // Must clear the flag, it was initialized as undefined.
    425   return info;
    426 }
    427 
    428 
    429 Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
    430   Handle<ExecutableAccessorInfo> info =
    431       Handle<ExecutableAccessorInfo>::cast(
    432           NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
    433   info->set_flag(0);  // Must clear the flag, it was initialized as undefined.
    434   return info;
    435 }
    436 
    437 
    438 Handle<Script> Factory::NewScript(Handle<String> source) {
    439   // Generate id for this script.
    440   Heap* heap = isolate()->heap();
    441   int id = heap->last_script_id()->value() + 1;
    442   if (!Smi::IsValid(id) || id < 0) id = 1;
    443   heap->set_last_script_id(Smi::FromInt(id));
    444 
    445   // Create and initialize script object.
    446   Handle<Foreign> wrapper = NewForeign(0, TENURED);
    447   Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
    448   script->set_source(*source);
    449   script->set_name(heap->undefined_value());
    450   script->set_id(Smi::FromInt(id));
    451   script->set_line_offset(Smi::FromInt(0));
    452   script->set_column_offset(Smi::FromInt(0));
    453   script->set_data(heap->undefined_value());
    454   script->set_context_data(heap->undefined_value());
    455   script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
    456   script->set_wrapper(*wrapper);
    457   script->set_line_ends(heap->undefined_value());
    458   script->set_eval_from_shared(heap->undefined_value());
    459   script->set_eval_from_instructions_offset(Smi::FromInt(0));
    460   script->set_flags(Smi::FromInt(0));
    461 
    462   return script;
    463 }
    464 
    465 
    466 Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
    467   CALL_HEAP_FUNCTION(isolate(),
    468                      isolate()->heap()->AllocateForeign(addr, pretenure),
    469                      Foreign);
    470 }
    471 
    472 
    473 Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
    474   return NewForeign((Address) desc, TENURED);
    475 }
    476 
    477 
    478 Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
    479   ASSERT(0 <= length);
    480   CALL_HEAP_FUNCTION(
    481       isolate(),
    482       isolate()->heap()->AllocateByteArray(length, pretenure),
    483       ByteArray);
    484 }
    485 
    486 
    487 Handle<ExternalArray> Factory::NewExternalArray(int length,
    488                                                 ExternalArrayType array_type,
    489                                                 void* external_pointer,
    490                                                 PretenureFlag pretenure) {
    491   ASSERT(0 <= length);
    492   CALL_HEAP_FUNCTION(
    493       isolate(),
    494       isolate()->heap()->AllocateExternalArray(length,
    495                                                array_type,
    496                                                external_pointer,
    497                                                pretenure),
    498       ExternalArray);
    499 }
    500 
    501 
    502 Handle<Cell> Factory::NewCell(Handle<Object> value) {
    503   AllowDeferredHandleDereference convert_to_cell;
    504   CALL_HEAP_FUNCTION(
    505       isolate(),
    506       isolate()->heap()->AllocateCell(*value),
    507       Cell);
    508 }
    509 
    510 
    511 Handle<PropertyCell> Factory::NewPropertyCell(Handle<Object> value) {
    512   AllowDeferredHandleDereference convert_to_cell;
    513   CALL_HEAP_FUNCTION(
    514       isolate(),
    515       isolate()->heap()->AllocatePropertyCell(*value),
    516       PropertyCell);
    517 }
    518 
    519 
    520 Handle<AllocationSite> Factory::NewAllocationSite() {
    521   CALL_HEAP_FUNCTION(
    522       isolate(),
    523       isolate()->heap()->AllocateAllocationSite(),
    524       AllocationSite);
    525 }
    526 
    527 
    528 Handle<Map> Factory::NewMap(InstanceType type,
    529                             int instance_size,
    530                             ElementsKind elements_kind) {
    531   CALL_HEAP_FUNCTION(
    532       isolate(),
    533       isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
    534       Map);
    535 }
    536 
    537 
    538 Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
    539   CALL_HEAP_FUNCTION(
    540       isolate(),
    541       isolate()->heap()->AllocateFunctionPrototype(*function),
    542       JSObject);
    543 }
    544 
    545 
    546 Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
    547   CALL_HEAP_FUNCTION(
    548       isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
    549 }
    550 
    551 
    552 Handle<Map> Factory::CopyMap(Handle<Map> src,
    553                              int extra_inobject_properties) {
    554   Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
    555   // Check that we do not overflow the instance size when adding the
    556   // extra inobject properties.
    557   int instance_size_delta = extra_inobject_properties * kPointerSize;
    558   int max_instance_size_delta =
    559       JSObject::kMaxInstanceSize - copy->instance_size();
    560   if (instance_size_delta > max_instance_size_delta) {
    561     // If the instance size overflows, we allocate as many properties
    562     // as we can as inobject properties.
    563     instance_size_delta = max_instance_size_delta;
    564     extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
    565   }
    566   // Adjust the map with the extra inobject properties.
    567   int inobject_properties =
    568       copy->inobject_properties() + extra_inobject_properties;
    569   copy->set_inobject_properties(inobject_properties);
    570   copy->set_unused_property_fields(inobject_properties);
    571   copy->set_instance_size(copy->instance_size() + instance_size_delta);
    572   copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
    573   return copy;
    574 }
    575 
    576 
    577 Handle<Map> Factory::CopyMap(Handle<Map> src) {
    578   CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
    579 }
    580 
    581 
    582 Handle<Map> Factory::GetElementsTransitionMap(
    583     Handle<JSObject> src,
    584     ElementsKind elements_kind) {
    585   Isolate* i = isolate();
    586   CALL_HEAP_FUNCTION(i,
    587                      src->GetElementsTransitionMap(i, elements_kind),
    588                      Map);
    589 }
    590 
    591 
    592 Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
    593   CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
    594 }
    595 
    596 
    597 Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
    598                                                int new_length) {
    599   CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
    600 }
    601 
    602 
    603 Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
    604     Handle<FixedDoubleArray> array) {
    605   CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
    606 }
    607 
    608 
    609 Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
    610     Handle<SharedFunctionInfo> function_info,
    611     Handle<Map> function_map,
    612     PretenureFlag pretenure) {
    613   CALL_HEAP_FUNCTION(
    614       isolate(),
    615       isolate()->heap()->AllocateFunction(*function_map,
    616                                           *function_info,
    617                                           isolate()->heap()->the_hole_value(),
    618                                           pretenure),
    619                      JSFunction);
    620 }
    621 
    622 
    623 static Handle<Map> MapForNewFunction(Isolate *isolate,
    624                                      Handle<SharedFunctionInfo> function_info) {
    625   Context *context = isolate->context()->native_context();
    626   int map_index = Context::FunctionMapIndex(function_info->language_mode(),
    627                                             function_info->is_generator());
    628   return Handle<Map>(Map::cast(context->get(map_index)));
    629 }
    630 
    631 
    632 Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
    633     Handle<SharedFunctionInfo> function_info,
    634     Handle<Context> context,
    635     PretenureFlag pretenure) {
    636   Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
    637       function_info,
    638       MapForNewFunction(isolate(), function_info),
    639       pretenure);
    640 
    641   if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
    642     function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
    643   }
    644 
    645   result->set_context(*context);
    646 
    647   int index = function_info->SearchOptimizedCodeMap(context->native_context());
    648   if (!function_info->bound() && index < 0) {
    649     int number_of_literals = function_info->num_literals();
    650     Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
    651     if (number_of_literals > 0) {
    652       // Store the native context in the literals array prefix. This
    653       // context will be used when creating object, regexp and array
    654       // literals in this function.
    655       literals->set(JSFunction::kLiteralNativeContextIndex,
    656                     context->native_context());
    657     }
    658     result->set_literals(*literals);
    659   }
    660 
    661   if (index > 0) {
    662     // Caching of optimized code enabled and optimized code found.
    663     function_info->InstallFromOptimizedCodeMap(*result, index);
    664     return result;
    665   }
    666 
    667   if (V8::UseCrankshaft() &&
    668       FLAG_always_opt &&
    669       result->is_compiled() &&
    670       !function_info->is_toplevel() &&
    671       function_info->allows_lazy_compilation() &&
    672       !function_info->optimization_disabled() &&
    673       !isolate()->DebuggerHasBreakPoints()) {
    674     result->MarkForLazyRecompilation();
    675   }
    676   return result;
    677 }
    678 
    679 
    680 Handle<Object> Factory::NewNumber(double value,
    681                                   PretenureFlag pretenure) {
    682   CALL_HEAP_FUNCTION(
    683       isolate(),
    684       isolate()->heap()->NumberFromDouble(value, pretenure), Object);
    685 }
    686 
    687 
    688 Handle<Object> Factory::NewNumberFromInt(int32_t value,
    689                                          PretenureFlag pretenure) {
    690   CALL_HEAP_FUNCTION(
    691       isolate(),
    692       isolate()->heap()->NumberFromInt32(value, pretenure), Object);
    693 }
    694 
    695 
    696 Handle<Object> Factory::NewNumberFromUint(uint32_t value,
    697                                          PretenureFlag pretenure) {
    698   CALL_HEAP_FUNCTION(
    699       isolate(),
    700       isolate()->heap()->NumberFromUint32(value, pretenure), Object);
    701 }
    702 
    703 
    704 Handle<HeapNumber> Factory::NewHeapNumber(double value,
    705                                           PretenureFlag pretenure) {
    706   CALL_HEAP_FUNCTION(
    707       isolate(),
    708       isolate()->heap()->AllocateHeapNumber(value, pretenure), HeapNumber);
    709 }
    710 
    711 
    712 Handle<JSObject> Factory::NewNeanderObject() {
    713   CALL_HEAP_FUNCTION(
    714       isolate(),
    715       isolate()->heap()->AllocateJSObjectFromMap(
    716           isolate()->heap()->neander_map()),
    717       JSObject);
    718 }
    719 
    720 
    721 Handle<Object> Factory::NewTypeError(const char* message,
    722                                      Vector< Handle<Object> > args) {
    723   return NewError("MakeTypeError", message, args);
    724 }
    725 
    726 
    727 Handle<Object> Factory::NewTypeError(Handle<String> message) {
    728   return NewError("$TypeError", message);
    729 }
    730 
    731 
    732 Handle<Object> Factory::NewRangeError(const char* message,
    733                                       Vector< Handle<Object> > args) {
    734   return NewError("MakeRangeError", message, args);
    735 }
    736 
    737 
    738 Handle<Object> Factory::NewRangeError(Handle<String> message) {
    739   return NewError("$RangeError", message);
    740 }
    741 
    742 
    743 Handle<Object> Factory::NewSyntaxError(const char* message,
    744                                        Handle<JSArray> args) {
    745   return NewError("MakeSyntaxError", message, args);
    746 }
    747 
    748 
    749 Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
    750   return NewError("$SyntaxError", message);
    751 }
    752 
    753 
    754 Handle<Object> Factory::NewReferenceError(const char* message,
    755                                           Vector< Handle<Object> > args) {
    756   return NewError("MakeReferenceError", message, args);
    757 }
    758 
    759 
    760 Handle<Object> Factory::NewReferenceError(Handle<String> message) {
    761   return NewError("$ReferenceError", message);
    762 }
    763 
    764 
    765 Handle<Object> Factory::NewError(const char* maker,
    766                                  const char* message,
    767                                  Vector< Handle<Object> > args) {
    768   // Instantiate a closeable HandleScope for EscapeFrom.
    769   v8::HandleScope scope(reinterpret_cast<v8::Isolate*>(isolate()));
    770   Handle<FixedArray> array = NewFixedArray(args.length());
    771   for (int i = 0; i < args.length(); i++) {
    772     array->set(i, *args[i]);
    773   }
    774   Handle<JSArray> object = NewJSArrayWithElements(array);
    775   Handle<Object> result = NewError(maker, message, object);
    776   return result.EscapeFrom(&scope);
    777 }
    778 
    779 
    780 Handle<Object> Factory::NewEvalError(const char* message,
    781                                      Vector< Handle<Object> > args) {
    782   return NewError("MakeEvalError", message, args);
    783 }
    784 
    785 
    786 Handle<Object> Factory::NewError(const char* message,
    787                                  Vector< Handle<Object> > args) {
    788   return NewError("MakeError", message, args);
    789 }
    790 
    791 
    792 Handle<String> Factory::EmergencyNewError(const char* message,
    793                                           Handle<JSArray> args) {
    794   const int kBufferSize = 1000;
    795   char buffer[kBufferSize];
    796   size_t space = kBufferSize;
    797   char* p = &buffer[0];
    798 
    799   Vector<char> v(buffer, kBufferSize);
    800   OS::StrNCpy(v, message, space);
    801   space -= Min(space, strlen(message));
    802   p = &buffer[kBufferSize] - space;
    803 
    804   for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
    805     if (space > 0) {
    806       *p++ = ' ';
    807       space--;
    808       if (space > 0) {
    809         MaybeObject* maybe_arg = args->GetElement(i);
    810         Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
    811         const char* arg = *arg_str->ToCString();
    812         Vector<char> v2(p, static_cast<int>(space));
    813         OS::StrNCpy(v2, arg, space);
    814         space -= Min(space, strlen(arg));
    815         p = &buffer[kBufferSize] - space;
    816       }
    817     }
    818   }
    819   if (space > 0) {
    820     *p = '\0';
    821   } else {
    822     buffer[kBufferSize - 1] = '\0';
    823   }
    824   Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
    825   return error_string;
    826 }
    827 
    828 
    829 Handle<Object> Factory::NewError(const char* maker,
    830                                  const char* message,
    831                                  Handle<JSArray> args) {
    832   Handle<String> make_str = InternalizeUtf8String(maker);
    833   Handle<Object> fun_obj(
    834       isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
    835       isolate());
    836   // If the builtins haven't been properly configured yet this error
    837   // constructor may not have been defined.  Bail out.
    838   if (!fun_obj->IsJSFunction()) {
    839     return EmergencyNewError(message, args);
    840   }
    841   Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
    842   Handle<Object> message_obj = InternalizeUtf8String(message);
    843   Handle<Object> argv[] = { message_obj, args };
    844 
    845   // Invoke the JavaScript factory method. If an exception is thrown while
    846   // running the factory method, use the exception as the result.
    847   bool caught_exception;
    848   Handle<Object> result = Execution::TryCall(fun,
    849                                              isolate()->js_builtins_object(),
    850                                              ARRAY_SIZE(argv),
    851                                              argv,
    852                                              &caught_exception);
    853   return result;
    854 }
    855 
    856 
    857 Handle<Object> Factory::NewError(Handle<String> message) {
    858   return NewError("$Error", message);
    859 }
    860 
    861 
    862 Handle<Object> Factory::NewError(const char* constructor,
    863                                  Handle<String> message) {
    864   Handle<String> constr = InternalizeUtf8String(constructor);
    865   Handle<JSFunction> fun = Handle<JSFunction>(
    866       JSFunction::cast(isolate()->js_builtins_object()->
    867                        GetPropertyNoExceptionThrown(*constr)));
    868   Handle<Object> argv[] = { message };
    869 
    870   // Invoke the JavaScript factory method. If an exception is thrown while
    871   // running the factory method, use the exception as the result.
    872   bool caught_exception;
    873   Handle<Object> result = Execution::TryCall(fun,
    874                                              isolate()->js_builtins_object(),
    875                                              ARRAY_SIZE(argv),
    876                                              argv,
    877                                              &caught_exception);
    878   return result;
    879 }
    880 
    881 
    882 Handle<JSFunction> Factory::NewFunction(Handle<String> name,
    883                                         InstanceType type,
    884                                         int instance_size,
    885                                         Handle<Code> code,
    886                                         bool force_initial_map) {
    887   // Allocate the function
    888   Handle<JSFunction> function = NewFunction(name, the_hole_value());
    889 
    890   // Set up the code pointer in both the shared function info and in
    891   // the function itself.
    892   function->shared()->set_code(*code);
    893   function->set_code(*code);
    894 
    895   if (force_initial_map ||
    896       type != JS_OBJECT_TYPE ||
    897       instance_size != JSObject::kHeaderSize) {
    898     Handle<Map> initial_map = NewMap(type, instance_size);
    899     Handle<JSObject> prototype = NewFunctionPrototype(function);
    900     initial_map->set_prototype(*prototype);
    901     function->set_initial_map(*initial_map);
    902     initial_map->set_constructor(*function);
    903   } else {
    904     ASSERT(!function->has_initial_map());
    905     ASSERT(!function->has_prototype());
    906   }
    907 
    908   return function;
    909 }
    910 
    911 
    912 Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
    913                                                      InstanceType type,
    914                                                      int instance_size,
    915                                                      Handle<JSObject> prototype,
    916                                                      Handle<Code> code,
    917                                                      bool force_initial_map) {
    918   // Allocate the function.
    919   Handle<JSFunction> function = NewFunction(name, prototype);
    920 
    921   // Set up the code pointer in both the shared function info and in
    922   // the function itself.
    923   function->shared()->set_code(*code);
    924   function->set_code(*code);
    925 
    926   if (force_initial_map ||
    927       type != JS_OBJECT_TYPE ||
    928       instance_size != JSObject::kHeaderSize) {
    929     Handle<Map> initial_map = NewMap(type,
    930                                      instance_size,
    931                                      GetInitialFastElementsKind());
    932     function->set_initial_map(*initial_map);
    933     initial_map->set_constructor(*function);
    934   }
    935 
    936   JSFunction::SetPrototype(function, prototype);
    937   return function;
    938 }
    939 
    940 
    941 Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
    942                                                         Handle<Code> code) {
    943   Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
    944                                                             CLASSIC_MODE);
    945   function->shared()->set_code(*code);
    946   function->set_code(*code);
    947   ASSERT(!function->has_initial_map());
    948   ASSERT(!function->has_prototype());
    949   return function;
    950 }
    951 
    952 
    953 Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
    954   CALL_HEAP_FUNCTION(
    955       isolate(),
    956       isolate()->heap()->AllocateScopeInfo(length),
    957       ScopeInfo);
    958 }
    959 
    960 
    961 Handle<JSObject> Factory::NewExternal(void* value) {
    962   CALL_HEAP_FUNCTION(isolate(),
    963                      isolate()->heap()->AllocateExternal(value),
    964                      JSObject);
    965 }
    966 
    967 
    968 Handle<Code> Factory::NewCode(const CodeDesc& desc,
    969                               Code::Flags flags,
    970                               Handle<Object> self_ref,
    971                               bool immovable,
    972                               bool crankshafted) {
    973   CALL_HEAP_FUNCTION(isolate(),
    974                      isolate()->heap()->CreateCode(
    975                          desc, flags, self_ref, immovable, crankshafted),
    976                      Code);
    977 }
    978 
    979 
    980 Handle<Code> Factory::CopyCode(Handle<Code> code) {
    981   CALL_HEAP_FUNCTION(isolate(),
    982                      isolate()->heap()->CopyCode(*code),
    983                      Code);
    984 }
    985 
    986 
    987 Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
    988   CALL_HEAP_FUNCTION(isolate(),
    989                      isolate()->heap()->CopyCode(*code, reloc_info),
    990                      Code);
    991 }
    992 
    993 
    994 Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
    995   CALL_HEAP_FUNCTION(isolate(),
    996                      isolate()->heap()->InternalizeString(*value), String);
    997 }
    998 
    999 
   1000 Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
   1001                                       PretenureFlag pretenure) {
   1002   CALL_HEAP_FUNCTION(
   1003       isolate(),
   1004       isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
   1005 }
   1006 
   1007 
   1008 Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
   1009                                       Handle<ScopeInfo> scope_info) {
   1010   CALL_HEAP_FUNCTION(
   1011       isolate(),
   1012       isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
   1013 }
   1014 
   1015 
   1016 Handle<GlobalObject> Factory::NewGlobalObject(
   1017     Handle<JSFunction> constructor) {
   1018   CALL_HEAP_FUNCTION(isolate(),
   1019                      isolate()->heap()->AllocateGlobalObject(*constructor),
   1020                      GlobalObject);
   1021 }
   1022 
   1023 
   1024 
   1025 Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
   1026                                              PretenureFlag pretenure,
   1027                                              bool alloc_props) {
   1028   CALL_HEAP_FUNCTION(
   1029       isolate(),
   1030       isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure, alloc_props),
   1031       JSObject);
   1032 }
   1033 
   1034 
   1035 Handle<JSArray> Factory::NewJSArray(int capacity,
   1036                                     ElementsKind elements_kind,
   1037                                     PretenureFlag pretenure) {
   1038   if (capacity != 0) {
   1039     elements_kind = GetHoleyElementsKind(elements_kind);
   1040   }
   1041   CALL_HEAP_FUNCTION(isolate(),
   1042                      isolate()->heap()->AllocateJSArrayAndStorage(
   1043                          elements_kind,
   1044                          0,
   1045                          capacity,
   1046                          INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
   1047                          pretenure),
   1048                      JSArray);
   1049 }
   1050 
   1051 
   1052 Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
   1053                                                 ElementsKind elements_kind,
   1054                                                 PretenureFlag pretenure) {
   1055   CALL_HEAP_FUNCTION(
   1056       isolate(),
   1057       isolate()->heap()->AllocateJSArrayWithElements(*elements,
   1058                                                      elements_kind,
   1059                                                      elements->length(),
   1060                                                      pretenure),
   1061       JSArray);
   1062 }
   1063 
   1064 
   1065 void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
   1066                                            int capacity,
   1067                                            int length) {
   1068   ElementsAccessor* accessor = array->GetElementsAccessor();
   1069   CALL_HEAP_FUNCTION_VOID(
   1070       isolate(),
   1071       accessor->SetCapacityAndLength(*array, capacity, length));
   1072 }
   1073 
   1074 
   1075 void Factory::SetContent(Handle<JSArray> array,
   1076                          Handle<FixedArrayBase> elements) {
   1077   CALL_HEAP_FUNCTION_VOID(
   1078       isolate(),
   1079       array->SetContent(*elements));
   1080 }
   1081 
   1082 
   1083 void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
   1084   CALL_HEAP_FUNCTION_VOID(
   1085       isolate(),
   1086       array->EnsureCanContainHeapObjectElements());
   1087 }
   1088 
   1089 
   1090 void Factory::EnsureCanContainElements(Handle<JSArray> array,
   1091                                        Handle<FixedArrayBase> elements,
   1092                                        uint32_t length,
   1093                                        EnsureElementsMode mode) {
   1094   CALL_HEAP_FUNCTION_VOID(
   1095       isolate(),
   1096       array->EnsureCanContainElements(*elements, length, mode));
   1097 }
   1098 
   1099 
   1100 Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
   1101   Handle<JSFunction> array_buffer_fun(
   1102       isolate()->context()->native_context()->array_buffer_fun());
   1103   CALL_HEAP_FUNCTION(
   1104       isolate(),
   1105       isolate()->heap()->AllocateJSObject(*array_buffer_fun),
   1106       JSArrayBuffer);
   1107 }
   1108 
   1109 
   1110 Handle<JSDataView> Factory::NewJSDataView() {
   1111   Handle<JSFunction> data_view_fun(
   1112       isolate()->context()->native_context()->data_view_fun());
   1113   CALL_HEAP_FUNCTION(
   1114       isolate(),
   1115       isolate()->heap()->AllocateJSObject(*data_view_fun),
   1116       JSDataView);
   1117 }
   1118 
   1119 
   1120 static JSFunction* GetTypedArrayFun(ExternalArrayType type,
   1121                                     Isolate* isolate) {
   1122   Context* native_context = isolate->context()->native_context();
   1123   switch (type) {
   1124     case kExternalUnsignedByteArray:
   1125       return native_context->uint8_array_fun();
   1126 
   1127     case kExternalByteArray:
   1128       return native_context->int8_array_fun();
   1129 
   1130     case kExternalUnsignedShortArray:
   1131       return native_context->uint16_array_fun();
   1132 
   1133     case kExternalShortArray:
   1134       return native_context->int16_array_fun();
   1135 
   1136     case kExternalUnsignedIntArray:
   1137       return native_context->uint32_array_fun();
   1138 
   1139     case kExternalIntArray:
   1140       return native_context->int32_array_fun();
   1141 
   1142     case kExternalFloatArray:
   1143       return native_context->float_array_fun();
   1144 
   1145     case kExternalDoubleArray:
   1146       return native_context->double_array_fun();
   1147 
   1148     case kExternalPixelArray:
   1149       return native_context->uint8c_array_fun();
   1150 
   1151     default:
   1152       UNREACHABLE();
   1153       return NULL;
   1154   }
   1155 }
   1156 
   1157 
   1158 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) {
   1159   Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate()));
   1160 
   1161   CALL_HEAP_FUNCTION(
   1162       isolate(),
   1163       isolate()->heap()->AllocateJSObject(*typed_array_fun_handle),
   1164       JSTypedArray);
   1165 }
   1166 
   1167 
   1168 Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
   1169                                     Handle<Object> prototype) {
   1170   CALL_HEAP_FUNCTION(
   1171       isolate(),
   1172       isolate()->heap()->AllocateJSProxy(*handler, *prototype),
   1173       JSProxy);
   1174 }
   1175 
   1176 
   1177 void Factory::BecomeJSObject(Handle<JSReceiver> object) {
   1178   CALL_HEAP_FUNCTION_VOID(
   1179       isolate(),
   1180       isolate()->heap()->ReinitializeJSReceiver(
   1181           *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
   1182 }
   1183 
   1184 
   1185 void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
   1186   CALL_HEAP_FUNCTION_VOID(
   1187       isolate(),
   1188       isolate()->heap()->ReinitializeJSReceiver(
   1189           *object, JS_FUNCTION_TYPE, JSFunction::kSize));
   1190 }
   1191 
   1192 
   1193 void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
   1194   CALL_HEAP_FUNCTION_VOID(
   1195       isolate(),
   1196       object->SetIdentityHash(hash, ALLOW_CREATION));
   1197 }
   1198 
   1199 
   1200 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
   1201     Handle<String> name,
   1202     int number_of_literals,
   1203     bool is_generator,
   1204     Handle<Code> code,
   1205     Handle<ScopeInfo> scope_info) {
   1206   Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
   1207   shared->set_code(*code);
   1208   shared->set_scope_info(*scope_info);
   1209   int literals_array_size = number_of_literals;
   1210   // If the function contains object, regexp or array literals,
   1211   // allocate extra space for a literals array prefix containing the
   1212   // context.
   1213   if (number_of_literals > 0) {
   1214     literals_array_size += JSFunction::kLiteralsPrefixSize;
   1215   }
   1216   shared->set_num_literals(literals_array_size);
   1217   if (is_generator) {
   1218     shared->set_instance_class_name(isolate()->heap()->Generator_string());
   1219     shared->DisableOptimization(kGenerator);
   1220   }
   1221   return shared;
   1222 }
   1223 
   1224 
   1225 Handle<JSMessageObject> Factory::NewJSMessageObject(
   1226     Handle<String> type,
   1227     Handle<JSArray> arguments,
   1228     int start_position,
   1229     int end_position,
   1230     Handle<Object> script,
   1231     Handle<Object> stack_trace,
   1232     Handle<Object> stack_frames) {
   1233   CALL_HEAP_FUNCTION(isolate(),
   1234                      isolate()->heap()->AllocateJSMessageObject(*type,
   1235                          *arguments,
   1236                          start_position,
   1237                          end_position,
   1238                          *script,
   1239                          *stack_trace,
   1240                          *stack_frames),
   1241                      JSMessageObject);
   1242 }
   1243 
   1244 
   1245 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
   1246   CALL_HEAP_FUNCTION(isolate(),
   1247                      isolate()->heap()->AllocateSharedFunctionInfo(*name),
   1248                      SharedFunctionInfo);
   1249 }
   1250 
   1251 
   1252 Handle<String> Factory::NumberToString(Handle<Object> number) {
   1253   CALL_HEAP_FUNCTION(isolate(),
   1254                      isolate()->heap()->NumberToString(*number), String);
   1255 }
   1256 
   1257 
   1258 Handle<String> Factory::Uint32ToString(uint32_t value) {
   1259   CALL_HEAP_FUNCTION(isolate(),
   1260                      isolate()->heap()->Uint32ToString(value), String);
   1261 }
   1262 
   1263 
   1264 Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
   1265     Handle<SeededNumberDictionary> dictionary,
   1266     uint32_t key,
   1267     Handle<Object> value) {
   1268   CALL_HEAP_FUNCTION(isolate(),
   1269                      dictionary->AtNumberPut(key, *value),
   1270                      SeededNumberDictionary);
   1271 }
   1272 
   1273 
   1274 Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
   1275     Handle<UnseededNumberDictionary> dictionary,
   1276     uint32_t key,
   1277     Handle<Object> value) {
   1278   CALL_HEAP_FUNCTION(isolate(),
   1279                      dictionary->AtNumberPut(key, *value),
   1280                      UnseededNumberDictionary);
   1281 }
   1282 
   1283 
   1284 Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
   1285                                               Handle<Object> prototype) {
   1286   Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
   1287   CALL_HEAP_FUNCTION(
   1288       isolate(),
   1289       isolate()->heap()->AllocateFunction(*isolate()->function_map(),
   1290                                           *function_share,
   1291                                           *prototype),
   1292       JSFunction);
   1293 }
   1294 
   1295 
   1296 Handle<JSFunction> Factory::NewFunction(Handle<String> name,
   1297                                         Handle<Object> prototype) {
   1298   Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
   1299   fun->set_context(isolate()->context()->native_context());
   1300   return fun;
   1301 }
   1302 
   1303 
   1304 Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
   1305     Handle<String> name,
   1306     LanguageMode language_mode) {
   1307   Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
   1308   Handle<Map> map = (language_mode == CLASSIC_MODE)
   1309       ? isolate()->function_without_prototype_map()
   1310       : isolate()->strict_mode_function_without_prototype_map();
   1311   CALL_HEAP_FUNCTION(isolate(),
   1312                      isolate()->heap()->AllocateFunction(
   1313                          *map,
   1314                          *function_share,
   1315                          *the_hole_value()),
   1316                      JSFunction);
   1317 }
   1318 
   1319 
   1320 Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
   1321     Handle<String> name,
   1322     LanguageMode language_mode) {
   1323   Handle<JSFunction> fun =
   1324       NewFunctionWithoutPrototypeHelper(name, language_mode);
   1325   fun->set_context(isolate()->context()->native_context());
   1326   return fun;
   1327 }
   1328 
   1329 
   1330 Handle<Object> Factory::ToObject(Handle<Object> object) {
   1331   CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
   1332 }
   1333 
   1334 
   1335 Handle<Object> Factory::ToObject(Handle<Object> object,
   1336                                  Handle<Context> native_context) {
   1337   CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
   1338 }
   1339 
   1340 
   1341 #ifdef ENABLE_DEBUGGER_SUPPORT
   1342 Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
   1343   // Get the original code of the function.
   1344   Handle<Code> code(shared->code());
   1345 
   1346   // Create a copy of the code before allocating the debug info object to avoid
   1347   // allocation while setting up the debug info object.
   1348   Handle<Code> original_code(*Factory::CopyCode(code));
   1349 
   1350   // Allocate initial fixed array for active break points before allocating the
   1351   // debug info object to avoid allocation while setting up the debug info
   1352   // object.
   1353   Handle<FixedArray> break_points(
   1354       NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
   1355 
   1356   // Create and set up the debug info object. Debug info contains function, a
   1357   // copy of the original code, the executing code and initial fixed array for
   1358   // active break points.
   1359   Handle<DebugInfo> debug_info =
   1360       Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
   1361   debug_info->set_shared(*shared);
   1362   debug_info->set_original_code(*original_code);
   1363   debug_info->set_code(*code);
   1364   debug_info->set_break_points(*break_points);
   1365 
   1366   // Link debug info to function.
   1367   shared->set_debug_info(*debug_info);
   1368 
   1369   return debug_info;
   1370 }
   1371 #endif
   1372 
   1373 
   1374 Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
   1375                                              int length) {
   1376   CALL_HEAP_FUNCTION(
   1377       isolate(),
   1378       isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
   1379 }
   1380 
   1381 
   1382 Handle<JSFunction> Factory::CreateApiFunction(
   1383     Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
   1384   Handle<Code> code = isolate()->builtins()->HandleApiCall();
   1385   Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
   1386 
   1387   int internal_field_count = 0;
   1388   if (!obj->instance_template()->IsUndefined()) {
   1389     Handle<ObjectTemplateInfo> instance_template =
   1390         Handle<ObjectTemplateInfo>(
   1391             ObjectTemplateInfo::cast(obj->instance_template()));
   1392     internal_field_count =
   1393         Smi::cast(instance_template->internal_field_count())->value();
   1394   }
   1395 
   1396   // TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing
   1397   // JSObject::GetHeaderSize.
   1398   int instance_size = kPointerSize * internal_field_count;
   1399   InstanceType type;
   1400   switch (instance_type) {
   1401     case JavaScriptObject:
   1402       type = JS_OBJECT_TYPE;
   1403       instance_size += JSObject::kHeaderSize;
   1404       break;
   1405     case InnerGlobalObject:
   1406       type = JS_GLOBAL_OBJECT_TYPE;
   1407       instance_size += JSGlobalObject::kSize;
   1408       break;
   1409     case OuterGlobalObject:
   1410       type = JS_GLOBAL_PROXY_TYPE;
   1411       instance_size += JSGlobalProxy::kSize;
   1412       break;
   1413     default:
   1414       UNREACHABLE();
   1415       type = JS_OBJECT_TYPE;  // Keep the compiler happy.
   1416       break;
   1417   }
   1418 
   1419   Handle<JSFunction> result =
   1420       NewFunction(Factory::empty_string(),
   1421                   type,
   1422                   instance_size,
   1423                   code,
   1424                   true);
   1425 
   1426   // Set length.
   1427   result->shared()->set_length(obj->length());
   1428 
   1429   // Set class name.
   1430   Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
   1431   if (class_name->IsString()) {
   1432     result->shared()->set_instance_class_name(*class_name);
   1433     result->shared()->set_name(*class_name);
   1434   }
   1435 
   1436   Handle<Map> map = Handle<Map>(result->initial_map());
   1437 
   1438   // Mark as undetectable if needed.
   1439   if (obj->undetectable()) {
   1440     map->set_is_undetectable();
   1441   }
   1442 
   1443   // Mark as hidden for the __proto__ accessor if needed.
   1444   if (obj->hidden_prototype()) {
   1445     map->set_is_hidden_prototype();
   1446   }
   1447 
   1448   // Mark as needs_access_check if needed.
   1449   if (obj->needs_access_check()) {
   1450     map->set_is_access_check_needed(true);
   1451   }
   1452 
   1453   // Set interceptor information in the map.
   1454   if (!obj->named_property_handler()->IsUndefined()) {
   1455     map->set_has_named_interceptor();
   1456   }
   1457   if (!obj->indexed_property_handler()->IsUndefined()) {
   1458     map->set_has_indexed_interceptor();
   1459   }
   1460 
   1461   // Set instance call-as-function information in the map.
   1462   if (!obj->instance_call_handler()->IsUndefined()) {
   1463     map->set_has_instance_call_handler();
   1464   }
   1465 
   1466   result->shared()->set_function_data(*obj);
   1467   result->shared()->set_construct_stub(*construct_stub);
   1468   result->shared()->DontAdaptArguments();
   1469 
   1470   // Recursively copy parent templates' accessors, 'data' may be modified.
   1471   int max_number_of_additional_properties = 0;
   1472   FunctionTemplateInfo* info = *obj;
   1473   while (true) {
   1474     Object* props = info->property_accessors();
   1475     if (!props->IsUndefined()) {
   1476       Handle<Object> props_handle(props, isolate());
   1477       NeanderArray props_array(props_handle);
   1478       max_number_of_additional_properties += props_array.length();
   1479     }
   1480     Object* parent = info->parent_template();
   1481     if (parent->IsUndefined()) break;
   1482     info = FunctionTemplateInfo::cast(parent);
   1483   }
   1484 
   1485   Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
   1486 
   1487   while (true) {
   1488     Handle<Object> props = Handle<Object>(obj->property_accessors(),
   1489                                           isolate());
   1490     if (!props->IsUndefined()) {
   1491       Map::AppendCallbackDescriptors(map, props);
   1492     }
   1493     Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
   1494     if (parent->IsUndefined()) break;
   1495     obj = Handle<FunctionTemplateInfo>::cast(parent);
   1496   }
   1497 
   1498   ASSERT(result->shared()->IsApiFunction());
   1499   return result;
   1500 }
   1501 
   1502 
   1503 Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
   1504   CALL_HEAP_FUNCTION(isolate(),
   1505                      MapCache::Allocate(isolate()->heap(),
   1506                                         at_least_space_for),
   1507                      MapCache);
   1508 }
   1509 
   1510 
   1511 MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
   1512                                                        FixedArray* keys,
   1513                                                        Map* map) {
   1514   Object* result;
   1515   { MaybeObject* maybe_result =
   1516         MapCache::cast(context->map_cache())->Put(keys, map);
   1517     if (!maybe_result->ToObject(&result)) return maybe_result;
   1518   }
   1519   context->set_map_cache(MapCache::cast(result));
   1520   return result;
   1521 }
   1522 
   1523 
   1524 Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
   1525                                         Handle<FixedArray> keys,
   1526                                         Handle<Map> map) {
   1527   CALL_HEAP_FUNCTION(isolate(),
   1528                      UpdateMapCacheWith(*context, *keys, *map), MapCache);
   1529 }
   1530 
   1531 
   1532 Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
   1533                                                Handle<FixedArray> keys) {
   1534   if (context->map_cache()->IsUndefined()) {
   1535     // Allocate the new map cache for the native context.
   1536     Handle<MapCache> new_cache = NewMapCache(24);
   1537     context->set_map_cache(*new_cache);
   1538   }
   1539   // Check to see whether there is a matching element in the cache.
   1540   Handle<MapCache> cache =
   1541       Handle<MapCache>(MapCache::cast(context->map_cache()));
   1542   Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
   1543   if (result->IsMap()) return Handle<Map>::cast(result);
   1544   // Create a new map and add it to the cache.
   1545   Handle<Map> map =
   1546       CopyMap(Handle<Map>(context->object_function()->initial_map()),
   1547               keys->length());
   1548   AddToMapCache(context, keys, map);
   1549   return Handle<Map>(map);
   1550 }
   1551 
   1552 
   1553 void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
   1554                                 JSRegExp::Type type,
   1555                                 Handle<String> source,
   1556                                 JSRegExp::Flags flags,
   1557                                 Handle<Object> data) {
   1558   Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
   1559 
   1560   store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
   1561   store->set(JSRegExp::kSourceIndex, *source);
   1562   store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
   1563   store->set(JSRegExp::kAtomPatternIndex, *data);
   1564   regexp->set_data(*store);
   1565 }
   1566 
   1567 void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
   1568                                     JSRegExp::Type type,
   1569                                     Handle<String> source,
   1570                                     JSRegExp::Flags flags,
   1571                                     int capture_count) {
   1572   Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
   1573   Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
   1574   store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
   1575   store->set(JSRegExp::kSourceIndex, *source);
   1576   store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
   1577   store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
   1578   store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
   1579   store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
   1580   store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
   1581   store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
   1582   store->set(JSRegExp::kIrregexpCaptureCountIndex,
   1583              Smi::FromInt(capture_count));
   1584   regexp->set_data(*store);
   1585 }
   1586 
   1587 
   1588 
   1589 void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
   1590                                 Handle<JSObject> instance,
   1591                                 bool* pending_exception) {
   1592   // Configure the instance by adding the properties specified by the
   1593   // instance template.
   1594   Handle<Object> instance_template(desc->instance_template(), isolate());
   1595   if (!instance_template->IsUndefined()) {
   1596     Execution::ConfigureInstance(instance,
   1597                                  instance_template,
   1598                                  pending_exception);
   1599   } else {
   1600     *pending_exception = false;
   1601   }
   1602 }
   1603 
   1604 
   1605 Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
   1606   Heap* h = isolate()->heap();
   1607   if (name->Equals(h->undefined_string())) return undefined_value();
   1608   if (name->Equals(h->nan_string())) return nan_value();
   1609   if (name->Equals(h->infinity_string())) return infinity_value();
   1610   return Handle<Object>::null();
   1611 }
   1612 
   1613 
   1614 Handle<Object> Factory::ToBoolean(bool value) {
   1615   return value ? true_value() : false_value();
   1616 }
   1617 
   1618 
   1619 } }  // namespace v8::internal
   1620