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_LAYOUT_INFO_H 17 #define ECMASCRIPT_LAYOUT_INFO_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_symbol.h" 21 #include "ecmascript/property_attributes.h" 22 #include "ecmascript/tagged_array.h" 23 24 namespace panda::ecmascript { 25 struct Properties { 26 JSTaggedValue key_; 27 JSTaggedValue attr_; 28 }; 29 30 class LayoutInfo : private TaggedArray { 31 public: 32 static constexpr int MIN_PROPERTIES_LENGTH = JSObject::MIN_PROPERTIES_LENGTH; 33 static constexpr int MAX_PROPERTIES_LENGTH = PropertyAttributes::MAX_FAST_PROPS_CAPACITY; 34 static constexpr uint32_t ELEMENTS_INDEX_LOG2 = 1; 35 static constexpr uint32_t ATTR_INDEX_OFFSET = 1; 36 Cast(TaggedObject * obj)37 inline static LayoutInfo *Cast(TaggedObject *obj) 38 { 39 ASSERT(JSTaggedValue(obj).IsTaggedArray()); 40 return reinterpret_cast<LayoutInfo *>(obj); 41 } 42 UncheckCast(TaggedObject * obj)43 inline static LayoutInfo *UncheckCast(TaggedObject *obj) 44 { 45 return reinterpret_cast<LayoutInfo *>(obj); 46 } 47 GetLayoutInfoFromHClass(const JSThread * thread,const JSHClass * hclass)48 static LayoutInfo* GetLayoutInfoFromHClass(const JSThread *thread, const JSHClass* hclass) 49 { 50 return LayoutInfo::Cast(hclass->GetLayout(thread).GetTaggedObject()); 51 } 52 53 void Initialize(const JSThread *thread, int num = 0); 54 int GetPropertiesCapacity() const; 55 int NumberOfElements() const; 56 void SetNumberOfElements(const JSThread *thread, int properties); 57 uint32_t GetKeyIndex(int index) const; 58 uint32_t GetAttrIndex(int index) const; 59 void SetWithoutBarrier(uint32_t idx, const JSTaggedValue &value); 60 void SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr); 61 void SetKey(const JSThread *thread, int index, const JSTaggedValue &key); 62 void SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr); 63 JSTaggedValue GetKey(const JSThread *thread, int index) const; 64 PropertyAttributes GetAttr(const JSThread *thread, int index) const; 65 template<RBMode mode = RBMode::DEFAULT_RB> 66 PropertyAttributes GetAttr(const JSThread *thread, int index) const; 67 JSTaggedValue GetSortedKey(const JSThread *thread, int index) const; 68 uint32_t GetSortedIndex(const JSThread *thread, int index) const; 69 void SetSortedIndex(const JSThread *thread, int index, int sortedIndex); 70 bool CheckIsDuplicateKey(const JSThread *thread, int curKeyIdx, uint32_t curKeyHashCode, const JSTaggedValue &key); 71 template<bool checkDuplicateKeys = false> 72 void AddKey(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr); 73 void SetIsNotHole(const JSThread *thread, int index); 74 void UpdateTrackTypeAttr(const JSThread *thread, int index, const PropertyAttributes &attr); 75 void SetIsPGODumped(const JSThread *thread, int index); 76 GetLength()77 inline uint32_t GetLength() const 78 { 79 return TaggedArray::GetLength(); 80 } 81 GetProperties()82 inline Properties *GetProperties() const 83 { 84 return reinterpret_cast<Properties *>(reinterpret_cast<uintptr_t>(this) + TaggedArray::DATA_OFFSET); 85 } 86 ComputeArrayLength(uint32_t properties_number)87 static inline uint32_t ComputeArrayLength(uint32_t properties_number) 88 { 89 return (properties_number << ELEMENTS_INDEX_LOG2); 90 } 91 ComputeGrowCapacity(uint32_t old_capacity)92 static inline uint32_t ComputeGrowCapacity(uint32_t old_capacity) 93 { 94 uint32_t new_capacity = old_capacity + MIN_PROPERTIES_LENGTH; 95 return new_capacity > MAX_PROPERTIES_LENGTH ? MAX_PROPERTIES_LENGTH : new_capacity; 96 } 97 98 int FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key, int propertiesNumber); 99 int BinarySearch(const JSThread *thread, JSTaggedValue key, int propertiesNumber); 100 void GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray); 101 void GetAllKeysForSerialization(const JSThread *thread, int end, std::vector<JSTaggedValue> &keyVector); 102 void GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength, 103 TaggedArray *keyArray, uint32_t filter); 104 std::pair<uint32_t, uint32_t> GetNumOfEnumKeys(const JSThread *thread, int end) const; 105 void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys, 106 JSHandle<TaggedQueue> shadowQueue, int32_t lastLength); 107 void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys); 108 109 void DumpFieldIndexByPGO(const JSThread *thread, int index, pgo::HClassLayoutDesc* desc); 110 bool UpdateFieldIndexByPGO(const JSThread *thread, int index, pgo::HClassLayoutDesc* desc); 111 CString GetSymbolKeyString(const JSThread *thread, JSTaggedValue key); 112 DECL_DUMP() 113 }; 114 } // namespace panda::ecmascript 115 116 #endif // ECMASCRIPT_LAYOUT_INFO_H 117