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 24namespace panda::ecmascript { 25struct Properties { 26 JSTaggedValue key_; 27 JSTaggedValue attr_; 28}; 29 30class LayoutInfo : private TaggedArray { 31public: 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 37 inline static LayoutInfo *Cast(TaggedObject *obj) 38 { 39 ASSERT(JSTaggedValue(obj).IsTaggedArray()); 40 return reinterpret_cast<LayoutInfo *>(obj); 41 } 42 43 inline static LayoutInfo *UncheckCast(TaggedObject *obj) 44 { 45 return reinterpret_cast<LayoutInfo *>(obj); 46 } 47 48 static LayoutInfo* GetLayoutInfoFromHClass(const JSHClass* hclass) 49 { 50 return LayoutInfo::Cast(hclass->GetLayout().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(int index) const; 64 PropertyAttributes GetAttr(int index) const; 65 JSTaggedValue GetSortedKey(int index) const; 66 uint32_t GetSortedIndex(int index) const; 67 void SetSortedIndex(const JSThread *thread, int index, int sortedIndex); 68 template<bool checkDuplicateKeys = false> 69 void AddKey(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr); 70 void SetIsNotHole(const JSThread *thread, int index); 71 void UpdateTrackTypeAttr(int index, const PropertyAttributes &attr); 72 void SetIsPGODumped(int index); 73 74 inline uint32_t GetLength() const 75 { 76 return TaggedArray::GetLength(); 77 } 78 79 inline Properties *GetProperties() const 80 { 81 return reinterpret_cast<Properties *>(reinterpret_cast<uintptr_t>(this) + TaggedArray::DATA_OFFSET); 82 } 83 84 static inline uint32_t ComputeArrayLength(uint32_t properties_number) 85 { 86 return (properties_number << ELEMENTS_INDEX_LOG2); 87 } 88 89 static inline uint32_t ComputeGrowCapacity(uint32_t old_capacity) 90 { 91 uint32_t new_capacity = old_capacity + MIN_PROPERTIES_LENGTH; 92 return new_capacity > MAX_PROPERTIES_LENGTH ? MAX_PROPERTIES_LENGTH : new_capacity; 93 } 94 95 int FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key, int propertiesNumber); 96 int BinarySearch(JSTaggedValue key, int propertiesNumber); 97 void GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray, 98 const JSHandle<JSObject> object); 99 void GetAllKeysForSerialization(int end, std::vector<JSTaggedValue> &keyVector); 100 void GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength, 101 TaggedArray *keyArray, const JSHandle<JSObject> object, uint32_t filter); 102 std::pair<uint32_t, uint32_t> GetNumOfEnumKeys(int end, const JSObject *object) const; 103 void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys, 104 JSHandle<TaggedQueue> shadowQueue, const JSHandle<JSObject> object, 105 int32_t lastLength); 106 void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys, 107 const JSHandle<JSObject> object); 108 109 void DumpFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc); 110 bool UpdateFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc); 111 CString GetSymbolKeyString(JSTaggedValue key); 112 DECL_DUMP() 113 114private: 115 bool IsUninitializedProperty(const JSObject* object, uint32_t index) const; 116}; 117} // namespace panda::ecmascript 118 119#endif // ECMASCRIPT_LAYOUT_INFO_H 120