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_WEAK_VECTOR_H 17 #define ECMASCRIPT_WEAK_VECTOR_H 18 19 #include "ecmascript/js_handle.h" 20 #include "ecmascript/js_thread.h" 21 #include "ecmascript/tagged_array.h" 22 23 namespace panda::ecmascript { 24 class WeakVector : public TaggedArray { 25 public: Cast(ObjectHeader * object)26 static WeakVector *Cast(ObjectHeader *object) 27 { 28 return static_cast<WeakVector *>(object); 29 } 30 31 static constexpr uint32_t DEFALUT_CAPACITY = 4; 32 static JSHandle<WeakVector> Create(const JSThread *thread, uint32_t capacity = DEFALUT_CAPACITY); 33 static JSHandle<WeakVector> Grow(const JSThread *thread, const JSHandle<WeakVector> &old, uint32_t newCapacity); 34 uint32_t PushBack(const JSThread *thread, JSTaggedValue value); 35 // just set index value to Hole 36 bool Delete(const JSThread *thread, uint32_t index); 37 38 inline uint32_t GetEnd() const; 39 40 inline bool Full() const; 41 42 inline bool Empty() const; 43 44 inline uint32_t GetCapacity() const; 45 46 inline JSTaggedValue Get(uint32_t index) const; 47 48 inline void Set(const JSThread *thread, uint32_t index, JSTaggedValue value); 49 50 private: 51 static const uint32_t MIN_CAPACITY = 2; 52 static const uint32_t END_INDEX = 0; 53 static const uint32_t ELEMENTS_START_INDEX = 1; 54 static const uint32_t MAX_VECTOR_INDEX = TaggedArray::MAX_ARRAY_INDEX - ELEMENTS_START_INDEX; 55 VectorToArrayIndex(uint32_t index)56 inline static constexpr uint32_t VectorToArrayIndex(uint32_t index) 57 { 58 return index + ELEMENTS_START_INDEX; 59 } 60 61 inline void SetEnd(const JSThread *thread, uint32_t end); 62 }; 63 } // namespace panda::ecmascript 64 #endif // ECMASCRIPT_WEAK_VECTOR_H 65