146
Handle<AccessorPair> Factory::NewAccessorPair() {
147
CALL_HEAP_FUNCTION(isolate(),
148
isolate()->heap()->AllocateAccessorPair(),
153
Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
154
CALL_HEAP_FUNCTION(isolate(),
155
isolate()->heap()->AllocateTypeFeedbackInfo(),
136
160
// Symbols are created in the old generation (data space).
137
161
Handle<String> Factory::LookupSymbol(Vector<const char> string) {
138
162
CALL_HEAP_FUNCTION(isolate(),
264
Handle<Context> Factory::NewGlobalContext() {
267
isolate()->heap()->AllocateGlobalContext(),
288
Handle<Context> Factory::NewNativeContext() {
291
isolate()->heap()->AllocateNativeContext(),
296
Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
297
Handle<ScopeInfo> scope_info) {
300
isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
305
Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
308
isolate()->heap()->AllocateModuleContext(*scope_info),
305
Handle<Context> Factory::NewBlockContext(
306
Handle<JSFunction> function,
307
Handle<Context> previous,
308
Handle<SerializedScopeInfo> scope_info) {
346
Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
347
Handle<Context> previous,
348
Handle<ScopeInfo> scope_info) {
309
349
CALL_HEAP_FUNCTION(
311
351
isolate()->heap()->AllocateBlockContext(*function,
433
Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
434
CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
477
Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
479
isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
438
483
Handle<Map> Factory::CopyMap(Handle<Map> src,
439
484
int extra_inobject_properties) {
440
Handle<Map> copy = CopyMapDropDescriptors(src);
485
Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
441
486
// Check that we do not overflow the instance size when adding the
442
487
// extra inobject properties.
443
488
int instance_size_delta = extra_inobject_properties * kPointerSize;
463
Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
464
CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
468
Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
469
CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
473
Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
474
CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
508
Handle<Map> Factory::CopyMap(Handle<Map> src) {
509
CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
478
513
Handle<Map> Factory::GetElementsTransitionMap(
480
ElementsKind elements_kind,
481
bool safe_to_add_transition) {
482
CALL_HEAP_FUNCTION(isolate(),
483
src->GetElementsTransitionMap(elements_kind,
484
safe_to_add_transition),
514
Handle<JSObject> src,
515
ElementsKind elements_kind) {
516
Isolate* i = isolate();
517
CALL_HEAP_FUNCTION(i,
518
src->GetElementsTransitionMap(i, elements_kind),
511
551
PretenureFlag pretenure) {
512
552
Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
514
function_info->strict_mode()
515
? isolate()->strict_mode_function_map()
516
: isolate()->function_map(),
554
function_info->is_classic_mode()
555
? isolate()->function_map()
556
: isolate()->strict_mode_function_map(),
559
if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
560
function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
519
563
result->set_context(*context);
520
int number_of_literals = function_info->num_literals();
521
Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
522
if (number_of_literals > 0) {
523
// Store the object, regexp and array functions in the literals
524
// array prefix. These functions will be used when creating
525
// object, regexp and array literals in this function.
526
literals->set(JSFunction::kLiteralGlobalContextIndex,
527
context->global_context());
529
result->set_literals(*literals);
530
result->set_next_function_link(isolate()->heap()->undefined_value());
565
int index = function_info->SearchOptimizedCodeMap(context->native_context());
566
if (!function_info->bound() && index < 0) {
567
int number_of_literals = function_info->num_literals();
568
Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
569
if (number_of_literals > 0) {
570
// Store the native context in the literals array prefix. This
571
// context will be used when creating object, regexp and array
572
// literals in this function.
573
literals->set(JSFunction::kLiteralNativeContextIndex,
574
context->native_context());
576
result->set_literals(*literals);
580
// Caching of optimized code enabled and optimized code found.
581
function_info->InstallFromOptimizedCodeMap(*result, index);
532
585
if (V8::UseCrankshaft() &&
533
586
FLAG_always_opt &&
534
587
result->is_compiled() &&
535
588
!function_info->is_toplevel() &&
536
function_info->allows_lazy_compilation()) {
589
function_info->allows_lazy_compilation() &&
590
!function_info->optimization_disabled()) {
537
591
result->MarkForLazyRecompilation();
551
Handle<Object> Factory::NewNumberFromInt(int value) {
605
Handle<Object> Factory::NewNumberFromInt(int32_t value,
606
PretenureFlag pretenure) {
552
607
CALL_HEAP_FUNCTION(
554
isolate()->heap()->NumberFromInt32(value), Object);
609
isolate()->heap()->NumberFromInt32(value, pretenure), Object);
558
Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
613
Handle<Object> Factory::NewNumberFromUint(uint32_t value,
614
PretenureFlag pretenure) {
559
615
CALL_HEAP_FUNCTION(
561
isolate()->heap()->NumberFromUint32(value), Object);
617
isolate()->heap()->NumberFromUint32(value, pretenure), Object);
698
Handle<String> Factory::EmergencyNewError(const char* type,
699
Handle<JSArray> args) {
700
const int kBufferSize = 1000;
701
char buffer[kBufferSize];
702
size_t space = kBufferSize;
703
char* p = &buffer[0];
705
Vector<char> v(buffer, kBufferSize);
706
OS::StrNCpy(v, type, space);
707
space -= Min(space, strlen(type));
708
p = &buffer[kBufferSize] - space;
710
for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
715
MaybeObject* maybe_arg = args->GetElement(i);
716
Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
717
const char* arg = *arg_str->ToCString();
718
Vector<char> v2(p, static_cast<int>(space));
719
OS::StrNCpy(v2, arg, space);
720
space -= Min(space, strlen(arg));
721
p = &buffer[kBufferSize] - space;
728
buffer[kBufferSize - 1] = '\0';
730
Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
642
735
Handle<Object> Factory::NewError(const char* maker,
643
736
const char* type,
644
737
Handle<JSArray> args) {
647
740
isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
648
741
// If the builtins haven't been properly configured yet this error
649
742
// constructor may not have been defined. Bail out.
650
if (!fun_obj->IsJSFunction())
651
return undefined_value();
743
if (!fun_obj->IsJSFunction()) {
744
return EmergencyNewError(type, args);
652
746
Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
653
747
Handle<Object> type_obj = LookupAsciiSymbol(type);
654
Object** argv[2] = { type_obj.location(),
655
Handle<Object>::cast(args).location() };
748
Handle<Object> argv[] = { type_obj, args };
657
750
// Invoke the JavaScript factory method. If an exception is thrown while
658
751
// running the factory method, use the exception as the result.
659
752
bool caught_exception;
660
753
Handle<Object> result = Execution::TryCall(fun,
661
isolate()->js_builtins_object(), 2, argv, &caught_exception);
754
isolate()->js_builtins_object(),
674
770
Handle<JSFunction> fun = Handle<JSFunction>(
675
771
JSFunction::cast(isolate()->js_builtins_object()->
676
772
GetPropertyNoExceptionThrown(*constr)));
677
Object** argv[1] = { Handle<Object>::cast(message).location() };
773
Handle<Object> argv[] = { message };
679
775
// Invoke the JavaScript factory method. If an exception is thrown while
680
776
// running the factory method, use the exception as the result.
681
777
bool caught_exception;
682
778
Handle<Object> result = Execution::TryCall(fun,
683
isolate()->js_builtins_object(), 1, argv, &caught_exception);
779
isolate()->js_builtins_object(),
741
842
// property that refers to the function.
742
843
SetPrototypeProperty(function, prototype);
743
844
// Currently safe because it is only invoked from Genesis.
744
SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
845
CHECK_NOT_EMPTY_HANDLE(isolate(),
846
JSObject::SetLocalPropertyIgnoreAttributes(
847
prototype, constructor_symbol(),
848
function, DONT_ENUM));
794
MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
795
DescriptorArray* array,
798
PropertyAttributes attributes) {
799
CallbacksDescriptor desc(key, value, attributes);
800
MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
805
// Allocate the new array.
806
Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
807
Handle<DescriptorArray> array,
809
Handle<Object> value,
810
PropertyAttributes attributes) {
811
CALL_HEAP_FUNCTION(isolate(),
812
DoCopyInsert(*array, *key, *value, attributes),
817
898
Handle<String> Factory::SymbolFromString(Handle<String> value) {
818
899
CALL_HEAP_FUNCTION(isolate(),
819
900
isolate()->heap()->LookupSymbol(*value), String);
823
Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
824
Handle<DescriptorArray> array,
825
Handle<Object> descriptors) {
826
v8::NeanderArray callbacks(descriptors);
827
int nof_callbacks = callbacks.length();
828
Handle<DescriptorArray> result =
829
NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
831
// Number of descriptors added to the result so far.
832
int descriptor_count = 0;
834
// Copy the descriptors from the array.
835
for (int i = 0; i < array->number_of_descriptors(); i++) {
836
if (array->GetType(i) != NULL_DESCRIPTOR) {
837
result->CopyFrom(descriptor_count++, *array, i);
841
// Number of duplicates detected.
844
// Fill in new callback descriptors. Process the callbacks from
845
// back to front so that the last callback with a given name takes
846
// precedence over previously added callbacks with that name.
847
for (int i = nof_callbacks - 1; i >= 0; i--) {
848
Handle<AccessorInfo> entry =
849
Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
850
// Ensure the key is a symbol before writing into the instance descriptor.
852
SymbolFromString(Handle<String>(String::cast(entry->name())));
853
// Check if a descriptor with this name already exists before writing.
854
if (result->LinearSearch(*key, descriptor_count) ==
855
DescriptorArray::kNotFound) {
856
CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
857
result->Set(descriptor_count, &desc);
864
// If duplicates were detected, allocate a result of the right size
865
// and transfer the elements.
866
if (duplicates > 0) {
867
int number_of_descriptors = result->number_of_descriptors() - duplicates;
868
Handle<DescriptorArray> new_result =
869
NewDescriptorArray(number_of_descriptors);
870
for (int i = 0; i < number_of_descriptors; i++) {
871
new_result->CopyFrom(i, *result, i);
876
// Sort the result before returning.
882
904
Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
883
905
PretenureFlag pretenure) {
884
906
CALL_HEAP_FUNCTION(
907
937
Handle<JSArray> Factory::NewJSArray(int capacity,
938
ElementsKind elements_kind,
908
939
PretenureFlag pretenure) {
909
Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
910
940
CALL_HEAP_FUNCTION(isolate(),
911
Handle<JSArray>::cast(obj)->Initialize(capacity),
941
isolate()->heap()->AllocateJSArrayAndStorage(
945
INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
916
Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
951
Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
952
ElementsKind elements_kind,
917
953
PretenureFlag pretenure) {
918
Handle<JSArray> result =
919
Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
921
result->SetContent(*elements);
956
isolate()->heap()->AllocateJSArrayWithElements(*elements,
963
void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
966
ElementsAccessor* accessor = array->GetElementsAccessor();
967
CALL_HEAP_FUNCTION_VOID(
969
accessor->SetCapacityAndLength(*array, capacity, length));
973
void Factory::SetContent(Handle<JSArray> array,
974
Handle<FixedArrayBase> elements) {
975
CALL_HEAP_FUNCTION_VOID(
977
array->SetContent(*elements));
981
void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
982
CALL_HEAP_FUNCTION_VOID(
984
array->EnsureCanContainHeapObjectElements());
988
void Factory::EnsureCanContainElements(Handle<JSArray> array,
989
Handle<FixedArrayBase> elements,
991
EnsureElementsMode mode) {
992
CALL_HEAP_FUNCTION_VOID(
994
array->EnsureCanContainElements(*elements, length, mode));
1035
1120
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1036
1121
Handle<Object> prototype) {
1037
1122
Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
1038
fun->set_context(isolate()->context()->global_context());
1123
fun->set_context(isolate()->context()->native_context());
1043
1128
Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
1044
1129
Handle<String> name,
1045
StrictModeFlag strict_mode) {
1130
LanguageMode language_mode) {
1046
1131
Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
1047
Handle<Map> map = strict_mode == kStrictMode
1048
? isolate()->strict_mode_function_without_prototype_map()
1049
: isolate()->function_without_prototype_map();
1132
Handle<Map> map = (language_mode == CLASSIC_MODE)
1133
? isolate()->function_without_prototype_map()
1134
: isolate()->strict_mode_function_without_prototype_map();
1050
1135
CALL_HEAP_FUNCTION(isolate(),
1051
1136
isolate()->heap()->AllocateFunction(
1199
1285
result->shared()->DontAdaptArguments();
1201
1287
// Recursively copy parent templates' accessors, 'data' may be modified.
1202
Handle<DescriptorArray> array =
1203
Handle<DescriptorArray>(map->instance_descriptors());
1288
int max_number_of_additional_properties = 0;
1289
FunctionTemplateInfo* info = *obj;
1291
Object* props = info->property_accessors();
1292
if (!props->IsUndefined()) {
1293
Handle<Object> props_handle(props);
1294
NeanderArray props_array(props_handle);
1295
max_number_of_additional_properties += props_array.length();
1297
Object* parent = info->parent_template();
1298
if (parent->IsUndefined()) break;
1299
info = FunctionTemplateInfo::cast(parent);
1302
Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1205
1305
Handle<Object> props = Handle<Object>(obj->property_accessors());
1206
1306
if (!props->IsUndefined()) {
1207
array = CopyAppendCallbackDescriptors(array, props);
1307
Map::AppendCallbackDescriptors(map, props);
1209
1309
Handle<Object> parent = Handle<Object>(obj->parent_template());
1210
1310
if (parent->IsUndefined()) break;
1211
1311
obj = Handle<FunctionTemplateInfo>::cast(parent);
1213
if (!array->IsEmpty()) {
1214
map->set_instance_descriptors(*array);
1217
1314
ASSERT(result->shared()->IsApiFunction());
1419
Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1420
Heap* h = isolate()->heap();
1421
if (name->Equals(h->undefined_symbol())) return undefined_value();
1422
if (name->Equals(h->nan_symbol())) return nan_value();
1423
if (name->Equals(h->infinity_symbol())) return infinity_value();
1424
return Handle<Object>::null();
1428
Handle<Object> Factory::ToBoolean(bool value) {
1429
return Handle<Object>(value
1430
? isolate()->heap()->true_value()
1431
: isolate()->heap()->false_value());
1322
1435
} } // namespace v8::internal