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
10 #include "src/heap/heap-write-barrier-inl.h"
11 #include "src/objects/map-inl.h"
12 #include "src/objects/primitive-heap-object-inl.h"
13
14 // Has to be the last include (doesn't have include guards):
15 #include "src/objects/object-macros.h"
16
17 namespace v8 {
18 namespace internal {
19
20 #include "torque-generated/src/objects/name-tq-inl.inc"
21
22 TQ_OBJECT_CONSTRUCTORS_IMPL(Name)
TQ_OBJECT_CONSTRUCTORS_IMPL(Symbol)23 TQ_OBJECT_CONSTRUCTORS_IMPL(Symbol)
24
25 BIT_FIELD_ACCESSORS(Symbol, flags, is_private, Symbol::IsPrivateBit)
26 BIT_FIELD_ACCESSORS(Symbol, flags, is_well_known_symbol,
27 Symbol::IsWellKnownSymbolBit)
28 BIT_FIELD_ACCESSORS(Symbol, flags, is_in_public_symbol_table,
29 Symbol::IsInPublicSymbolTableBit)
30 BIT_FIELD_ACCESSORS(Symbol, flags, is_interesting_symbol,
31 Symbol::IsInterestingSymbolBit)
32
33 bool Symbol::is_private_brand() const {
34 bool value = Symbol::IsPrivateBrandBit::decode(flags());
35 DCHECK_IMPLIES(value, is_private());
36 return value;
37 }
38
set_is_private_brand()39 void Symbol::set_is_private_brand() {
40 set_flags(Symbol::IsPrivateBit::update(flags(), true));
41 set_flags(Symbol::IsPrivateNameBit::update(flags(), true));
42 set_flags(Symbol::IsPrivateBrandBit::update(flags(), true));
43 }
44
is_private_name()45 bool Symbol::is_private_name() const {
46 bool value = Symbol::IsPrivateNameBit::decode(flags());
47 DCHECK_IMPLIES(value, is_private());
48 return value;
49 }
50
set_is_private_name()51 void Symbol::set_is_private_name() {
52 // TODO(gsathya): Re-order the bits to have these next to each other
53 // and just do the bit shifts once.
54 set_flags(Symbol::IsPrivateBit::update(flags(), true));
55 set_flags(Symbol::IsPrivateNameBit::update(flags(), true));
56 }
57
DEF_GETTER(Name,IsUniqueName,bool)58 DEF_GETTER(Name, IsUniqueName, bool) {
59 uint32_t type = map(isolate).instance_type();
60 bool result = (type & (kIsNotStringMask | kIsNotInternalizedMask)) !=
61 (kStringTag | kNotInternalizedTag);
62 SLOW_DCHECK(result == HeapObject::IsUniqueName());
63 return result;
64 }
65
Equals(Name other)66 bool Name::Equals(Name other) {
67 if (other == *this) return true;
68 if ((this->IsInternalizedString() && other.IsInternalizedString()) ||
69 this->IsSymbol() || other.IsSymbol()) {
70 return false;
71 }
72 return String::cast(*this).SlowEquals(String::cast(other));
73 }
74
Equals(Isolate * isolate,Handle<Name> one,Handle<Name> two)75 bool Name::Equals(Isolate* isolate, Handle<Name> one, Handle<Name> two) {
76 if (one.is_identical_to(two)) return true;
77 if ((one->IsInternalizedString() && two->IsInternalizedString()) ||
78 one->IsSymbol() || two->IsSymbol()) {
79 return false;
80 }
81 return String::SlowEquals(isolate, Handle<String>::cast(one),
82 Handle<String>::cast(two));
83 }
84
IsHashFieldComputed(uint32_t field)85 bool Name::IsHashFieldComputed(uint32_t field) {
86 return (field & kHashNotComputedMask) == 0;
87 }
88
HasHashCode()89 bool Name::HasHashCode() { return IsHashFieldComputed(hash_field()); }
90
Hash()91 uint32_t Name::Hash() {
92 // Fast case: has hash code already been computed?
93 uint32_t field = hash_field();
94 if (IsHashFieldComputed(field)) return field >> kHashShift;
95 // Slow case: compute hash code and set it. Has to be a string.
96 return String::cast(*this).ComputeAndSetHash();
97 }
98
hash()99 uint32_t Name::hash() const {
100 uint32_t field = hash_field();
101 DCHECK(IsHashFieldComputed(field));
102 return field >> kHashShift;
103 }
104
DEF_GETTER(Name,IsInterestingSymbol,bool)105 DEF_GETTER(Name, IsInterestingSymbol, bool) {
106 return IsSymbol(isolate) && Symbol::cast(*this).is_interesting_symbol();
107 }
108
DEF_GETTER(Name,IsPrivate,bool)109 DEF_GETTER(Name, IsPrivate, bool) {
110 return this->IsSymbol(isolate) && Symbol::cast(*this).is_private();
111 }
112
DEF_GETTER(Name,IsPrivateName,bool)113 DEF_GETTER(Name, IsPrivateName, bool) {
114 bool is_private_name =
115 this->IsSymbol(isolate) && Symbol::cast(*this).is_private_name();
116 DCHECK_IMPLIES(is_private_name, IsPrivate());
117 return is_private_name;
118 }
119
DEF_GETTER(Name,IsPrivateBrand,bool)120 DEF_GETTER(Name, IsPrivateBrand, bool) {
121 bool is_private_brand =
122 this->IsSymbol(isolate) && Symbol::cast(*this).is_private_brand();
123 DCHECK_IMPLIES(is_private_brand, IsPrivateName());
124 return is_private_brand;
125 }
126
AsArrayIndex(uint32_t * index)127 bool Name::AsArrayIndex(uint32_t* index) {
128 return IsString() && String::cast(*this).AsArrayIndex(index);
129 }
130
AsIntegerIndex(size_t * index)131 bool Name::AsIntegerIndex(size_t* index) {
132 return IsString() && String::cast(*this).AsIntegerIndex(index);
133 }
134
135 // static
ContainsCachedArrayIndex(uint32_t hash)136 bool Name::ContainsCachedArrayIndex(uint32_t hash) {
137 return (hash & Name::kDoesNotContainCachedArrayIndexMask) == 0;
138 }
139
140 } // namespace internal
141 } // namespace v8
142
143 #include "src/objects/object-macros-undef.h"
144
145 #endif // V8_OBJECTS_NAME_INL_H_
146