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_CAPACITY_OF_PROPERTIES; 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 42 void Initialize(const JSThread *thread, int num = 0); 43 int GetPropertiesCapacity() const; 44 int NumberOfElements() const; 45 void SetNumberOfElements(const JSThread *thread, int properties); 46 uint32_t GetKeyIndex(int index) const; 47 uint32_t GetAttrIndex(int index) const; 48 void SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr); 49 void SetKey(const JSThread *thread, int index, const JSTaggedValue &key); 50 void SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr); 51 JSTaggedValue GetKey(int index) const; 52 PropertyAttributes GetAttr(int index) const; 53 JSTaggedValue GetSortedKey(int index) const; 54 uint32_t GetSortedIndex(int index) const; 55 void SetSortedIndex(const JSThread *thread, int index, int sortedIndex); 56 void AddKey(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr); 57 void SetIsNotHole(const JSThread *thread, int index); 58 GetLength()59 inline uint32_t GetLength() const 60 { 61 return TaggedArray::GetLength(); 62 } 63 GetProperties()64 inline Properties *GetProperties() const 65 { 66 return reinterpret_cast<Properties *>(reinterpret_cast<uintptr_t>(this) + TaggedArray::DATA_OFFSET); 67 } 68 ComputeArrayLength(uint32_t properties_number)69 static inline uint32_t ComputeArrayLength(uint32_t properties_number) 70 { 71 return (properties_number << ELEMENTS_INDEX_LOG2); 72 } 73 ComputeGrowCapacity(uint32_t old_capacity)74 static inline uint32_t ComputeGrowCapacity(uint32_t old_capacity) 75 { 76 uint32_t new_capacity = old_capacity + MIN_PROPERTIES_LENGTH; 77 return new_capacity > MAX_PROPERTIES_LENGTH ? MAX_PROPERTIES_LENGTH : new_capacity; 78 } 79 80 int FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key, int propertiesNumber); 81 int BinarySearch(JSTaggedValue key, int propertiesNumber); 82 void GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray, 83 const JSHandle<JSObject> object); 84 void GetAllKeys(int end, std::vector<JSTaggedValue> &keyVector, const JSHandle<JSObject> object); 85 void GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength, 86 TaggedArray *keyArray, const JSHandle<JSObject> object, uint32_t filter); 87 void GetAllEnumKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray, uint32_t *keys, 88 const JSHandle<JSObject> object); 89 90 void DumpFieldIndexForProfile(int index, PGOHClassLayoutDesc &desc, PGOObjKind kind); 91 DECL_DUMP() 92 93 private: 94 bool IsUninitializedProperty(const JSHandle<JSObject> object, uint32_t index); 95 }; 96 } // namespace panda::ecmascript 97 98 #endif // ECMASCRIPT_LAYOUT_INFO_H 99