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