factory.cc revision c7cc028aaeedbbfa11c11d0b7b243b3d9e837ed9
1// Copyright 2011 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 "macro-assembler.h" 35#include "objects.h" 36#include "objects-visiting.h" 37#include "scopeinfo.h" 38 39namespace v8 { 40namespace internal { 41 42 43Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { 44 ASSERT(0 <= size); 45 CALL_HEAP_FUNCTION( 46 isolate(), 47 isolate()->heap()->AllocateFixedArray(size, pretenure), 48 FixedArray); 49} 50 51 52Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size, 53 PretenureFlag pretenure) { 54 ASSERT(0 <= size); 55 CALL_HEAP_FUNCTION( 56 isolate(), 57 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure), 58 FixedArray); 59} 60 61 62Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size, 63 PretenureFlag pretenure) { 64 ASSERT(0 <= size); 65 CALL_HEAP_FUNCTION( 66 isolate(), 67 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure), 68 FixedDoubleArray); 69} 70 71 72Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) { 73 ASSERT(0 <= at_least_space_for); 74 CALL_HEAP_FUNCTION(isolate(), 75 StringDictionary::Allocate(at_least_space_for), 76 StringDictionary); 77} 78 79 80Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary( 81 int at_least_space_for) { 82 ASSERT(0 <= at_least_space_for); 83 CALL_HEAP_FUNCTION(isolate(), 84 SeededNumberDictionary::Allocate(at_least_space_for), 85 SeededNumberDictionary); 86} 87 88 89Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary( 90 int at_least_space_for) { 91 ASSERT(0 <= at_least_space_for); 92 CALL_HEAP_FUNCTION(isolate(), 93 UnseededNumberDictionary::Allocate(at_least_space_for), 94 UnseededNumberDictionary); 95} 96 97 98Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) { 99 ASSERT(0 <= at_least_space_for); 100 CALL_HEAP_FUNCTION(isolate(), 101 ObjectHashSet::Allocate(at_least_space_for), 102 ObjectHashSet); 103} 104 105 106Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) { 107 ASSERT(0 <= at_least_space_for); 108 CALL_HEAP_FUNCTION(isolate(), 109 ObjectHashTable::Allocate(at_least_space_for), 110 ObjectHashTable); 111} 112 113 114Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { 115 ASSERT(0 <= number_of_descriptors); 116 CALL_HEAP_FUNCTION(isolate(), 117 DescriptorArray::Allocate(number_of_descriptors), 118 DescriptorArray); 119} 120 121 122Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData( 123 int deopt_entry_count, 124 PretenureFlag pretenure) { 125 ASSERT(deopt_entry_count > 0); 126 CALL_HEAP_FUNCTION(isolate(), 127 DeoptimizationInputData::Allocate(deopt_entry_count, 128 pretenure), 129 DeoptimizationInputData); 130} 131 132 133Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData( 134 int deopt_entry_count, 135 PretenureFlag pretenure) { 136 ASSERT(deopt_entry_count > 0); 137 CALL_HEAP_FUNCTION(isolate(), 138 DeoptimizationOutputData::Allocate(deopt_entry_count, 139 pretenure), 140 DeoptimizationOutputData); 141} 142 143 144Handle<AccessorPair> Factory::NewAccessorPair() { 145 CALL_HEAP_FUNCTION(isolate(), 146 isolate()->heap()->AllocateAccessorPair(), 147 AccessorPair); 148} 149 150 151// Symbols are created in the old generation (data space). 152Handle<String> Factory::LookupSymbol(Vector<const char> string) { 153 CALL_HEAP_FUNCTION(isolate(), 154 isolate()->heap()->LookupSymbol(string), 155 String); 156} 157 158// Symbols are created in the old generation (data space). 159Handle<String> Factory::LookupSymbol(Handle<String> string) { 160 CALL_HEAP_FUNCTION(isolate(), 161 isolate()->heap()->LookupSymbol(*string), 162 String); 163} 164 165Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) { 166 CALL_HEAP_FUNCTION(isolate(), 167 isolate()->heap()->LookupAsciiSymbol(string), 168 String); 169} 170 171 172Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string, 173 int from, 174 int length) { 175 CALL_HEAP_FUNCTION(isolate(), 176 isolate()->heap()->LookupAsciiSymbol(string, 177 from, 178 length), 179 String); 180} 181 182 183Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) { 184 CALL_HEAP_FUNCTION(isolate(), 185 isolate()->heap()->LookupTwoByteSymbol(string), 186 String); 187} 188 189 190Handle<String> Factory::NewStringFromAscii(Vector<const char> string, 191 PretenureFlag pretenure) { 192 CALL_HEAP_FUNCTION( 193 isolate(), 194 isolate()->heap()->AllocateStringFromAscii(string, pretenure), 195 String); 196} 197 198Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, 199 PretenureFlag pretenure) { 200 CALL_HEAP_FUNCTION( 201 isolate(), 202 isolate()->heap()->AllocateStringFromUtf8(string, pretenure), 203 String); 204} 205 206 207Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string, 208 PretenureFlag pretenure) { 209 CALL_HEAP_FUNCTION( 210 isolate(), 211 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure), 212 String); 213} 214 215 216Handle<SeqAsciiString> Factory::NewRawAsciiString(int length, 217 PretenureFlag pretenure) { 218 CALL_HEAP_FUNCTION( 219 isolate(), 220 isolate()->heap()->AllocateRawAsciiString(length, pretenure), 221 SeqAsciiString); 222} 223 224 225Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length, 226 PretenureFlag pretenure) { 227 CALL_HEAP_FUNCTION( 228 isolate(), 229 isolate()->heap()->AllocateRawTwoByteString(length, pretenure), 230 SeqTwoByteString); 231} 232 233 234Handle<String> Factory::NewConsString(Handle<String> first, 235 Handle<String> second) { 236 CALL_HEAP_FUNCTION(isolate(), 237 isolate()->heap()->AllocateConsString(*first, *second), 238 String); 239} 240 241 242Handle<String> Factory::NewSubString(Handle<String> str, 243 int begin, 244 int end) { 245 CALL_HEAP_FUNCTION(isolate(), 246 str->SubString(begin, end), 247 String); 248} 249 250 251Handle<String> Factory::NewProperSubString(Handle<String> str, 252 int begin, 253 int end) { 254 ASSERT(begin > 0 || end < str->length()); 255 CALL_HEAP_FUNCTION(isolate(), 256 isolate()->heap()->AllocateSubString(*str, begin, end), 257 String); 258} 259 260 261Handle<String> Factory::NewExternalStringFromAscii( 262 const ExternalAsciiString::Resource* resource) { 263 CALL_HEAP_FUNCTION( 264 isolate(), 265 isolate()->heap()->AllocateExternalStringFromAscii(resource), 266 String); 267} 268 269 270Handle<String> Factory::NewExternalStringFromTwoByte( 271 const ExternalTwoByteString::Resource* resource) { 272 CALL_HEAP_FUNCTION( 273 isolate(), 274 isolate()->heap()->AllocateExternalStringFromTwoByte(resource), 275 String); 276} 277 278 279Handle<Context> Factory::NewGlobalContext() { 280 CALL_HEAP_FUNCTION( 281 isolate(), 282 isolate()->heap()->AllocateGlobalContext(), 283 Context); 284} 285 286 287Handle<Context> Factory::NewFunctionContext(int length, 288 Handle<JSFunction> function) { 289 CALL_HEAP_FUNCTION( 290 isolate(), 291 isolate()->heap()->AllocateFunctionContext(length, *function), 292 Context); 293} 294 295 296Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function, 297 Handle<Context> previous, 298 Handle<String> name, 299 Handle<Object> thrown_object) { 300 CALL_HEAP_FUNCTION( 301 isolate(), 302 isolate()->heap()->AllocateCatchContext(*function, 303 *previous, 304 *name, 305 *thrown_object), 306 Context); 307} 308 309 310Handle<Context> Factory::NewWithContext(Handle<JSFunction> function, 311 Handle<Context> previous, 312 Handle<JSObject> extension) { 313 CALL_HEAP_FUNCTION( 314 isolate(), 315 isolate()->heap()->AllocateWithContext(*function, *previous, *extension), 316 Context); 317} 318 319 320Handle<Context> Factory::NewBlockContext( 321 Handle<JSFunction> function, 322 Handle<Context> previous, 323 Handle<ScopeInfo> scope_info) { 324 CALL_HEAP_FUNCTION( 325 isolate(), 326 isolate()->heap()->AllocateBlockContext(*function, 327 *previous, 328 *scope_info), 329 Context); 330} 331 332 333Handle<Struct> Factory::NewStruct(InstanceType type) { 334 CALL_HEAP_FUNCTION( 335 isolate(), 336 isolate()->heap()->AllocateStruct(type), 337 Struct); 338} 339 340 341Handle<AccessorInfo> Factory::NewAccessorInfo() { 342 Handle<AccessorInfo> info = 343 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); 344 info->set_flag(0); // Must clear the flag, it was initialized as undefined. 345 return info; 346} 347 348 349Handle<Script> Factory::NewScript(Handle<String> source) { 350 // Generate id for this script. 351 int id; 352 Heap* heap = isolate()->heap(); 353 if (heap->last_script_id()->IsUndefined()) { 354 // Script ids start from one. 355 id = 1; 356 } else { 357 // Increment id, wrap when positive smi is exhausted. 358 id = Smi::cast(heap->last_script_id())->value(); 359 id++; 360 if (!Smi::IsValid(id)) { 361 id = 0; 362 } 363 } 364 heap->SetLastScriptId(Smi::FromInt(id)); 365 366 // Create and initialize script object. 367 Handle<Foreign> wrapper = NewForeign(0, TENURED); 368 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); 369 script->set_source(*source); 370 script->set_name(heap->undefined_value()); 371 script->set_id(heap->last_script_id()); 372 script->set_line_offset(Smi::FromInt(0)); 373 script->set_column_offset(Smi::FromInt(0)); 374 script->set_data(heap->undefined_value()); 375 script->set_context_data(heap->undefined_value()); 376 script->set_type(Smi::FromInt(Script::TYPE_NORMAL)); 377 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST)); 378 script->set_wrapper(*wrapper); 379 script->set_line_ends(heap->undefined_value()); 380 script->set_eval_from_shared(heap->undefined_value()); 381 script->set_eval_from_instructions_offset(Smi::FromInt(0)); 382 383 return script; 384} 385 386 387Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) { 388 CALL_HEAP_FUNCTION(isolate(), 389 isolate()->heap()->AllocateForeign(addr, pretenure), 390 Foreign); 391} 392 393 394Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) { 395 return NewForeign((Address) desc, TENURED); 396} 397 398 399Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { 400 ASSERT(0 <= length); 401 CALL_HEAP_FUNCTION( 402 isolate(), 403 isolate()->heap()->AllocateByteArray(length, pretenure), 404 ByteArray); 405} 406 407 408Handle<ExternalArray> Factory::NewExternalArray(int length, 409 ExternalArrayType array_type, 410 void* external_pointer, 411 PretenureFlag pretenure) { 412 ASSERT(0 <= length); 413 CALL_HEAP_FUNCTION( 414 isolate(), 415 isolate()->heap()->AllocateExternalArray(length, 416 array_type, 417 external_pointer, 418 pretenure), 419 ExternalArray); 420} 421 422 423Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell( 424 Handle<Object> value) { 425 CALL_HEAP_FUNCTION( 426 isolate(), 427 isolate()->heap()->AllocateJSGlobalPropertyCell(*value), 428 JSGlobalPropertyCell); 429} 430 431 432Handle<Map> Factory::NewMap(InstanceType type, 433 int instance_size, 434 ElementsKind elements_kind) { 435 CALL_HEAP_FUNCTION( 436 isolate(), 437 isolate()->heap()->AllocateMap(type, instance_size, elements_kind), 438 Map); 439} 440 441 442Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { 443 CALL_HEAP_FUNCTION( 444 isolate(), 445 isolate()->heap()->AllocateFunctionPrototype(*function), 446 JSObject); 447} 448 449 450Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { 451 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map); 452} 453 454 455Handle<Map> Factory::CopyMap(Handle<Map> src, 456 int extra_inobject_properties) { 457 Handle<Map> copy = CopyMapDropDescriptors(src); 458 // Check that we do not overflow the instance size when adding the 459 // extra inobject properties. 460 int instance_size_delta = extra_inobject_properties * kPointerSize; 461 int max_instance_size_delta = 462 JSObject::kMaxInstanceSize - copy->instance_size(); 463 if (instance_size_delta > max_instance_size_delta) { 464 // If the instance size overflows, we allocate as many properties 465 // as we can as inobject properties. 466 instance_size_delta = max_instance_size_delta; 467 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; 468 } 469 // Adjust the map with the extra inobject properties. 470 int inobject_properties = 471 copy->inobject_properties() + extra_inobject_properties; 472 copy->set_inobject_properties(inobject_properties); 473 copy->set_unused_property_fields(inobject_properties); 474 copy->set_instance_size(copy->instance_size() + instance_size_delta); 475 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy)); 476 return copy; 477} 478 479 480Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { 481 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map); 482} 483 484 485Handle<Map> Factory::GetElementsTransitionMap( 486 Handle<JSObject> src, 487 ElementsKind elements_kind) { 488 CALL_HEAP_FUNCTION(isolate(), 489 src->GetElementsTransitionMap(elements_kind), 490 Map); 491} 492 493 494Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { 495 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray); 496} 497 498 499Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray( 500 Handle<FixedDoubleArray> array) { 501 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray); 502} 503 504 505Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo( 506 Handle<SharedFunctionInfo> function_info, 507 Handle<Map> function_map, 508 PretenureFlag pretenure) { 509 CALL_HEAP_FUNCTION( 510 isolate(), 511 isolate()->heap()->AllocateFunction(*function_map, 512 *function_info, 513 isolate()->heap()->the_hole_value(), 514 pretenure), 515 JSFunction); 516} 517 518 519Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( 520 Handle<SharedFunctionInfo> function_info, 521 Handle<Context> context, 522 PretenureFlag pretenure) { 523 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo( 524 function_info, 525 function_info->is_classic_mode() 526 ? isolate()->function_map() 527 : isolate()->strict_mode_function_map(), 528 pretenure); 529 530 result->set_context(*context); 531 if (!function_info->bound()) { 532 int number_of_literals = function_info->num_literals(); 533 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure); 534 if (number_of_literals > 0) { 535 // Store the object, regexp and array functions in the literals 536 // array prefix. These functions will be used when creating 537 // object, regexp and array literals in this function. 538 literals->set(JSFunction::kLiteralGlobalContextIndex, 539 context->global_context()); 540 } 541 result->set_literals(*literals); 542 } else { 543 result->set_function_bindings(isolate()->heap()->empty_fixed_array()); 544 } 545 result->set_next_function_link(isolate()->heap()->undefined_value()); 546 547 if (V8::UseCrankshaft() && 548 FLAG_always_opt && 549 result->is_compiled() && 550 !function_info->is_toplevel() && 551 function_info->allows_lazy_compilation()) { 552 result->MarkForLazyRecompilation(); 553 } 554 return result; 555} 556 557 558Handle<Object> Factory::NewNumber(double value, 559 PretenureFlag pretenure) { 560 CALL_HEAP_FUNCTION( 561 isolate(), 562 isolate()->heap()->NumberFromDouble(value, pretenure), Object); 563} 564 565 566Handle<Object> Factory::NewNumberFromInt(int32_t value, 567 PretenureFlag pretenure) { 568 CALL_HEAP_FUNCTION( 569 isolate(), 570 isolate()->heap()->NumberFromInt32(value, pretenure), Object); 571} 572 573 574Handle<Object> Factory::NewNumberFromUint(uint32_t value, 575 PretenureFlag pretenure) { 576 CALL_HEAP_FUNCTION( 577 isolate(), 578 isolate()->heap()->NumberFromUint32(value, pretenure), Object); 579} 580 581 582Handle<JSObject> Factory::NewNeanderObject() { 583 CALL_HEAP_FUNCTION( 584 isolate(), 585 isolate()->heap()->AllocateJSObjectFromMap( 586 isolate()->heap()->neander_map()), 587 JSObject); 588} 589 590 591Handle<Object> Factory::NewTypeError(const char* type, 592 Vector< Handle<Object> > args) { 593 return NewError("MakeTypeError", type, args); 594} 595 596 597Handle<Object> Factory::NewTypeError(Handle<String> message) { 598 return NewError("$TypeError", message); 599} 600 601 602Handle<Object> Factory::NewRangeError(const char* type, 603 Vector< Handle<Object> > args) { 604 return NewError("MakeRangeError", type, args); 605} 606 607 608Handle<Object> Factory::NewRangeError(Handle<String> message) { 609 return NewError("$RangeError", message); 610} 611 612 613Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { 614 return NewError("MakeSyntaxError", type, args); 615} 616 617 618Handle<Object> Factory::NewSyntaxError(Handle<String> message) { 619 return NewError("$SyntaxError", message); 620} 621 622 623Handle<Object> Factory::NewReferenceError(const char* type, 624 Vector< Handle<Object> > args) { 625 return NewError("MakeReferenceError", type, args); 626} 627 628 629Handle<Object> Factory::NewReferenceError(Handle<String> message) { 630 return NewError("$ReferenceError", message); 631} 632 633 634Handle<Object> Factory::NewError(const char* maker, const char* type, 635 Vector< Handle<Object> > args) { 636 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. 637 Handle<FixedArray> array = NewFixedArray(args.length()); 638 for (int i = 0; i < args.length(); i++) { 639 array->set(i, *args[i]); 640 } 641 Handle<JSArray> object = NewJSArrayWithElements(array); 642 Handle<Object> result = NewError(maker, type, object); 643 return result.EscapeFrom(&scope); 644} 645 646 647Handle<Object> Factory::NewEvalError(const char* type, 648 Vector< Handle<Object> > args) { 649 return NewError("MakeEvalError", type, args); 650} 651 652 653Handle<Object> Factory::NewError(const char* type, 654 Vector< Handle<Object> > args) { 655 return NewError("MakeError", type, args); 656} 657 658 659Handle<Object> Factory::NewError(const char* maker, 660 const char* type, 661 Handle<JSArray> args) { 662 Handle<String> make_str = LookupAsciiSymbol(maker); 663 Handle<Object> fun_obj( 664 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str)); 665 // If the builtins haven't been properly configured yet this error 666 // constructor may not have been defined. Bail out. 667 if (!fun_obj->IsJSFunction()) 668 return undefined_value(); 669 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); 670 Handle<Object> type_obj = LookupAsciiSymbol(type); 671 Handle<Object> argv[] = { type_obj, args }; 672 673 // Invoke the JavaScript factory method. If an exception is thrown while 674 // running the factory method, use the exception as the result. 675 bool caught_exception; 676 Handle<Object> result = Execution::TryCall(fun, 677 isolate()->js_builtins_object(), 678 ARRAY_SIZE(argv), 679 argv, 680 &caught_exception); 681 return result; 682} 683 684 685Handle<Object> Factory::NewError(Handle<String> message) { 686 return NewError("$Error", message); 687} 688 689 690Handle<Object> Factory::NewError(const char* constructor, 691 Handle<String> message) { 692 Handle<String> constr = LookupAsciiSymbol(constructor); 693 Handle<JSFunction> fun = Handle<JSFunction>( 694 JSFunction::cast(isolate()->js_builtins_object()-> 695 GetPropertyNoExceptionThrown(*constr))); 696 Handle<Object> argv[] = { message }; 697 698 // Invoke the JavaScript factory method. If an exception is thrown while 699 // running the factory method, use the exception as the result. 700 bool caught_exception; 701 Handle<Object> result = Execution::TryCall(fun, 702 isolate()->js_builtins_object(), 703 ARRAY_SIZE(argv), 704 argv, 705 &caught_exception); 706 return result; 707} 708 709 710Handle<JSFunction> Factory::NewFunction(Handle<String> name, 711 InstanceType type, 712 int instance_size, 713 Handle<Code> code, 714 bool force_initial_map) { 715 // Allocate the function 716 Handle<JSFunction> function = NewFunction(name, the_hole_value()); 717 718 // Set up the code pointer in both the shared function info and in 719 // the function itself. 720 function->shared()->set_code(*code); 721 function->set_code(*code); 722 723 if (force_initial_map || 724 type != JS_OBJECT_TYPE || 725 instance_size != JSObject::kHeaderSize) { 726 Handle<Map> initial_map = NewMap(type, instance_size); 727 Handle<JSObject> prototype = NewFunctionPrototype(function); 728 initial_map->set_prototype(*prototype); 729 function->set_initial_map(*initial_map); 730 initial_map->set_constructor(*function); 731 } else { 732 ASSERT(!function->has_initial_map()); 733 ASSERT(!function->has_prototype()); 734 } 735 736 return function; 737} 738 739 740Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, 741 InstanceType type, 742 int instance_size, 743 Handle<JSObject> prototype, 744 Handle<Code> code, 745 bool force_initial_map) { 746 // Allocate the function. 747 Handle<JSFunction> function = NewFunction(name, prototype); 748 749 // Set up the code pointer in both the shared function info and in 750 // the function itself. 751 function->shared()->set_code(*code); 752 function->set_code(*code); 753 754 if (force_initial_map || 755 type != JS_OBJECT_TYPE || 756 instance_size != JSObject::kHeaderSize) { 757 ElementsKind default_elements_kind = FLAG_smi_only_arrays 758 ? FAST_SMI_ONLY_ELEMENTS 759 : FAST_ELEMENTS; 760 Handle<Map> initial_map = NewMap(type, 761 instance_size, 762 default_elements_kind); 763 function->set_initial_map(*initial_map); 764 initial_map->set_constructor(*function); 765 } 766 767 // Set function.prototype and give the prototype a constructor 768 // property that refers to the function. 769 SetPrototypeProperty(function, prototype); 770 // Currently safe because it is only invoked from Genesis. 771 CHECK_NOT_EMPTY_HANDLE(isolate(), 772 JSObject::SetLocalPropertyIgnoreAttributes( 773 prototype, constructor_symbol(), 774 function, DONT_ENUM)); 775 return function; 776} 777 778 779Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, 780 Handle<Code> code) { 781 Handle<JSFunction> function = NewFunctionWithoutPrototype(name, 782 CLASSIC_MODE); 783 function->shared()->set_code(*code); 784 function->set_code(*code); 785 ASSERT(!function->has_initial_map()); 786 ASSERT(!function->has_prototype()); 787 return function; 788} 789 790 791Handle<ScopeInfo> Factory::NewScopeInfo(int length) { 792 CALL_HEAP_FUNCTION( 793 isolate(), 794 isolate()->heap()->AllocateScopeInfo(length), 795 ScopeInfo); 796} 797 798 799Handle<Code> Factory::NewCode(const CodeDesc& desc, 800 Code::Flags flags, 801 Handle<Object> self_ref, 802 bool immovable) { 803 CALL_HEAP_FUNCTION(isolate(), 804 isolate()->heap()->CreateCode( 805 desc, flags, self_ref, immovable), 806 Code); 807} 808 809 810Handle<Code> Factory::CopyCode(Handle<Code> code) { 811 CALL_HEAP_FUNCTION(isolate(), 812 isolate()->heap()->CopyCode(*code), 813 Code); 814} 815 816 817Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { 818 CALL_HEAP_FUNCTION(isolate(), 819 isolate()->heap()->CopyCode(*code, reloc_info), 820 Code); 821} 822 823 824MUST_USE_RESULT static inline MaybeObject* DoCopyInsert( 825 DescriptorArray* array, 826 String* key, 827 Object* value, 828 PropertyAttributes attributes) { 829 CallbacksDescriptor desc(key, value, attributes); 830 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); 831 return obj; 832} 833 834 835// Allocate the new array. 836Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor( 837 Handle<DescriptorArray> array, 838 Handle<String> key, 839 Handle<Object> value, 840 PropertyAttributes attributes) { 841 CALL_HEAP_FUNCTION(isolate(), 842 DoCopyInsert(*array, *key, *value, attributes), 843 DescriptorArray); 844} 845 846 847Handle<String> Factory::SymbolFromString(Handle<String> value) { 848 CALL_HEAP_FUNCTION(isolate(), 849 isolate()->heap()->LookupSymbol(*value), String); 850} 851 852 853Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( 854 Handle<DescriptorArray> array, 855 Handle<Object> descriptors) { 856 v8::NeanderArray callbacks(descriptors); 857 int nof_callbacks = callbacks.length(); 858 Handle<DescriptorArray> result = 859 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); 860 861 // Number of descriptors added to the result so far. 862 int descriptor_count = 0; 863 864 // Ensure that marking will not progress and change color of objects. 865 DescriptorArray::WhitenessWitness witness(*result); 866 867 // Copy the descriptors from the array. 868 for (int i = 0; i < array->number_of_descriptors(); i++) { 869 if (!array->IsNullDescriptor(i)) { 870 result->CopyFrom(descriptor_count++, *array, i, witness); 871 } 872 } 873 874 // Number of duplicates detected. 875 int duplicates = 0; 876 877 // Fill in new callback descriptors. Process the callbacks from 878 // back to front so that the last callback with a given name takes 879 // precedence over previously added callbacks with that name. 880 for (int i = nof_callbacks - 1; i >= 0; i--) { 881 Handle<AccessorInfo> entry = 882 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); 883 // Ensure the key is a symbol before writing into the instance descriptor. 884 Handle<String> key = 885 SymbolFromString(Handle<String>(String::cast(entry->name()))); 886 // Check if a descriptor with this name already exists before writing. 887 if (result->LinearSearch(*key, descriptor_count) == 888 DescriptorArray::kNotFound) { 889 CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); 890 result->Set(descriptor_count, &desc, witness); 891 descriptor_count++; 892 } else { 893 duplicates++; 894 } 895 } 896 897 // If duplicates were detected, allocate a result of the right size 898 // and transfer the elements. 899 if (duplicates > 0) { 900 int number_of_descriptors = result->number_of_descriptors() - duplicates; 901 Handle<DescriptorArray> new_result = 902 NewDescriptorArray(number_of_descriptors); 903 for (int i = 0; i < number_of_descriptors; i++) { 904 new_result->CopyFrom(i, *result, i, witness); 905 } 906 result = new_result; 907 } 908 909 // Sort the result before returning. 910 result->Sort(witness); 911 return result; 912} 913 914 915Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, 916 PretenureFlag pretenure) { 917 CALL_HEAP_FUNCTION( 918 isolate(), 919 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject); 920} 921 922 923Handle<GlobalObject> Factory::NewGlobalObject( 924 Handle<JSFunction> constructor) { 925 CALL_HEAP_FUNCTION(isolate(), 926 isolate()->heap()->AllocateGlobalObject(*constructor), 927 GlobalObject); 928} 929 930 931 932Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { 933 CALL_HEAP_FUNCTION( 934 isolate(), 935 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED), 936 JSObject); 937} 938 939 940Handle<JSArray> Factory::NewJSArray(int capacity, 941 PretenureFlag pretenure) { 942 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure); 943 CALL_HEAP_FUNCTION(isolate(), 944 Handle<JSArray>::cast(obj)->Initialize(capacity), 945 JSArray); 946} 947 948 949Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements, 950 PretenureFlag pretenure) { 951 Handle<JSArray> result = 952 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(), 953 pretenure)); 954 result->set_length(Smi::FromInt(0)); 955 SetContent(result, elements); 956 return result; 957} 958 959 960void Factory::SetElementsCapacityAndLength(Handle<JSArray> array, 961 int capacity, 962 int length) { 963 ElementsAccessor* accessor = array->GetElementsAccessor(); 964 CALL_HEAP_FUNCTION_VOID( 965 isolate(), 966 accessor->SetCapacityAndLength(*array, capacity, length)); 967} 968 969 970void Factory::SetContent(Handle<JSArray> array, 971 Handle<FixedArrayBase> elements) { 972 CALL_HEAP_FUNCTION_VOID( 973 isolate(), 974 array->SetContent(*elements)); 975} 976 977 978void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) { 979 CALL_HEAP_FUNCTION_VOID( 980 isolate(), 981 array->EnsureCanContainHeapObjectElements()); 982} 983 984 985void Factory::EnsureCanContainElements(Handle<JSArray> array, 986 Handle<FixedArrayBase> elements, 987 EnsureElementsMode mode) { 988 CALL_HEAP_FUNCTION_VOID( 989 isolate(), 990 array->EnsureCanContainElements(*elements, mode)); 991} 992 993 994Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler, 995 Handle<Object> prototype) { 996 CALL_HEAP_FUNCTION( 997 isolate(), 998 isolate()->heap()->AllocateJSProxy(*handler, *prototype), 999 JSProxy); 1000} 1001 1002 1003void Factory::BecomeJSObject(Handle<JSReceiver> object) { 1004 CALL_HEAP_FUNCTION_VOID( 1005 isolate(), 1006 isolate()->heap()->ReinitializeJSReceiver( 1007 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize)); 1008} 1009 1010 1011void Factory::BecomeJSFunction(Handle<JSReceiver> object) { 1012 CALL_HEAP_FUNCTION_VOID( 1013 isolate(), 1014 isolate()->heap()->ReinitializeJSReceiver( 1015 *object, JS_FUNCTION_TYPE, JSFunction::kSize)); 1016} 1017 1018 1019void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) { 1020 CALL_HEAP_FUNCTION_VOID( 1021 isolate(), 1022 object->SetIdentityHash(hash, ALLOW_CREATION)); 1023} 1024 1025 1026Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( 1027 Handle<String> name, 1028 int number_of_literals, 1029 Handle<Code> code, 1030 Handle<ScopeInfo> scope_info) { 1031 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); 1032 shared->set_code(*code); 1033 shared->set_scope_info(*scope_info); 1034 int literals_array_size = number_of_literals; 1035 // If the function contains object, regexp or array literals, 1036 // allocate extra space for a literals array prefix containing the 1037 // context. 1038 if (number_of_literals > 0) { 1039 literals_array_size += JSFunction::kLiteralsPrefixSize; 1040 } 1041 shared->set_num_literals(literals_array_size); 1042 return shared; 1043} 1044 1045 1046Handle<JSMessageObject> Factory::NewJSMessageObject( 1047 Handle<String> type, 1048 Handle<JSArray> arguments, 1049 int start_position, 1050 int end_position, 1051 Handle<Object> script, 1052 Handle<Object> stack_trace, 1053 Handle<Object> stack_frames) { 1054 CALL_HEAP_FUNCTION(isolate(), 1055 isolate()->heap()->AllocateJSMessageObject(*type, 1056 *arguments, 1057 start_position, 1058 end_position, 1059 *script, 1060 *stack_trace, 1061 *stack_frames), 1062 JSMessageObject); 1063} 1064 1065Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { 1066 CALL_HEAP_FUNCTION(isolate(), 1067 isolate()->heap()->AllocateSharedFunctionInfo(*name), 1068 SharedFunctionInfo); 1069} 1070 1071 1072Handle<String> Factory::NumberToString(Handle<Object> number) { 1073 CALL_HEAP_FUNCTION(isolate(), 1074 isolate()->heap()->NumberToString(*number), String); 1075} 1076 1077 1078Handle<String> Factory::Uint32ToString(uint32_t value) { 1079 CALL_HEAP_FUNCTION(isolate(), 1080 isolate()->heap()->Uint32ToString(value), String); 1081} 1082 1083 1084Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut( 1085 Handle<SeededNumberDictionary> dictionary, 1086 uint32_t key, 1087 Handle<Object> value) { 1088 CALL_HEAP_FUNCTION(isolate(), 1089 dictionary->AtNumberPut(key, *value), 1090 SeededNumberDictionary); 1091} 1092 1093 1094Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut( 1095 Handle<UnseededNumberDictionary> dictionary, 1096 uint32_t key, 1097 Handle<Object> value) { 1098 CALL_HEAP_FUNCTION(isolate(), 1099 dictionary->AtNumberPut(key, *value), 1100 UnseededNumberDictionary); 1101} 1102 1103 1104Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, 1105 Handle<Object> prototype) { 1106 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); 1107 CALL_HEAP_FUNCTION( 1108 isolate(), 1109 isolate()->heap()->AllocateFunction(*isolate()->function_map(), 1110 *function_share, 1111 *prototype), 1112 JSFunction); 1113} 1114 1115 1116Handle<JSFunction> Factory::NewFunction(Handle<String> name, 1117 Handle<Object> prototype) { 1118 Handle<JSFunction> fun = NewFunctionHelper(name, prototype); 1119 fun->set_context(isolate()->context()->global_context()); 1120 return fun; 1121} 1122 1123 1124Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper( 1125 Handle<String> name, 1126 LanguageMode language_mode) { 1127 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); 1128 Handle<Map> map = (language_mode == CLASSIC_MODE) 1129 ? isolate()->function_without_prototype_map() 1130 : isolate()->strict_mode_function_without_prototype_map(); 1131 CALL_HEAP_FUNCTION(isolate(), 1132 isolate()->heap()->AllocateFunction( 1133 *map, 1134 *function_share, 1135 *the_hole_value()), 1136 JSFunction); 1137} 1138 1139 1140Handle<JSFunction> Factory::NewFunctionWithoutPrototype( 1141 Handle<String> name, 1142 LanguageMode language_mode) { 1143 Handle<JSFunction> fun = 1144 NewFunctionWithoutPrototypeHelper(name, language_mode); 1145 fun->set_context(isolate()->context()->global_context()); 1146 return fun; 1147} 1148 1149 1150Handle<Object> Factory::ToObject(Handle<Object> object) { 1151 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object); 1152} 1153 1154 1155Handle<Object> Factory::ToObject(Handle<Object> object, 1156 Handle<Context> global_context) { 1157 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object); 1158} 1159 1160 1161#ifdef ENABLE_DEBUGGER_SUPPORT 1162Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { 1163 // Get the original code of the function. 1164 Handle<Code> code(shared->code()); 1165 1166 // Create a copy of the code before allocating the debug info object to avoid 1167 // allocation while setting up the debug info object. 1168 Handle<Code> original_code(*Factory::CopyCode(code)); 1169 1170 // Allocate initial fixed array for active break points before allocating the 1171 // debug info object to avoid allocation while setting up the debug info 1172 // object. 1173 Handle<FixedArray> break_points( 1174 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); 1175 1176 // Create and set up the debug info object. Debug info contains function, a 1177 // copy of the original code, the executing code and initial fixed array for 1178 // active break points. 1179 Handle<DebugInfo> debug_info = 1180 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE)); 1181 debug_info->set_shared(*shared); 1182 debug_info->set_original_code(*original_code); 1183 debug_info->set_code(*code); 1184 debug_info->set_break_points(*break_points); 1185 1186 // Link debug info to function. 1187 shared->set_debug_info(*debug_info); 1188 1189 return debug_info; 1190} 1191#endif 1192 1193 1194Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, 1195 int length) { 1196 CALL_HEAP_FUNCTION( 1197 isolate(), 1198 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject); 1199} 1200 1201 1202Handle<JSFunction> Factory::CreateApiFunction( 1203 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { 1204 Handle<Code> code = isolate()->builtins()->HandleApiCall(); 1205 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); 1206 1207 int internal_field_count = 0; 1208 if (!obj->instance_template()->IsUndefined()) { 1209 Handle<ObjectTemplateInfo> instance_template = 1210 Handle<ObjectTemplateInfo>( 1211 ObjectTemplateInfo::cast(obj->instance_template())); 1212 internal_field_count = 1213 Smi::cast(instance_template->internal_field_count())->value(); 1214 } 1215 1216 int instance_size = kPointerSize * internal_field_count; 1217 InstanceType type = INVALID_TYPE; 1218 switch (instance_type) { 1219 case JavaScriptObject: 1220 type = JS_OBJECT_TYPE; 1221 instance_size += JSObject::kHeaderSize; 1222 break; 1223 case InnerGlobalObject: 1224 type = JS_GLOBAL_OBJECT_TYPE; 1225 instance_size += JSGlobalObject::kSize; 1226 break; 1227 case OuterGlobalObject: 1228 type = JS_GLOBAL_PROXY_TYPE; 1229 instance_size += JSGlobalProxy::kSize; 1230 break; 1231 default: 1232 break; 1233 } 1234 ASSERT(type != INVALID_TYPE); 1235 1236 Handle<JSFunction> result = 1237 NewFunction(Factory::empty_symbol(), 1238 type, 1239 instance_size, 1240 code, 1241 true); 1242 // Set class name. 1243 Handle<Object> class_name = Handle<Object>(obj->class_name()); 1244 if (class_name->IsString()) { 1245 result->shared()->set_instance_class_name(*class_name); 1246 result->shared()->set_name(*class_name); 1247 } 1248 1249 Handle<Map> map = Handle<Map>(result->initial_map()); 1250 1251 // Mark as undetectable if needed. 1252 if (obj->undetectable()) { 1253 map->set_is_undetectable(); 1254 } 1255 1256 // Mark as hidden for the __proto__ accessor if needed. 1257 if (obj->hidden_prototype()) { 1258 map->set_is_hidden_prototype(); 1259 } 1260 1261 // Mark as needs_access_check if needed. 1262 if (obj->needs_access_check()) { 1263 map->set_is_access_check_needed(true); 1264 } 1265 1266 // Set interceptor information in the map. 1267 if (!obj->named_property_handler()->IsUndefined()) { 1268 map->set_has_named_interceptor(); 1269 } 1270 if (!obj->indexed_property_handler()->IsUndefined()) { 1271 map->set_has_indexed_interceptor(); 1272 } 1273 1274 // Set instance call-as-function information in the map. 1275 if (!obj->instance_call_handler()->IsUndefined()) { 1276 map->set_has_instance_call_handler(); 1277 } 1278 1279 result->shared()->set_function_data(*obj); 1280 result->shared()->set_construct_stub(*construct_stub); 1281 result->shared()->DontAdaptArguments(); 1282 1283 // Recursively copy parent templates' accessors, 'data' may be modified. 1284 Handle<DescriptorArray> array = 1285 Handle<DescriptorArray>(map->instance_descriptors()); 1286 while (true) { 1287 Handle<Object> props = Handle<Object>(obj->property_accessors()); 1288 if (!props->IsUndefined()) { 1289 array = CopyAppendCallbackDescriptors(array, props); 1290 } 1291 Handle<Object> parent = Handle<Object>(obj->parent_template()); 1292 if (parent->IsUndefined()) break; 1293 obj = Handle<FunctionTemplateInfo>::cast(parent); 1294 } 1295 if (!array->IsEmpty()) { 1296 map->set_instance_descriptors(*array); 1297 } 1298 1299 ASSERT(result->shared()->IsApiFunction()); 1300 return result; 1301} 1302 1303 1304Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { 1305 CALL_HEAP_FUNCTION(isolate(), 1306 MapCache::Allocate(at_least_space_for), MapCache); 1307} 1308 1309 1310MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context, 1311 FixedArray* keys, 1312 Map* map) { 1313 Object* result; 1314 { MaybeObject* maybe_result = 1315 MapCache::cast(context->map_cache())->Put(keys, map); 1316 if (!maybe_result->ToObject(&result)) return maybe_result; 1317 } 1318 context->set_map_cache(MapCache::cast(result)); 1319 return result; 1320} 1321 1322 1323Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, 1324 Handle<FixedArray> keys, 1325 Handle<Map> map) { 1326 CALL_HEAP_FUNCTION(isolate(), 1327 UpdateMapCacheWith(*context, *keys, *map), MapCache); 1328} 1329 1330 1331Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, 1332 Handle<FixedArray> keys) { 1333 if (context->map_cache()->IsUndefined()) { 1334 // Allocate the new map cache for the global context. 1335 Handle<MapCache> new_cache = NewMapCache(24); 1336 context->set_map_cache(*new_cache); 1337 } 1338 // Check to see whether there is a matching element in the cache. 1339 Handle<MapCache> cache = 1340 Handle<MapCache>(MapCache::cast(context->map_cache())); 1341 Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); 1342 if (result->IsMap()) return Handle<Map>::cast(result); 1343 // Create a new map and add it to the cache. 1344 Handle<Map> map = 1345 CopyMap(Handle<Map>(context->object_function()->initial_map()), 1346 keys->length()); 1347 AddToMapCache(context, keys, map); 1348 return Handle<Map>(map); 1349} 1350 1351 1352void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, 1353 JSRegExp::Type type, 1354 Handle<String> source, 1355 JSRegExp::Flags flags, 1356 Handle<Object> data) { 1357 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); 1358 1359 store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); 1360 store->set(JSRegExp::kSourceIndex, *source); 1361 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); 1362 store->set(JSRegExp::kAtomPatternIndex, *data); 1363 regexp->set_data(*store); 1364} 1365 1366void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, 1367 JSRegExp::Type type, 1368 Handle<String> source, 1369 JSRegExp::Flags flags, 1370 int capture_count) { 1371 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); 1372 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue); 1373 store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); 1374 store->set(JSRegExp::kSourceIndex, *source); 1375 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); 1376 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized); 1377 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized); 1378 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized); 1379 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized); 1380 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); 1381 store->set(JSRegExp::kIrregexpCaptureCountIndex, 1382 Smi::FromInt(capture_count)); 1383 regexp->set_data(*store); 1384} 1385 1386 1387 1388void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, 1389 Handle<JSObject> instance, 1390 bool* pending_exception) { 1391 // Configure the instance by adding the properties specified by the 1392 // instance template. 1393 Handle<Object> instance_template = Handle<Object>(desc->instance_template()); 1394 if (!instance_template->IsUndefined()) { 1395 Execution::ConfigureInstance(instance, 1396 instance_template, 1397 pending_exception); 1398 } else { 1399 *pending_exception = false; 1400 } 1401} 1402 1403 1404Handle<Object> Factory::GlobalConstantFor(Handle<String> name) { 1405 Heap* h = isolate()->heap(); 1406 if (name->Equals(h->undefined_symbol())) return undefined_value(); 1407 if (name->Equals(h->nan_symbol())) return nan_value(); 1408 if (name->Equals(h->infinity_symbol())) return infinity_value(); 1409 return Handle<Object>::null(); 1410} 1411 1412 1413Handle<Object> Factory::ToBoolean(bool value) { 1414 return Handle<Object>(value 1415 ? isolate()->heap()->true_value() 1416 : isolate()->heap()->false_value()); 1417} 1418 1419 1420} } // namespace v8::internal 1421