forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.h
More file actions
498 lines (405 loc) Β· 19.9 KB
/
dictionary.h
File metadata and controls
498 lines (405 loc) Β· 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// 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_DICTIONARY_H_
#define V8_OBJECTS_DICTIONARY_H_
#include <optional>
#include "src/base/export-template.h"
#include "src/common/globals.h"
#include "src/objects/hash-table.h"
#include "src/objects/property-array.h"
#include "src/objects/smi.h"
#include "src/roots/roots.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace v8::internal {
#ifdef V8_ENABLE_SWISS_NAME_DICTIONARY
class SwissNameDictionary;
using PropertyDictionary = SwissNameDictionary;
#else
using PropertyDictionary = NameDictionary;
#endif
template <typename Derived, typename Shape>
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) Dictionary
: public HashTable<Derived, Shape> {
using DerivedHashTable = HashTable<Derived, Shape>;
public:
using TodoShape = Shape;
using Key = typename TodoShape::Key;
inline Tagged<Object> ValueAt(InternalIndex entry);
inline Tagged<Object> ValueAt(PtrComprCageBase cage_base,
InternalIndex entry);
inline Tagged<Object> ValueAt(InternalIndex entry, SeqCstAccessTag);
inline Tagged<Object> ValueAt(PtrComprCageBase cage_base, InternalIndex entry,
SeqCstAccessTag);
// Returns {} if we would be reading out of the bounds of the object.
inline std::optional<Tagged<Object>> TryValueAt(InternalIndex entry);
// Set the value for entry.
inline void ValueAtPut(InternalIndex entry, Tagged<Object> value);
inline void ValueAtPut(InternalIndex entry, Tagged<Object> value,
SeqCstAccessTag);
// Swap the value for the entry.
inline Tagged<Object> ValueAtSwap(InternalIndex entry, Tagged<Object> value,
SeqCstAccessTag);
// Compare and swap the value for the entry.
inline Tagged<Object> ValueAtCompareAndSwap(InternalIndex entry,
Tagged<Object> expected,
Tagged<Object> value,
SeqCstAccessTag);
// Returns the property details for the property at entry.
inline PropertyDetails DetailsAt(InternalIndex entry);
// Set the details for entry.
inline void DetailsAtPut(InternalIndex entry, PropertyDetails value);
static const bool kIsOrderedDictionaryType = false;
// Delete a property from the dictionary.
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
V8_WARN_UNUSED_RESULT static HandleType<Derived> DeleteEntry(
Isolate* isolate, HandleType<Derived> dictionary, InternalIndex entry);
// Attempt to shrink the dictionary after deletion of key.
template <template <typename> typename HandleType>
V8_WARN_UNUSED_RESULT static inline HandleType<Derived> Shrink(
Isolate* isolate, HandleType<Derived> dictionary)
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
{
return DerivedHashTable::Shrink(isolate, dictionary);
}
int NumberOfEnumerableProperties();
// Returns the key (slow).
Tagged<Object> SlowReverseLookup(Tagged<Object> value);
inline void ClearEntry(InternalIndex entry);
// Sets the entry to (key, value) pair.
inline void SetEntry(InternalIndex entry, Tagged<Object> key,
Tagged<Object> value, PropertyDetails details);
// Garbage collection support.
inline ObjectSlot RawFieldOfValueAt(InternalIndex entry);
template <typename IsolateT, template <typename> typename HandleType,
AllocationType key_allocation =
std::is_same<IsolateT, Isolate>::value ? AllocationType::kYoung
: AllocationType::kOld>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
V8_WARN_UNUSED_RESULT static HandleType<Derived> Add(
IsolateT* isolate, HandleType<Derived> dictionary, Key key,
DirectHandle<Object> value, PropertyDetails details,
InternalIndex* entry_out = nullptr);
// This method is only safe to use when it is guaranteed that the dictionary
// doesn't need to grow.
// The number of elements stored is not updated. Use
// |SetInitialNumberOfElements| to update the number in one go.
template <typename IsolateT, template <typename> typename HandleType,
AllocationType key_allocation =
std::is_same<IsolateT, Isolate>::value ? AllocationType::kYoung
: AllocationType::kOld>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
static void UncheckedAdd(IsolateT* isolate, HandleType<Derived> dictionary,
Key key, DirectHandle<Object> value,
PropertyDetails details);
static Handle<Derived> ShallowCopy(
Isolate* isolate, DirectHandle<Derived> dictionary,
AllocationType allocation = AllocationType::kYoung);
protected:
// Generic at put operation.
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
V8_WARN_UNUSED_RESULT static HandleType<Derived> AtPut(
Isolate* isolate, HandleType<Derived> dictionary, Key key,
DirectHandle<Object> value, PropertyDetails details);
static void UncheckedAtPut(Isolate* isolate, DirectHandle<Derived> dictionary,
Key key, DirectHandle<Object> value,
PropertyDetails details);
};
#define EXTERN_DECLARE_DICTIONARY(DERIVED, SHAPE) \
EXTERN_DECLARE_HASH_TABLE(DERIVED, SHAPE) \
extern template class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) \
Dictionary<DERIVED, SHAPE>;
template <typename Key>
class BaseDictionaryShape : public BaseShape<Key> {
public:
static const bool kHasDetails = true;
template <typename Dictionary>
static inline PropertyDetails DetailsAt(Tagged<Dictionary> dict,
InternalIndex entry);
template <typename Dictionary>
static inline void DetailsAtPut(Tagged<Dictionary> dict, InternalIndex entry,
PropertyDetails value);
static const bool kDoHashSpreading = false;
static const uint32_t kHashBits = 0;
};
class BaseNameDictionaryShape : public BaseDictionaryShape<DirectHandle<Name>> {
public:
static inline bool IsMatch(DirectHandle<Name> key, Tagged<Object> other);
static inline uint32_t Hash(ReadOnlyRoots roots, DirectHandle<Name> key);
static inline uint32_t HashForObject(ReadOnlyRoots roots,
Tagged<Object> object);
template <AllocationType allocation = AllocationType::kYoung>
static inline DirectHandle<Object> AsHandle(Isolate* isolate,
DirectHandle<Name> key);
template <AllocationType allocation = AllocationType::kOld>
static inline DirectHandle<Object> AsHandle(LocalIsolate* isolate,
DirectHandle<Name> key);
static const int kEntryValueIndex = 1;
};
class NameDictionaryShape : public BaseNameDictionaryShape {
public:
static const int kPrefixSize = 3;
static const int kEntrySize = 3;
static const bool kMatchNeedsHoleCheck = false;
};
template <typename Derived, typename Shape>
class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) BaseNameDictionary
: public Dictionary<Derived, Shape> {
using Key = typename Shape::Key;
public:
static const int kNextEnumerationIndexIndex =
HashTableBase::kPrefixStartIndex;
static const int kObjectHashIndex = kNextEnumerationIndexIndex + 1;
static const int kEntryValueIndex = 1;
inline void SetHash(int hash);
inline int Hash() const;
// Creates a new dictionary.
template <typename IsolateT>
V8_WARN_UNUSED_RESULT static Handle<Derived> New(
IsolateT* isolate, int at_least_space_for,
AllocationType allocation = AllocationType::kYoung,
MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY);
// Allocate the next enumeration index. Possibly updates all enumeration
// indices in the table.
static int NextEnumerationIndex(Isolate* isolate,
DirectHandle<Derived> dictionary);
// Accessors for next enumeration index.
inline int next_enumeration_index();
inline void set_next_enumeration_index(int index);
// Return the key indices sorted by its enumeration index.
static DirectHandle<FixedArray> IterationIndices(
Isolate* isolate, DirectHandle<Derived> dictionary);
template <typename IsolateT, template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
V8_WARN_UNUSED_RESULT static HandleType<Derived>
AddNoUpdateNextEnumerationIndex(IsolateT* isolate,
HandleType<Derived> dictionary, Key key,
DirectHandle<Object> value,
PropertyDetails details,
InternalIndex* entry_out = nullptr);
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Derived>, DirectHandle<Derived>>)
V8_WARN_UNUSED_RESULT static HandleType<Derived> Add(
Isolate* isolate, HandleType<Derived> dictionary, Key key,
DirectHandle<Object> value, PropertyDetails details,
InternalIndex* entry_out = nullptr);
// Exposed for NameDictionaryLookupForwardedString slow path for forwarded
// strings.
using Dictionary<Derived, Shape>::FindInsertionEntry;
};
class SimpleNameDictionaryShape : public BaseNameDictionaryShape {
public:
static const bool kHasDetails = false;
static const bool kMatchNeedsHoleCheck = false;
static const int kPrefixSize = 0;
static const int kEntrySize = 2;
template <typename Dictionary>
static inline PropertyDetails DetailsAt(Tagged<Dictionary> dict,
InternalIndex entry) {
UNREACHABLE();
}
template <typename Dictionary>
static inline void DetailsAtPut(Tagged<Dictionary> dict, InternalIndex entry,
PropertyDetails value) {
UNREACHABLE();
}
};
EXTERN_DECLARE_DICTIONARY(SimpleNameDictionary, SimpleNameDictionaryShape)
// A simple Name-to-Object dictionary.
class SimpleNameDictionary
: public Dictionary<SimpleNameDictionary, SimpleNameDictionaryShape> {
public:
static inline DirectHandle<Map> GetMap(RootsTable& roots);
V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static Handle<SimpleNameDictionary>
Set(Isolate* isolate, Handle<SimpleNameDictionary> dictionary,
DirectHandle<Name> key, DirectHandle<Object> value);
static const int kEntryValueIndex = 1;
};
#define EXTERN_DECLARE_BASE_NAME_DICTIONARY(DERIVED, SHAPE) \
EXTERN_DECLARE_DICTIONARY(DERIVED, SHAPE) \
extern template class EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) \
BaseNameDictionary<DERIVED, SHAPE>;
EXTERN_DECLARE_BASE_NAME_DICTIONARY(NameDictionary, NameDictionaryShape)
class V8_EXPORT_PRIVATE NameDictionary
: public BaseNameDictionary<NameDictionary, NameDictionaryShape> {
public:
static inline DirectHandle<Map> GetMap(RootsTable& roots);
DECL_PRINTER(NameDictionary)
static const int kFlagsIndex = kObjectHashIndex + 1;
static const int kEntryValueIndex = 1;
static const int kEntryDetailsIndex = 2;
static const int kInitialCapacity = 2;
inline Tagged<Name> NameAt(InternalIndex entry);
inline Tagged<Name> NameAt(PtrComprCageBase cage_base, InternalIndex entry);
inline void set_hash(int hash);
inline int hash() const;
// Note: Flags are stored as smi, so only 31 bits are usable.
using MayHaveInterestingPropertiesBit = base::BitField<bool, 0, 1, uint32_t>;
DECL_BOOLEAN_ACCESSORS(may_have_interesting_properties)
static constexpr int kFlagsDefault = 0;
inline uint32_t flags() const;
inline void set_flags(uint32_t flags);
// Creates a new NameDictionary.
template <typename IsolateT>
V8_WARN_UNUSED_RESULT static Handle<NameDictionary> New(
IsolateT* isolate, int at_least_space_for,
AllocationType allocation = AllocationType::kYoung,
MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY);
};
class V8_EXPORT_PRIVATE GlobalDictionaryShape : public BaseNameDictionaryShape {
public:
static inline bool IsMatch(DirectHandle<Name> key, Tagged<Object> other);
static inline uint32_t HashForObject(ReadOnlyRoots roots,
Tagged<Object> object);
static const bool kMatchNeedsHoleCheck = true;
static const int kPrefixSize = 2;
static const int kEntrySize = 1;
template <typename Dictionary>
static inline PropertyDetails DetailsAt(Tagged<Dictionary> dict,
InternalIndex entry);
template <typename Dictionary>
static inline void DetailsAtPut(Tagged<Dictionary> dict, InternalIndex entry,
PropertyDetails value);
static inline Tagged<Object> Unwrap(Tagged<Object> key);
};
EXTERN_DECLARE_BASE_NAME_DICTIONARY(GlobalDictionary, GlobalDictionaryShape)
class V8_EXPORT_PRIVATE GlobalDictionary
: public BaseNameDictionary<GlobalDictionary, GlobalDictionaryShape> {
public:
static inline DirectHandle<Map> GetMap(RootsTable& roots);
DECL_PRINTER(GlobalDictionary)
inline Tagged<Object> ValueAt(InternalIndex entry);
inline Tagged<Object> ValueAt(PtrComprCageBase cage_base,
InternalIndex entry);
inline Tagged<PropertyCell> CellAt(InternalIndex entry);
inline Tagged<PropertyCell> CellAt(PtrComprCageBase cage_base,
InternalIndex entry);
inline void SetEntry(InternalIndex entry, Tagged<Object> key,
Tagged<Object> value, PropertyDetails details);
inline void ClearEntry(InternalIndex entry);
inline Tagged<Name> NameAt(InternalIndex entry);
inline Tagged<Name> NameAt(PtrComprCageBase cage_base, InternalIndex entry);
inline void ValueAtPut(InternalIndex entry, Tagged<Object> value);
std::optional<Tagged<PropertyCell>>
TryFindPropertyCellForConcurrentLookupIterator(Isolate* isolate,
DirectHandle<Name> name,
RelaxedLoadTag tag);
};
class NumberDictionaryBaseShape : public BaseDictionaryShape<uint32_t> {
public:
static inline bool IsMatch(uint32_t key, Tagged<Object> other);
template <AllocationType allocation = AllocationType::kYoung>
static inline DirectHandle<Object> AsHandle(Isolate* isolate, uint32_t key);
template <AllocationType allocation = AllocationType::kOld>
static inline DirectHandle<Object> AsHandle(LocalIsolate* isolate,
uint32_t key);
static inline uint32_t Hash(ReadOnlyRoots roots, uint32_t key);
static inline uint32_t HashForObject(ReadOnlyRoots roots,
Tagged<Object> object);
static const bool kMatchNeedsHoleCheck = true;
};
class NumberDictionaryShape : public NumberDictionaryBaseShape {
public:
static const int kPrefixSize = 1;
static const int kEntrySize = 3;
};
class SimpleNumberDictionaryShape : public NumberDictionaryBaseShape {
public:
static const bool kHasDetails = false;
static const int kPrefixSize = 0;
static const int kEntrySize = 2;
template <typename Dictionary>
static inline PropertyDetails DetailsAt(Tagged<Dictionary> dict,
InternalIndex entry) {
UNREACHABLE();
}
template <typename Dictionary>
static inline void DetailsAtPut(Tagged<Dictionary> dict, InternalIndex entry,
PropertyDetails value) {
UNREACHABLE();
}
};
EXTERN_DECLARE_DICTIONARY(SimpleNumberDictionary, SimpleNumberDictionaryShape)
// SimpleNumberDictionary is used to map number to an entry.
class SimpleNumberDictionary
: public Dictionary<SimpleNumberDictionary, SimpleNumberDictionaryShape> {
public:
static inline DirectHandle<Map> GetMap(RootsTable& roots);
// Type specific at put (default NONE attributes is used when adding).
V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static Handle<SimpleNumberDictionary>
Set(Isolate* isolate, Handle<SimpleNumberDictionary> dictionary, uint32_t key,
DirectHandle<Object> value);
static const int kEntryValueIndex = 1;
};
EXTERN_DECLARE_DICTIONARY(NumberDictionary, NumberDictionaryShape)
// NumberDictionary is used as elements backing store and provides a bitfield
// and stores property details for every entry.
class NumberDictionary
: public Dictionary<NumberDictionary, NumberDictionaryShape> {
public:
static inline DirectHandle<Map> GetMap(RootsTable& roots);
DECL_PRINTER(NumberDictionary)
// Type specific at put (default NONE attributes is used when adding).
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<NumberDictionary>,
DirectHandle<NumberDictionary>>)
V8_WARN_UNUSED_RESULT static HandleType<NumberDictionary> Set(
Isolate* isolate, HandleType<NumberDictionary> dictionary, uint32_t key,
DirectHandle<Object> value,
DirectHandle<JSObject> dictionary_holder = DirectHandle<JSObject>::null(),
PropertyDetails details = PropertyDetails::Empty());
// This method is only safe to use when it is guaranteed that the dictionary
// doesn't need to grow.
// The number of elements stored and the maximum index is not updated. Use
// |SetInitialNumberOfElements| and |UpdateMaxNumberKey| to update the number
// in one go.
static void UncheckedSet(Isolate* isolate,
DirectHandle<NumberDictionary> dictionary,
uint32_t key, DirectHandle<Object> value);
static const int kMaxNumberKeyIndex = kPrefixStartIndex;
void UpdateMaxNumberKey(uint32_t key,
DirectHandle<JSObject> dictionary_holder);
// Sorting support
void CopyValuesTo(Tagged<FixedArray> elements);
// If slow elements are required we will never go back to fast-case
// for the elements kept in this dictionary. We require slow
// elements if an element has been added at an index larger than
// kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
// when defining a getter or setter with a number key.
inline bool requires_slow_elements();
inline void set_requires_slow_elements();
// Get the value of the max number key that has been added to this
// dictionary. max_number_key can only be called if
// requires_slow_elements returns false.
inline uint32_t max_number_key();
static const int kEntryValueIndex = 1;
static const int kEntryDetailsIndex = 2;
// Bit masks.
static const int kRequiresSlowElementsMask = 1;
static const int kRequiresSlowElementsTagSize = 1;
static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
// JSObjects prefer dictionary elements if the dictionary saves this much
// memory compared to a fast elements backing store.
static const uint32_t kPreferFastElementsSizeFactor = 3;
};
// The comparator is passed two indices |a| and |b|, and it returns < 0 when the
// property at index |a| comes before the property at index |b| in the
// enumeration order.
template <typename Dictionary>
struct EnumIndexComparator {
explicit EnumIndexComparator(Tagged<Dictionary> dict) : dict(dict) {}
bool operator()(Tagged_t a, Tagged_t b) {
PropertyDetails details_a(dict->DetailsAt(
InternalIndex(Tagged<Smi>(static_cast<Address>(a)).value())));
PropertyDetails details_b(dict->DetailsAt(
InternalIndex(Tagged<Smi>(static_cast<Address>(b)).value())));
return details_a.dictionary_index() < details_b.dictionary_index();
}
Tagged<Dictionary> dict;
};
} // namespace v8::internal
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_DICTIONARY_H_