name.h 7.09 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_H_
#define V8_OBJECTS_NAME_H_

8
#include "src/objects/heap-object.h"
9
#include "src/objects/objects.h"
10
#include "torque-generated/class-definitions-tq.h"
11 12 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 {

// The Name abstract class captures anything that can be used as a property
// name, i.e., strings and symbols.  All names store a hash value.
20
class Name : public TorqueGeneratedName<Name, HeapObject> {
21 22 23 24 25 26 27 28
 public:
  // Tells whether the hash code has been computed.
  inline bool HasHashCode();

  // Returns a hash value used for the property table
  inline uint32_t Hash();

  // Equality operations.
29
  inline bool Equals(Name other);
30 31
  inline static bool Equals(Isolate* isolate, Handle<Name> one,
                            Handle<Name> two);
32 33 34 35

  // Conversion.
  inline bool AsArrayIndex(uint32_t* index);

36 37 38 39 40 41
  // An "interesting symbol" is a well-known symbol, like @@toStringTag,
  // that's often looked up on random objects but is usually not present.
  // We optimize this by setting a flag on the object's map when such
  // symbol properties are added, so we can optimize lookups on objects
  // that don't have the flag.
  inline bool IsInterestingSymbol() const;
42
  inline bool IsInterestingSymbol(Isolate* isolate) const;
43

44
  // If the name is private, it can only name own properties.
45 46
  inline bool IsPrivate() const;
  inline bool IsPrivate(Isolate* isolate) const;
47

48
  // If the name is a private name, it should behave like a private
49
  // symbol but also throw on property access miss.
50 51
  inline bool IsPrivateName() const;
  inline bool IsPrivateName(Isolate* isolate) const;
52

53
  inline bool IsUniqueName() const;
54
  inline bool IsUniqueName(Isolate* isolate) const;
55

56 57
  static inline bool ContainsCachedArrayIndex(uint32_t hash);

58 59
  // Return a string version of this name that is converted according to the
  // rules described in ES6 section 9.2.11.
60
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToFunctionName(
61
      Isolate* isolate, Handle<Name> name);
62
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToFunctionName(
63
      Isolate* isolate, Handle<Name> name, Handle<String> prefix);
64

65
  DECL_PRINTER(Name)
66 67
  void NameShortPrint();
  int NameShortPrint(Vector<char> str);
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  // Mask constant for checking if a name has a computed hash code
  // and if it is a string that is an array index.  The least significant bit
  // indicates whether a hash code has been computed.  If the hash code has
  // been computed the 2nd bit tells whether the string can be used as an
  // array index.
  static const int kHashNotComputedMask = 1;
  static const int kIsNotArrayIndexMask = 1 << 1;
  static const int kNofHashBitFields = 2;

  // Shift constant retrieving hash code from hash field.
  static const int kHashShift = kNofHashBitFields;

  // Only these bits are relevant in the hash, since the top two are shifted
  // out.
  static const uint32_t kHashBitMask = 0xffffffffu >> kHashShift;

  // Array index strings this short can keep their index in the hash field.
  static const int kMaxCachedArrayIndexLength = 7;

  // Maximum number of characters to consider when trying to convert a string
  // value into an array index.
  static const int kMaxArrayIndexSize = 10;

  // For strings which are array indexes the hash value has the string length
  // mixed into the hash, mainly to avoid a hash value of zero which would be
  // the case for the string '0'. 24 bits are used for the array index value.
  static const int kArrayIndexValueBits = 24;
  static const int kArrayIndexLengthBits =
      kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;

  STATIC_ASSERT(kArrayIndexLengthBits > 0);
  STATIC_ASSERT(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));

102 103 104 105 106
  using ArrayIndexValueBits =
      BitField<unsigned int, kNofHashBitFields, kArrayIndexValueBits>;
  using ArrayIndexLengthBits =
      BitField<unsigned int, kNofHashBitFields + kArrayIndexValueBits,
               kArrayIndexLengthBits>;
107 108 109 110

  // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
  // could use a mask to test if the length of string is less than or equal to
  // kMaxCachedArrayIndexLength.
111 112
  static_assert(base::bits::IsPowerOfTwo(kMaxCachedArrayIndexLength + 1),
                "(kMaxCachedArrayIndexLength + 1) must be power of two");
113

114 115 116
  // When any of these bits is set then the hash field does not contain a cached
  // array index.
  static const unsigned int kDoesNotContainCachedArrayIndexMask =
117 118 119 120 121 122 123 124 125 126 127
      (~static_cast<unsigned>(kMaxCachedArrayIndexLength)
       << ArrayIndexLengthBits::kShift) |
      kIsNotArrayIndexMask;

  // Value of empty hash field indicating that the hash is not computed.
  static const int kEmptyHashField =
      kIsNotArrayIndexMask | kHashNotComputedMask;

 protected:
  static inline bool IsHashFieldComputed(uint32_t field);

128
  TQ_OBJECT_CONSTRUCTORS(Name)
129 130 131
};

// ES6 symbols.
132
class Symbol : public TorqueGeneratedSymbol<Symbol, Name> {
133 134 135 136 137 138 139 140 141 142
 public:
  // [is_private]: Whether this is a private symbol.  Private symbols can only
  // be used to designate own properties of objects.
  DECL_BOOLEAN_ACCESSORS(is_private)

  // [is_well_known_symbol]: Whether this is a spec-defined well-known symbol,
  // or not. Well-known symbols do not throw when an access check fails during
  // a load.
  DECL_BOOLEAN_ACCESSORS(is_well_known_symbol)

143 144 145 146 147 148
  // [is_interesting_symbol]: Whether this is an "interesting symbol", which
  // is a well-known symbol like @@toStringTag that's often looked up on
  // random objects but is usually not present. See Name::IsInterestingSymbol()
  // for a detailed description.
  DECL_BOOLEAN_ACCESSORS(is_interesting_symbol)

149 150 151 152
  // [is_in_public_symbol_table]: Whether this is a symbol created by
  // Symbol.for. Calling Symbol.keyFor on such a symbol simply needs
  // to return the attached name.
  DECL_BOOLEAN_ACCESSORS(is_in_public_symbol_table)
153

154
  // [is_private_name]: Whether this is a private name.  Private names
155 156 157 158
  // are the same as private symbols except they throw on missing
  // property access.
  //
  // This also sets the is_private bit.
159 160
  inline bool is_private_name() const;
  inline void set_is_private_name();
161

162
  // Dispatched behavior.
163
  DECL_PRINTER(Symbol)
164
  DECL_VERIFIER(Symbol)
165

166
// Flags layout.
167 168 169 170 171
#define FLAGS_BIT_FIELDS(V, _)            \
  V(IsPrivateBit, bool, 1, _)             \
  V(IsWellKnownSymbolBit, bool, 1, _)     \
  V(IsInPublicSymbolTableBit, bool, 1, _) \
  V(IsInterestingSymbolBit, bool, 1, _)   \
172
  V(IsPrivateNameBit, bool, 1, _)
173 174 175 176

  DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
#undef FLAGS_BIT_FIELDS

177
  using BodyDescriptor = FixedBodyDescriptor<kNameOffset, kSize, kSize>;
178 179 180 181

  void SymbolShortPrint(std::ostream& os);

 private:
182
  const char* PrivateSymbolToName() const;
183

184
  // TODO(cbruni): remove once the new maptracer is in place.
185 186
  friend class Name;  // For PrivateSymbolToName.

187
  TQ_OBJECT_CONSTRUCTORS(Symbol)
188 189 190 191 192 193 194 195
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_NAME_H_