Home | History | Annotate | Download | only in objects
      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 
     12 // Has to be the last include (doesn't have include guards):
     13 #include "src/objects/object-macros.h"
     14 
     15 namespace v8 {
     16 namespace internal {
     17 
     18 CAST_ACCESSOR(Name)
     19 CAST_ACCESSOR(Symbol)
     20 
     21 ACCESSORS(Symbol, name, Object, kNameOffset)
     22 SMI_ACCESSORS(Symbol, flags, kFlagsOffset)
     23 BOOL_ACCESSORS(Symbol, flags, is_private, kPrivateBit)
     24 BOOL_ACCESSORS(Symbol, flags, is_well_known_symbol, kWellKnownSymbolBit)
     25 BOOL_ACCESSORS(Symbol, flags, is_public, kPublicBit)
     26 BOOL_ACCESSORS(Symbol, flags, is_interesting_symbol, kInterestingSymbolBit)
     27 
     28 bool Symbol::is_private_field() const {
     29   bool value = BooleanBit::get(flags(), kPrivateFieldBit);
     30   DCHECK_IMPLIES(value, is_private());
     31   return value;
     32 }
     33 
     34 void Symbol::set_is_private_field() {
     35   int old_value = flags();
     36   // TODO(gsathya): Re-order the bits to have these next to each other
     37   // and just do the bit shifts once.
     38   set_flags(BooleanBit::set(old_value, kPrivateBit, true) |
     39             BooleanBit::set(old_value, kPrivateFieldBit, true));
     40 }
     41 
     42 bool Name::IsUniqueName() const {
     43   uint32_t type = map()->instance_type();
     44   return (type & (kIsNotStringMask | kIsNotInternalizedMask)) !=
     45          (kStringTag | kNotInternalizedTag);
     46 }
     47 
     48 uint32_t Name::hash_field() {
     49   return READ_UINT32_FIELD(this, kHashFieldOffset);
     50 }
     51 
     52 void Name::set_hash_field(uint32_t value) {
     53   WRITE_UINT32_FIELD(this, kHashFieldOffset, value);
     54 #if V8_HOST_ARCH_64_BIT
     55 #if V8_TARGET_LITTLE_ENDIAN
     56   WRITE_UINT32_FIELD(this, kHashFieldSlot + kInt32Size, 0);
     57 #else
     58   WRITE_UINT32_FIELD(this, kHashFieldSlot, 0);
     59 #endif
     60 #endif
     61 }
     62 
     63 bool 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 
     72 bool 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 
     82 bool Name::IsHashFieldComputed(uint32_t field) {
     83   return (field & kHashNotComputedMask) == 0;
     84 }
     85 
     86 bool Name::HasHashCode() { return IsHashFieldComputed(hash_field()); }
     87 
     88 uint32_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   // Also the string must be writable, because read-only strings will have their
     94   // hash values precomputed.
     95   return String::cast(this)->ComputeAndSetHash(
     96       Heap::FromWritableHeapObject(this)->isolate());
     97 }
     98 
     99 bool Name::IsInterestingSymbol() const {
    100   return IsSymbol() && Symbol::cast(this)->is_interesting_symbol();
    101 }
    102 
    103 bool Name::IsPrivate() {
    104   return this->IsSymbol() && Symbol::cast(this)->is_private();
    105 }
    106 
    107 bool Name::IsPrivateField() {
    108   bool is_private_field =
    109       this->IsSymbol() && Symbol::cast(this)->is_private_field();
    110   DCHECK_IMPLIES(is_private_field, IsPrivate());
    111   return is_private_field;
    112 }
    113 
    114 bool Name::AsArrayIndex(uint32_t* index) {
    115   return IsString() && String::cast(this)->AsArrayIndex(index);
    116 }
    117 
    118 // static
    119 bool Name::ContainsCachedArrayIndex(uint32_t hash) {
    120   return (hash & Name::kDoesNotContainCachedArrayIndexMask) == 0;
    121 }
    122 
    123 }  // namespace internal
    124 }  // namespace v8
    125 
    126 #include "src/objects/object-macros-undef.h"
    127 
    128 #endif  // V8_OBJECTS_NAME_INL_H_
    129