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