• 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 
PushBack(const JSThread * thread,JSTaggedValue value)61 uint32_t WeakVector::PushBack(const JSThread *thread, JSTaggedValue value)
62 {
63     uint32_t end = GetEnd();
64     if (end == GetCapacity()) {
65         return TaggedArray::MAX_ARRAY_INDEX;
66     }
67 
68     Set(thread, end, value);
69     SetEnd(thread, end + 1);
70     return end;
71 }
72 }  // namespace panda::ecmascript
73