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 #ifndef ECMASCRIPT_TEMPLATE_MAP_H 17 #define ECMASCRIPT_TEMPLATE_MAP_H 18 19 #include "ecmascript/js_array.h" 20 #include "ecmascript/tagged_hash_table.h" 21 22 namespace panda::ecmascript { 23 class TemplateMap : public TaggedHashTable<TemplateMap> { 24 public: 25 using HashTable = TaggedHashTable<TemplateMap>; IsMatch(const JSThread * thread,const JSTaggedValue & key,const JSTaggedValue & other)26 static inline bool IsMatch([[maybe_unused]] const JSThread *thread, const JSTaggedValue &key, 27 const JSTaggedValue &other) 28 { 29 return key == other; 30 } Hash(const JSThread * thread,const JSTaggedValue & obj)31 static inline int Hash([[maybe_unused]] const JSThread *thread, const JSTaggedValue &obj) 32 { 33 ASSERT(obj.IsJSArray()); 34 JSArray *array = JSArray::Cast(obj.GetTaggedObject()); 35 uint32_t len = array->GetArrayLength(); 36 return len; 37 } GetKeyIndex(int entry)38 inline static int GetKeyIndex(int entry) 39 { 40 return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX; 41 } GetValueIndex(int entry)42 inline static int GetValueIndex(int entry) 43 { 44 return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX; 45 } GetEntryIndex(int entry)46 inline static int GetEntryIndex(int entry) 47 { 48 return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize(); 49 } GetEntrySize()50 inline static int GetEntrySize() 51 { 52 return ENTRY_SIZE; 53 } Cast(TaggedObject * object)54 static TemplateMap *Cast(TaggedObject *object) 55 { 56 return reinterpret_cast<TemplateMap *>(object); 57 } 58 static const int DEFAULT_ELEMENTS_NUMBER = 16; 59 static JSHandle<TemplateMap> Create(JSThread *thread, int numberOfElements = DEFAULT_ELEMENTS_NUMBER) 60 { 61 return HashTable::Create(thread, numberOfElements); 62 } ComputeCompactSize(const JSThread * thread,const JSHandle<TemplateMap> & table,int computeHashTableSize,int tableSize,int addedElements)63 static int ComputeCompactSize([[maybe_unused]] const JSThread *thread, 64 [[maybe_unused]] const JSHandle<TemplateMap> &table, int computeHashTableSize, 65 [[maybe_unused]] int tableSize, [[maybe_unused]] int addedElements) 66 { 67 return computeHashTableSize; 68 } 69 static const int ENTRY_SIZE = 2; 70 static const int ENTRY_KEY_INDEX = 0; 71 static const int ENTRY_VALUE_INDEX = 1; 72 }; 73 } // namespace panda::ecmascript 74 #endif // ECMASCRIPT_TEMPLATE_MAP_H 75