1 // Copyright 2017 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_HEAP_FACTORY_INL_H_
6 #define V8_HEAP_FACTORY_INL_H_
7
8 #include "src/heap/factory.h"
9
10 // Clients of this interface shouldn't depend on lots of heap internals.
11 // Do not include anything from src/heap here!
12 #include "src/handles-inl.h"
13 #include "src/objects-inl.h"
14 #include "src/string-hasher.h"
15
16 namespace v8 {
17 namespace internal {
18
19 #define ROOT_ACCESSOR(type, name, camel_name) \
20 Handle<type> Factory::name() { \
21 return Handle<type>(bit_cast<type**>( \
22 &isolate()->heap()->roots_[Heap::k##camel_name##RootIndex])); \
23 }
24 ROOT_LIST(ROOT_ACCESSOR)
25 #undef ROOT_ACCESSOR
26
27 #define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
28 Handle<Map> Factory::name##_map() { \
29 return Handle<Map>(bit_cast<Map**>( \
30 &isolate()->heap()->roots_[Heap::k##Name##MapRootIndex])); \
31 }
STRUCT_LIST(STRUCT_MAP_ACCESSOR)32 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
33 #undef STRUCT_MAP_ACCESSOR
34
35 #define ALLOCATION_SITE_MAP_ACCESSOR(NAME, Name, Size, name) \
36 Handle<Map> Factory::name##_map() { \
37 return Handle<Map>(bit_cast<Map**>( \
38 &isolate()->heap()->roots_[Heap::k##Name##Size##MapRootIndex])); \
39 }
40 ALLOCATION_SITE_LIST(ALLOCATION_SITE_MAP_ACCESSOR)
41 #undef ALLOCATION_SITE_MAP_ACCESSOR
42
43 #define DATA_HANDLER_MAP_ACCESSOR(NAME, Name, Size, name) \
44 Handle<Map> Factory::name##_map() { \
45 return Handle<Map>(bit_cast<Map**>( \
46 &isolate()->heap()->roots_[Heap::k##Name##Size##MapRootIndex])); \
47 }
48 DATA_HANDLER_LIST(DATA_HANDLER_MAP_ACCESSOR)
49 #undef DATA_HANDLER_MAP_ACCESSOR
50
51 #define STRING_ACCESSOR(name, str) \
52 Handle<String> Factory::name() { \
53 return Handle<String>(bit_cast<String**>( \
54 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
55 }
56 INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
57 #undef STRING_ACCESSOR
58
59 #define SYMBOL_ACCESSOR(name) \
60 Handle<Symbol> Factory::name() { \
61 return Handle<Symbol>(bit_cast<Symbol**>( \
62 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
63 }
64 PRIVATE_SYMBOL_LIST(SYMBOL_ACCESSOR)
65 #undef SYMBOL_ACCESSOR
66
67 #define SYMBOL_ACCESSOR(name, description) \
68 Handle<Symbol> Factory::name() { \
69 return Handle<Symbol>(bit_cast<Symbol**>( \
70 &isolate()->heap()->roots_[Heap::k##name##RootIndex])); \
71 }
72 PUBLIC_SYMBOL_LIST(SYMBOL_ACCESSOR)
73 WELL_KNOWN_SYMBOL_LIST(SYMBOL_ACCESSOR)
74 #undef SYMBOL_ACCESSOR
75
76 #define ACCESSOR_INFO_ACCESSOR(accessor_name, AccessorName) \
77 Handle<AccessorInfo> Factory::accessor_name##_accessor() { \
78 return Handle<AccessorInfo>(bit_cast<AccessorInfo**>( \
79 &isolate() \
80 ->heap() \
81 ->roots_[Heap::k##AccessorName##AccessorRootIndex])); \
82 }
83 ACCESSOR_INFO_LIST(ACCESSOR_INFO_ACCESSOR)
84 #undef ACCESSOR_INFO_ACCESSOR
85
86 Handle<String> Factory::InternalizeString(Handle<String> string) {
87 if (string->IsInternalizedString()) return string;
88 return StringTable::LookupString(isolate(), string);
89 }
90
InternalizeName(Handle<Name> name)91 Handle<Name> Factory::InternalizeName(Handle<Name> name) {
92 if (name->IsUniqueName()) return name;
93 return StringTable::LookupString(isolate(), Handle<String>::cast(name));
94 }
95
NewSubString(Handle<String> str,int begin,int end)96 Handle<String> Factory::NewSubString(Handle<String> str, int begin, int end) {
97 if (begin == 0 && end == str->length()) return str;
98 return NewProperSubString(str, begin, end);
99 }
100
NewNumberFromSize(size_t value,PretenureFlag pretenure)101 Handle<Object> Factory::NewNumberFromSize(size_t value,
102 PretenureFlag pretenure) {
103 // We can't use Smi::IsValid() here because that operates on a signed
104 // intptr_t, and casting from size_t could create a bogus sign bit.
105 if (value <= static_cast<size_t>(Smi::kMaxValue)) {
106 return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
107 isolate());
108 }
109 return NewNumber(static_cast<double>(value), pretenure);
110 }
111
NewNumberFromInt64(int64_t value,PretenureFlag pretenure)112 Handle<Object> Factory::NewNumberFromInt64(int64_t value,
113 PretenureFlag pretenure) {
114 if (value <= std::numeric_limits<int32_t>::max() &&
115 value >= std::numeric_limits<int32_t>::min() &&
116 Smi::IsValid(static_cast<int32_t>(value))) {
117 return Handle<Object>(Smi::FromInt(static_cast<int32_t>(value)), isolate());
118 }
119 return NewNumber(static_cast<double>(value), pretenure);
120 }
121
NewHeapNumber(double value,PretenureFlag pretenure)122 Handle<HeapNumber> Factory::NewHeapNumber(double value,
123 PretenureFlag pretenure) {
124 Handle<HeapNumber> heap_number = NewHeapNumber(pretenure);
125 heap_number->set_value(value);
126 return heap_number;
127 }
128
NewMutableHeapNumber(double value,PretenureFlag pretenure)129 Handle<MutableHeapNumber> Factory::NewMutableHeapNumber(
130 double value, PretenureFlag pretenure) {
131 Handle<MutableHeapNumber> number = NewMutableHeapNumber(pretenure);
132 number->set_value(value);
133 return number;
134 }
135
NewHeapNumberFromBits(uint64_t bits,PretenureFlag pretenure)136 Handle<HeapNumber> Factory::NewHeapNumberFromBits(uint64_t bits,
137 PretenureFlag pretenure) {
138 Handle<HeapNumber> heap_number = NewHeapNumber(pretenure);
139 heap_number->set_value_as_bits(bits);
140 return heap_number;
141 }
142
NewMutableHeapNumberFromBits(uint64_t bits,PretenureFlag pretenure)143 Handle<MutableHeapNumber> Factory::NewMutableHeapNumberFromBits(
144 uint64_t bits, PretenureFlag pretenure) {
145 Handle<MutableHeapNumber> number = NewMutableHeapNumber(pretenure);
146 number->set_value_as_bits(bits);
147 return number;
148 }
149
NewMutableHeapNumberWithHoleNaN(PretenureFlag pretenure)150 Handle<MutableHeapNumber> Factory::NewMutableHeapNumberWithHoleNaN(
151 PretenureFlag pretenure) {
152 return NewMutableHeapNumberFromBits(kHoleNanInt64, pretenure);
153 }
154
NewJSArrayWithElements(Handle<FixedArrayBase> elements,ElementsKind elements_kind,PretenureFlag pretenure)155 Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
156 ElementsKind elements_kind,
157 PretenureFlag pretenure) {
158 return NewJSArrayWithElements(elements, elements_kind, elements->length(),
159 pretenure);
160 }
161
NewURIError()162 Handle<Object> Factory::NewURIError() {
163 return NewError(isolate()->uri_error_function(),
164 MessageTemplate::kURIMalformed);
165 }
166
Uint32ToString(uint32_t value,bool check_cache)167 Handle<String> Factory::Uint32ToString(uint32_t value, bool check_cache) {
168 Handle<String> result;
169 int32_t int32v = static_cast<int32_t>(value);
170 if (int32v >= 0 && Smi::IsValid(int32v)) {
171 result = NumberToString(Smi::FromInt(int32v), check_cache);
172 } else {
173 result = NumberToString(NewNumberFromUint(value), check_cache);
174 }
175
176 if (result->length() <= String::kMaxArrayIndexSize &&
177 result->hash_field() == String::kEmptyHashField) {
178 uint32_t field = StringHasher::MakeArrayIndexHash(value, result->length());
179 result->set_hash_field(field);
180 }
181 return result;
182 }
183
184 } // namespace internal
185 } // namespace v8
186
187 #endif // V8_HEAP_FACTORY_INL_H_
188