1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_FACTORY_H_ 6 #define V8_FACTORY_H_ 7 8 #include "src/globals.h" 9 #include "src/isolate.h" 10 #include "src/messages.h" 11 #include "src/type-feedback-vector.h" 12 13 namespace v8 { 14 namespace internal { 15 16 enum FunctionMode { 17 // With prototype. 18 FUNCTION_WITH_WRITEABLE_PROTOTYPE, 19 FUNCTION_WITH_READONLY_PROTOTYPE, 20 // Without prototype. 21 FUNCTION_WITHOUT_PROTOTYPE 22 }; 23 24 // Interface for handle based allocation. 25 class V8_EXPORT_PRIVATE Factory final { 26 public: 27 Handle<Oddball> NewOddball(Handle<Map> map, const char* to_string, 28 Handle<Object> to_number, const char* type_of, 29 byte kind); 30 31 // Allocates a fixed array initialized with undefined values. 32 Handle<FixedArray> NewFixedArray(int size, 33 PretenureFlag pretenure = NOT_TENURED); 34 // Tries allocating a fixed array initialized with undefined values. 35 // In case of an allocation failure (OOM) an empty handle is returned. 36 // The caller has to manually signal an 37 // v8::internal::Heap::FatalProcessOutOfMemory typically by calling 38 // NewFixedArray as a fallback. 39 MUST_USE_RESULT 40 MaybeHandle<FixedArray> TryNewFixedArray( 41 int size, PretenureFlag pretenure = NOT_TENURED); 42 43 // Allocate a new fixed array with non-existing entries (the hole). 44 Handle<FixedArray> NewFixedArrayWithHoles( 45 int size, 46 PretenureFlag pretenure = NOT_TENURED); 47 48 // Allocates an uninitialized fixed array. It must be filled by the caller. 49 Handle<FixedArray> NewUninitializedFixedArray(int size); 50 51 // Allocate a new uninitialized fixed double array. 52 // The function returns a pre-allocated empty fixed array for capacity = 0, 53 // so the return type must be the general fixed array class. 54 Handle<FixedArrayBase> NewFixedDoubleArray( 55 int size, 56 PretenureFlag pretenure = NOT_TENURED); 57 58 // Allocate a new fixed double array with hole values. 59 Handle<FixedArrayBase> NewFixedDoubleArrayWithHoles( 60 int size, 61 PretenureFlag pretenure = NOT_TENURED); 62 63 Handle<FrameArray> NewFrameArray(int number_of_frames, 64 PretenureFlag pretenure = NOT_TENURED); 65 66 Handle<OrderedHashSet> NewOrderedHashSet(); 67 Handle<OrderedHashMap> NewOrderedHashMap(); 68 69 // Create a new boxed value. 70 Handle<Box> NewBox(Handle<Object> value); 71 72 // Create a new PromiseReactionJobInfo struct. 73 Handle<PromiseReactionJobInfo> NewPromiseReactionJobInfo( 74 Handle<Object> value, Handle<Object> tasks, Handle<Object> deferred, 75 Handle<Object> debug_id, Handle<Object> debug_name, 76 Handle<Context> context); 77 78 // Create a new PromiseResolveThenableJobInfo struct. 79 Handle<PromiseResolveThenableJobInfo> NewPromiseResolveThenableJobInfo( 80 Handle<JSReceiver> thenable, Handle<JSReceiver> then, 81 Handle<JSFunction> resolve, Handle<JSFunction> reject, 82 Handle<Object> debug_id, Handle<Object> debug_name, 83 Handle<Context> context); 84 85 // Create a new PrototypeInfo struct. 86 Handle<PrototypeInfo> NewPrototypeInfo(); 87 88 // Create a new Tuple3 struct. 89 Handle<Tuple3> NewTuple3(Handle<Object> value1, Handle<Object> value2, 90 Handle<Object> value3); 91 92 // Create a new ContextExtension struct. 93 Handle<ContextExtension> NewContextExtension(Handle<ScopeInfo> scope_info, 94 Handle<Object> extension); 95 96 // Create a pre-tenured empty AccessorPair. 97 Handle<AccessorPair> NewAccessorPair(); 98 99 // Create an empty TypeFeedbackInfo. 100 Handle<TypeFeedbackInfo> NewTypeFeedbackInfo(); 101 102 // Finds the internalized copy for string in the string table. 103 // If not found, a new string is added to the table and returned. 104 Handle<String> InternalizeUtf8String(Vector<const char> str); InternalizeUtf8String(const char * str)105 Handle<String> InternalizeUtf8String(const char* str) { 106 return InternalizeUtf8String(CStrVector(str)); 107 } 108 109 Handle<String> InternalizeOneByteString(Vector<const uint8_t> str); 110 Handle<String> InternalizeOneByteString( 111 Handle<SeqOneByteString>, int from, int length); 112 113 Handle<String> InternalizeTwoByteString(Vector<const uc16> str); 114 115 template<class StringTableKey> 116 Handle<String> InternalizeStringWithKey(StringTableKey* key); 117 118 // Internalized strings are created in the old generation (data space). InternalizeString(Handle<String> string)119 Handle<String> InternalizeString(Handle<String> string) { 120 if (string->IsInternalizedString()) return string; 121 return StringTable::LookupString(isolate(), string); 122 } 123 InternalizeName(Handle<Name> name)124 Handle<Name> InternalizeName(Handle<Name> name) { 125 if (name->IsUniqueName()) return name; 126 return StringTable::LookupString(isolate(), Handle<String>::cast(name)); 127 } 128 129 // String creation functions. Most of the string creation functions take 130 // a Heap::PretenureFlag argument to optionally request that they be 131 // allocated in the old generation. The pretenure flag defaults to 132 // DONT_TENURE. 133 // 134 // Creates a new String object. There are two String encodings: one-byte and 135 // two-byte. One should choose between the three string factory functions 136 // based on the encoding of the string buffer that the string is 137 // initialized from. 138 // - ...FromOneByte initializes the string from a buffer that is Latin1 139 // encoded (it does not check that the buffer is Latin1 encoded) and 140 // the result will be Latin1 encoded. 141 // - ...FromUtf8 initializes the string from a buffer that is UTF-8 142 // encoded. If the characters are all ASCII characters, the result 143 // will be Latin1 encoded, otherwise it will converted to two-byte. 144 // - ...FromTwoByte initializes the string from a buffer that is two-byte 145 // encoded. If the characters are all Latin1 characters, the result 146 // will be converted to Latin1, otherwise it will be left as two-byte. 147 // 148 // One-byte strings are pretenured when used as keys in the SourceCodeCache. 149 MUST_USE_RESULT MaybeHandle<String> NewStringFromOneByte( 150 Vector<const uint8_t> str, PretenureFlag pretenure = NOT_TENURED); 151 152 template <size_t N> 153 inline Handle<String> NewStringFromStaticChars( 154 const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) { 155 DCHECK(N == StrLength(str) + 1); 156 return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure) 157 .ToHandleChecked(); 158 } 159 160 inline Handle<String> NewStringFromAsciiChecked( 161 const char* str, 162 PretenureFlag pretenure = NOT_TENURED) { 163 return NewStringFromOneByte( 164 OneByteVector(str), pretenure).ToHandleChecked(); 165 } 166 167 168 // Allocates and fully initializes a String. There are two String encodings: 169 // one-byte and two-byte. One should choose between the threestring 170 // allocation functions based on the encoding of the string buffer used to 171 // initialized the string. 172 // - ...FromOneByte initializes the string from a buffer that is Latin1 173 // encoded (it does not check that the buffer is Latin1 encoded) and the 174 // result will be Latin1 encoded. 175 // - ...FromUTF8 initializes the string from a buffer that is UTF-8 176 // encoded. If the characters are all ASCII characters, the result 177 // will be Latin1 encoded, otherwise it will converted to two-byte. 178 // - ...FromTwoByte initializes the string from a buffer that is two-byte 179 // encoded. If the characters are all Latin1 characters, the 180 // result will be converted to Latin1, otherwise it will be left as 181 // two-byte. 182 183 // TODO(dcarney): remove this function. 184 MUST_USE_RESULT inline MaybeHandle<String> NewStringFromAscii( 185 Vector<const char> str, 186 PretenureFlag pretenure = NOT_TENURED) { 187 return NewStringFromOneByte(Vector<const uint8_t>::cast(str), pretenure); 188 } 189 190 // UTF8 strings are pretenured when used for regexp literal patterns and 191 // flags in the parser. 192 MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8( 193 Vector<const char> str, PretenureFlag pretenure = NOT_TENURED); 194 195 MUST_USE_RESULT MaybeHandle<String> NewStringFromUtf8SubString( 196 Handle<SeqOneByteString> str, int begin, int end, 197 PretenureFlag pretenure = NOT_TENURED); 198 199 MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte( 200 Vector<const uc16> str, PretenureFlag pretenure = NOT_TENURED); 201 202 MUST_USE_RESULT MaybeHandle<String> NewStringFromTwoByte( 203 const ZoneVector<uc16>* str, PretenureFlag pretenure = NOT_TENURED); 204 205 Handle<JSStringIterator> NewJSStringIterator(Handle<String> string); 206 207 // Allocates an internalized string in old space based on the character 208 // stream. 209 Handle<String> NewInternalizedStringFromUtf8(Vector<const char> str, 210 int chars, uint32_t hash_field); 211 212 Handle<String> NewOneByteInternalizedString(Vector<const uint8_t> str, 213 uint32_t hash_field); 214 215 Handle<String> NewOneByteInternalizedSubString( 216 Handle<SeqOneByteString> string, int offset, int length, 217 uint32_t hash_field); 218 219 Handle<String> NewTwoByteInternalizedString(Vector<const uc16> str, 220 uint32_t hash_field); 221 222 Handle<String> NewInternalizedStringImpl(Handle<String> string, int chars, 223 uint32_t hash_field); 224 225 // Compute the matching internalized string map for a string if possible. 226 // Empty handle is returned if string is in new space or not flattened. 227 MUST_USE_RESULT MaybeHandle<Map> InternalizedStringMapForString( 228 Handle<String> string); 229 230 // Allocates and partially initializes an one-byte or two-byte String. The 231 // characters of the string are uninitialized. Currently used in regexp code 232 // only, where they are pretenured. 233 MUST_USE_RESULT MaybeHandle<SeqOneByteString> NewRawOneByteString( 234 int length, 235 PretenureFlag pretenure = NOT_TENURED); 236 MUST_USE_RESULT MaybeHandle<SeqTwoByteString> NewRawTwoByteString( 237 int length, 238 PretenureFlag pretenure = NOT_TENURED); 239 240 // Creates a single character string where the character has given code. 241 // A cache is used for Latin1 codes. 242 Handle<String> LookupSingleCharacterStringFromCode(uint32_t code); 243 244 // Create a new cons string object which consists of a pair of strings. 245 MUST_USE_RESULT MaybeHandle<String> NewConsString(Handle<String> left, 246 Handle<String> right); 247 248 // Create or lookup a single characters tring made up of a utf16 surrogate 249 // pair. 250 Handle<String> NewSurrogatePairString(uint16_t lead, uint16_t trail); 251 252 // Create a new string object which holds a proper substring of a string. 253 Handle<String> NewProperSubString(Handle<String> str, 254 int begin, 255 int end); 256 257 // Create a new string object which holds a substring of a string. NewSubString(Handle<String> str,int begin,int end)258 Handle<String> NewSubString(Handle<String> str, int begin, int end) { 259 if (begin == 0 && end == str->length()) return str; 260 return NewProperSubString(str, begin, end); 261 } 262 263 // Creates a new external String object. There are two String encodings 264 // in the system: one-byte and two-byte. Unlike other String types, it does 265 // not make sense to have a UTF-8 factory function for external strings, 266 // because we cannot change the underlying buffer. Note that these strings 267 // are backed by a string resource that resides outside the V8 heap. 268 MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromOneByte( 269 const ExternalOneByteString::Resource* resource); 270 MUST_USE_RESULT MaybeHandle<String> NewExternalStringFromTwoByte( 271 const ExternalTwoByteString::Resource* resource); 272 // Create a new external string object for one-byte encoded native script. 273 // It does not cache the resource data pointer. 274 Handle<ExternalOneByteString> NewNativeSourceString( 275 const ExternalOneByteString::Resource* resource); 276 277 // Create a symbol. 278 Handle<Symbol> NewSymbol(); 279 Handle<Symbol> NewPrivateSymbol(); 280 281 // Create a global (but otherwise uninitialized) context. 282 Handle<Context> NewNativeContext(); 283 284 // Create a script context. 285 Handle<Context> NewScriptContext(Handle<JSFunction> function, 286 Handle<ScopeInfo> scope_info); 287 288 // Create an empty script context table. 289 Handle<ScriptContextTable> NewScriptContextTable(); 290 291 // Create a module context. 292 Handle<Context> NewModuleContext(Handle<Module> module, 293 Handle<JSFunction> function, 294 Handle<ScopeInfo> scope_info); 295 296 // Create a function context. 297 Handle<Context> NewFunctionContext(int length, Handle<JSFunction> function); 298 299 // Create a catch context. 300 Handle<Context> NewCatchContext(Handle<JSFunction> function, 301 Handle<Context> previous, 302 Handle<ScopeInfo> scope_info, 303 Handle<String> name, 304 Handle<Object> thrown_object); 305 306 // Create a 'with' context. 307 Handle<Context> NewWithContext(Handle<JSFunction> function, 308 Handle<Context> previous, 309 Handle<ScopeInfo> scope_info, 310 Handle<JSReceiver> extension); 311 312 Handle<Context> NewDebugEvaluateContext(Handle<Context> previous, 313 Handle<ScopeInfo> scope_info, 314 Handle<JSReceiver> extension, 315 Handle<Context> wrapped, 316 Handle<StringSet> whitelist); 317 318 // Create a block context. 319 Handle<Context> NewBlockContext(Handle<JSFunction> function, 320 Handle<Context> previous, 321 Handle<ScopeInfo> scope_info); 322 // Create a promise context. 323 Handle<Context> NewPromiseResolvingFunctionContext(int length); 324 325 // Allocate a new struct. The struct is pretenured (allocated directly in 326 // the old generation). 327 Handle<Struct> NewStruct(InstanceType type); 328 329 Handle<AliasedArgumentsEntry> NewAliasedArgumentsEntry( 330 int aliased_context_slot); 331 332 Handle<AccessorInfo> NewAccessorInfo(); 333 334 Handle<Script> NewScript(Handle<String> source); 335 336 // Foreign objects are pretenured when allocated by the bootstrapper. 337 Handle<Foreign> NewForeign(Address addr, 338 PretenureFlag pretenure = NOT_TENURED); 339 340 // Allocate a new foreign object. The foreign is pretenured (allocated 341 // directly in the old generation). 342 Handle<Foreign> NewForeign(const AccessorDescriptor* foreign); 343 344 Handle<ByteArray> NewByteArray(int length, 345 PretenureFlag pretenure = NOT_TENURED); 346 347 Handle<BytecodeArray> NewBytecodeArray(int length, const byte* raw_bytecodes, 348 int frame_size, int parameter_count, 349 Handle<FixedArray> constant_pool); 350 351 Handle<FixedTypedArrayBase> NewFixedTypedArrayWithExternalPointer( 352 int length, ExternalArrayType array_type, void* external_pointer, 353 PretenureFlag pretenure = NOT_TENURED); 354 355 Handle<FixedTypedArrayBase> NewFixedTypedArray( 356 int length, ExternalArrayType array_type, bool initialize, 357 PretenureFlag pretenure = NOT_TENURED); 358 359 Handle<Cell> NewCell(Handle<Object> value); 360 361 Handle<PropertyCell> NewPropertyCell(); 362 363 Handle<WeakCell> NewWeakCell(Handle<HeapObject> value); 364 365 Handle<TransitionArray> NewTransitionArray(int capacity); 366 367 // Allocate a tenured AllocationSite. It's payload is null. 368 Handle<AllocationSite> NewAllocationSite(); 369 370 Handle<Map> NewMap( 371 InstanceType type, 372 int instance_size, 373 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND); 374 375 Handle<HeapObject> NewFillerObject(int size, 376 bool double_align, 377 AllocationSpace space); 378 379 Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function); 380 381 Handle<JSObject> CopyJSObject(Handle<JSObject> object); 382 383 Handle<JSObject> CopyJSObjectWithAllocationSite(Handle<JSObject> object, 384 Handle<AllocationSite> site); 385 386 Handle<FixedArray> CopyFixedArrayWithMap(Handle<FixedArray> array, 387 Handle<Map> map); 388 389 Handle<FixedArray> CopyFixedArrayAndGrow( 390 Handle<FixedArray> array, int grow_by, 391 PretenureFlag pretenure = NOT_TENURED); 392 393 Handle<FixedArray> CopyFixedArrayUpTo(Handle<FixedArray> array, int new_len, 394 PretenureFlag pretenure = NOT_TENURED); 395 396 Handle<FixedArray> CopyFixedArray(Handle<FixedArray> array); 397 398 // This method expects a COW array in new space, and creates a copy 399 // of it in old space. 400 Handle<FixedArray> CopyAndTenureFixedCOWArray(Handle<FixedArray> array); 401 402 Handle<FixedDoubleArray> CopyFixedDoubleArray( 403 Handle<FixedDoubleArray> array); 404 405 // Numbers (e.g. literals) are pretenured by the parser. 406 // The return value may be a smi or a heap number. 407 Handle<Object> NewNumber(double value, 408 PretenureFlag pretenure = NOT_TENURED); 409 410 Handle<Object> NewNumberFromInt(int32_t value, 411 PretenureFlag pretenure = NOT_TENURED); 412 Handle<Object> NewNumberFromUint(uint32_t value, 413 PretenureFlag pretenure = NOT_TENURED); 414 Handle<Object> NewNumberFromSize(size_t value, 415 PretenureFlag pretenure = NOT_TENURED) { 416 // We can't use Smi::IsValid() here because that operates on a signed 417 // intptr_t, and casting from size_t could create a bogus sign bit. 418 if (value <= static_cast<size_t>(Smi::kMaxValue)) { 419 return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)), 420 isolate()); 421 } 422 return NewNumber(static_cast<double>(value), pretenure); 423 } 424 Handle<Object> NewNumberFromInt64(int64_t value, 425 PretenureFlag pretenure = NOT_TENURED) { 426 if (value <= std::numeric_limits<int32_t>::max() && 427 value >= std::numeric_limits<int32_t>::min() && 428 Smi::IsValid(static_cast<int32_t>(value))) { 429 return Handle<Object>(Smi::FromInt(static_cast<int32_t>(value)), 430 isolate()); 431 } 432 return NewNumber(static_cast<double>(value), pretenure); 433 } 434 Handle<HeapNumber> NewHeapNumber(double value, 435 MutableMode mode = IMMUTABLE, 436 PretenureFlag pretenure = NOT_TENURED); 437 438 #define SIMD128_NEW_DECL(TYPE, Type, type, lane_count, lane_type) \ 439 Handle<Type> New##Type(lane_type lanes[lane_count], \ 440 PretenureFlag pretenure = NOT_TENURED); 441 SIMD128_TYPES(SIMD128_NEW_DECL) 442 #undef SIMD128_NEW_DECL 443 444 Handle<JSWeakMap> NewJSWeakMap(); 445 446 Handle<JSObject> NewArgumentsObject(Handle<JSFunction> callee, int length); 447 448 // JS objects are pretenured when allocated by the bootstrapper and 449 // runtime. 450 Handle<JSObject> NewJSObject(Handle<JSFunction> constructor, 451 PretenureFlag pretenure = NOT_TENURED); 452 // JSObject without a prototype. 453 Handle<JSObject> NewJSObjectWithNullProto(); 454 455 // Global objects are pretenured and initialized based on a constructor. 456 Handle<JSGlobalObject> NewJSGlobalObject(Handle<JSFunction> constructor); 457 458 // JS objects are pretenured when allocated by the bootstrapper and 459 // runtime. 460 Handle<JSObject> NewJSObjectFromMap( 461 Handle<Map> map, 462 PretenureFlag pretenure = NOT_TENURED, 463 Handle<AllocationSite> allocation_site = Handle<AllocationSite>::null()); 464 465 // JS arrays are pretenured when allocated by the parser. 466 467 // Create a JSArray with a specified length and elements initialized 468 // according to the specified mode. 469 Handle<JSArray> NewJSArray( 470 ElementsKind elements_kind, int length, int capacity, 471 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS, 472 PretenureFlag pretenure = NOT_TENURED); 473 474 Handle<JSArray> NewJSArray( 475 int capacity, ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND, 476 PretenureFlag pretenure = NOT_TENURED) { 477 if (capacity != 0) { 478 elements_kind = GetHoleyElementsKind(elements_kind); 479 } 480 return NewJSArray(elements_kind, 0, capacity, 481 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, pretenure); 482 } 483 484 // Create a JSArray with the given elements. 485 Handle<JSArray> NewJSArrayWithElements(Handle<FixedArrayBase> elements, 486 ElementsKind elements_kind, int length, 487 PretenureFlag pretenure = NOT_TENURED); 488 489 Handle<JSArray> NewJSArrayWithElements( 490 Handle<FixedArrayBase> elements, 491 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND, 492 PretenureFlag pretenure = NOT_TENURED) { 493 return NewJSArrayWithElements(elements, elements_kind, elements->length(), 494 pretenure); 495 } 496 497 void NewJSArrayStorage( 498 Handle<JSArray> array, 499 int length, 500 int capacity, 501 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS); 502 503 Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function); 504 505 Handle<JSModuleNamespace> NewJSModuleNamespace(); 506 507 Handle<Module> NewModule(Handle<SharedFunctionInfo> code); 508 509 Handle<JSArrayBuffer> NewJSArrayBuffer( 510 SharedFlag shared = SharedFlag::kNotShared, 511 PretenureFlag pretenure = NOT_TENURED); 512 513 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type, 514 PretenureFlag pretenure = NOT_TENURED); 515 516 Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind, 517 PretenureFlag pretenure = NOT_TENURED); 518 519 // Creates a new JSTypedArray with the specified buffer. 520 Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type, 521 Handle<JSArrayBuffer> buffer, 522 size_t byte_offset, size_t length, 523 PretenureFlag pretenure = NOT_TENURED); 524 525 // Creates a new on-heap JSTypedArray. 526 Handle<JSTypedArray> NewJSTypedArray(ElementsKind elements_kind, 527 size_t number_of_elements, 528 PretenureFlag pretenure = NOT_TENURED); 529 530 Handle<JSDataView> NewJSDataView(); 531 Handle<JSDataView> NewJSDataView(Handle<JSArrayBuffer> buffer, 532 size_t byte_offset, size_t byte_length); 533 534 Handle<JSIteratorResult> NewJSIteratorResult(Handle<Object> value, bool done); 535 536 Handle<JSMap> NewJSMap(); 537 Handle<JSSet> NewJSSet(); 538 539 // TODO(aandrey): Maybe these should take table, index and kind arguments. 540 Handle<JSMapIterator> NewJSMapIterator(); 541 Handle<JSSetIterator> NewJSSetIterator(); 542 543 Handle<JSFixedArrayIterator> NewJSFixedArrayIterator( 544 Handle<FixedArray> array); 545 546 // Allocates a bound function. 547 MaybeHandle<JSBoundFunction> NewJSBoundFunction( 548 Handle<JSReceiver> target_function, Handle<Object> bound_this, 549 Vector<Handle<Object>> bound_args); 550 551 // Allocates a Harmony proxy. 552 Handle<JSProxy> NewJSProxy(Handle<JSReceiver> target, 553 Handle<JSReceiver> handler); 554 555 // Reinitialize an JSGlobalProxy based on a constructor. The object 556 // must have the same size as objects allocated using the 557 // constructor. The object is reinitialized and behaves as an 558 // object that has been freshly allocated using the constructor. 559 void ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> global, 560 Handle<JSFunction> constructor); 561 562 Handle<JSGlobalProxy> NewUninitializedJSGlobalProxy(int size); 563 564 Handle<JSFunction> NewFunction(Handle<Map> map, 565 Handle<SharedFunctionInfo> info, 566 Handle<Object> context_or_undefined, 567 PretenureFlag pretenure = TENURED); 568 Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code, 569 Handle<Object> prototype, 570 bool is_strict = false); 571 Handle<JSFunction> NewFunction(Handle<String> name); 572 Handle<JSFunction> NewFunctionWithoutPrototype(Handle<String> name, 573 Handle<Code> code, 574 bool is_strict = false); 575 576 Handle<JSFunction> NewFunctionFromSharedFunctionInfo( 577 Handle<Map> initial_map, Handle<SharedFunctionInfo> function_info, 578 Handle<Object> context_or_undefined, PretenureFlag pretenure = TENURED); 579 580 Handle<JSFunction> NewFunctionFromSharedFunctionInfo( 581 Handle<SharedFunctionInfo> function_info, Handle<Context> context, 582 PretenureFlag pretenure = TENURED); 583 584 Handle<JSFunction> NewFunction(Handle<String> name, Handle<Code> code, 585 Handle<Object> prototype, InstanceType type, 586 int instance_size, 587 bool is_strict = false); 588 Handle<JSFunction> NewFunction(Handle<String> name, 589 Handle<Code> code, 590 InstanceType type, 591 int instance_size); 592 Handle<JSFunction> NewFunction(Handle<Map> map, Handle<String> name, 593 MaybeHandle<Code> maybe_code); 594 595 // Create a serialized scope info. 596 Handle<ScopeInfo> NewScopeInfo(int length); 597 598 Handle<ModuleInfoEntry> NewModuleInfoEntry(); 599 Handle<ModuleInfo> NewModuleInfo(); 600 601 // Create an External object for V8's external API. 602 Handle<JSObject> NewExternal(void* value); 603 604 // The reference to the Code object is stored in self_reference. 605 // This allows generated code to reference its own Code object 606 // by containing this handle. 607 Handle<Code> NewCode(const CodeDesc& desc, 608 Code::Flags flags, 609 Handle<Object> self_reference, 610 bool immovable = false, 611 bool crankshafted = false, 612 int prologue_offset = Code::kPrologueOffsetNotSet, 613 bool is_debug = false); 614 615 Handle<Code> CopyCode(Handle<Code> code); 616 617 Handle<BytecodeArray> CopyBytecodeArray(Handle<BytecodeArray>); 618 619 // Interface for creating error objects. 620 Handle<Object> NewError(Handle<JSFunction> constructor, 621 Handle<String> message); 622 623 Handle<Object> NewInvalidStringLengthError(); 624 NewURIError()625 Handle<Object> NewURIError() { 626 return NewError(isolate()->uri_error_function(), 627 MessageTemplate::kURIMalformed); 628 } 629 630 Handle<Object> NewError(Handle<JSFunction> constructor, 631 MessageTemplate::Template template_index, 632 Handle<Object> arg0 = Handle<Object>(), 633 Handle<Object> arg1 = Handle<Object>(), 634 Handle<Object> arg2 = Handle<Object>()); 635 636 #define DECLARE_ERROR(NAME) \ 637 Handle<Object> New##NAME(MessageTemplate::Template template_index, \ 638 Handle<Object> arg0 = Handle<Object>(), \ 639 Handle<Object> arg1 = Handle<Object>(), \ 640 Handle<Object> arg2 = Handle<Object>()); 641 DECLARE_ERROR(Error) 642 DECLARE_ERROR(EvalError) 643 DECLARE_ERROR(RangeError) 644 DECLARE_ERROR(ReferenceError) 645 DECLARE_ERROR(SyntaxError) 646 DECLARE_ERROR(TypeError) 647 DECLARE_ERROR(WasmCompileError) 648 DECLARE_ERROR(WasmRuntimeError) 649 #undef DECLARE_ERROR 650 651 Handle<String> NumberToString(Handle<Object> number, 652 bool check_number_string_cache = true); 653 Uint32ToString(uint32_t value)654 Handle<String> Uint32ToString(uint32_t value) { 655 return NumberToString(NewNumberFromUint(value)); 656 } 657 658 Handle<JSFunction> InstallMembers(Handle<JSFunction> function); 659 660 #define ROOT_ACCESSOR(type, name, camel_name) \ 661 inline Handle<type> name() { \ 662 return Handle<type>(bit_cast<type**>( \ 663 &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \ 664 } 665 ROOT_LIST(ROOT_ACCESSOR) 666 #undef ROOT_ACCESSOR 667 668 #define STRUCT_MAP_ACCESSOR(NAME, Name, name) \ 669 inline Handle<Map> name##_map() { \ 670 return Handle<Map>(bit_cast<Map**>( \ 671 &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \ 672 } 673 STRUCT_LIST(STRUCT_MAP_ACCESSOR) 674 #undef STRUCT_MAP_ACCESSOR 675 676 #define STRING_ACCESSOR(name, str) \ 677 inline Handle<String> name() { \ 678 return Handle<String>(bit_cast<String**>( \ 679 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ 680 } 681 INTERNALIZED_STRING_LIST(STRING_ACCESSOR) 682 #undef STRING_ACCESSOR 683 684 #define SYMBOL_ACCESSOR(name) \ 685 inline Handle<Symbol> name() { \ 686 return Handle<Symbol>(bit_cast<Symbol**>( \ 687 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ 688 } 689 PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR) 690 #undef SYMBOL_ACCESSOR 691 692 #define SYMBOL_ACCESSOR(name, description) \ 693 inline Handle<Symbol> name() { \ 694 return Handle<Symbol>(bit_cast<Symbol**>( \ 695 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ 696 } 697 PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR) 698 WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR) 699 #undef SYMBOL_ACCESSOR 700 701 // Allocates a new SharedFunctionInfo object. 702 Handle<SharedFunctionInfo> NewSharedFunctionInfo( 703 Handle<String> name, int number_of_literals, FunctionKind kind, 704 Handle<Code> code, Handle<ScopeInfo> scope_info); 705 Handle<SharedFunctionInfo> NewSharedFunctionInfo(Handle<String> name, 706 MaybeHandle<Code> code, 707 bool is_constructor); 708 IsFunctionModeWithPrototype(FunctionMode function_mode)709 static bool IsFunctionModeWithPrototype(FunctionMode function_mode) { 710 return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE || 711 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE); 712 } 713 714 Handle<Map> CreateSloppyFunctionMap(FunctionMode function_mode); 715 716 Handle<Map> CreateStrictFunctionMap(FunctionMode function_mode, 717 Handle<JSFunction> empty_function); 718 719 // Allocates a new JSMessageObject object. 720 Handle<JSMessageObject> NewJSMessageObject(MessageTemplate::Template message, 721 Handle<Object> argument, 722 int start_position, 723 int end_position, 724 Handle<Object> script, 725 Handle<Object> stack_frames); 726 727 Handle<DebugInfo> NewDebugInfo(Handle<SharedFunctionInfo> shared); 728 729 // Return a map for given number of properties using the map cache in the 730 // native context. 731 Handle<Map> ObjectLiteralMapFromCache(Handle<Context> context, 732 int number_of_properties, 733 bool* is_result_from_cache); 734 735 Handle<RegExpMatchInfo> NewRegExpMatchInfo(); 736 737 // Creates a new FixedArray that holds the data associated with the 738 // atom regexp and stores it in the regexp. 739 void SetRegExpAtomData(Handle<JSRegExp> regexp, 740 JSRegExp::Type type, 741 Handle<String> source, 742 JSRegExp::Flags flags, 743 Handle<Object> match_pattern); 744 745 // Creates a new FixedArray that holds the data associated with the 746 // irregexp regexp and stores it in the regexp. 747 void SetRegExpIrregexpData(Handle<JSRegExp> regexp, 748 JSRegExp::Type type, 749 Handle<String> source, 750 JSRegExp::Flags flags, 751 int capture_count); 752 753 // Returns the value for a known global constant (a property of the global 754 // object which is neither configurable nor writable) like 'undefined'. 755 // Returns a null handle when the given name is unknown. 756 Handle<Object> GlobalConstantFor(Handle<Name> name); 757 758 // Converts the given boolean condition to JavaScript boolean value. 759 Handle<Object> ToBoolean(bool value); 760 761 // Converts the given ToPrimitive hint to it's string representation. 762 Handle<String> ToPrimitiveHintString(ToPrimitiveHint hint); 763 764 private: isolate()765 Isolate* isolate() { return reinterpret_cast<Isolate*>(this); } 766 767 // Creates a heap object based on the map. The fields of the heap object are 768 // not initialized by New<>() functions. It's the responsibility of the caller 769 // to do that. 770 template<typename T> 771 Handle<T> New(Handle<Map> map, AllocationSpace space); 772 773 template<typename T> 774 Handle<T> New(Handle<Map> map, 775 AllocationSpace space, 776 Handle<AllocationSite> allocation_site); 777 778 MaybeHandle<String> NewStringFromTwoByte(const uc16* string, int length, 779 PretenureFlag pretenure); 780 781 // Creates a code object that is not yet fully initialized yet. 782 inline Handle<Code> NewCodeRaw(int object_size, bool immovable); 783 784 // Attempt to find the number in a small cache. If we finds it, return 785 // the string representation of the number. Otherwise return undefined. 786 Handle<Object> GetNumberStringCache(Handle<Object> number); 787 788 // Update the cache with a new number-string pair. 789 void SetNumberStringCache(Handle<Object> number, Handle<String> string); 790 791 // Create a JSArray with no elements and no length. 792 Handle<JSArray> NewJSArray(ElementsKind elements_kind, 793 PretenureFlag pretenure = NOT_TENURED); 794 795 void SetFunctionInstanceDescriptor(Handle<Map> map, 796 FunctionMode function_mode); 797 798 void SetStrictFunctionInstanceDescriptor(Handle<Map> map, 799 FunctionMode function_mode); 800 }; 801 802 } // namespace internal 803 } // namespace v8 804 805 #endif // V8_FACTORY_H_ 806