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_TAGGED_DICTIONARY_H 17 #define ECMASCRIPT_TAGGED_DICTIONARY_H 18 19 #include "ecmascript/tagged_hash_table.h" 20 21 namespace panda::ecmascript { 22 class NameDictionary : public OrderTaggedHashTable<NameDictionary> { 23 public: 24 using OrderHashTableT = OrderTaggedHashTable<NameDictionary>; GetKeyIndex(int entry)25 inline static int GetKeyIndex(int entry) 26 { 27 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX; 28 } GetValueIndex(int entry)29 inline static int GetValueIndex(int entry) 30 { 31 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX; 32 } GetEntryIndex(int entry)33 inline static int GetEntryIndex(int entry) 34 { 35 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize(); 36 } GetEntrySize()37 inline static int GetEntrySize() 38 { 39 return ENTRY_SIZE; 40 } 41 static int Hash(const JSTaggedValue &key); 42 static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other); 43 static JSHandle<NameDictionary> Create(const JSThread *thread, 44 int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER); 45 // Returns the property metaData for the property at entry. 46 PropertyAttributes GetAttributes(int entry) const; 47 void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData); 48 void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value, 49 const PropertyAttributes &metaData); 50 void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value, 51 const PropertyAttributes &metaData); 52 void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value); 53 void UpdateAttributes(int entry, const PropertyAttributes &metaData); 54 void ClearEntry(const JSThread *thread, int entry); 55 void GetAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray) const; 56 void GetAllEnumKeys(const JSThread *thread, int offset, TaggedArray *keyArray, uint32_t *keys) const; CompKey(const std::pair<JSTaggedValue,PropertyAttributes> & a,const std::pair<JSTaggedValue,PropertyAttributes> & b)57 static inline bool CompKey(const std::pair<JSTaggedValue, PropertyAttributes> &a, 58 const std::pair<JSTaggedValue, PropertyAttributes> &b) 59 { 60 return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder(); 61 } 62 DECL_DUMP() 63 64 static constexpr int ENTRY_KEY_INDEX = 0; 65 static constexpr int ENTRY_VALUE_INDEX = 1; 66 static constexpr int ENTRY_DETAILS_INDEX = 2; 67 static constexpr int ENTRY_SIZE = 3; 68 }; 69 70 class NumberDictionary : public OrderTaggedHashTable<NumberDictionary> { 71 public: 72 using OrderHashTableT = OrderTaggedHashTable<NumberDictionary>; GetKeyIndex(int entry)73 inline static int GetKeyIndex(int entry) 74 { 75 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX; 76 } GetValueIndex(int entry)77 inline static int GetValueIndex(int entry) 78 { 79 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX; 80 } GetEntryIndex(int entry)81 inline static int GetEntryIndex(int entry) 82 { 83 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize(); 84 } GetEntrySize()85 inline static int GetEntrySize() 86 { 87 return ENTRY_SIZE; 88 } 89 static int Hash(const JSTaggedValue &key); 90 static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other); 91 static JSHandle<NumberDictionary> Create(const JSThread *thread, 92 int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER); 93 // Returns the property metaData for the property at entry. 94 PropertyAttributes GetAttributes(int entry) const; 95 void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData); 96 void SetEntry([[maybe_unused]] const JSThread *thread, int entry, const JSTaggedValue &key, 97 const JSTaggedValue &value, const PropertyAttributes &metaData); 98 void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value, 99 const PropertyAttributes &metaData); 100 void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value); 101 void UpdateAttributes(int entry, const PropertyAttributes &metaData); 102 void ClearEntry(const JSThread *thread, int entry); 103 104 static void GetAllKeys(const JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset, 105 const JSHandle<TaggedArray> &keyArray); 106 static void GetAllEnumKeys(const JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset, 107 const JSHandle<TaggedArray> &keyArray, uint32_t *keys); CompKey(const JSTaggedValue & a,const JSTaggedValue & b)108 static inline bool CompKey(const JSTaggedValue &a, const JSTaggedValue &b) 109 { 110 ASSERT(a.IsNumber() && b.IsNumber()); 111 return a.GetNumber() < b.GetNumber(); 112 } 113 DECL_DUMP() 114 115 static constexpr int ENTRY_KEY_INDEX = 0; 116 static constexpr int ENTRY_VALUE_INDEX = 1; 117 static constexpr int ENTRY_DETAILS_INDEX = 2; 118 static constexpr int ENTRY_SIZE = 3; 119 }; 120 } // namespace panda::ecmascript 121 #endif // ECMASCRIPT_NEW_DICTIONARY_H 122