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