/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/linked_hash_table.h" #include "ecmascript/js_object-inl.h" namespace panda::ecmascript { template JSHandle LinkedHashTable::Create(const JSThread *thread, int numberOfElements, MemSpaceKind spaceKind) { ASSERT_PRINT(numberOfElements > 0, "size must be a non-negative integer"); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto capacity = static_cast(numberOfElements); ASSERT_PRINT(helpers::math::IsPowerOfTwo(capacity), "capacity must be pow of '2'"); int length = ELEMENTS_START_INDEX + numberOfElements + numberOfElements * (HashObject::ENTRY_SIZE + 1); JSHandle table; if (spaceKind == MemSpaceKind::SHARED) { table = JSHandle(factory->NewSOldSpaceTaggedArray(length)); } else { table = JSHandle(factory->NewTaggedArray(length)); } table->SetNumberOfElements(thread, 0); table->SetNumberOfDeletedElements(thread, 0); table->SetCapacity(thread, capacity); return table; } template JSHandle LinkedHashTable::Insert(const JSThread *thread, const JSHandle &table, const JSHandle &key, const JSHandle &value) { ASSERT(IsKey(key.GetTaggedValue())); int hash = LinkedHash::Hash(thread, key.GetTaggedValue()); int entry = table->FindElement(thread, key.GetTaggedValue()); if (entry != -1) { table->SetValue(thread, entry, value.GetTaggedValue()); return table; } JSHandle newTable = GrowCapacity(thread, table); uint32_t bucket = newTable->HashToBucket(hash); entry = newTable->NumberOfElements() + newTable->NumberOfDeletedElements(); newTable->InsertNewEntry(thread, bucket, entry); newTable->SetKey(thread, entry, key.GetTaggedValue()); newTable->SetValue(thread, entry, value.GetTaggedValue()); newTable->SetNumberOfElements(thread, newTable->NumberOfElements() + 1); return newTable; } template JSHandle LinkedHashTable::InsertWeakRef(const JSThread *thread, const JSHandle &table, const JSHandle &key, const JSHandle &value) { ALLOW_LOCAL_TO_SHARE_WEAK_REF_HANDLE; ASSERT(IsKey(key.GetTaggedValue())); int hash = LinkedHash::Hash(thread, key.GetTaggedValue()); int entry = table->FindElement(thread, key.GetTaggedValue()); if (entry != -1) { table->SetValue(thread, entry, value.GetTaggedValue()); return table; } JSHandle newTable = GrowCapacity(thread, table); uint32_t bucket = newTable->HashToBucket(hash); entry = newTable->NumberOfElements() + newTable->NumberOfDeletedElements(); newTable->InsertNewEntry(thread, bucket, entry); JSTaggedValue weakKey(key->CreateAndGetWeakRef()); newTable->SetKey(thread, entry, weakKey); // The ENTRY_VALUE_INDEX of LinkedHashSet is 0. SetValue will cause the overwitten key. if (std::is_same_v) { newTable->SetValue(thread, entry, value.GetTaggedValue()); } newTable->SetNumberOfElements(thread, newTable->NumberOfElements() + 1); return newTable; } template JSHandle LinkedHashTable::GrowCapacity(const JSThread *thread, const JSHandle &table, int numberOfAddedElements) { if (table->HasSufficientCapacity(numberOfAddedElements)) { return table; } int newCapacity = ComputeCapacity(table->NumberOfElements() + numberOfAddedElements); JSHandle newTable = Create(thread, newCapacity, table.GetTaggedValue().IsInSharedHeap() ? MemSpaceKind::SHARED : MemSpaceKind::LOCAL); table->Rehash(thread, *newTable); return newTable; } template JSHandle LinkedHashTable::Remove(const JSThread *thread, const JSHandle &table, const JSHandle &key) { int entry = table->FindElement(thread, key.GetTaggedValue()); if (entry == -1) { return table; } table->RemoveEntry(thread, entry); return Shrink(thread, table); } template JSHandle LinkedHashTable::Shrink(const JSThread *thread, const JSHandle &table, int additionalCapacity) { int newCapacity = ComputeCapacityWithShrink(table->Capacity(), table->NumberOfElements() + additionalCapacity); if (newCapacity == table->Capacity()) { return table; } JSHandle newTable = Create(thread, newCapacity, table.GetTaggedValue().IsInSharedHeap() ? MemSpaceKind::SHARED : MemSpaceKind::LOCAL); table->Rehash(thread, *newTable); return newTable; } // LinkedHashMap JSHandle LinkedHashMap::Create(const JSThread *thread, int numberOfElements, MemSpaceKind spaceKind) { return LinkedHashTable::Create(thread, numberOfElements, spaceKind); } JSHandle LinkedHashMap::Delete(const JSThread *thread, const JSHandle &obj, const JSHandle &key) { return LinkedHashTable::Remove(thread, obj, key); } JSHandle LinkedHashMap::Set(const JSThread *thread, const JSHandle &obj, const JSHandle &key, const JSHandle &value) { return LinkedHashTable::Insert(thread, obj, key, value); } JSHandle LinkedHashMap::SetWeakRef(const JSThread *thread, const JSHandle &obj, const JSHandle &key, const JSHandle &value) { return LinkedHashTable::InsertWeakRef(thread, obj, key, value); } JSTaggedValue LinkedHashMap::Get(const JSThread *thread, JSTaggedValue key) const { int entry = FindElement(thread, key); if (entry == -1) { return JSTaggedValue::Undefined(); } return GetValue(entry); } bool LinkedHashMap::Has(const JSThread *thread, JSTaggedValue key) const { int entry = FindElement(thread, key); return entry != -1; } JSHandle LinkedHashMap::Clear(const JSThread *thread, const JSHandle &table) { if (table->Capacity() == LinkedHashMap::MIN_CAPACITY) { table->FillRangeWithSpecialValue(JSTaggedValue::Hole(), LinkedHashMap::ELEMENTS_START_INDEX, table->GetLength()); table->SetNumberOfDeletedElements(thread, table->NumberOfDeletedElements() + table->NumberOfElements()); table->SetNumberOfElements(thread, 0); return table; } JSHandle newMap = LinkedHashMap::Create(thread, LinkedHashMap::MIN_CAPACITY, table.GetTaggedValue().IsInSharedHeap() ? MemSpaceKind::SHARED : MemSpaceKind::LOCAL); if (table->Capacity() > 0) { table->SetNextTable(thread, newMap.GetTaggedValue()); table->SetNumberOfDeletedElements(thread, -1); } return newMap; } JSHandle LinkedHashMap::Shrink(const JSThread *thread, const JSHandle &table, int additionalCapacity) { return LinkedHashTable::Shrink(thread, table, additionalCapacity); } // LinkedHashSet JSHandle LinkedHashSet::Create(const JSThread *thread, int numberOfElements, MemSpaceKind spaceKind) { return LinkedHashTable::Create(thread, numberOfElements, spaceKind); } JSHandle LinkedHashSet::Delete(const JSThread *thread, const JSHandle &obj, const JSHandle &key) { return LinkedHashTable::Remove(thread, obj, key); } JSHandle LinkedHashSet::Add(const JSThread *thread, const JSHandle &obj, const JSHandle &key) { return LinkedHashTable::Insert(thread, obj, key, key); } JSHandle LinkedHashSet::AddWeakRef(const JSThread *thread, const JSHandle &obj, const JSHandle &key) { return LinkedHashTable::InsertWeakRef(thread, obj, key, key); } bool LinkedHashSet::Has(const JSThread *thread, JSTaggedValue key) const { int entry = FindElement(thread, key); return entry != -1; } JSHandle LinkedHashSet::Clear(const JSThread *thread, const JSHandle &table) { if (table->Capacity() == LinkedHashSet::MIN_CAPACITY) { table->FillRangeWithSpecialValue(JSTaggedValue::Hole(), LinkedHashSet::ELEMENTS_START_INDEX, table->GetLength()); table->SetNumberOfDeletedElements(thread, table->NumberOfDeletedElements() + table->NumberOfElements()); table->SetNumberOfElements(thread, 0); return table; } JSHandle newSet = LinkedHashSet::Create(thread, LinkedHashSet::MIN_CAPACITY, table.GetTaggedValue().IsInSharedHeap() ? MemSpaceKind::SHARED : MemSpaceKind::LOCAL); if (table->Capacity() > 0) { table->SetNextTable(thread, newSet.GetTaggedValue()); table->SetNumberOfDeletedElements(thread, -1); } return newSet; } JSHandle LinkedHashSet::Shrink(const JSThread *thread, const JSHandle &table, int additionalCapacity) { return LinkedHashTable::Shrink(thread, table, additionalCapacity); } int LinkedHash::Hash(const JSThread *thread, JSTaggedValue key) { if (key.IsInt()) { return key.GetInt(); } if (key.IsSymbol()) { auto symbolString = JSSymbol::Cast(key.GetTaggedObject()); return symbolString->GetHashField(); } if (key.IsString()) { auto keyString = reinterpret_cast(key.GetTaggedObject()); return EcmaStringAccessor(keyString).GetHashcode(); } if (key.IsECMAObject()) { int32_t hash = ECMAObject::Cast(key.GetTaggedObject())->GetHash(); if (hash == 0) { hash = base::RandomGenerator::GenerateIdentityHash(); JSHandle ecmaObj(thread, key); ECMAObject::SetHash(thread, hash, ecmaObj); } return hash; } if (key.IsBigInt()) { uint32_t keyValue = BigInt::Cast(key.GetTaggedObject())->GetDigit(0); return GetHash32(reinterpret_cast(&keyValue), sizeof(keyValue) / sizeof(uint8_t)); } // Int, Double, Special and HeapObject(except symbol and string) if (key.IsDouble()) { key = JSTaggedValue::TryCastDoubleToInt32(key.GetDouble()); if (key.IsInt()) { return key.GetInt(); } } uint64_t keyValue = key.GetRawData(); return GetHash32(reinterpret_cast(&keyValue), sizeof(keyValue) / sizeof(uint8_t)); } } // namespace panda::ecmascript