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