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