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/ic/proto_change_details.h"
17 namespace panda::ecmascript {
18
CalcNewCapacity(uint32_t oldCapacity)19 static uint32_t CalcNewCapacity(uint32_t oldCapacity)
20 {
21 uint32_t initNewCap = oldCapacity + 1;
22 // 2: make sure we have at least 2 buffer to use.
23 uint32_t minGrowthBuffer = 2;
24 // 2: grow up by 50% each time.
25 return initNewCap + std::max(minGrowthBuffer, initNewCap / 2);
26 }
27
Add(const JSThread * thread,const JSHandle<ChangeListener> & array,const JSHandle<JSHClass> & value,uint32_t * index)28 JSHandle<ChangeListener> ChangeListener::Add(const JSThread *thread, const JSHandle<ChangeListener> &array,
29 const JSHandle<JSHClass> &value, uint32_t *index)
30 {
31 JSTaggedValue weakValue;
32 if (!array->Full()) {
33 weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
34 uint32_t arrayIndex = array->PushBack(thread, weakValue);
35 if (arrayIndex != TaggedArray::MAX_ARRAY_INDEX) {
36 if (index != nullptr) {
37 *index = arrayIndex;
38 }
39 return array;
40 }
41 LOG_ECMA(FATAL) << "this branch is unreachable";
42 UNREACHABLE();
43 }
44 // if exist hole, use it.
45 uint32_t holeIndex = CheckHole(thread, array);
46 if (holeIndex != TaggedArray::MAX_ARRAY_INDEX) {
47 weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
48 array->Set(thread, holeIndex, weakValue);
49 if (index != nullptr) {
50 *index = holeIndex;
51 }
52 return array;
53 }
54 // the vector is full and no hole exists.
55 uint32_t newCapacity = CalcNewCapacity(array->GetCapacity());
56 JSHandle<WeakVector> newArray = WeakVector::Grow(thread, JSHandle<WeakVector>(array), newCapacity);
57 weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
58 uint32_t arrayIndex = newArray->PushBack(thread, weakValue);
59 ASSERT(arrayIndex != TaggedArray::MAX_ARRAY_INDEX);
60 if (index != nullptr) {
61 *index = arrayIndex;
62 }
63 return JSHandle<ChangeListener>(newArray);
64 }
65
CheckHole(const JSThread * thread,const JSHandle<ChangeListener> & array)66 uint32_t ChangeListener::CheckHole(const JSThread *thread, const JSHandle<ChangeListener> &array)
67 {
68 for (uint32_t i = 0; i < array->GetEnd(); i++) {
69 JSTaggedValue value = array->Get(thread, i);
70 if (value.IsHole() || value.IsUndefined()) {
71 return i;
72 }
73 }
74 return TaggedArray::MAX_ARRAY_INDEX;
75 }
76
Get(const JSThread * thread,uint32_t index)77 JSTaggedValue ChangeListener::Get(const JSThread *thread, uint32_t index)
78 {
79 JSTaggedValue value = WeakVector::Get(thread, index);
80 if (!value.IsHeapObject()) {
81 return value;
82 }
83 return JSTaggedValue(value.GetTaggedWeakRef());
84 }
85 } // namespace panda::ecmascript