// 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_FACTORY_H_ #define V8_FACTORY_H_ #include "src/feedback-vector.h" #include "src/globals.h" #include "src/isolate.h" #include "src/messages.h" #include "src/objects/scope-info.h" namespace v8 { namespace internal { class BoilerplateDescription; class ConstantElementsPair; enum FunctionMode { // With prototype. FUNCTION_WITH_WRITEABLE_PROTOTYPE, FUNCTION_WITH_READONLY_PROTOTYPE, // Without prototype. FUNCTION_WITHOUT_PROTOTYPE }; // Interface for handle based allocation. class V8_EXPORT_PRIVATE Factory final { public: Handle NewOddball(Handle map, const char* to_string, Handle to_number, const char* type_of, byte kind); // Allocates a fixed array initialized with undefined values. Handle NewFixedArray(int size, 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. MUST_USE_RESULT MaybeHandle TryNewFixedArray( int size, PretenureFlag pretenure = NOT_TENURED); // Allocate a new fixed array with non-existing entries (the hole). Handle NewFixedArrayWithHoles( int size, PretenureFlag pretenure = NOT_TENURED); // Allocates an uninitialized fixed array. It must be filled by the caller. Handle NewUninitializedFixedArray(int size); // 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 NewBoilerplateDescription(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 capacity = 0, // so the return type must be the general fixed array class. Handle NewFixedDoubleArray( int size, PretenureFlag pretenure = NOT_TENURED); // Allocate a new fixed double array with hole values. Handle NewFixedDoubleArrayWithHoles( int size, PretenureFlag pretenure = NOT_TENURED); Handle NewFrameArray(int number_of_frames, PretenureFlag pretenure = NOT_TENURED); Handle NewOrderedHashSet(); Handle NewOrderedHashMap(); // Create a new PrototypeInfo struct. Handle NewPrototypeInfo(); // Create a new Tuple2 struct. Handle NewTuple2(Handle value1, Handle value2); // Create a new Tuple3 struct. Handle NewTuple3(Handle value1, Handle value2, Handle value3); // Create a new ContextExtension struct. Handle NewContextExtension(Handle scope_info, Handle extension); // Create a new ConstantElementsPair struct. Handle NewConstantElementsPair( ElementsKind elements_kind, Handle constant_values); // Create a pre-tenured empty AccessorPair. Handle NewAccessorPair(); // Create an empty TypeFeedbackInfo. Handle NewTypeFeedbackInfo(); // 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). Handle InternalizeString(Handle string) { if (string->IsInternalizedString()) return string; return StringTable::LookupString(isolate(), string); } Handle InternalizeName(Handle name) { if (name->IsUniqueName()) return name; return StringTable::LookupString(isolate(), Handle::cast(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. MUST_USE_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(); } // Allocates and fully initializes a String. There are two String encodings: // one-byte and two-byte. One should choose between the threestring // allocation functions based on the encoding of the string buffer used to // initialized the string. // - ...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. // TODO(dcarney): remove this function. MUST_USE_RESULT inline MaybeHandle NewStringFromAscii( Vector str, PretenureFlag pretenure = NOT_TENURED) { return NewStringFromOneByte(Vector::cast(str), pretenure); } // UTF8 strings are pretenured when used for regexp literal patterns and // flags in the parser. MUST_USE_RESULT MaybeHandle NewStringFromUtf8( Vector str, PretenureFlag pretenure = NOT_TENURED); MUST_USE_RESULT MaybeHandle NewStringFromUtf8SubString( Handle str, int begin, int end, PretenureFlag pretenure = NOT_TENURED); MUST_USE_RESULT MaybeHandle NewStringFromTwoByte( Vector str, PretenureFlag pretenure = NOT_TENURED); MUST_USE_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. MUST_USE_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. MUST_USE_RESULT MaybeHandle NewRawOneByteString( int length, PretenureFlag pretenure = NOT_TENURED); MUST_USE_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. MUST_USE_RESULT MaybeHandle NewConsString(Handle left, Handle right); // 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. Handle NewSubString(Handle str, int begin, int end) { if (begin == 0 && end == str->length()) return str; return NewProperSubString(str, begin, 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. MUST_USE_RESULT MaybeHandle NewExternalStringFromOneByte( const ExternalOneByteString::Resource* resource); MUST_USE_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. Handle NewSymbol(); Handle NewPrivateSymbol(); // Create a global (but otherwise uninitialized) context. Handle NewNativeContext(); // Create a script context. Handle NewScriptContext(Handle function, Handle scope_info); // Create an empty script context table. Handle NewScriptContextTable(); // Create a module context. Handle NewModuleContext(Handle module, Handle function, Handle scope_info); // Create a function or eval context. Handle NewFunctionContext(int length, Handle function, ScopeType scope_type); // Create a catch context. Handle NewCatchContext(Handle function, Handle previous, Handle scope_info, Handle name, Handle thrown_object); // Create a 'with' context. Handle NewWithContext(Handle function, 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 function, Handle previous, Handle scope_info); // Allocate a new struct. The struct is pretenured (allocated directly in // the old generation). Handle NewStruct(InstanceType type); Handle NewAliasedArgumentsEntry( int aliased_context_slot); Handle NewAccessorInfo(); Handle