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 int Hash(const uint8_t* str, int strSize); 46 47 static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other); 48 static bool IsMatch(const uint8_t* str, int size, const JSTaggedValue &other); 49 50 static JSHandle<NameDictionary> Create(const JSThread *thread, 51 int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER); 52 // Returns the property metaData for the property at entry. 53 PropertyAttributes GetAttributes(int entry) const; 54 void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData); 55 void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value, 56 const PropertyAttributes &metaData); 57 void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value, 58 const PropertyAttributes &metaData); 59 void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value); 60 void UpdateAttributes(int entry, const PropertyAttributes &metaData); 61 void ClearEntry(const JSThread *thread, int entry); 62 void GetAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray) const; 63 void GetAllKeysByFilter(const JSThread *thread, uint32_t &keyArrayEffectivelength, 64 TaggedArray *keyArray, uint32_t filter) const; 65 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)66 static inline bool CompKey(const std::pair<JSTaggedValue, PropertyAttributes> &a, 67 const std::pair<JSTaggedValue, PropertyAttributes> &b) 68 { 69 return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder(); 70 } 71 DECL_DUMP() 72 73 static constexpr int ENTRY_KEY_INDEX = 0; 74 static constexpr int ENTRY_VALUE_INDEX = 1; 75 static constexpr int ENTRY_DETAILS_INDEX = 2; 76 static constexpr int ENTRY_SIZE = 3; 77 }; 78 79 class NumberDictionary : public OrderTaggedHashTable<NumberDictionary> { 80 public: 81 using OrderHashTableT = OrderTaggedHashTable<NumberDictionary>; GetKeyIndex(int entry)82 inline static int GetKeyIndex(int entry) 83 { 84 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX; 85 } GetValueIndex(int entry)86 inline static int GetValueIndex(int entry) 87 { 88 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX; 89 } GetEntryIndex(int entry)90 inline static int GetEntryIndex(int entry) 91 { 92 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize(); 93 } GetEntrySize()94 inline static int GetEntrySize() 95 { 96 return ENTRY_SIZE; 97 } 98 static int Hash(const JSTaggedValue &key); 99 static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other); 100 static JSHandle<NumberDictionary> Create(const JSThread *thread, 101 int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER); 102 // Returns the property metaData for the property at entry. 103 PropertyAttributes GetAttributes(int entry) const; 104 void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData); 105 void SetEntry([[maybe_unused]] const JSThread *thread, int entry, const JSTaggedValue &key, 106 const JSTaggedValue &value, const PropertyAttributes &metaData); 107 void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value, 108 const PropertyAttributes &metaData); 109 void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value); 110 void UpdateAttributes(int entry, const PropertyAttributes &metaData); 111 void ClearEntry(const JSThread *thread, int entry); 112 113 static void GetAllKeys(const JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset, 114 const JSHandle<TaggedArray> &keyArray); 115 static void GetAllKeysByFilter(const JSThread *thread, const JSHandle<NumberDictionary> &obj, 116 uint32_t &keyArrayEffectivelength, const JSHandle<TaggedArray> &keyArray, uint32_t filter); 117 static void GetAllEnumKeys(const JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset, 118 const JSHandle<TaggedArray> &keyArray, uint32_t *keys); CompKey(const JSTaggedValue & a,const JSTaggedValue & b)119 static inline bool CompKey(const JSTaggedValue &a, const JSTaggedValue &b) 120 { 121 ASSERT(a.IsNumber() && b.IsNumber()); 122 return a.GetNumber() < b.GetNumber(); 123 } 124 DECL_DUMP() 125 126 static constexpr int ENTRY_KEY_INDEX = 0; 127 static constexpr int ENTRY_VALUE_INDEX = 1; 128 static constexpr int ENTRY_DETAILS_INDEX = 2; 129 static constexpr int ENTRY_SIZE = 3; 130 }; 131 } // namespace panda::ecmascript 132 #endif // ECMASCRIPT_NEW_DICTIONARY_H 133