• 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 #include "ecmascript/weak_vector.h"
17 
18 #include "ecmascript/object_factory.h"
19 
20 namespace panda::ecmascript {
Create(const JSThread * thread,uint32_t capacity)21 JSHandle<WeakVector> WeakVector::Create(const JSThread *thread, uint32_t capacity)
22 {
23     ASSERT(capacity < MAX_VECTOR_INDEX);
24 
25     uint32_t length = VectorToArrayIndex(capacity);
26     JSHandle<WeakVector> vector = JSHandle<WeakVector>(thread->GetEcmaVM()->GetFactory()->NewTaggedArray(length));
27 
28     vector->SetEnd(thread, 0);
29     return vector;
30 }
31 
Delete(const JSThread * thread,uint32_t index)32 bool WeakVector::Delete(const JSThread *thread, uint32_t index)
33 {
34     uint32_t end = GetEnd();
35     if (index < end) {
36         Set(thread, index, JSTaggedValue::Hole());
37         return true;
38     }
39     return false;
40 }
41 
Grow(const JSThread * thread,const JSHandle<WeakVector> & old,uint32_t newCapacity)42 JSHandle<WeakVector> WeakVector::Grow(const JSThread *thread, const JSHandle<WeakVector> &old, uint32_t newCapacity)
43 {
44     uint32_t oldCapacity = old->GetCapacity();
45     ASSERT(newCapacity > oldCapacity);
46     if (oldCapacity == MAX_VECTOR_INDEX) {
47         return old;
48     }
49 
50     if (newCapacity > MAX_VECTOR_INDEX) {
51         newCapacity = MAX_VECTOR_INDEX;
52     }
53 
54     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
55     JSHandle<TaggedArray> newVec = factory->CopyArray(JSHandle<TaggedArray>(old), VectorToArrayIndex(oldCapacity),
56                                                       VectorToArrayIndex(newCapacity));
57 
58     return JSHandle<WeakVector>(newVec);
59 }
60 
Append(const JSThread * thread,const JSHandle<WeakVector> & vec,const JSHandle<JSTaggedValue> & value,ElementType type)61 JSHandle<WeakVector> WeakVector::Append(const JSThread *thread, const JSHandle<WeakVector> &vec,
62                                         const JSHandle<JSTaggedValue> &value, ElementType type)
63 {
64     if (!vec->Full()) {
65         JSTaggedValue storeVal = GetStoreVal(value, type);
66         vec->PushBack(thread, storeVal);
67         return vec;
68     }
69 
70     return AppendToFullVec(thread, vec, value, type);
71 }
72 
FillOrAppend(const JSThread * thread,const JSHandle<WeakVector> & vec,const JSHandle<JSTaggedValue> & value,ElementType type)73 JSHandle<WeakVector> WeakVector::FillOrAppend(const JSThread *thread, const JSHandle<WeakVector> &vec,
74                                               const JSHandle<JSTaggedValue> &value, ElementType type)
75 {
76     if (!vec->Full()) {
77         JSTaggedValue storeVal = GetStoreVal(value, type);
78         vec->PushBack(thread, storeVal);
79         return vec;
80     }
81 
82     // if exist hole, use it.
83     uint32_t holeIndex = CheckHole(vec);
84     if (holeIndex != TaggedArray::MAX_ARRAY_INDEX) {
85         JSTaggedValue storeVal = GetStoreVal(value, type);
86         vec->Set(thread, holeIndex, storeVal);
87         return vec;
88     }
89 
90     return AppendToFullVec(thread, vec, value, type);
91 }
92 
AppendToFullVec(const JSThread * thread,const JSHandle<WeakVector> & vec,const JSHandle<JSTaggedValue> & value,ElementType type)93 JSHandle<WeakVector> WeakVector::AppendToFullVec(const JSThread *thread, const JSHandle<WeakVector> &vec,
94                                                  const JSHandle<JSTaggedValue> &value, ElementType type)
95 {
96     uint32_t newCapacity = vec->GetCapacity() + DEFAULT_GROW_SIZE;
97     JSHandle<WeakVector> newVec = WeakVector::Grow(thread, JSHandle<WeakVector>(vec), newCapacity);
98     JSTaggedValue storeVal = GetStoreVal(value, type);
99     [[maybe_unused]] uint32_t index = newVec->PushBack(thread, storeVal);
100     ASSERT(index != TaggedArray::MAX_ARRAY_INDEX);
101     return newVec;
102 }
103 
GetStoreVal(const JSHandle<JSTaggedValue> & value,ElementType type)104 JSTaggedValue WeakVector::GetStoreVal(const JSHandle<JSTaggedValue> &value, ElementType type)
105 {
106     if (type == ElementType::NORMAL) {
107         return value.GetTaggedValue();
108     }
109 
110     if (value->IsHeapObject()) {
111         return value->CreateAndGetWeakRef();
112     }
113     return value.GetTaggedValue();
114 }
115 
Copy(const JSThread * thread,const JSHandle<WeakVector> & vec,bool needExtend)116 JSHandle<WeakVector> WeakVector::Copy(const JSThread *thread, const JSHandle<WeakVector> &vec,
117                                       bool needExtend)
118 {
119     uint32_t capacity = vec->GetCapacity();
120     uint32_t oldLength = VectorToArrayIndex(capacity);
121     if (needExtend) {
122         capacity += DEFAULT_GROW_SIZE;
123     }
124     uint32_t newLength = VectorToArrayIndex(capacity);
125     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
126     JSHandle<TaggedArray> newVec = factory->CopyArray(JSHandle<TaggedArray>(vec), oldLength, newLength);
127     return JSHandle<WeakVector>(newVec);
128 }
129 
CheckHole(const JSHandle<WeakVector> & vec)130 uint32_t WeakVector::CheckHole(const JSHandle<WeakVector> &vec)
131 {
132     for (uint32_t i = 0; i < vec->GetEnd(); i++) {
133         JSTaggedValue value = vec->Get(i);
134         if (value.IsHole()) {
135             return i;
136         }
137     }
138     return TaggedArray::MAX_ARRAY_INDEX;
139 }
140 
PushBack(const JSThread * thread,JSTaggedValue value)141 uint32_t WeakVector::PushBack(const JSThread *thread, JSTaggedValue value)
142 {
143     uint32_t end = GetEnd();
144     if (end == GetCapacity()) {
145         return TaggedArray::MAX_ARRAY_INDEX;
146     }
147 
148     Set(thread, end, value);
149     SetEnd(thread, end + 1);
150     return end;
151 }
152 }  // namespace panda::ecmascript
153