14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_LAYOUT_INFO_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_LAYOUT_INFO_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_symbol.h"
214514f5e3Sopenharmony_ci#include "ecmascript/property_attributes.h"
224514f5e3Sopenharmony_ci#include "ecmascript/tagged_array.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_cistruct Properties {
264514f5e3Sopenharmony_ci    JSTaggedValue key_;
274514f5e3Sopenharmony_ci    JSTaggedValue attr_;
284514f5e3Sopenharmony_ci};
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ciclass LayoutInfo : private TaggedArray {
314514f5e3Sopenharmony_cipublic:
324514f5e3Sopenharmony_ci    static constexpr int MIN_PROPERTIES_LENGTH = JSObject::MIN_PROPERTIES_LENGTH;
334514f5e3Sopenharmony_ci    static constexpr int MAX_PROPERTIES_LENGTH = PropertyAttributes::MAX_FAST_PROPS_CAPACITY;
344514f5e3Sopenharmony_ci    static constexpr uint32_t ELEMENTS_INDEX_LOG2 = 1;
354514f5e3Sopenharmony_ci    static constexpr uint32_t ATTR_INDEX_OFFSET = 1;
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    inline static LayoutInfo *Cast(TaggedObject *obj)
384514f5e3Sopenharmony_ci    {
394514f5e3Sopenharmony_ci        ASSERT(JSTaggedValue(obj).IsTaggedArray());
404514f5e3Sopenharmony_ci        return reinterpret_cast<LayoutInfo *>(obj);
414514f5e3Sopenharmony_ci    }
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    inline static LayoutInfo *UncheckCast(TaggedObject *obj)
444514f5e3Sopenharmony_ci    {
454514f5e3Sopenharmony_ci        return reinterpret_cast<LayoutInfo *>(obj);
464514f5e3Sopenharmony_ci    }
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci    static LayoutInfo* GetLayoutInfoFromHClass(const JSHClass* hclass)
494514f5e3Sopenharmony_ci    {
504514f5e3Sopenharmony_ci        return LayoutInfo::Cast(hclass->GetLayout().GetTaggedObject());
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    void Initialize(const JSThread *thread, int num = 0);
544514f5e3Sopenharmony_ci    int GetPropertiesCapacity() const;
554514f5e3Sopenharmony_ci    int NumberOfElements() const;
564514f5e3Sopenharmony_ci    void SetNumberOfElements(const JSThread *thread, int properties);
574514f5e3Sopenharmony_ci    uint32_t GetKeyIndex(int index) const;
584514f5e3Sopenharmony_ci    uint32_t GetAttrIndex(int index) const;
594514f5e3Sopenharmony_ci    void SetWithoutBarrier(uint32_t idx, const JSTaggedValue &value);
604514f5e3Sopenharmony_ci    void SetPropertyInit(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr);
614514f5e3Sopenharmony_ci    void SetKey(const JSThread *thread, int index, const JSTaggedValue &key);
624514f5e3Sopenharmony_ci    void SetNormalAttr(const JSThread *thread, int index, const PropertyAttributes &attr);
634514f5e3Sopenharmony_ci    JSTaggedValue GetKey(int index) const;
644514f5e3Sopenharmony_ci    PropertyAttributes GetAttr(int index) const;
654514f5e3Sopenharmony_ci    JSTaggedValue GetSortedKey(int index) const;
664514f5e3Sopenharmony_ci    uint32_t GetSortedIndex(int index) const;
674514f5e3Sopenharmony_ci    void SetSortedIndex(const JSThread *thread, int index, int sortedIndex);
684514f5e3Sopenharmony_ci    template<bool checkDuplicateKeys = false>
694514f5e3Sopenharmony_ci    void AddKey(const JSThread *thread, int index, const JSTaggedValue &key, const PropertyAttributes &attr);
704514f5e3Sopenharmony_ci    void SetIsNotHole(const JSThread *thread, int index);
714514f5e3Sopenharmony_ci    void UpdateTrackTypeAttr(int index, const PropertyAttributes &attr);
724514f5e3Sopenharmony_ci    void SetIsPGODumped(int index);
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    inline uint32_t GetLength() const
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        return TaggedArray::GetLength();
774514f5e3Sopenharmony_ci    }
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    inline Properties *GetProperties() const
804514f5e3Sopenharmony_ci    {
814514f5e3Sopenharmony_ci        return reinterpret_cast<Properties *>(reinterpret_cast<uintptr_t>(this) + TaggedArray::DATA_OFFSET);
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    static inline uint32_t ComputeArrayLength(uint32_t properties_number)
854514f5e3Sopenharmony_ci    {
864514f5e3Sopenharmony_ci        return (properties_number << ELEMENTS_INDEX_LOG2);
874514f5e3Sopenharmony_ci    }
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    static inline uint32_t ComputeGrowCapacity(uint32_t old_capacity)
904514f5e3Sopenharmony_ci    {
914514f5e3Sopenharmony_ci        uint32_t new_capacity = old_capacity + MIN_PROPERTIES_LENGTH;
924514f5e3Sopenharmony_ci        return new_capacity > MAX_PROPERTIES_LENGTH ? MAX_PROPERTIES_LENGTH : new_capacity;
934514f5e3Sopenharmony_ci    }
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci    int FindElementWithCache(const JSThread *thread, JSHClass *cls, JSTaggedValue key, int propertiesNumber);
964514f5e3Sopenharmony_ci    int BinarySearch(JSTaggedValue key, int propertiesNumber);
974514f5e3Sopenharmony_ci    void GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray,
984514f5e3Sopenharmony_ci                    const JSHandle<JSObject> object);
994514f5e3Sopenharmony_ci    void GetAllKeysForSerialization(int end, std::vector<JSTaggedValue> &keyVector);
1004514f5e3Sopenharmony_ci    void GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength,
1014514f5e3Sopenharmony_ci        TaggedArray *keyArray, const JSHandle<JSObject> object, uint32_t filter);
1024514f5e3Sopenharmony_ci    std::pair<uint32_t, uint32_t> GetNumOfEnumKeys(int end, const JSObject *object) const;
1034514f5e3Sopenharmony_ci    void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys,
1044514f5e3Sopenharmony_ci                        JSHandle<TaggedQueue> shadowQueue, const JSHandle<JSObject> object,
1054514f5e3Sopenharmony_ci                        int32_t lastLength);
1064514f5e3Sopenharmony_ci    void GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys,
1074514f5e3Sopenharmony_ci                        const JSHandle<JSObject> object);
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    void DumpFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc);
1104514f5e3Sopenharmony_ci    bool UpdateFieldIndexByPGO(int index, pgo::HClassLayoutDesc* desc);
1114514f5e3Sopenharmony_ci    CString GetSymbolKeyString(JSTaggedValue key);
1124514f5e3Sopenharmony_ci    DECL_DUMP()
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ciprivate:
1154514f5e3Sopenharmony_ci    bool IsUninitializedProperty(const JSObject* object, uint32_t index) const;
1164514f5e3Sopenharmony_ci};
1174514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1184514f5e3Sopenharmony_ci
1194514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_LAYOUT_INFO_H
120