name-inl.h 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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_

#include "src/objects/name.h"

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 15 16 17 18 19
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

20 21
TQ_OBJECT_CONSTRUCTORS_IMPL(Name)
TQ_OBJECT_CONSTRUCTORS_IMPL(Symbol)
22

23 24 25
BIT_FIELD_ACCESSORS(Symbol, flags, is_private, Symbol::IsPrivateBit)
BIT_FIELD_ACCESSORS(Symbol, flags, is_well_known_symbol,
                    Symbol::IsWellKnownSymbolBit)
26 27
BIT_FIELD_ACCESSORS(Symbol, flags, is_in_public_symbol_table,
                    Symbol::IsInPublicSymbolTableBit)
28 29
BIT_FIELD_ACCESSORS(Symbol, flags, is_interesting_symbol,
                    Symbol::IsInterestingSymbolBit)
30

31 32 33 34 35 36 37 38 39 40 41 42
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));
}

43 44
bool Symbol::is_private_name() const {
  bool value = Symbol::IsPrivateNameBit::decode(flags());
45 46 47 48
  DCHECK_IMPLIES(value, is_private());
  return value;
}

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

56
DEF_GETTER(Name, IsUniqueName, bool) {
57
  uint32_t type = map(isolate).instance_type();
58 59 60 61
  bool result = (type & (kIsNotStringMask | kIsNotInternalizedMask)) !=
                (kStringTag | kNotInternalizedTag);
  SLOW_DCHECK(result == HeapObject::IsUniqueName());
  return result;
62 63
}

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

73
bool Name::Equals(Isolate* isolate, Handle<Name> one, Handle<Name> two) {
74 75 76 77 78
  if (one.is_identical_to(two)) return true;
  if ((one->IsInternalizedString() && two->IsInternalizedString()) ||
      one->IsSymbol() || two->IsSymbol()) {
    return false;
  }
79
  return String::SlowEquals(isolate, Handle<String>::cast(one),
80 81 82 83 84 85 86 87 88 89 90 91 92 93
                            Handle<String>::cast(two));
}

bool Name::IsHashFieldComputed(uint32_t field) {
  return (field & kHashNotComputedMask) == 0;
}

bool Name::HasHashCode() { return IsHashFieldComputed(hash_field()); }

uint32_t Name::Hash() {
  // Fast case: has hash code already been computed?
  uint32_t field = hash_field();
  if (IsHashFieldComputed(field)) return field >> kHashShift;
  // Slow case: compute hash code and set it. Has to be a string.
94
  return String::cast(*this).ComputeAndSetHash();
95 96
}

97
DEF_GETTER(Name, IsInterestingSymbol, bool) {
98
  return IsSymbol(isolate) && Symbol::cast(*this).is_interesting_symbol();
99 100
}

101
DEF_GETTER(Name, IsPrivate, bool) {
102
  return this->IsSymbol(isolate) && Symbol::cast(*this).is_private();
103 104
}

105
DEF_GETTER(Name, IsPrivateName, bool) {
106
  bool is_private_name =
107
      this->IsSymbol(isolate) && Symbol::cast(*this).is_private_name();
108 109
  DCHECK_IMPLIES(is_private_name, IsPrivate());
  return is_private_name;
110 111
}

112 113 114 115 116 117 118
DEF_GETTER(Name, IsPrivateBrand, bool) {
  bool is_private_brand =
      this->IsSymbol(isolate) && Symbol::cast(*this).is_private_brand();
  DCHECK_IMPLIES(is_private_brand, IsPrivateName());
  return is_private_brand;
}

119
bool Name::AsArrayIndex(uint32_t* index) {
120
  return IsString() && String::cast(*this).AsArrayIndex(index);
121 122
}

123 124 125 126
bool Name::AsIntegerIndex(size_t* index) {
  return IsString() && String::cast(*this).AsIntegerIndex(index);
}

127 128 129 130 131
// static
bool Name::ContainsCachedArrayIndex(uint32_t hash) {
  return (hash & Name::kDoesNotContainCachedArrayIndexMask) == 0;
}

132 133 134 135 136 137
}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_NAME_INL_H_