// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_HEAP_FACTORY_H_ #define V8_HEAP_FACTORY_H_ // Clients of this interface shouldn't depend on lots of heap internals. // Do not include anything from src/heap here! #include "src/builtins/builtins.h" #include "src/globals.h" #include "src/handles.h" #include "src/heap/heap.h" #include "src/maybe-handles.h" #include "src/messages.h" #include "src/objects/code.h" #include "src/objects/dictionary.h" #include "src/objects/hash-table.h" #include "src/objects/js-array-buffer.h" #include "src/objects/js-array.h" #include "src/objects/js-regexp.h" #include "src/objects/ordered-hash-table.h" #include "src/objects/string.h" namespace v8 { namespace internal { // Forward declarations. class AliasedArgumentsEntry; class ObjectBoilerplateDescription; class BreakPoint; class BreakPointInfo; class CallableTask; class CallbackTask; class CallHandlerInfo; class Expression; class ArrayBoilerplateDescription; class CoverageInfo; class DebugInfo; class EnumCache; class FreshlyAllocatedBigInt; class Isolate; class JSGeneratorObject; class JSMap; class JSMapIterator; class JSModuleNamespace; class JSPromise; class JSProxy; class JSSet; class JSSetIterator; class JSWeakMap; class LoadHandler; class ModuleInfo; class NativeContext; class NewFunctionArgs; class PreParsedScopeData; class PromiseResolveThenableJobTask; class RegExpMatchInfo; class ScriptContextTable; class StackFrameInfo; class StoreHandler; class TemplateObjectDescription; class UncompiledDataWithoutPreParsedScope; class UncompiledDataWithPreParsedScope; class WasmExportedFunctionData; struct SourceRange; template class ZoneVector; enum FunctionMode { kWithNameBit = 1 << 0, kWithHomeObjectBit = 1 << 1, kWithWritablePrototypeBit = 1 << 2, kWithReadonlyPrototypeBit = 1 << 3, kWithPrototypeBits = kWithWritablePrototypeBit | kWithReadonlyPrototypeBit, // Without prototype. FUNCTION_WITHOUT_PROTOTYPE = 0, METHOD_WITH_NAME = kWithNameBit, METHOD_WITH_HOME_OBJECT = kWithHomeObjectBit, METHOD_WITH_NAME_AND_HOME_OBJECT = kWithNameBit | kWithHomeObjectBit, // With writable prototype. FUNCTION_WITH_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit, FUNCTION_WITH_NAME_AND_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit | kWithNameBit, FUNCTION_WITH_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit | kWithHomeObjectBit, FUNCTION_WITH_NAME_AND_HOME_OBJECT_AND_WRITEABLE_PROTOTYPE = kWithWritablePrototypeBit | kWithNameBit | kWithHomeObjectBit, // With readonly prototype. FUNCTION_WITH_READONLY_PROTOTYPE = kWithReadonlyPrototypeBit, FUNCTION_WITH_NAME_AND_READONLY_PROTOTYPE = kWithReadonlyPrototypeBit | kWithNameBit, }; // Interface for handle based allocation. class V8_EXPORT_PRIVATE Factory { public: Handle NewOddball(Handle map, const char* to_string, Handle to_number, const char* type_of, byte kind, PretenureFlag pretenure = TENURED_READ_ONLY); // Marks self references within code generation. Handle NewSelfReferenceMarker(PretenureFlag pretenure = TENURED); // Allocates a fixed array-like object with given map and initialized with // undefined values. template Handle NewFixedArrayWithMap(RootIndex map_root_index, int length, PretenureFlag pretenure = NOT_TENURED); // Allocates a weak fixed array-like object with given map and initialized // with undefined values. template Handle NewWeakFixedArrayWithMap(RootIndex map_root_index, int length, PretenureFlag pretenure = NOT_TENURED); // Allocates a fixed array initialized with undefined values. Handle NewFixedArray(int length, PretenureFlag pretenure = NOT_TENURED); // Allocates a fixed array which may contain in-place weak references. The // array is initialized with undefined values Handle NewWeakFixedArray( int length, PretenureFlag pretenure = NOT_TENURED); // Allocates a property array initialized with undefined values. Handle NewPropertyArray(int length, PretenureFlag pretenure = NOT_TENURED); // Tries allocating a fixed array initialized with undefined values. // In case of an allocation failure (OOM) an empty handle is returned. // The caller has to manually signal an // v8::internal::Heap::FatalProcessOutOfMemory typically by calling // NewFixedArray as a fallback. V8_WARN_UNUSED_RESULT MaybeHandle TryNewFixedArray( int length, PretenureFlag pretenure = NOT_TENURED); // Allocate a new fixed array with non-existing entries (the hole). Handle NewFixedArrayWithHoles( int length, PretenureFlag pretenure = NOT_TENURED); // Allocates an uninitialized fixed array. It must be filled by the caller. Handle NewUninitializedFixedArray( int length, PretenureFlag pretenure = NOT_TENURED); // Allocates a feedback vector whose slots are initialized with undefined // values. Handle NewFeedbackVector( Handle shared, PretenureFlag pretenure = NOT_TENURED); // Allocates a fixed array for name-value pairs of boilerplate properties and // calculates the number of properties we need to store in the backing store. Handle NewObjectBoilerplateDescription( int boilerplate, int all_properties, int index_keys, bool has_seen_proto); // Allocate a new uninitialized fixed double array. // The function returns a pre-allocated empty fixed array for length = 0, // so the return type must be the general fixed array class. Handle NewFixedDoubleArray( int length, PretenureFlag pretenure = NOT_TENURED); // Allocate a new fixed double array with hole values. Handle NewFixedDoubleArrayWithHoles( int size, PretenureFlag pretenure = NOT_TENURED); // Allocates a FeedbackMedata object and zeroes the data section. Handle NewFeedbackMetadata(int slot_count, PretenureFlag tenure = TENURED); Handle NewFrameArray(int number_of_frames, PretenureFlag pretenure = NOT_TENURED); Handle NewOrderedHashSet(); Handle NewOrderedHashMap(); Handle NewSmallOrderedHashSet( int capacity = SmallOrderedHashSet::kMinCapacity, PretenureFlag pretenure = NOT_TENURED); Handle NewSmallOrderedHashMap( int capacity = SmallOrderedHashMap::kMinCapacity, PretenureFlag pretenure = NOT_TENURED); // Create a new PrototypeInfo struct. Handle NewPrototypeInfo(); // Create a new EnumCache struct. Handle NewEnumCache(Handle keys, Handle indices); // Create a new Tuple2 struct. Handle NewTuple2(Handle value1, Handle value2, PretenureFlag pretenure); // Create a new Tuple3 struct. Handle NewTuple3(Handle value1, Handle value2, Handle value3, PretenureFlag pretenure); // Create a new ArrayBoilerplateDescription struct. Handle NewArrayBoilerplateDescription( ElementsKind elements_kind, Handle constant_values); // Create a new TemplateObjectDescription struct. Handle NewTemplateObjectDescription( Handle raw_strings, Handle cooked_strings); // Create a pre-tenured empty AccessorPair. Handle NewAccessorPair(); // Finds the internalized copy for string in the string table. // If not found, a new string is added to the table and returned. Handle InternalizeUtf8String(Vector str); Handle InternalizeUtf8String(const char* str) { return InternalizeUtf8String(CStrVector(str)); } Handle InternalizeOneByteString(Vector str); Handle InternalizeOneByteString(Handle, int from, int length); Handle InternalizeTwoByteString(Vector str); template Handle InternalizeStringWithKey(StringTableKey* key); // Internalized strings are created in the old generation (data space). inline Handle InternalizeString(Handle string); inline Handle InternalizeName(Handle name); // String creation functions. Most of the string creation functions take // a Heap::PretenureFlag argument to optionally request that they be // allocated in the old generation. The pretenure flag defaults to // DONT_TENURE. // // Creates a new String object. There are two String encodings: one-byte and // two-byte. One should choose between the three string factory functions // based on the encoding of the string buffer that the string is // initialized from. // - ...FromOneByte initializes the string from a buffer that is Latin1 // encoded (it does not check that the buffer is Latin1 encoded) and // the result will be Latin1 encoded. // - ...FromUtf8 initializes the string from a buffer that is UTF-8 // encoded. If the characters are all ASCII characters, the result // will be Latin1 encoded, otherwise it will converted to two-byte. // - ...FromTwoByte initializes the string from a buffer that is two-byte // encoded. If the characters are all Latin1 characters, the result // will be converted to Latin1, otherwise it will be left as two-byte. // // One-byte strings are pretenured when used as keys in the SourceCodeCache. V8_WARN_UNUSED_RESULT MaybeHandle NewStringFromOneByte( Vector str, PretenureFlag pretenure = NOT_TENURED); template inline Handle NewStringFromStaticChars( const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) { DCHECK(N == StrLength(str) + 1); return NewStringFromOneByte(STATIC_CHAR_VECTOR(str), pretenure) .ToHandleChecked(); } inline Handle NewStringFromAsciiChecked( const char* str, PretenureFlag pretenure = NOT_TENURED) { return NewStringFromOneByte(OneByteVector(str), pretenure) .ToHandleChecked(); } // UTF8 strings are pretenured when used for regexp literal patterns and // flags in the parser. V8_WARN_UNUSED_RESULT MaybeHandle NewStringFromUtf8( Vector str, PretenureFlag pretenure = NOT_TENURED); V8_WARN_UNUSED_RESULT MaybeHandle NewStringFromUtf8SubString( Handle str, int begin, int end, PretenureFlag pretenure = NOT_TENURED); V8_WARN_UNUSED_RESULT MaybeHandle NewStringFromTwoByte( Vector str, PretenureFlag pretenure = NOT_TENURED); V8_WARN_UNUSED_RESULT MaybeHandle NewStringFromTwoByte( const ZoneVector* str, PretenureFlag pretenure = NOT_TENURED); Handle NewJSStringIterator(Handle string); // Allocates an internalized string in old space based on the character // stream. Handle NewInternalizedStringFromUtf8(Vector str, int chars, uint32_t hash_field); Handle NewOneByteInternalizedString(Vector str, uint32_t hash_field); Handle NewOneByteInternalizedSubString( Handle string, int offset, int length, uint32_t hash_field); Handle NewTwoByteInternalizedString(Vector str, uint32_t hash_field); Handle NewInternalizedStringImpl(Handle string, int chars, uint32_t hash_field); // Compute the matching internalized string map for a string if possible. // Empty handle is returned if string is in new space or not flattened. V8_WARN_UNUSED_RESULT MaybeHandle InternalizedStringMapForString( Handle string); // Creates an internalized copy of an external string. |string| must be // of type StringClass. template Handle InternalizeExternalString(Handle string); // Allocates and partially initializes an one-byte or two-byte String. The // characters of the string are uninitialized. Currently used in regexp code // only, where they are pretenured. V8_WARN_UNUSED_RESULT MaybeHandle NewRawOneByteString( int length, PretenureFlag pretenure = NOT_TENURED); V8_WARN_UNUSED_RESULT MaybeHandle NewRawTwoByteString( int length, PretenureFlag pretenure = NOT_TENURED); // Creates a single character string where the character has given code. // A cache is used for Latin1 codes. Handle LookupSingleCharacterStringFromCode(uint32_t code); // Create a new cons string object which consists of a pair of strings. V8_WARN_UNUSED_RESULT MaybeHandle NewConsString(Handle left, Handle right); V8_WARN_UNUSED_RESULT Handle NewConsString(Handle left, Handle right, int length, bool one_byte); // Create or lookup a single characters tring made up of a utf16 surrogate // pair. Handle NewSurrogatePairString(uint16_t lead, uint16_t trail); // Create a new string object which holds a proper substring of a string. Handle NewProperSubString(Handle str, int begin, int end); // Create a new string object which holds a substring of a string. inline Handle NewSubString(Handle str, int begin, int end); // Creates a new external String object. There are two String encodings // in the system: one-byte and two-byte. Unlike other String types, it does // not make sense to have a UTF-8 factory function for external strings, // because we cannot change the underlying buffer. Note that these strings // are backed by a string resource that resides outside the V8 heap. V8_WARN_UNUSED_RESULT MaybeHandle NewExternalStringFromOneByte( const ExternalOneByteString::Resource* resource); V8_WARN_UNUSED_RESULT MaybeHandle NewExternalStringFromTwoByte( const ExternalTwoByteString::Resource* resource); // Create a new external string object for one-byte encoded native script. // It does not cache the resource data pointer. Handle NewNativeSourceString( const ExternalOneByteString::Resource* resource); // Create a symbol in old or read-only space. Handle NewSymbol(PretenureFlag pretenure = TENURED); Handle NewPrivateSymbol(PretenureFlag pretenure = TENURED); Handle NewPrivateFieldSymbol(); // Create a global (but otherwise uninitialized) context. Handle NewNativeContext(); // Create a script context. Handle NewScriptContext(Handle outer, Handle scope_info); // Create an empty script context table. Handle NewScriptContextTable(); // Create a module context. Handle NewModuleContext(Handle module, Handle outer, Handle scope_info); // Create a function or eval context. Handle NewFunctionContext(Handle outer, Handle scope_info); // Create a catch context. Handle NewCatchContext(Handle previous, Handle scope_info, Handle thrown_object); // Create a 'with' context. Handle NewWithContext(Handle previous, Handle scope_info, Handle extension); Handle NewDebugEvaluateContext(Handle previous, Handle scope_info, Handle extension, Handle wrapped, Handle whitelist); // Create a block context. Handle NewBlockContext(Handle previous, Handle scope_info); // Create a context that's used by builtin functions. // // These are similar to function context but don't have a previous // context or any scope info. These are used to store spec defined // context values. Handle NewBuiltinContext(Handle native_context, int length); Handle NewStruct(InstanceType type, PretenureFlag pretenure = NOT_TENURED); Handle NewAliasedArgumentsEntry( int aliased_context_slot); Handle NewAccessorInfo(); Handle