// 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/isolate.h" namespace v8 { namespace internal { // Interface for handle based allocation. class Factory V8_FINAL { public: Handle NewOddball(Handle map, const char* to_string, Handle to_number, byte kind); // Allocates a fixed array initialized with undefined values. Handle NewFixedArray( 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); // 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 NewConstantPoolArray( const ConstantPoolArray::NumberOfEntries& small); Handle NewExtendedConstantPoolArray( const ConstantPoolArray::NumberOfEntries& small, const ConstantPoolArray::NumberOfEntries& extended); Handle NewOrderedHashSet(); Handle NewOrderedHashMap(); // Create a new boxed value. Handle NewBox(Handle value); // 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 InternalizeString(Handle str); Handle InternalizeOneByteString(Vector str); Handle InternalizeOneByteString( Handle, int from, int length); Handle InternalizeTwoByteString(Vector str); template Handle InternalizeStringWithKey(StringTableKey* key); // 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: ASCII 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. // - ...FromAscii initializes the string from a buffer that is ASCII // encoded (it does not check that the buffer is ASCII encoded) and // the result will be ASCII encoded. // - ...FromUtf8 initializes the string from a buffer that is UTF-8 // encoded. If the characters are all single-byte characters, the // result will be ASCII 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 single-byte characters, // the result will be converted to ASCII, otherwise it will be left as // two byte. // // ASCII strings are pretenured when used as keys in the SourceCodeCache. MUST_USE_RESULT MaybeHandle NewStringFromOneByte( Vector str, PretenureFlag pretenure = NOT_TENURED); template inline Handle NewStringFromStaticAscii( const char (&str)[N], PretenureFlag pretenure = NOT_TENURED) { ASSERT(N == StrLength(str) + 1); return NewStringFromOneByte( STATIC_ASCII_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: ASCII and two byte. One should choose between the three string // allocation functions based on the encoding of the string buffer used to // initialized the string. // - ...FromAscii initializes the string from a buffer that is ASCII // encoded (it does not check that the buffer is ASCII encoded) and the // result will be ASCII encoded. // - ...FromUTF8 initializes the string from a buffer that is UTF-8 // encoded. If the characters are all single-byte characters, the // result will be ASCII 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 single-byte characters, the // result will be converted to ASCII, 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 NewStringFromTwoByte( Vector str, PretenureFlag pretenure = NOT_TENURED); // Allocates an internalized string in old space based on the character // stream. MUST_USE_RESULT Handle NewInternalizedStringFromUtf8( Vector str, int chars, uint32_t hash_field); MUST_USE_RESULT Handle NewOneByteInternalizedString( Vector str, uint32_t hash_field); MUST_USE_RESULT Handle NewTwoByteInternalizedString( Vector str, uint32_t hash_field); MUST_USE_RESULT 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); // Allocates and partially initializes an ASCII or TwoByte 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 ASCII 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 a new sequential string containing the concatenation of the inputs. Handle NewFlatConcatString(Handle first, Handle second); // 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: ASCII 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 NewExternalStringFromAscii( const ExternalAsciiString::Resource* resource); MUST_USE_RESULT MaybeHandle NewExternalStringFromTwoByte( const ExternalTwoByteString::Resource* resource); // Create a symbol. Handle NewSymbol(); Handle NewPrivateSymbol(); // Create a global (but otherwise uninitialized) context. Handle NewNativeContext(); // Create a global context. Handle NewGlobalContext(Handle function, Handle scope_info); // Create a module context. Handle NewModuleContext(Handle scope_info); // Create a function context. Handle NewFunctionContext(int length, Handle function); // Create a catch context. Handle NewCatchContext(Handle function, Handle previous, Handle name, Handle thrown_object); // Create a 'with' context. Handle NewWithContext(Handle function, Handle previous, Handle extension); // 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 NewCodeCache(); Handle NewAliasedArgumentsEntry( int aliased_context_slot); Handle NewDeclaredAccessorDescriptor(); Handle NewDeclaredAccessorInfo(); Handle NewExecutableAccessorInfo(); Handle