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