14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/ic/proto_change_details.h"
174514f5e3Sopenharmony_cinamespace panda::ecmascript {
184514f5e3Sopenharmony_ciJSHandle<ChangeListener> ChangeListener::Add(const JSThread *thread, const JSHandle<ChangeListener> &array,
194514f5e3Sopenharmony_ci                                             const JSHandle<JSHClass> &value, uint32_t *index)
204514f5e3Sopenharmony_ci{
214514f5e3Sopenharmony_ci    JSTaggedValue weakValue;
224514f5e3Sopenharmony_ci    if (!array->Full()) {
234514f5e3Sopenharmony_ci        weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
244514f5e3Sopenharmony_ci        uint32_t arrayIndex = array->PushBack(thread, weakValue);
254514f5e3Sopenharmony_ci        if (arrayIndex != TaggedArray::MAX_ARRAY_INDEX) {
264514f5e3Sopenharmony_ci            if (index != nullptr) {
274514f5e3Sopenharmony_ci                *index = arrayIndex;
284514f5e3Sopenharmony_ci            }
294514f5e3Sopenharmony_ci            return array;
304514f5e3Sopenharmony_ci        }
314514f5e3Sopenharmony_ci        LOG_ECMA(FATAL) << "this branch is unreachable";
324514f5e3Sopenharmony_ci        UNREACHABLE();
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci    // if exist hole, use it.
354514f5e3Sopenharmony_ci    uint32_t holeIndex = CheckHole(array);
364514f5e3Sopenharmony_ci    if (holeIndex != TaggedArray::MAX_ARRAY_INDEX) {
374514f5e3Sopenharmony_ci        weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
384514f5e3Sopenharmony_ci        array->Set(thread, holeIndex, weakValue);
394514f5e3Sopenharmony_ci        if (index != nullptr) {
404514f5e3Sopenharmony_ci            *index = holeIndex;
414514f5e3Sopenharmony_ci        }
424514f5e3Sopenharmony_ci        return array;
434514f5e3Sopenharmony_ci    }
444514f5e3Sopenharmony_ci    // the vector is full and no hole exists.
454514f5e3Sopenharmony_ci    JSHandle<WeakVector> newArray = WeakVector::Grow(thread, JSHandle<WeakVector>(array), array->GetCapacity() + 1);
464514f5e3Sopenharmony_ci    weakValue = JSTaggedValue(value.GetTaggedValue().CreateAndGetWeakRef());
474514f5e3Sopenharmony_ci    uint32_t arrayIndex = newArray->PushBack(thread, weakValue);
484514f5e3Sopenharmony_ci    ASSERT(arrayIndex != TaggedArray::MAX_ARRAY_INDEX);
494514f5e3Sopenharmony_ci    if (index != nullptr) {
504514f5e3Sopenharmony_ci        *index = arrayIndex;
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci    return JSHandle<ChangeListener>(newArray);
534514f5e3Sopenharmony_ci}
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ciuint32_t ChangeListener::CheckHole(const JSHandle<ChangeListener> &array)
564514f5e3Sopenharmony_ci{
574514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < array->GetEnd(); i++) {
584514f5e3Sopenharmony_ci        JSTaggedValue value = array->Get(i);
594514f5e3Sopenharmony_ci        if (value.IsHole() || value.IsUndefined()) {
604514f5e3Sopenharmony_ci            return i;
614514f5e3Sopenharmony_ci        }
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci    return TaggedArray::MAX_ARRAY_INDEX;
644514f5e3Sopenharmony_ci}
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ciJSTaggedValue ChangeListener::Get(uint32_t index)
674514f5e3Sopenharmony_ci{
684514f5e3Sopenharmony_ci    JSTaggedValue value = WeakVector::Get(index);
694514f5e3Sopenharmony_ci    if (!value.IsHeapObject()) {
704514f5e3Sopenharmony_ci        return value;
714514f5e3Sopenharmony_ci    }
724514f5e3Sopenharmony_ci    return JSTaggedValue(value.GetTaggedWeakRef());
734514f5e3Sopenharmony_ci}
744514f5e3Sopenharmony_ci}  // namespace panda::ecmascript