v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
factory-base-inl.h
Go to the documentation of this file.
1// Copyright 2020 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_BASE_INL_H_
6#define V8_HEAP_FACTORY_BASE_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
11#include <type_traits>
12
16#include "src/objects/map.h"
18#include "src/objects/smi.h"
20#include "src/roots/roots.h"
21
22namespace v8 {
23namespace internal {
24
25#define RO_ROOT_ACCESSOR(Type, name, CamelName) \
26 template <typename Impl> \
27 Handle<Type> FactoryBase<Impl>::name() { \
28 return isolate()->roots_table().name(); \
29 }
31#undef ROOT_ACCESSOR
32
33#define MUTABLE_ROOT_ACCESSOR(Type, name, CamelName) \
34 template <typename Impl> \
35 Handle<Type> FactoryBase<Impl>::name() { \
36 return handle(isolate()->heap()->name(), isolate()); \
37 }
39#undef ROOT_ACCESSOR
40
41template <typename Impl>
43 return value ? Cast<Boolean>(impl()->true_value())
44 : Cast<Boolean>(impl()->false_value());
45}
46
47template <typename Impl>
48template <AllocationType allocation>
50 // Materialize as a SMI if possible.
51 int32_t int_value;
52 if (DoubleToSmiInteger(value, &int_value)) {
53 return handle(Smi::FromInt(int_value), isolate());
54 }
55 return NewHeapNumber<allocation>(value);
56}
57
58template <typename Impl>
59template <AllocationType allocation>
61 if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate());
62 // Bypass NewNumber to avoid various redundant checks.
63 return NewHeapNumber<allocation>(FastI2D(value));
64}
65
66template <typename Impl>
67template <AllocationType allocation>
69 int32_t int32v = static_cast<int32_t>(value);
70 if (int32v >= 0 && Smi::IsValid(int32v)) {
71 return handle(Smi::FromInt(int32v), isolate());
72 }
73 return NewHeapNumber<allocation>(FastUI2D(value));
74}
75
76template <typename Impl>
77template <AllocationType allocation>
79 // We can't use Smi::IsValid() here because that operates on a signed
80 // intptr_t, and casting from size_t could create a bogus sign bit.
81 if (value <= static_cast<size_t>(Smi::kMaxValue)) {
82 return direct_handle(Smi::FromIntptr(static_cast<intptr_t>(value)),
83 isolate());
84 }
85 return NewHeapNumber<allocation>(static_cast<double>(value));
86}
87
88template <typename Impl>
89template <AllocationType allocation>
91 if (value <= std::numeric_limits<int32_t>::max() &&
92 value >= std::numeric_limits<int32_t>::min() &&
93 Smi::IsValid(static_cast<int32_t>(value))) {
94 return direct_handle(Smi::FromInt(static_cast<int32_t>(value)), isolate());
95 }
96 return NewHeapNumber<allocation>(static_cast<double>(value));
97}
98
99template <typename Impl>
100template <AllocationType allocation>
102 Handle<HeapNumber> heap_number = NewHeapNumber<allocation>();
103 heap_number->set_value(value);
104 return heap_number;
105}
106
107template <typename Impl>
108template <AllocationType allocation>
110 Handle<HeapNumber> heap_number = NewHeapNumber<allocation>();
111 heap_number->set_value_as_bits(bits);
112 return heap_number;
113}
114
115template <typename Impl>
116template <AllocationType allocation>
118 return NewHeapNumberFromBits<allocation>(kHoleNanInt64);
120
121template <typename Impl>
122template <AllocationType allocation>
124 Handle<HeapNumber> heap_number = NewHeapNumber<allocation>();
125 heap_number->set_value_as_bits(
126 (static_cast<uint64_t>(kHoleNanUpper32) << 32) | value);
127 return heap_number;
128}
130template <typename Impl>
131template <typename StructType>
133 InstanceType type, AllocationType allocation) {
134 ReadOnlyRoots roots = read_only_roots();
135 Tagged<Map> map = Map::GetMapFor(roots, type);
136 int size;
137 if constexpr (std::is_base_of_v<StructLayout, StructType>) {
138 size = sizeof(StructType);
139 } else {
140 size = StructType::kSize;
141 }
142 return Cast<StructType>(NewStructInternal(roots, map, size, allocation));
143}
144
145template <typename Impl>
147 Tagged<Map> map, int size,
148 AllocationType allocation) {
149 DCHECK_EQ(size, map->instance_size());
150 Tagged<HeapObject> result = AllocateRawWithImmortalMap(size, allocation, map);
152 Tagged<Undefined> undefined = roots.undefined_value();
153 int length = (size >> kTaggedSizeLog2) - 1;
154 MemsetTagged(str->RawField(Struct::kHeaderSize), undefined, length);
155 return str;
156}
157
158} // namespace internal
159} // namespace v8
160
161#endif // V8_HEAP_FACTORY_BASE_INL_H_
Handle< Boolean > ToBoolean(bool value)
Handle< Number > NewNumberFromInt(int32_t value)
DirectHandle< Number > NewNumberFromSize(size_t value)
Handle< HeapNumber > NewHeapNumberFromBits(uint64_t bits)
Handle< HeapNumber > NewHeapInt32(int32_t value)
Handle< Number > NewNumberFromUint(uint32_t value)
Tagged< StructType > NewStructInternal(InstanceType type, AllocationType allocation)
Handle< Number > NewNumber(double value)
Handle< HeapNumber > NewHeapNumberWithHoleNaN()
DirectHandle< Number > NewNumberFromInt64(int64_t value)
Handle< HeapNumber > NewHeapNumber()
static Tagged< Map > GetMapFor(ReadOnlyRoots roots, InstanceType type)
Definition map-inl.h:855
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static constexpr Tagged< Smi > FromIntptr(intptr_t value)
Definition smi.h:43
static bool constexpr IsValid(T value)
Definition smi.h:67
static constexpr int kMaxValue
Definition smi.h:101
#define RO_ROOT_ACCESSOR(Type, name, CamelName)
#define MUTABLE_ROOT_ACCESSOR(Type, name, CamelName)
Isolate * isolate
ZoneVector< RpoNumber > & result
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
bool DoubleToSmiInteger(double value, int *smi_int_value)
constexpr uint64_t kHoleNanInt64
Definition globals.h:1960
void MemsetTagged(Tagged_t *start, Tagged< MaybeObject > value, size_t counter)
Definition slots-inl.h:486
V8_INLINE DirectHandle< T > direct_handle(Tagged< T > object, Isolate *isolate)
double FastI2D(int x)
constexpr int kTaggedSizeLog2
Definition globals.h:543
constexpr uint32_t kHoleNanUpper32
Definition globals.h:1952
return value
Definition map-inl.h:893
double FastUI2D(unsigned x)
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 READ_ONLY_ROOT_LIST(V)
Definition roots.h:468
#define DCHECK_EQ(v1, v2)
Definition logging.h:485