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-inl.h" 22 23 namespace panda::ecmascript { 24 class WeakVector : public TaggedArray { 25 public: 26 enum ElementType { 27 NORMAL = 0, 28 WEAK, 29 }; 30 Cast(TaggedObject * object)31 static WeakVector *Cast(TaggedObject *object) 32 { 33 return static_cast<WeakVector *>(object); 34 } 35 36 static constexpr uint32_t DEFAULT_CAPACITY = 4; 37 static constexpr uint32_t DEFAULT_GROW_SIZE = 4; 38 static JSHandle<WeakVector> Create(const JSThread *thread, uint32_t capacity = DEFAULT_CAPACITY); 39 static JSHandle<WeakVector> Grow(const JSThread *thread, const JSHandle<WeakVector> &old, uint32_t newCapacity); 40 static JSHandle<WeakVector> Append(const JSThread *thread, const JSHandle<WeakVector> &vec, 41 const JSHandle<JSTaggedValue> &value, ElementType type = ElementType::NORMAL); 42 static JSHandle<WeakVector> FillOrAppend(const JSThread *thread, const JSHandle<WeakVector> &vec, 43 const JSHandle<JSTaggedValue> &value, 44 ElementType type = ElementType::NORMAL); 45 static JSHandle<WeakVector> Copy(const JSThread *thread, const JSHandle<WeakVector> &vec, bool needExtend = false); 46 uint32_t PushBack(const JSThread *thread, JSTaggedValue value); 47 // just set index value to Hole 48 bool Delete(const JSThread *thread, uint32_t index); 49 GetEnd()50 inline uint32_t GetEnd() const 51 { 52 return TaggedArray::GetExtraLength(); 53 } 54 Full()55 inline bool Full() const 56 { 57 return GetEnd() == GetCapacity(); 58 } 59 Empty()60 inline bool Empty() const 61 { 62 return GetEnd() == 0; 63 } 64 GetCapacity()65 inline uint32_t GetCapacity() const 66 { 67 return TaggedArray::GetLength(); 68 } 69 Get(uint32_t index)70 inline JSTaggedValue Get(uint32_t index) const 71 { 72 ASSERT(index < GetCapacity()); 73 return TaggedArray::Get(VectorToArrayIndex(index)); 74 } 75 Set(const JSThread * thread,uint32_t index,JSTaggedValue value)76 inline void Set(const JSThread *thread, uint32_t index, JSTaggedValue value) 77 { 78 ASSERT(index < GetCapacity()); 79 TaggedArray::Set(thread, VectorToArrayIndex(index), value); 80 } 81 82 template <class Callback> Iterate(const Callback & cb)83 void Iterate(const Callback &cb) 84 { 85 uint32_t end = GetEnd(); 86 for (uint32_t index = 0; index < end; ++index) { 87 cb(Get(index)); 88 } 89 } 90 91 private: 92 static const uint32_t MIN_CAPACITY = 2; 93 static const uint32_t MAX_VECTOR_INDEX = TaggedArray::MAX_ARRAY_INDEX; 94 VectorToArrayIndex(uint32_t index)95 inline static constexpr uint32_t VectorToArrayIndex(uint32_t index) 96 { 97 return index; 98 } 99 SetEnd(const JSThread * thread,uint32_t end)100 inline void SetEnd([[maybe_unused]] const JSThread *thread, uint32_t end) 101 { 102 ASSERT(end <= GetCapacity()); 103 SetExtraLength(end); 104 } 105 106 static JSTaggedValue GetStoreVal(const JSHandle<JSTaggedValue> &value, ElementType type); 107 108 static uint32_t CheckHole(const JSHandle<WeakVector> &vec); 109 110 static JSHandle<WeakVector> AppendToFullVec(const JSThread *thread, const JSHandle<WeakVector> &vec, 111 const JSHandle<JSTaggedValue> &value, ElementType type); 112 }; 113 } // namespace panda::ecmascript 114 #endif // ECMASCRIPT_WEAK_VECTOR_H 115