name-inl.h 5.56 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_NAME_INL_H_
#define V8_OBJECTS_NAME_INL_H_

8
#include "src/base/logging.h"
9
#include "src/heap/heap-write-barrier-inl.h"
10
#include "src/objects/map-inl.h"
11
#include "src/objects/name.h"
12
#include "src/objects/primitive-heap-object-inl.h"
13
#include "src/objects/string-inl.h"
14

15 16 17 18 19 20
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

21 22
#include "torque-generated/src/objects/name-tq-inl.inc"

23 24
TQ_OBJECT_CONSTRUCTORS_IMPL(Name)
TQ_OBJECT_CONSTRUCTORS_IMPL(Symbol)
25

26 27 28
BIT_FIELD_ACCESSORS(Symbol, flags, is_private, Symbol::IsPrivateBit)
BIT_FIELD_ACCESSORS(Symbol, flags, is_well_known_symbol,
                    Symbol::IsWellKnownSymbolBit)
29 30
BIT_FIELD_ACCESSORS(Symbol, flags, is_in_public_symbol_table,
                    Symbol::IsInPublicSymbolTableBit)
31 32
BIT_FIELD_ACCESSORS(Symbol, flags, is_interesting_symbol,
                    Symbol::IsInterestingSymbolBit)
33

34 35 36 37 38 39 40 41 42 43 44 45
bool Symbol::is_private_brand() const {
  bool value = Symbol::IsPrivateBrandBit::decode(flags());
  DCHECK_IMPLIES(value, is_private());
  return value;
}

void Symbol::set_is_private_brand() {
  set_flags(Symbol::IsPrivateBit::update(flags(), true));
  set_flags(Symbol::IsPrivateNameBit::update(flags(), true));
  set_flags(Symbol::IsPrivateBrandBit::update(flags(), true));
}

46 47
bool Symbol::is_private_name() const {
  bool value = Symbol::IsPrivateNameBit::decode(flags());
48 49 50 51
  DCHECK_IMPLIES(value, is_private());
  return value;
}

52
void Symbol::set_is_private_name() {
53 54
  // TODO(gsathya): Re-order the bits to have these next to each other
  // and just do the bit shifts once.
55
  set_flags(Symbol::IsPrivateBit::update(flags(), true));
56
  set_flags(Symbol::IsPrivateNameBit::update(flags(), true));
57 58
}

59
DEF_GETTER(Name, IsUniqueName, bool) {
60
  uint32_t type = map(cage_base).instance_type();
61 62 63
  bool result = (type & (kIsNotStringMask | kIsNotInternalizedMask)) !=
                (kStringTag | kNotInternalizedTag);
  SLOW_DCHECK(result == HeapObject::IsUniqueName());
64
  DCHECK_IMPLIES(result, HasHashCode());
65
  return result;
66 67
}

68 69
bool Name::Equals(Name other) {
  if (other == *this) return true;
70 71
  if ((this->IsInternalizedString() && other.IsInternalizedString()) ||
      this->IsSymbol() || other.IsSymbol()) {
72 73
    return false;
  }
74
  return String::cast(*this).SlowEquals(String::cast(other));
75 76
}

77
bool Name::Equals(Isolate* isolate, Handle<Name> one, Handle<Name> two) {
78 79 80 81 82
  if (one.is_identical_to(two)) return true;
  if ((one->IsInternalizedString() && two->IsInternalizedString()) ||
      one->IsSymbol() || two->IsSymbol()) {
    return false;
  }
83
  return String::SlowEquals(isolate, Handle<String>::cast(one),
84 85 86
                            Handle<String>::cast(two));
}

87 88
bool Name::IsHashFieldComputed(uint32_t raw_hash_field) {
  return (raw_hash_field & kHashNotComputedMask) == 0;
89 90
}

Patrick Thier's avatar
Patrick Thier committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
bool Name::IsHash(uint32_t raw_hash_field) {
  return HashFieldTypeBits::decode(raw_hash_field) == HashFieldType::kHash;
}

bool Name::IsIntegerIndex(uint32_t raw_hash_field) {
  return HashFieldTypeBits::decode(raw_hash_field) ==
         HashFieldType::kIntegerIndex;
}

bool Name::IsForwardingIndex(uint32_t raw_hash_field) {
  return HashFieldTypeBits::decode(raw_hash_field) ==
         HashFieldType::kForwardingIndex;
}

uint32_t Name::CreateHashFieldValue(uint32_t hash, HashFieldType type) {
  return HashBits::encode(hash & HashBits::kMax) |
         HashFieldTypeBits::encode(type);
}

110
bool Name::HasHashCode() const { return IsHashFieldComputed(raw_hash_field()); }
111

112
uint32_t Name::EnsureHash() {
113
  // Fast case: has hash code already been computed?
114
  uint32_t field = raw_hash_field();
Patrick Thier's avatar
Patrick Thier committed
115
  if (IsHashFieldComputed(field)) return HashBits::decode(field);
116
  // Slow case: compute hash code and set it. Has to be a string.
117
  return String::cast(*this).ComputeAndSetHash();
118 119
}

120 121 122
uint32_t Name::EnsureHash(const SharedStringAccessGuardIfNeeded& access_guard) {
  // Fast case: has hash code already been computed?
  uint32_t field = raw_hash_field();
Patrick Thier's avatar
Patrick Thier committed
123
  if (IsHashFieldComputed(field)) return HashBits::decode(field);
124 125 126 127
  // Slow case: compute hash code and set it. Has to be a string.
  return String::cast(*this).ComputeAndSetHash(access_guard);
}

128
uint32_t Name::hash() const {
129
  uint32_t field = raw_hash_field();
130
  DCHECK(IsHashFieldComputed(field));
Patrick Thier's avatar
Patrick Thier committed
131
  return HashBits::decode(field);
132 133
}

134
DEF_GETTER(Name, IsInterestingSymbol, bool) {
135
  return IsSymbol(cage_base) && Symbol::cast(*this).is_interesting_symbol();
136 137
}

138
DEF_GETTER(Name, IsPrivate, bool) {
139
  return this->IsSymbol(cage_base) && Symbol::cast(*this).is_private();
140 141
}

142
DEF_GETTER(Name, IsPrivateName, bool) {
143
  bool is_private_name =
144
      this->IsSymbol(cage_base) && Symbol::cast(*this).is_private_name();
145 146
  DCHECK_IMPLIES(is_private_name, IsPrivate());
  return is_private_name;
147 148
}

149 150
DEF_GETTER(Name, IsPrivateBrand, bool) {
  bool is_private_brand =
151
      this->IsSymbol(cage_base) && Symbol::cast(*this).is_private_brand();
152 153 154 155
  DCHECK_IMPLIES(is_private_brand, IsPrivateName());
  return is_private_brand;
}

156
bool Name::AsArrayIndex(uint32_t* index) {
157
  return IsString() && String::cast(*this).AsArrayIndex(index);
158 159
}

160 161 162 163
bool Name::AsIntegerIndex(size_t* index) {
  return IsString() && String::cast(*this).AsIntegerIndex(index);
}

164
// static
165 166
bool Name::ContainsCachedArrayIndex(uint32_t raw_hash_field) {
  return (raw_hash_field & Name::kDoesNotContainCachedArrayIndexMask) == 0;
167 168
}

169 170 171 172 173 174
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_NAME_INL_H_