V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
descriptor-array.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_DESCRIPTOR_ARRAY_H_
6 #define V8_OBJECTS_DESCRIPTOR_ARRAY_H_
7 
8 #include "src/objects.h"
9 #include "src/objects/fixed-array.h"
10 
11 // Has to be the last include (doesn't have include guards):
12 #include "src/objects/object-macros.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 template <typename T>
18 class Handle;
19 
20 class Isolate;
21 class MaybeObjectSlot;
22 
23 // An EnumCache is a pair used to hold keys and indices caches.
24 class EnumCache : public Tuple2 {
25  public:
26  DECL_ACCESSORS2(keys, FixedArray)
27  DECL_ACCESSORS2(indices, FixedArray)
28 
29  DECL_CAST(EnumCache)
30 
31  // Layout description.
32  static const int kKeysOffset = kValue1Offset;
33  static const int kIndicesOffset = kValue2Offset;
34 
35  private:
36  DISALLOW_IMPLICIT_CONSTRUCTORS(EnumCache);
37 };
38 
39 // A DescriptorArray is a custom array that holds instance descriptors.
40 // It has the following layout:
41 // Header:
42 // [16:0 bits]: number_of_all_descriptors (including slack)
43 // [32:16 bits]: number_of_descriptors
44 // [48:32 bits]: number_of_marked_descriptors (used by GC)
45 // [64:48 bits]: alignment filler
46 // [kEnumCacheOffset]: enum cache
47 // Elements:
48 // [kHeaderSize + 0]: first key (and internalized String)
49 // [kHeaderSize + 1]: first descriptor details (see PropertyDetails)
50 // [kHeaderSize + 2]: first value for constants / Smi(1) when not used
51 // Slack:
52 // [kHeaderSize + number of descriptors * 3]: start of slack
53 // The "value" fields store either values or field types. A field type is either
54 // FieldType::None(), FieldType::Any() or a weak reference to a Map. All other
55 // references are strong.
56 class DescriptorArray : public HeapObject {
57  public:
58  DECL_INT16_ACCESSORS(number_of_all_descriptors)
59  DECL_INT16_ACCESSORS(number_of_descriptors)
60  inline int16_t number_of_slack_descriptors() const;
61  inline int number_of_entries() const;
62  DECL_ACCESSORS(enum_cache, EnumCache)
63 
64  void ClearEnumCache();
65  inline void CopyEnumCacheFrom(DescriptorArray* array);
66  static void InitializeOrChangeEnumCache(Handle<DescriptorArray> descriptors,
67  Isolate* isolate,
68  Handle<FixedArray> keys,
69  Handle<FixedArray> indices);
70 
71  // Accessors for fetching instance descriptor at descriptor number.
72  inline Name GetKey(int descriptor_number);
73  inline Object* GetStrongValue(int descriptor_number);
74  inline void SetValue(int descriptor_number, Object* value);
75  inline MaybeObject GetValue(int descriptor_number);
76  inline PropertyDetails GetDetails(int descriptor_number);
77  inline int GetFieldIndex(int descriptor_number);
78  inline FieldType GetFieldType(int descriptor_number);
79 
80  inline Name GetSortedKey(int descriptor_number);
81  inline int GetSortedKeyIndex(int descriptor_number);
82  inline void SetSortedKey(int pointer, int descriptor_number);
83 
84  // Accessor for complete descriptor.
85  inline void Set(int descriptor_number, Descriptor* desc);
86  inline void Set(int descriptor_number, Name key, MaybeObject value,
87  PropertyDetails details);
88  void Replace(int descriptor_number, Descriptor* descriptor);
89 
90  // Generalizes constness, representation and field type of all field
91  // descriptors.
92  void GeneralizeAllFields();
93 
94  // Append automatically sets the enumeration index. This should only be used
95  // to add descriptors in bulk at the end, followed by sorting the descriptor
96  // array.
97  inline void Append(Descriptor* desc);
98 
99  static Handle<DescriptorArray> CopyUpTo(Isolate* isolate,
101  int enumeration_index, int slack = 0);
102 
103  static Handle<DescriptorArray> CopyUpToAddAttributes(
104  Isolate* isolate, Handle<DescriptorArray> desc, int enumeration_index,
105  PropertyAttributes attributes, int slack = 0);
106 
107  static Handle<DescriptorArray> CopyForFastObjectClone(
108  Isolate* isolate, Handle<DescriptorArray> desc, int enumeration_index,
109  int slack = 0);
110 
111  // Sort the instance descriptors by the hash codes of their keys.
112  void Sort();
113 
114  // Search the instance descriptors for given name.
115  V8_INLINE int Search(Name name, int number_of_own_descriptors);
116  V8_INLINE int Search(Name name, Map map);
117 
118  // As the above, but uses DescriptorLookupCache and updates it when
119  // necessary.
120  V8_INLINE int SearchWithCache(Isolate* isolate, Name name, Map map);
121 
122  bool IsEqualUpTo(DescriptorArray* desc, int nof_descriptors);
123 
124  // Allocates a DescriptorArray, but returns the singleton
125  // empty descriptor array object if number_of_descriptors is 0.
126  static Handle<DescriptorArray> Allocate(Isolate* isolate, int nof_descriptors,
127  int slack);
128 
129  void Initialize(EnumCache* enum_cache, HeapObject* undefined_value,
130  int nof_descriptors, int slack);
131 
132  DECL_CAST(DescriptorArray)
133 
134  // Constant for denoting key was not found.
135  static const int kNotFound = -1;
136 
137  // Layout description.
138 #define DESCRIPTOR_ARRAY_FIELDS(V) \
139  V(kNumberOfAllDescriptorsOffset, kUInt16Size) \
140  V(kNumberOfDescriptorsOffset, kUInt16Size) \
141  V(kNumberOfMarkedDescriptorsOffset, kUInt16Size) \
142  V(kFiller16BitsOffset, kUInt16Size) \
143  V(kPointersStartOffset, 0) \
144  V(kEnumCacheOffset, kTaggedSize) \
145  V(kHeaderSize, 0)
146 
147  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
148  DESCRIPTOR_ARRAY_FIELDS)
149 #undef DESCRIPTOR_ARRAY_FIELDS
150 
151  STATIC_ASSERT(IsAligned(kPointersStartOffset, kTaggedSize));
152  STATIC_ASSERT(IsAligned(kHeaderSize, kTaggedSize));
153 
154  // Garbage collection support.
155  DECL_INT16_ACCESSORS(number_of_marked_descriptors)
156  static constexpr int SizeFor(int number_of_all_descriptors) {
157  return offset(number_of_all_descriptors * kEntrySize);
158  }
159  static constexpr int OffsetOfDescriptorAt(int descriptor) {
160  return offset(descriptor * kEntrySize);
161  }
162  inline ObjectSlot GetFirstPointerSlot();
163  inline ObjectSlot GetDescriptorSlot(int descriptor);
164  inline ObjectSlot GetKeySlot(int descriptor);
165  inline MaybeObjectSlot GetValueSlot(int descriptor);
166 
168 
169  // Layout of descriptor.
170  // Naming is consistent with Dictionary classes for easy templating.
171  static const int kEntryKeyIndex = 0;
172  static const int kEntryDetailsIndex = 1;
173  static const int kEntryValueIndex = 2;
174  static const int kEntrySize = 3;
175 
176  // Print all the descriptors.
177  void PrintDescriptors(std::ostream& os);
178  void PrintDescriptorDetails(std::ostream& os, int descriptor,
179  PropertyDetails::PrintMode mode);
180 
181  DECL_PRINTER(DescriptorArray)
182  DECL_VERIFIER(DescriptorArray)
183 
184 #ifdef DEBUG
185  // Is the descriptor array sorted and without duplicates?
186  bool IsSortedNoDuplicates(int valid_descriptors = -1);
187 
188  // Are two DescriptorArrays equal?
189  bool IsEqualTo(DescriptorArray* other);
190 #endif
191 
192  static constexpr int ToDetailsIndex(int descriptor_number) {
193  return (descriptor_number * kEntrySize) + kEntryDetailsIndex;
194  }
195 
196  // Conversion from descriptor number to array indices.
197  static constexpr int ToKeyIndex(int descriptor_number) {
198  return (descriptor_number * kEntrySize) + kEntryKeyIndex;
199  }
200 
201  static constexpr int ToValueIndex(int descriptor_number) {
202  return (descriptor_number * kEntrySize) + kEntryValueIndex;
203  }
204 
205  private:
206  DECL_INT16_ACCESSORS(filler16bits)
207  // Low-level per-element accessors.
208  static constexpr int offset(int index) {
209  return kHeaderSize + index * kPointerSize;
210  }
211  inline int length() const;
212  inline MaybeObject get(int index) const;
213  inline void set(int index, MaybeObject value);
214 
215  // Transfer a complete descriptor from the src descriptor array to this
216  // descriptor array.
217  void CopyFrom(int index, DescriptorArray* src);
218 
219  // Swap first and second descriptor.
220  inline void SwapSortedKeys(int first, int second);
221 
222  DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
223 };
224 
225 } // namespace internal
226 } // namespace v8
227 
228 #include "src/objects/object-macros-undef.h"
229 
230 #endif // V8_OBJECTS_DESCRIPTOR_ARRAY_H_
Definition: libplatform.h:13
Definition: v8.h:3740