• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/linked_hash_table.h"
17 
18 #include "ecmascript/js_object-inl.h"
19 #include "ecmascript/object_factory.h"
20 
21 namespace panda::ecmascript {
22 template<typename Derived, typename HashObject>
Create(const JSThread * thread,int numberOfElements)23 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::Create(const JSThread *thread, int numberOfElements)
24 {
25     ASSERT_PRINT(numberOfElements > 0, "size must be a non-negative integer");
26     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
27     auto capacity = static_cast<uint32_t>(numberOfElements);
28     ASSERT_PRINT(helpers::math::IsPowerOfTwo(capacity), "capacity must be pow of '2'");
29     int length = ELEMENTS_START_INDEX + numberOfElements + numberOfElements * (HashObject::ENTRY_SIZE + 1);
30 
31     auto table = JSHandle<Derived>(factory->NewTaggedArray(length));
32     table->SetNumberOfElements(thread, 0);
33     table->SetNumberOfDeletedElements(thread, 0);
34     table->SetCapacity(thread, capacity);
35     return table;
36 }
37 
38 template<typename Derived, typename HashObject>
Insert(const JSThread * thread,const JSHandle<Derived> & table,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)39 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::Insert(const JSThread *thread, const JSHandle<Derived> &table,
40                                                                const JSHandle<JSTaggedValue> &key,
41                                                                const JSHandle<JSTaggedValue> &value)
42 {
43     ASSERT(IsKey(key.GetTaggedValue()));
44     int hash = LinkedHash::Hash(thread, key.GetTaggedValue());
45     int entry = table->FindElement(thread, key.GetTaggedValue());
46     if (entry != -1) {
47         table->SetValue(thread, entry, value.GetTaggedValue());
48         return table;
49     }
50 
51     JSHandle<Derived> newTable = GrowCapacity(thread, table);
52 
53     uint32_t bucket = newTable->HashToBucket(hash);
54     entry = newTable->NumberOfElements() + newTable->NumberOfDeletedElements();
55     newTable->InsertNewEntry(thread, bucket, entry);
56     newTable->SetKey(thread, entry, key.GetTaggedValue());
57     newTable->SetValue(thread, entry, value.GetTaggedValue());
58     newTable->SetNumberOfElements(thread, newTable->NumberOfElements() + 1);
59 
60     return newTable;
61 }
62 
63 template<typename Derived, typename HashObject>
InsertWeakRef(const JSThread * thread,const JSHandle<Derived> & table,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)64 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::InsertWeakRef(const JSThread *thread,
65     const JSHandle<Derived> &table, const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value)
66 {
67     ASSERT(IsKey(key.GetTaggedValue()));
68     int hash = LinkedHash::Hash(thread, key.GetTaggedValue());
69     int entry = table->FindElement(thread, key.GetTaggedValue());
70     if (entry != -1) {
71         table->SetValue(thread, entry, value.GetTaggedValue());
72         return table;
73     }
74 
75     JSHandle<Derived> newTable = GrowCapacity(thread, table);
76 
77     uint32_t bucket = newTable->HashToBucket(hash);
78     entry = newTable->NumberOfElements() + newTable->NumberOfDeletedElements();
79     newTable->InsertNewEntry(thread, bucket, entry);
80     JSTaggedValue weakKey(key->CreateAndGetWeakRef());
81     newTable->SetKey(thread, entry, weakKey);
82     // The ENTRY_VALUE_INDEX of LinkedHashSet is 0. SetValue will cause the overwitten key.
83     if (std::is_same_v<LinkedHashMap, Derived>) {
84         newTable->SetValue(thread, entry, value.GetTaggedValue());
85     }
86     newTable->SetNumberOfElements(thread, newTable->NumberOfElements() + 1);
87 
88     return newTable;
89 }
90 
91 template<typename Derived, typename HashObject>
GrowCapacity(const JSThread * thread,const JSHandle<Derived> & table,int numberOfAddedElements)92 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::GrowCapacity(const JSThread *thread,
93     const JSHandle<Derived> &table, int numberOfAddedElements)
94 {
95     if (table->HasSufficientCapacity(numberOfAddedElements)) {
96         return table;
97     }
98     int newCapacity = ComputeCapacity(table->NumberOfElements() + numberOfAddedElements);
99     JSHandle<Derived> newTable = Create(thread, newCapacity);
100     table->Rehash(thread, *newTable);
101     return newTable;
102 }
103 
104 template<typename Derived, typename HashObject>
Remove(const JSThread * thread,const JSHandle<Derived> & table,const JSHandle<JSTaggedValue> & key)105 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::Remove(const JSThread *thread, const JSHandle<Derived> &table,
106                                                                const JSHandle<JSTaggedValue> &key)
107 {
108     int entry = table->FindElement(thread, key.GetTaggedValue());
109     if (entry == -1) {
110         return table;
111     }
112 
113     table->RemoveEntry(thread, entry);
114     return Shrink(thread, table);
115 }
116 
117 template<typename Derived, typename HashObject>
Shrink(const JSThread * thread,const JSHandle<Derived> & table,int additionalCapacity)118 JSHandle<Derived> LinkedHashTable<Derived, HashObject>::Shrink(const JSThread *thread, const JSHandle<Derived> &table,
119     int additionalCapacity)
120 {
121     int newCapacity = ComputeCapacityWithShrink(table->Capacity(), table->NumberOfElements() + additionalCapacity);
122     if (newCapacity == table->Capacity()) {
123         return table;
124     }
125 
126     JSHandle<Derived> newTable = Create(thread, newCapacity);
127 
128     table->Rehash(thread, *newTable);
129     return newTable;
130 }
131 
132 // LinkedHashMap
Create(const JSThread * thread,int numberOfElements)133 JSHandle<LinkedHashMap> LinkedHashMap::Create(const JSThread *thread, int numberOfElements)
134 {
135     return LinkedHashTable<LinkedHashMap, LinkedHashMapObject>::Create(thread, numberOfElements);
136 }
137 
Delete(const JSThread * thread,const JSHandle<LinkedHashMap> & obj,const JSHandle<JSTaggedValue> & key)138 JSHandle<LinkedHashMap> LinkedHashMap::Delete(const JSThread *thread, const JSHandle<LinkedHashMap> &obj,
139                                               const JSHandle<JSTaggedValue> &key)
140 {
141     return LinkedHashTable<LinkedHashMap, LinkedHashMapObject>::Remove(thread, obj, key);
142 }
143 
Set(const JSThread * thread,const JSHandle<LinkedHashMap> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)144 JSHandle<LinkedHashMap> LinkedHashMap::Set(const JSThread *thread, const JSHandle<LinkedHashMap> &obj,
145     const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value)
146 {
147     return LinkedHashTable<LinkedHashMap, LinkedHashMapObject>::Insert(thread, obj, key, value);
148 }
149 
SetWeakRef(const JSThread * thread,const JSHandle<LinkedHashMap> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)150 JSHandle<LinkedHashMap> LinkedHashMap::SetWeakRef(const JSThread *thread, const JSHandle<LinkedHashMap> &obj,
151     const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value)
152 {
153     return LinkedHashTable<LinkedHashMap, LinkedHashMapObject>::InsertWeakRef(thread, obj, key, value);
154 }
155 
Get(const JSThread * thread,JSTaggedValue key) const156 JSTaggedValue LinkedHashMap::Get(const JSThread *thread, JSTaggedValue key) const
157 {
158     int entry = FindElement(thread, key);
159     if (entry == -1) {
160         return JSTaggedValue::Undefined();
161     }
162     return GetValue(entry);
163 }
164 
Has(const JSThread * thread,JSTaggedValue key) const165 bool LinkedHashMap::Has(const JSThread *thread, JSTaggedValue key) const
166 {
167     int entry = FindElement(thread, key);
168     return entry != -1;
169 }
170 
Clear(const JSThread * thread,const JSHandle<LinkedHashMap> & table)171 JSHandle<LinkedHashMap> LinkedHashMap::Clear(const JSThread *thread, const JSHandle<LinkedHashMap> &table)
172 {
173     JSHandle<LinkedHashMap> newMap = LinkedHashMap::Create(thread);
174     if (table->Capacity() > 0) {
175         table->SetNextTable(thread, newMap.GetTaggedValue());
176         table->SetNumberOfDeletedElements(thread, -1);
177     }
178     return newMap;
179 }
180 
Shrink(const JSThread * thread,const JSHandle<LinkedHashMap> & table,int additionalCapacity)181 JSHandle<LinkedHashMap> LinkedHashMap::Shrink(const JSThread *thread, const JSHandle<LinkedHashMap> &table,
182                                               int additionalCapacity)
183 {
184     return LinkedHashTable<LinkedHashMap, LinkedHashMapObject>::Shrink(thread, table, additionalCapacity);
185 }
186 
187 // LinkedHashSet
Create(const JSThread * thread,int numberOfElements)188 JSHandle<LinkedHashSet> LinkedHashSet::Create(const JSThread *thread, int numberOfElements)
189 {
190     return LinkedHashTable<LinkedHashSet, LinkedHashSetObject>::Create(thread, numberOfElements);
191 }
192 
Delete(const JSThread * thread,const JSHandle<LinkedHashSet> & obj,const JSHandle<JSTaggedValue> & key)193 JSHandle<LinkedHashSet> LinkedHashSet::Delete(const JSThread *thread, const JSHandle<LinkedHashSet> &obj,
194                                               const JSHandle<JSTaggedValue> &key)
195 {
196     return LinkedHashTable<LinkedHashSet, LinkedHashSetObject>::Remove(thread, obj, key);
197 }
198 
Add(const JSThread * thread,const JSHandle<LinkedHashSet> & obj,const JSHandle<JSTaggedValue> & key)199 JSHandle<LinkedHashSet> LinkedHashSet::Add(const JSThread *thread, const JSHandle<LinkedHashSet> &obj,
200                                            const JSHandle<JSTaggedValue> &key)
201 {
202     return LinkedHashTable<LinkedHashSet, LinkedHashSetObject>::Insert(thread, obj, key, key);
203 }
204 
AddWeakRef(const JSThread * thread,const JSHandle<LinkedHashSet> & obj,const JSHandle<JSTaggedValue> & key)205 JSHandle<LinkedHashSet> LinkedHashSet::AddWeakRef(const JSThread *thread, const JSHandle<LinkedHashSet> &obj,
206     const JSHandle<JSTaggedValue> &key)
207 {
208     return LinkedHashTable<LinkedHashSet, LinkedHashSetObject>::InsertWeakRef(thread, obj, key, key);
209 }
210 
Has(const JSThread * thread,JSTaggedValue key) const211 bool LinkedHashSet::Has(const JSThread *thread, JSTaggedValue key) const
212 {
213     int entry = FindElement(thread, key);
214     return entry != -1;
215 }
216 
Clear(const JSThread * thread,const JSHandle<LinkedHashSet> & table)217 JSHandle<LinkedHashSet> LinkedHashSet::Clear(const JSThread *thread, const JSHandle<LinkedHashSet> &table)
218 {
219     JSHandle<LinkedHashSet> newSet = LinkedHashSet::Create(thread);
220     if (table->Capacity() > 0) {
221         table->SetNextTable(thread, newSet.GetTaggedValue());
222         table->SetNumberOfDeletedElements(thread, -1);
223     }
224     return newSet;
225 }
226 
Shrink(const JSThread * thread,const JSHandle<LinkedHashSet> & table,int additionalCapacity)227 JSHandle<LinkedHashSet> LinkedHashSet::Shrink(const JSThread *thread, const JSHandle<LinkedHashSet> &table,
228     int additionalCapacity)
229 {
230     return LinkedHashTable<LinkedHashSet, LinkedHashSetObject>::Shrink(thread, table, additionalCapacity);
231 }
232 
Hash(const JSThread * thread,JSTaggedValue key)233 int LinkedHash::Hash(const JSThread *thread, JSTaggedValue key)
234 {
235     if (key.IsSymbol()) {
236         auto symbolString = JSSymbol::Cast(key.GetTaggedObject());
237         return symbolString->GetHashField();
238     }
239     if (key.IsString()) {
240         auto keyString = reinterpret_cast<EcmaString *>(key.GetTaggedObject());
241         return EcmaStringAccessor(keyString).GetHashcode();
242     }
243     if (key.IsECMAObject()) {
244         int32_t hash = ECMAObject::Cast(key.GetTaggedObject())->GetHash();
245         if (hash == 0) {
246             hash = base::RandomGenerator::GenerateIdentityHash();
247             JSHandle<ECMAObject> ecmaObj(thread, key);
248             ECMAObject::SetHash(thread, hash, ecmaObj);
249         }
250         return hash;
251     }
252     if (key.IsBigInt()) {
253         uint32_t keyValue = BigInt::Cast(key.GetTaggedObject())->GetDigit(0);
254         return GetHash32(reinterpret_cast<uint8_t *>(&keyValue), sizeof(keyValue) / sizeof(uint8_t));
255     }
256     // Int, Double, Special and HeapObject(except symbol and string)
257     if (key.IsDouble()) {
258         key = JSTaggedValue::TryCastDoubleToInt32(key.GetDouble());
259     }
260     uint64_t keyValue = key.GetRawData();
261     return GetHash32(reinterpret_cast<uint8_t *>(&keyValue), sizeof(keyValue) / sizeof(uint8_t));
262 }
263 }  // namespace panda::ecmascript
264