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