v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
name-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_OBJECTS_NAME_INL_H_
6#define V8_OBJECTS_NAME_INL_H_
7
8#include "src/objects/name.h"
9// Include the non-inl header before the rest of the headers.
10
11#include "src/base/logging.h"
14#include "src/objects/map-inl.h"
18
19// Has to be the last include (doesn't have include guards):
21
22namespace v8 {
23namespace internal {
24
29 WriteBarrierMode mode) {
30 SLOW_DCHECK(IsString(value) || IsUndefined(value));
31 description_.store(this, value, mode);
32}
33
35BIT_FIELD_ACCESSORS(Symbol, flags, is_well_known_symbol,
36 Symbol::IsWellKnownSymbolBit)
38 Symbol::IsInPublicSymbolTableBit)
39BIT_FIELD_ACCESSORS(Symbol, flags, is_interesting_symbol,
40 Symbol::IsInterestingSymbolBit)
41
42bool Symbol::is_private_brand() const {
44 DCHECK_IMPLIES(value, is_private());
45 return value;
46}
47
52}
53
56 DCHECK_IMPLIES(value, is_private());
57 return value;
58}
59
61 // TODO(gsathya): Re-order the bits to have these next to each other
62 // and just do the bit shifts once.
65}
66
68 uint32_t type = obj->map()->instance_type();
72 DCHECK_IMPLIES(result, obj->HasHashCode());
73 return result;
74}
75
77 if (other == this) return true;
78 if ((IsInternalizedString(this) && IsInternalizedString(other)) ||
79 IsSymbol(this) || IsSymbol(other)) {
80 return false;
81 }
82 return Cast<String>(this)->SlowEquals(Cast<String>(other));
83}
84
87 if (one.is_identical_to(two)) return true;
88 if ((IsInternalizedString(*one) && IsInternalizedString(*two)) ||
89 IsSymbol(*one) || IsSymbol(*two)) {
90 return false;
91 }
92 return String::SlowEquals(isolate, Cast<String>(one), Cast<String>(two));
93}
94
95// static
96bool Name::IsHashFieldComputed(uint32_t raw_hash_field) {
98}
99
100// static
101bool Name::IsHash(uint32_t raw_hash_field) {
103}
104
105// static
106bool Name::IsIntegerIndex(uint32_t raw_hash_field) {
109}
110
111// static
116
117// static
123
124// static
130
131// static
137// static
144
145// static
152
153bool Name::HasHashCode() const {
154 uint32_t field = raw_hash_field();
155 return IsHashFieldComputed(field) || IsForwardingIndex(field);
156}
166
167uint32_t Name::GetRawHashFromForwardingTable(uint32_t raw_hash) const {
168 DCHECK(IsForwardingIndex(raw_hash));
169 // TODO(pthier): Add parameter for isolate so we don't need to calculate it.
170 Isolate* isolate = Isolate::Current();
171 const int index = ForwardingIndexValueBits::decode(raw_hash);
172 return isolate->string_forwarding_table()->GetRawHash(isolate, index);
173}
174
176 // Fast case: has hash code already been computed?
177 uint32_t field = raw_hash_field(kAcquireLoad);
178 if (IsHashFieldComputed(field)) return field;
179 // The computed hash might be stored in the forwarding table.
180 if (V8_UNLIKELY(IsForwardingIndex(field))) {
181 return GetRawHashFromForwardingTable(field);
182 }
183 // Slow case: compute hash code and set it. Has to be a string.
184 return Cast<String>(this)->ComputeAndSetRawHash();
185}
186
188 const SharedStringAccessGuardIfNeeded& access_guard) {
189 // Fast case: has hash code already been computed?
190 uint32_t field = raw_hash_field(kAcquireLoad);
191 if (IsHashFieldComputed(field)) return field;
192 // The computed hash might be stored in the forwarding table.
193 if (V8_UNLIKELY(IsForwardingIndex(field))) {
194 return GetRawHashFromForwardingTable(field);
195 }
196 // Slow case: compute hash code and set it. Has to be a string.
197 return Cast<String>(this)->ComputeAndSetRawHash(access_guard);
198}
199
200uint32_t Name::RawHash() {
201 uint32_t field = raw_hash_field(kAcquireLoad);
202 if (V8_UNLIKELY(IsForwardingIndex(field))) {
203 return GetRawHashFromForwardingTable(field);
204 }
205 return field;
206}
207
209
211 return HashBits::decode(EnsureRawHash(access_guard));
212}
213
215 uint32_t field_value = kEmptyHashField;
216 bool result = raw_hash_field_.compare_exchange_strong(field_value, hash);
217 USE(result);
218 // CAS can only fail if the string is shared or we use the forwarding table
219 // for all strings and the hash was already set (by another thread) or it is
220 // a forwarding index (that overwrites the previous hash).
221 // In all cases we don't want overwrite the old value, so we don't handle the
222 // failure case.
224 v8_flags.always_use_string_forwarding_table) &&
225 (field_value == hash || IsForwardingIndex(hash)));
226}
227
228uint32_t Name::hash() const {
229 uint32_t field = raw_hash_field(kAcquireLoad);
230 if (V8_UNLIKELY(!IsHashFieldComputed(field))) {
233 }
234 return HashBits::decode(field);
235}
236
237bool Name::TryGetHash(uint32_t* hash) const {
238 uint32_t field = raw_hash_field(kAcquireLoad);
239 if (IsHashFieldComputed(field)) {
240 *hash = HashBits::decode(field);
241 return true;
242 }
243 if (V8_UNLIKELY(IsForwardingIndex(field))) {
245 return true;
246 }
247 return false;
248}
249
251 // TODO(ishell): consider using ReadOnlyRoots::IsNameForProtector() trick for
252 // these strings and interesting symbols.
253 return (IsSymbol(this) && Cast<Symbol>(this)->is_interesting_symbol()) ||
254 this == *isolate->factory()->toJSON_string() ||
255 this == *isolate->factory()->get_string();
256}
257
259 return IsSymbol(this) && Cast<Symbol>(this)->is_private();
260}
261
263 bool is_private_name =
264 IsSymbol(this) && Cast<Symbol>(this)->is_private_name();
265 DCHECK_IMPLIES(is_private_name, IsPrivate());
266 return is_private_name;
267}
268
270 bool is_private_brand =
271 IsSymbol(this) && Cast<Symbol>(this)->is_private_brand();
272 DCHECK_IMPLIES(is_private_brand, IsPrivateName());
273 return is_private_brand;
274}
275
277 uint32_t index;
278 return AsArrayIndex(&index);
279}
280
281bool Name::AsArrayIndex(uint32_t* index) {
282 return IsString(this) && Cast<String>(this)->AsArrayIndex(index);
283}
284
285bool Name::AsIntegerIndex(size_t* index) {
286 return IsString(this) && Cast<String>(this)->AsIntegerIndex(index);
287}
288
289// static
290bool Name::ContainsCachedArrayIndex(uint32_t raw_hash_field) {
292}
293
294} // namespace internal
295} // namespace v8
296
298
299#endif // V8_OBJECTS_NAME_INL_H_
#define one
#define SLOW_DCHECK(condition)
Definition checks.h:21
static constexpr U kMax
Definition bit-field.h:44
static constexpr T decode(U value)
Definition bit-field.h:66
static constexpr U encode(T value)
Definition bit-field.h:55
static V8_NODISCARD constexpr U update(U previous, T value)
Definition bit-field.h:61
static V8_INLINE Isolate * Current()
Definition isolate-inl.h:35
bool HasForwardingIndex(AcquireLoadTag) const
Definition name-inl.h:157
std::atomic_uint32_t raw_hash_field_
Definition name.h:241
static bool IsIntegerIndex(uint32_t raw_hash_field)
Definition name-inl.h:106
bool IsInteresting(Isolate *isolate)
Definition name-inl.h:250
static bool IsForwardingIndex(uint32_t raw_hash_field)
Definition name-inl.h:112
uint32_t EnsureHash()
Definition name-inl.h:208
static bool IsHash(uint32_t raw_hash_field)
Definition name-inl.h:101
static uint32_t CreateHashFieldValue(uint32_t hash, HashFieldType type)
Definition name-inl.h:132
bool TryGetHash(uint32_t *hash) const
Definition name-inl.h:237
static bool ContainsCachedArrayIndex(uint32_t hash)
Definition name-inl.h:290
void set_raw_hash_field_if_empty(uint32_t hash)
Definition name-inl.h:214
static constexpr int kEmptyHashField
Definition name.h:133
bool Equals(Tagged< Name > other)
Definition name-inl.h:76
uint32_t raw_hash_field() const
Definition name.h:47
static constexpr int kHashNotComputedMask
Definition name.h:131
static const unsigned int kDoesNotContainCachedArrayIndexMask
Definition name.h:190
bool IsPrivateBrand()
Definition name-inl.h:269
bool HasHashCode() const
Definition name-inl.h:153
static uint32_t CreateExternalForwardingIndex(uint32_t index)
Definition name-inl.h:146
bool HasExternalForwardingIndex(AcquireLoadTag) const
Definition name-inl.h:163
static uint32_t CreateInternalizedForwardingIndex(uint32_t index)
Definition name-inl.h:138
uint32_t EnsureRawHash()
Definition name-inl.h:175
uint32_t RawHash()
Definition name-inl.h:200
bool AsArrayIndex(uint32_t *index)
Definition name-inl.h:281
static bool IsInternalizedForwardingIndex(uint32_t raw_hash_field)
Definition name-inl.h:118
bool AsIntegerIndex(size_t *index)
Definition name-inl.h:285
static bool IsExternalForwardingIndex(uint32_t raw_hash_field)
Definition name-inl.h:125
uint32_t GetRawHashFromForwardingTable(uint32_t raw_hash) const
Definition name-inl.h:167
uint32_t hash() const
Definition name-inl.h:228
bool HasInternalizedForwardingIndex(AcquireLoadTag) const
Definition name-inl.h:160
static bool IsHashFieldComputed(uint32_t raw_hash_field)
Definition name-inl.h:96
V8_EXPORT_PRIVATE bool SlowEquals(Tagged< String > other) const
Definition string.cc:1219
TaggedMember< PrimitiveHeapObject > description_
Definition name.h:327
uint32_t flags() const
Definition name.h:319
void set_is_private_name()
Definition name-inl.h:60
bool is_private() const
void set_description(Tagged< PrimitiveHeapObject > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
Definition name-inl.h:28
void set_flags(uint32_t value)
Definition name.h:320
Tagged< PrimitiveHeapObject > description() const
Definition name-inl.h:25
bool is_private_name() const
Definition name-inl.h:54
ZoneVector< RpoNumber > & result
const uint32_t kNotInternalizedTag
const uint32_t kStringTag
Flag flags[]
Definition flags.cc:3797
bool IsShared(Tagged< Object > obj)
V8_EXPORT_PRIVATE FlagValues v8_flags
bool IsUniqueName(Tagged< Name > obj)
const uint32_t kIsNotInternalizedMask
return value
Definition map-inl.h:893
is_in_public_symbol_table
Definition name-inl.h:37
const uint32_t kIsNotStringMask
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
static constexpr AcquireLoadTag kAcquireLoad
Definition globals.h:2908
#define DEF_HEAP_OBJECT_PREDICATE(holder, name)
#define BIT_FIELD_ACCESSORS(holder, field, name, BitField)
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define USE(...)
Definition macros.h:293
#define V8_UNLIKELY(condition)
Definition v8config.h:660