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