• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
Cast(TaggedObject * object)26     static WeakVector *Cast(TaggedObject *object)
27     {
28         return static_cast<WeakVector *>(object);
29     }
30 
31     static constexpr uint32_t DEFAULT_CAPACITY = 4;
32     static JSHandle<WeakVector> Create(const JSThread *thread, uint32_t capacity = DEFAULT_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 
GetEnd()38     inline uint32_t GetEnd() const
39     {
40         return TaggedArray::Get(END_INDEX).GetArrayLength();
41     }
42 
Full()43     inline bool Full() const
44     {
45         return GetEnd() == GetCapacity();
46     }
47 
Empty()48     inline bool Empty() const
49     {
50         return GetEnd() == 0;
51     }
52 
GetCapacity()53     inline uint32_t GetCapacity() const
54     {
55         return TaggedArray::GetLength() - ELEMENTS_START_INDEX;
56     }
57 
Get(uint32_t index)58     inline JSTaggedValue Get(uint32_t index) const
59     {
60         ASSERT(index < GetCapacity());
61         return TaggedArray::Get(VectorToArrayIndex(index));
62     }
63 
Set(const JSThread * thread,uint32_t index,JSTaggedValue value)64     inline void Set(const JSThread *thread, uint32_t index, JSTaggedValue value)
65     {
66         ASSERT(index < GetCapacity());
67         TaggedArray::Set(thread, VectorToArrayIndex(index), value);
68     }
69 
70 private:
71     static const uint32_t MIN_CAPACITY = 2;
72     static const uint32_t END_INDEX = 0;
73     static const uint32_t ELEMENTS_START_INDEX = 1;
74     static const uint32_t MAX_VECTOR_INDEX = TaggedArray::MAX_ARRAY_INDEX - ELEMENTS_START_INDEX;
75 
VectorToArrayIndex(uint32_t index)76     inline static constexpr uint32_t VectorToArrayIndex(uint32_t index)
77     {
78         return index + ELEMENTS_START_INDEX;
79     }
80 
SetEnd(const JSThread * thread,uint32_t end)81     inline void SetEnd(const JSThread *thread, uint32_t end)
82     {
83         ASSERT(end <= GetCapacity());
84         TaggedArray::Set(thread, END_INDEX, JSTaggedValue(end));
85     }
86 };
87 }  // namespace panda::ecmascript
88 #endif  // ECMASCRIPT_WEAK_VECTOR_H
89