Line data Source code
1 : // Copyright 2019 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_JS_COLLECTION_ITERATOR_H_
6 : #define V8_OBJECTS_JS_COLLECTION_ITERATOR_H_
7 :
8 : #include "src/globals.h"
9 : #include "src/objects.h"
10 : #include "src/objects/js-objects.h"
11 : #include "src/objects/smi.h"
12 :
13 : // Has to be the last include (doesn't have include guards):
14 : #include "src/objects/object-macros.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : class JSCollectionIterator : public JSObject {
20 : public:
21 : // [table]: the backing hash table mapping keys to values.
22 : DECL_ACCESSORS(table, Object)
23 :
24 : // [index]: The index into the data table.
25 : DECL_ACCESSORS(index, Object)
26 :
27 : void JSCollectionIteratorPrint(std::ostream& os, const char* name);
28 :
29 : DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
30 : TORQUE_GENERATED_JSCOLLECTION_ITERATOR_FIELDS)
31 :
32 : OBJECT_CONSTRUCTORS(JSCollectionIterator, JSObject);
33 : };
34 :
35 : // OrderedHashTableIterator is an iterator that iterates over the keys and
36 : // values of an OrderedHashTable.
37 : //
38 : // The iterator has a reference to the underlying OrderedHashTable data,
39 : // [table], as well as the current [index] the iterator is at.
40 : //
41 : // When the OrderedHashTable is rehashed it adds a reference from the old table
42 : // to the new table as well as storing enough data about the changes so that the
43 : // iterator [index] can be adjusted accordingly.
44 : //
45 : // When the [Next] result from the iterator is requested, the iterator checks if
46 : // there is a newer table that it needs to transition to.
47 : template <class Derived, class TableType>
48 : class OrderedHashTableIterator : public JSCollectionIterator {
49 : public:
50 : // Whether the iterator has more elements. This needs to be called before
51 : // calling |CurrentKey| and/or |CurrentValue|.
52 : bool HasMore();
53 :
54 : // Move the index forward one.
55 0 : void MoveNext() { set_index(Smi::FromInt(Smi::ToInt(index()) + 1)); }
56 :
57 : // Returns the current key of the iterator. This should only be called when
58 : // |HasMore| returns true.
59 : inline Object CurrentKey();
60 :
61 : private:
62 : // Transitions the iterator to the non obsolete backing store. This is a NOP
63 : // if the [table] is not obsolete.
64 : void Transition();
65 :
66 : OBJECT_CONSTRUCTORS(OrderedHashTableIterator, JSCollectionIterator);
67 : };
68 :
69 : } // namespace internal
70 : } // namespace v8
71 :
72 : #include "src/objects/object-macros-undef.h"
73 :
74 : #endif // V8_OBJECTS_JS_COLLECTION_ITERATOR_H_
|