Line data Source code
1 : // Copyright 2018 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_ORDERED_HASH_TABLE_INL_H_
6 : #define V8_OBJECTS_ORDERED_HASH_TABLE_INL_H_
7 :
8 : #include "src/objects/ordered-hash-table.h"
9 :
10 : #include "src/heap/heap.h"
11 : #include "src/objects-inl.h"
12 : #include "src/objects/compressed-slots.h"
13 : #include "src/objects/fixed-array-inl.h"
14 : #include "src/objects/slots.h"
15 :
16 : // Has to be the last include (doesn't have include guards):
17 : #include "src/objects/object-macros.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 :
22 : CAST_ACCESSOR(OrderedNameDictionary)
23 : CAST_ACCESSOR(SmallOrderedNameDictionary)
24 : CAST_ACCESSOR(OrderedHashMap)
25 : CAST_ACCESSOR(OrderedHashSet)
26 : CAST_ACCESSOR(SmallOrderedHashMap)
27 : CAST_ACCESSOR(SmallOrderedHashSet)
28 :
29 : template <class Derived, int entrysize>
30 : OrderedHashTable<Derived, entrysize>::OrderedHashTable(Address ptr)
31 : : FixedArray(ptr) {}
32 :
33 : OrderedHashSet::OrderedHashSet(Address ptr)
34 : : OrderedHashTable<OrderedHashSet, 1>(ptr) {
35 : SLOW_DCHECK(IsOrderedHashSet());
36 : }
37 :
38 : OrderedHashMap::OrderedHashMap(Address ptr)
39 : : OrderedHashTable<OrderedHashMap, 2>(ptr) {
40 : SLOW_DCHECK(IsOrderedHashMap());
41 : }
42 :
43 : OrderedNameDictionary::OrderedNameDictionary(Address ptr)
44 : : OrderedHashTable<OrderedNameDictionary, 3>(ptr) {
45 : SLOW_DCHECK(IsOrderedNameDictionary());
46 : }
47 :
48 : template <class Derived>
49 : SmallOrderedHashTable<Derived>::SmallOrderedHashTable(Address ptr)
50 : : HeapObject(ptr) {}
51 :
52 : template <class Derived>
53 : Object SmallOrderedHashTable<Derived>::KeyAt(int entry) const {
54 : DCHECK_LT(entry, Capacity());
55 0 : Offset entry_offset = GetDataEntryOffset(entry, Derived::kKeyIndex);
56 3646625 : return READ_FIELD(*this, entry_offset);
57 : }
58 :
59 : template <class Derived>
60 : Object SmallOrderedHashTable<Derived>::GetDataEntry(int entry,
61 : int relative_index) {
62 : DCHECK_LT(entry, Capacity());
63 : DCHECK_LE(static_cast<unsigned>(relative_index), Derived::kEntrySize);
64 2600 : Offset entry_offset = GetDataEntryOffset(entry, relative_index);
65 28335 : return READ_FIELD(*this, entry_offset);
66 : }
67 :
68 : OBJECT_CONSTRUCTORS_IMPL(SmallOrderedHashSet,
69 : SmallOrderedHashTable<SmallOrderedHashSet>)
70 : OBJECT_CONSTRUCTORS_IMPL(SmallOrderedHashMap,
71 : SmallOrderedHashTable<SmallOrderedHashMap>)
72 : OBJECT_CONSTRUCTORS_IMPL(SmallOrderedNameDictionary,
73 : SmallOrderedHashTable<SmallOrderedNameDictionary>)
74 :
75 56 : RootIndex OrderedHashSet::GetMapRootIndex() {
76 56 : return RootIndex::kOrderedHashSetMap;
77 : }
78 :
79 56 : RootIndex OrderedHashMap::GetMapRootIndex() {
80 56 : return RootIndex::kOrderedHashMapMap;
81 : }
82 :
83 : RootIndex OrderedNameDictionary::GetMapRootIndex() {
84 : return RootIndex::kOrderedNameDictionaryMap;
85 : }
86 :
87 : RootIndex SmallOrderedNameDictionary::GetMapRootIndex() {
88 : return RootIndex::kSmallOrderedNameDictionaryMap;
89 : }
90 :
91 4 : RootIndex SmallOrderedHashMap::GetMapRootIndex() {
92 4 : return RootIndex::kSmallOrderedHashMapMap;
93 : }
94 :
95 4 : RootIndex SmallOrderedHashSet::GetMapRootIndex() {
96 4 : return RootIndex::kSmallOrderedHashSetMap;
97 : }
98 :
99 5395 : inline Object OrderedHashMap::ValueAt(int entry) {
100 : DCHECK_NE(entry, kNotFound);
101 : DCHECK_LT(entry, UsedCapacity());
102 10790 : return get(EntryToIndex(entry) + kValueOffset);
103 : }
104 :
105 35 : inline Object OrderedNameDictionary::ValueAt(int entry) {
106 : DCHECK_NE(entry, kNotFound);
107 : DCHECK_LT(entry, UsedCapacity());
108 70 : return get(EntryToIndex(entry) + kValueOffset);
109 : }
110 :
111 : // Set the value for entry.
112 10 : inline void OrderedNameDictionary::ValueAtPut(int entry, Object value) {
113 : DCHECK_NE(entry, kNotFound);
114 : DCHECK_LT(entry, UsedCapacity());
115 10 : this->set(EntryToIndex(entry) + kValueOffset, value);
116 10 : }
117 :
118 : // Returns the property details for the property at entry.
119 25 : inline PropertyDetails OrderedNameDictionary::DetailsAt(int entry) {
120 : DCHECK_NE(entry, kNotFound);
121 : DCHECK_LT(entry, this->UsedCapacity());
122 : // TODO(gsathya): Optimize the cast away.
123 : return PropertyDetails(
124 50 : Smi::cast(get(EntryToIndex(entry) + kPropertyDetailsOffset)));
125 : }
126 :
127 10 : inline void OrderedNameDictionary::DetailsAtPut(int entry,
128 : PropertyDetails value) {
129 : DCHECK_NE(entry, kNotFound);
130 : DCHECK_LT(entry, this->UsedCapacity());
131 : // TODO(gsathya): Optimize the cast away.
132 : this->set(EntryToIndex(entry) + kPropertyDetailsOffset, value.AsSmi());
133 10 : }
134 :
135 1305 : inline Object SmallOrderedNameDictionary::ValueAt(int entry) {
136 2610 : return this->GetDataEntry(entry, kValueIndex);
137 : }
138 :
139 : // Set the value for entry.
140 : inline void SmallOrderedNameDictionary::ValueAtPut(int entry, Object value) {
141 10 : this->SetDataEntry(entry, kValueIndex, value);
142 : }
143 :
144 : // Returns the property details for the property at entry.
145 1295 : inline PropertyDetails SmallOrderedNameDictionary::DetailsAt(int entry) {
146 : // TODO(gsathya): Optimize the cast away. And store this in the data table.
147 : return PropertyDetails(
148 2590 : Smi::cast(this->GetDataEntry(entry, kPropertyDetailsIndex)));
149 : }
150 :
151 : // Set the details for entry.
152 : inline void SmallOrderedNameDictionary::DetailsAtPut(int entry,
153 : PropertyDetails value) {
154 : // TODO(gsathya): Optimize the cast away. And store this in the data table.
155 10 : this->SetDataEntry(entry, kPropertyDetailsIndex, value.AsSmi());
156 : }
157 :
158 : inline bool OrderedHashSet::Is(Handle<HeapObject> table) {
159 10 : return table->IsOrderedHashSet();
160 : }
161 :
162 : inline bool OrderedHashMap::Is(Handle<HeapObject> table) {
163 10 : return table->IsOrderedHashMap();
164 : }
165 :
166 2624015 : inline bool SmallOrderedHashSet::Is(Handle<HeapObject> table) {
167 2624025 : return table->IsSmallOrderedHashSet();
168 : }
169 :
170 2624015 : inline bool SmallOrderedHashMap::Is(Handle<HeapObject> table) {
171 2624025 : return table->IsSmallOrderedHashMap();
172 : }
173 :
174 : template <class Derived>
175 51575 : void SmallOrderedHashTable<Derived>::SetDataEntry(int entry, int relative_index,
176 : Object value) {
177 : DCHECK_NE(kNotFound, entry);
178 : int entry_offset = GetDataEntryOffset(entry, relative_index);
179 51575 : RELAXED_WRITE_FIELD(*this, entry_offset, value);
180 51575 : WRITE_BARRIER(*this, entry_offset, value);
181 51575 : }
182 :
183 : template <class Derived, class TableType>
184 0 : Object OrderedHashTableIterator<Derived, TableType>::CurrentKey() {
185 0 : TableType table = TableType::cast(this->table());
186 : int index = Smi::ToInt(this->index());
187 0 : Object key = table->KeyAt(index);
188 : DCHECK(!key->IsTheHole());
189 0 : return key;
190 : }
191 :
192 : inline void SmallOrderedNameDictionary::SetHash(int hash) {
193 : DCHECK(PropertyArray::HashField::is_valid(hash));
194 370 : WRITE_INT_FIELD(*this, PrefixOffset(), hash);
195 : }
196 :
197 : inline int SmallOrderedNameDictionary::Hash() {
198 1440 : int hash = READ_INT_FIELD(*this, PrefixOffset());
199 : DCHECK(PropertyArray::HashField::is_valid(hash));
200 : return hash;
201 : }
202 :
203 : inline void OrderedNameDictionary::SetHash(int hash) {
204 : DCHECK(PropertyArray::HashField::is_valid(hash));
205 : this->set(PrefixIndex(), Smi::FromInt(hash));
206 : }
207 :
208 : inline int OrderedNameDictionary::Hash() {
209 : Object hash_obj = this->get(PrefixIndex());
210 : int hash = Smi::ToInt(hash_obj);
211 : DCHECK(PropertyArray::HashField::is_valid(hash));
212 : return hash;
213 : }
214 :
215 : } // namespace internal
216 : } // namespace v8
217 :
218 : #include "src/objects/object-macros-undef.h"
219 :
220 : #endif // V8_OBJECTS_ORDERED_HASH_TABLE_INL_H_
|