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_WEAK_VECTOR_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_WEAK_VECTOR_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
214514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_cinamespace panda::ecmascript {
244514f5e3Sopenharmony_ciclass WeakVector : public TaggedArray {
254514f5e3Sopenharmony_cipublic:
264514f5e3Sopenharmony_ci    enum ElementType {
274514f5e3Sopenharmony_ci        NORMAL = 0,
284514f5e3Sopenharmony_ci        WEAK,
294514f5e3Sopenharmony_ci    };
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci    static WeakVector *Cast(TaggedObject *object)
324514f5e3Sopenharmony_ci    {
334514f5e3Sopenharmony_ci        return static_cast<WeakVector *>(object);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    static constexpr uint32_t DEFAULT_CAPACITY = 4;
374514f5e3Sopenharmony_ci    static constexpr uint32_t DEFAULT_GROW_SIZE = 4;
384514f5e3Sopenharmony_ci    static JSHandle<WeakVector> Create(const JSThread *thread, uint32_t capacity = DEFAULT_CAPACITY,
394514f5e3Sopenharmony_ci                                       MemSpaceType type = MemSpaceType::SEMI_SPACE);
404514f5e3Sopenharmony_ci    static JSHandle<WeakVector> Grow(const JSThread *thread, const JSHandle<WeakVector> &old, uint32_t newCapacity);
414514f5e3Sopenharmony_ci    static JSHandle<WeakVector> Append(const JSThread *thread, const JSHandle<WeakVector> &vec,
424514f5e3Sopenharmony_ci                                       const JSHandle<JSTaggedValue> &value, ElementType type = ElementType::NORMAL);
434514f5e3Sopenharmony_ci    static JSHandle<WeakVector> FillOrAppend(const JSThread *thread, const JSHandle<WeakVector> &vec,
444514f5e3Sopenharmony_ci                                             const JSHandle<JSTaggedValue> &value,
454514f5e3Sopenharmony_ci                                             ElementType type = ElementType::NORMAL);
464514f5e3Sopenharmony_ci    static JSHandle<WeakVector> Copy(const JSThread *thread, const JSHandle<WeakVector> &vec, bool needExtend = false);
474514f5e3Sopenharmony_ci    uint32_t PushBack(const JSThread *thread, JSTaggedValue value);
484514f5e3Sopenharmony_ci    // just set index value to Hole
494514f5e3Sopenharmony_ci    bool Delete(const JSThread *thread, uint32_t index);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    inline uint32_t GetEnd() const
524514f5e3Sopenharmony_ci    {
534514f5e3Sopenharmony_ci        return TaggedArray::GetExtraLength();
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    inline bool Full() const
574514f5e3Sopenharmony_ci    {
584514f5e3Sopenharmony_ci        return GetEnd() == GetCapacity();
594514f5e3Sopenharmony_ci    }
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    inline bool Empty() const
624514f5e3Sopenharmony_ci    {
634514f5e3Sopenharmony_ci        return GetEnd() == 0;
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    inline uint32_t GetCapacity() const
674514f5e3Sopenharmony_ci    {
684514f5e3Sopenharmony_ci        return TaggedArray::GetLength();
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    inline JSTaggedValue Get(uint32_t index) const
724514f5e3Sopenharmony_ci    {
734514f5e3Sopenharmony_ci        ASSERT(index < GetCapacity());
744514f5e3Sopenharmony_ci        return TaggedArray::Get(VectorToArrayIndex(index));
754514f5e3Sopenharmony_ci    }
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    inline void Set(const JSThread *thread, uint32_t index, JSTaggedValue value)
784514f5e3Sopenharmony_ci    {
794514f5e3Sopenharmony_ci        ASSERT(index < GetCapacity());
804514f5e3Sopenharmony_ci        TaggedArray::Set(thread, VectorToArrayIndex(index), value);
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    template <class Callback>
844514f5e3Sopenharmony_ci    void Iterate(const Callback &cb)
854514f5e3Sopenharmony_ci    {
864514f5e3Sopenharmony_ci        uint32_t end = GetEnd();
874514f5e3Sopenharmony_ci        for (uint32_t index = 0; index < end; ++index) {
884514f5e3Sopenharmony_ci            cb(Get(index));
894514f5e3Sopenharmony_ci        }
904514f5e3Sopenharmony_ci    }
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ciprivate:
934514f5e3Sopenharmony_ci    static const uint32_t MIN_CAPACITY = 2;
944514f5e3Sopenharmony_ci    static const uint32_t MAX_VECTOR_INDEX = TaggedArray::MAX_ARRAY_INDEX;
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci    inline static constexpr uint32_t VectorToArrayIndex(uint32_t index)
974514f5e3Sopenharmony_ci    {
984514f5e3Sopenharmony_ci        return index;
994514f5e3Sopenharmony_ci    }
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    inline void SetEnd([[maybe_unused]] const JSThread *thread, uint32_t end)
1024514f5e3Sopenharmony_ci    {
1034514f5e3Sopenharmony_ci        ASSERT(end <= GetCapacity());
1044514f5e3Sopenharmony_ci        SetExtraLength(end);
1054514f5e3Sopenharmony_ci    }
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ci    static JSTaggedValue GetStoreVal(const JSHandle<JSTaggedValue> &value, ElementType type);
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    static uint32_t CheckHole(const JSHandle<WeakVector> &vec);
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ci    static JSHandle<WeakVector> AppendToFullVec(const JSThread *thread, const JSHandle<WeakVector> &vec,
1124514f5e3Sopenharmony_ci                                                const JSHandle<JSTaggedValue> &value, ElementType type);
1134514f5e3Sopenharmony_ci};
1144514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1154514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_WEAK_VECTOR_H
116