1 /* 2 * Copyright (c) 2022 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 #ifndef ECMASCRIPT_TAGGED_HASH_ARRAY_H 17 #define ECMASCRIPT_TAGGED_HASH_ARRAY_H 18 19 #include "ecmascript/js_api/js_api_hashmap.h" 20 #include "ecmascript/js_object-inl.h" 21 #include "ecmascript/js_object.h" 22 #include "ecmascript/js_tagged_value-inl.h" 23 #include "ecmascript/tagged_node.h" 24 25 namespace panda::ecmascript { 26 class TaggedHashArray : public TaggedArray { 27 public: 28 static constexpr uint32_t MAXIMUM_CAPACITY = 1 << 30; 29 static constexpr uint32_t UNTREEIFY_THRESHOLD = 6; 30 static constexpr uint32_t DEFAULT_INITIAL_CAPACITY = 1 << 4; 31 static constexpr float DEFAULT_LOAD_FACTOR = 0.75f; 32 static constexpr uint32_t TREEIFY_THRESHOLD = 8; 33 Cast(TaggedObject * object)34 static TaggedHashArray *Cast(TaggedObject *object) 35 { 36 ASSERT(JSTaggedValue(object).IsTaggedArray()); 37 return static_cast<TaggedHashArray *>(object); 38 } 39 static JSTaggedValue Create(const JSThread *thread, uint32_t numberOfElements = DEFAULT_INITIAL_CAPACITY); 40 static JSTaggedValue SetVal(JSThread *thread, JSHandle<TaggedHashArray> table, int hash, 41 JSHandle<JSTaggedValue> key, JSHandle<JSTaggedValue> value); 42 static JSHandle<TaggedHashArray> Resize(JSThread *thread, const JSHandle<TaggedHashArray> &oldTab, 43 uint32_t Capacity); 44 static JSHandle<LinkedNode> NewLinkedNode(JSThread *thread, int hash, JSHandle<JSTaggedValue> key, 45 JSHandle<JSTaggedValue> value); 46 static JSHandle<LinkedNode> CreateLinkedNodeFrom(JSThread *thread, JSHandle<RBTreeNode> treeNode); 47 static JSHandle<RBTreeNode> NewTreeNode(JSThread *thread, int hash, JSHandle<JSTaggedValue> key, 48 JSHandle<JSTaggedValue> value); 49 static JSHandle<RBTreeNode> CreateTreeNodeFrom(JSThread *thread, JSHandle<LinkedNode> linkedNode); 50 static JSHandle<JSTaggedValue> GetCurrentNode(JSThread *thread, JSMutableHandle<TaggedQueue> &queue, 51 const JSHandle<TaggedHashArray> &tableArr, uint32_t &index); 52 JSTaggedValue GetNode(JSThread *thread, int hash, JSTaggedValue key); 53 JSTaggedValue RemoveNode(JSThread *thread, int hash, JSTaggedValue key); 54 void Clear(JSThread *thread); IsKey(JSTaggedValue key)55 inline static bool IsKey(JSTaggedValue key) 56 { 57 return !key.IsHole(); 58 } 59 60 DECL_DUMP() 61 private: 62 static void TreeingBin(JSThread *thread, const JSHandle<TaggedHashArray> &tab, int hash); 63 static void NodeDisperse(JSThread *thread, const JSHandle<TaggedHashArray> &newTab, 64 JSTaggedValue nodeVa, int index, int oldCapacity); 65 }; 66 } // namespace panda::ecmascript 67 #endif // ECMASCRIPT_TAGGED_HASH_ARRAY_H 68