// Copyright 2017 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_INL_H_ #define V8_HEAP_FACTORY_INL_H_ #include "src/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/handles-inl.h" #include "src/objects-inl.h" #include "src/string-hasher.h" namespace v8 { namespace internal { #define ROOT_ACCESSOR(type, name, camel_name) \ Handle Factory::name() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \ } ROOT_LIST(ROOT_ACCESSOR) #undef ROOT_ACCESSOR #define STRUCT_MAP_ACCESSOR(NAME, Name, name) \ Handle Factory::name##_map() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \ } STRUCT_LIST(STRUCT_MAP_ACCESSOR) #undef STRUCT_MAP_ACCESSOR #define ALLOCATION_SITE_MAP_ACCESSOR(NAME, Name, Size, name) \ Handle Factory::name##_map() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##Name##Size##MapRootIndex])); \ } ALLOCATION_SITE_LIST(ALLOCATION_SITE_MAP_ACCESSOR) #undef ALLOCATION_SITE_MAP_ACCESSOR #define DATA_HANDLER_MAP_ACCESSOR(NAME, Name, Size, name) \ Handle Factory::name##_map() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##Name##Size##MapRootIndex])); \ } DATA_HANDLER_LIST(DATA_HANDLER_MAP_ACCESSOR) #undef DATA_HANDLER_MAP_ACCESSOR #define STRING_ACCESSOR(name, str) \ Handle Factory::name() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ } INTERNALIZED_STRING_LIST(STRING_ACCESSOR) #undef STRING_ACCESSOR #define SYMBOL_ACCESSOR(name) \ Handle Factory::name() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ } PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR) #undef SYMBOL_ACCESSOR #define SYMBOL_ACCESSOR(name, description) \ Handle Factory::name() { \ return Handle(bit_cast( \ &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \ } PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR) WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR) #undef SYMBOL_ACCESSOR #define ACCESSOR_INFO_ACCESSOR(accessor_name, AccessorName) \ Handle Factory::accessor_name##_accessor() { \ return Handle(bit_cast( \ &isolate() \ ->heap() \ ->roots_[Heap::k##AccessorName##AccessorRootIndex])); \ } ACCESSOR_INFO_LIST(ACCESSOR_INFO_ACCESSOR) #undef ACCESSOR_INFO_ACCESSOR Handle Factory::InternalizeString(Handle string) { if (string->IsInternalizedString()) return string; return StringTable::LookupString(isolate(), string); } Handle Factory::InternalizeName(Handle name) { if (name->IsUniqueName()) return name; return StringTable::LookupString(isolate(), Handle::cast(name)); } Handle Factory::NewSubString(Handle str, int begin, int end) { if (begin == 0 && end == str->length()) return str; return NewProperSubString(str, begin, end); } Handle Factory::NewNumberFromSize(size_t value, PretenureFlag pretenure) { // We can't use Smi::IsValid() here because that operates on a signed // intptr_t, and casting from size_t could create a bogus sign bit. if (value <= static_cast(Smi::kMaxValue)) { return Handle(Smi::FromIntptr(static_cast(value)), isolate()); } return NewNumber(static_cast(value), pretenure); } Handle Factory::NewNumberFromInt64(int64_t value, PretenureFlag pretenure) { if (value <= std::numeric_limits::max() && value >= std::numeric_limits::min() && Smi::IsValid(static_cast(value))) { return Handle(Smi::FromInt(static_cast(value)), isolate()); } return NewNumber(static_cast(value), pretenure); } Handle Factory::NewHeapNumber(double value, PretenureFlag pretenure) { Handle heap_number = NewHeapNumber(pretenure); heap_number->set_value(value); return heap_number; } Handle Factory::NewMutableHeapNumber( double value, PretenureFlag pretenure) { Handle number = NewMutableHeapNumber(pretenure); number->set_value(value); return number; } Handle Factory::NewHeapNumberFromBits(uint64_t bits, PretenureFlag pretenure) { Handle heap_number = NewHeapNumber(pretenure); heap_number->set_value_as_bits(bits); return heap_number; } Handle Factory::NewMutableHeapNumberFromBits( uint64_t bits, PretenureFlag pretenure) { Handle number = NewMutableHeapNumber(pretenure); number->set_value_as_bits(bits); return number; } Handle Factory::NewMutableHeapNumberWithHoleNaN( PretenureFlag pretenure) { return NewMutableHeapNumberFromBits(kHoleNanInt64, pretenure); } Handle Factory::NewJSArrayWithElements(Handle elements, ElementsKind elements_kind, PretenureFlag pretenure) { return NewJSArrayWithElements(elements, elements_kind, elements->length(), pretenure); } Handle Factory::NewURIError() { return NewError(isolate()->uri_error_function(), MessageTemplate::kURIMalformed); } Handle Factory::Uint32ToString(uint32_t value, bool check_cache) { Handle result; int32_t int32v = static_cast(value); if (int32v >= 0 && Smi::IsValid(int32v)) { result = NumberToString(Smi::FromInt(int32v), check_cache); } else { result = NumberToString(NewNumberFromUint(value), check_cache); } if (result->length() <= String::kMaxArrayIndexSize && result->hash_field() == String::kEmptyHashField) { uint32_t field = StringHasher::MakeArrayIndexHash(value, result->length()); result->set_hash_field(field); } return result; } } // namespace internal } // namespace v8 #endif // V8_HEAP_FACTORY_INL_H_