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_GLOBAL_DICTIONARY_H 17 #define ECMASCRIPT_GLOBAL_DICTIONARY_H 18 19 #include "ecmascript/ecma_string.h" 20 #include "ecmascript/ic/property_box.h" 21 #include "ecmascript/js_handle.h" 22 #include "ecmascript/js_tagged_value.h" 23 #include "ecmascript/property_attributes.h" 24 #include "ecmascript/ecma_vm.h" 25 #include "ecmascript/object_factory.h" 26 #include "ecmascript/tagged_hash_table.h" 27 28 namespace panda { 29 namespace ecmascript { 30 class GlobalDictionary : public OrderTaggedHashTable<GlobalDictionary> { 31 public: 32 using OrderHashTableT = OrderTaggedHashTable<GlobalDictionary>; GetKeyIndex(int entry)33 inline static int GetKeyIndex(int entry) 34 { 35 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX; 36 } GetValueIndex(int entry)37 inline static int GetValueIndex(int entry) 38 { 39 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX; 40 } GetEntryIndex(int entry)41 inline static int GetEntryIndex(int entry) 42 { 43 return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize(); 44 } GetEntrySize()45 inline static int GetEntrySize() 46 { 47 return ENTRY_SIZE; 48 } 49 static inline bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other); 50 51 static inline int Hash(const JSTaggedValue &key); 52 inline static void InvalidatePropertyBox(JSThread *thread, const JSHandle<GlobalDictionary> &dictHandle, int entry, 53 const PropertyAttributes &metaData); 54 55 inline static void InvalidateAndReplaceEntry(JSThread *thread, const JSHandle<GlobalDictionary> &dictHandle, 56 int entry, const JSHandle<JSTaggedValue> &oldValue); 57 58 inline PropertyBox *GetBox(int entry) const; 59 60 inline JSTaggedValue GetValue(int entry) const; 61 62 inline PropertyAttributes GetAttributes(int entry) const; 63 64 inline void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value, 65 const PropertyAttributes &detail); 66 67 inline void ClearEntry([[maybe_unused]] const JSThread *thread, int entry); 68 69 inline void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value, 70 const PropertyAttributes &metaData); 71 72 inline void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value); 73 74 inline void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData); 75 76 inline void GetAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray) const; 77 78 inline void GetEnumAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray, uint32_t *keys) const; 79 80 static bool inline CompKey(const std::pair<JSTaggedValue, uint32_t> &a, 81 const std::pair<JSTaggedValue, uint32_t> &b); 82 83 DECL_DUMP() 84 85 public: 86 static constexpr int ENTRY_KEY_INDEX = 0; 87 static constexpr int ENTRY_VALUE_INDEX = 1; 88 static constexpr int ENTRY_DETAILS_INDEX = 2; 89 static constexpr int ENTRY_SIZE = 3; 90 }; 91 } // namespace ecmascript 92 } // namespace panda 93 #endif 94