v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
factory-inl.h
Go to the documentation of this file.
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// Include the non-inl header before the rest of the headers.
10
11// Clients of this interface shouldn't depend on lots of heap internals.
12// Do not include anything from src/heap here!
13// TODO(all): Remove the heap-inl.h include below.
14#include "src/common/globals.h"
18#include "src/heap/heap-inl.h" // For MaxNumberToStringCacheSize.
20#include "src/objects/foreign.h"
24#include "src/objects/oddball.h"
28
29namespace v8 {
30namespace internal {
31
32#define ROOT_ACCESSOR(Type, name, CamelName) \
33 Handle<Type> Factory::name() { \
34 return Handle<Type>(&isolate()->roots_table()[RootIndex::k##CamelName]); \
35 }
37#undef ROOT_ACCESSOR
38
39template <typename T>
40 requires(std::is_convertible_v<Handle<T>, Handle<String>>)
42 // T should be a subtype of String, which is enforced by the second template
43 // argument.
44 if (IsInternalizedString(*string)) return string;
45 return indirect_handle(
46 isolate()->string_table()->LookupString(isolate(), string), isolate());
47}
48
49template <typename T>
50 requires(std::is_convertible_v<Handle<T>, Handle<Name>>)
52 // T should be a subtype of Name, which is enforced by the second template
53 // argument.
54 if (IsUniqueName(*name)) return name;
55 return indirect_handle(
56 isolate()->string_table()->LookupString(isolate(), Cast<String>(name)),
57 isolate());
58}
59
60template <typename T>
61 requires(std::is_convertible_v<DirectHandle<T>, DirectHandle<String>>)
63 // T should be a subtype of String, which is enforced by the second template
64 // argument.
65 if (IsInternalizedString(*string)) return string;
66 return isolate()->string_table()->LookupString(isolate(), string);
67}
68
69template <typename T>
70 requires(std::is_convertible_v<DirectHandle<T>, DirectHandle<Name>>)
72 // T should be a subtype of Name, which is enforced by the second template
73 // argument.
74 if (IsUniqueName(*name)) return name;
76}
77
78template <size_t N>
80 AllocationType allocation) {
81 DCHECK_EQ(N, strlen(str) + 1);
82 return NewStringFromOneByte(base::StaticOneByteVector(str), allocation)
83 .ToHandleChecked();
84}
85
86template <typename T, template <typename> typename HandleType>
87 requires(std::is_convertible_v<HandleType<T>, HandleType<String>>)
88HandleType<String> Factory::NewSubString(HandleType<T> str, uint32_t begin,
89 uint32_t end) {
90 if (begin == 0 && end == str->length()) return str;
91 return NewProperSubString(str, begin, end);
92}
93
95 DirectHandle<FixedArrayBase> elements, ElementsKind elements_kind,
96 AllocationType allocation) {
97 return NewJSArrayWithElements(elements, elements_kind, elements->length(),
98 allocation);
99}
100
102 DirectHandle<Map> map, int number_of_slow_properties,
103 AllocationType allocation, DirectHandle<AllocationSite> allocation_site,
104 NewJSObjectType new_js_object_type) {
105 auto js_object =
106 map->is_dictionary_map()
107 ? NewSlowJSObjectFromMap(map, number_of_slow_properties, allocation,
108 allocation_site, new_js_object_type)
109 : NewJSObjectFromMap(map, allocation, allocation_site,
110 new_js_object_type);
111 return js_object;
112}
113
118
119template <ExternalPointerTag tag>
121 AllocationType allocation_type) {
122 // Statically ensure that it is safe to allocate foreigns in paged spaces.
123 static_assert(Foreign::kSize <= kMaxRegularHeapObjectSize);
124 Tagged<Map> map = *foreign_map();
126 AllocateRawWithImmortalMap(map->instance_size(), allocation_type, map));
128 foreign->init_foreign_address<tag>(isolate(), addr);
129 return handle(foreign, isolate());
130}
131
133 return NewError(isolate()->uri_error_function(),
134 MessageTemplate::kURIMalformed);
135}
136
140
142 return isolate()->heap()->allocator();
143}
144
149
151 Handle<TrustedObject> interpreter_data) {
152 // This DCHECK requires this function to be in -inl.h.
153 DCHECK(IsInterpreterData(*interpreter_data) ||
154 IsBytecodeArray(*interpreter_data));
155 interpreter_data_ = interpreter_data;
156 return *this;
157}
158
160 DirectHandle<String> js_string) {
161 if (!IsUndefined(number_string_cache()->get(hash * 2), isolate()) &&
162 !isolate()->MemorySaverModeEnabled()) {
163 int full_size = isolate()->heap()->MaxNumberToStringCacheSize();
164 if (number_string_cache()->length() != full_size) {
165 DirectHandle<FixedArray> new_cache =
167 isolate()->heap()->set_number_string_cache(*new_cache);
168 return;
169 }
170 }
172 Tagged<FixedArray> cache = *number_string_cache();
173 cache->set(hash * 2, *number);
174 cache->set(hash * 2 + 1, *js_string);
175}
176
178 int hash) {
180 Tagged<FixedArray> cache = *number_string_cache();
181 Tagged<Object> key = cache->get(hash * 2);
182 if (key == number ||
183 (IsHeapNumber(key) && IsHeapNumber(number) &&
184 Cast<HeapNumber>(key)->value() == Cast<HeapNumber>(number)->value())) {
185 return Handle<String>(Cast<String>(cache->get(hash * 2 + 1)), isolate());
186 }
187 return undefined_value();
188}
189
190} // namespace internal
191} // namespace v8
192
193#endif // V8_HEAP_FACTORY_INL_H_
MaybeHandle< String > NewStringFromOneByte(base::Vector< const uint8_t > string, AllocationType allocation=AllocationType::kYoung)
Tagged< HeapObject > AllocateRawWithImmortalMap(int size, AllocationType allocation, Tagged< Map > map, AllocationAlignment alignment=kTaggedAligned)
Handle< FixedArray > NewFixedArray(int length, AllocationType allocation=AllocationType::kYoung)
CodeBuilder & set_source_position_table(Handle< TrustedByteArray > table)
Definition factory.h:1188
CodeBuilder & set_interpreter_data(Handle< TrustedObject > interpreter_data)
CodeBuilder & set_empty_source_position_table()
ReadOnlyRoots read_only_roots() const
Isolate * isolate() const
Definition factory.h:1281
V8_INLINE void NumberToStringCacheSet(DirectHandle< Object > number, int hash, DirectHandle< String > js_string)
Handle< Name > InternalizeName(Handle< T > name)
Definition factory-inl.h:51
Handle< Foreign > NewForeign(Address addr, AllocationType allocation_type=AllocationType::kYoung)
V8_INLINE HeapAllocator * allocator() const
DirectHandle< Object > NewURIError()
V8_INLINE Handle< Object > NumberToStringCacheGet(Tagged< Object > number, int hash)
Handle< JSArray > NewJSArrayWithElements(DirectHandle< FixedArrayBase > elements, ElementsKind elements_kind, int length, AllocationType allocation=AllocationType::kYoung)
Definition factory.cc:3228
Handle< String > InternalizeString(base::Vector< const char > str, bool convert_encoding=false)
Definition factory.h:216
Handle< String > NewStringFromStaticChars(const char(&str)[N], AllocationType allocation=AllocationType::kYoung)
Definition factory-inl.h:79
Handle< JSObject > NewJSObjectFromMap(DirectHandle< Map > map, AllocationType allocation=AllocationType::kYoung, DirectHandle< AllocationSite > allocation_site=DirectHandle< AllocationSite >::null(), NewJSObjectType=NewJSObjectType::kNoAPIWrapper)
Definition factory.cc:3135
Handle< JSObject > NewError(DirectHandle< JSFunction > constructor, DirectHandle< String > message, DirectHandle< Object > options={})
Definition factory.cc:2799
Handle< JSObject > NewSlowJSObjectFromMap(DirectHandle< Map > map, int number_of_slow_properties, AllocationType allocation=AllocationType::kYoung, DirectHandle< AllocationSite > allocation_site=DirectHandle< AllocationSite >::null(), NewJSObjectType=NewJSObjectType::kNoAPIWrapper)
Definition factory.cc:3165
Handle< JSObject > NewFastOrSlowJSObjectFromMap(DirectHandle< Map > map, int number_of_slow_properties, AllocationType allocation=AllocationType::kYoung, DirectHandle< AllocationSite > allocation_site=DirectHandle< AllocationSite >::null(), NewJSObjectType=NewJSObjectType::kNoAPIWrapper)
HandleType< String > NewSubString(HandleType< T > str, uint32_t begin, uint32_t end)
Definition factory-inl.h:88
HeapAllocator * allocator()
Definition heap.h:1640
int MaxNumberToStringCacheSize() const
Definition heap-inl.h:397
StringTable * string_table() const
Definition isolate.h:781
v8::internal::Factory * factory()
Definition isolate.h:1527
static const int kInitialCapacity
Definition dictionary.h:250
DirectHandle< String > LookupString(Isolate *isolate, DirectHandle< String > key)
int end
#define ROOT_ACCESSOR(Type, name, CamelName)
Isolate * isolate
Vector< const uint8_t > StaticOneByteVector(const char(&array)[N])
Definition vector.h:346
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
constexpr int kMaxRegularHeapObjectSize
Definition globals.h:680
V8_INLINE IndirectHandle< T > indirect_handle(DirectHandle< T > handle)
Definition handles.h:757
constexpr int N
too high values may cause the compiler to set high thresholds for inlining to as much as possible avoid inlined allocation of objects that cannot escape trace load stores from virtual maglev objects use TurboFan fast string builder analyze liveness of environment slots and zap dead values trace TurboFan load elimination emit data about basic block usage in builtins to this enable builtin reordering when run mksnapshot flag for emit warnings when applying builtin profile data verify register allocation in TurboFan randomly schedule instructions to stress dependency tracking enable store store elimination in TurboFan rewrite far to near simulate GC compiler thread race related to allow float parameters to be passed in simulator mode JS Wasm Run additional turbo_optimize_inlined_js_wasm_wrappers enable experimental feedback collection in generic lowering enable Turboshaft s WasmLoadElimination enable Turboshaft s low level load elimination for JS enable Turboshaft s escape analysis for string concatenation use enable Turbolev features that we want to ship in the not too far future trace individual Turboshaft reduction steps trace intermediate Turboshaft reduction steps invocation count threshold for early optimization Enables optimizations which favor memory size over execution speed Enables sampling allocation profiler with X as a sample interval min size of a semi the new space consists of two semi spaces max size of the Collect garbage after Collect garbage after keeps maps alive for< n > old space garbage collections print one detailed trace line in name
Definition flags.cc:2086
bool IsUniqueName(Tagged< Name > obj)
return value
Definition map-inl.h:893
template const char * string
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define MUTABLE_ROOT_LIST(V)
Definition roots.h:483
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485