14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 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/js_api/js_api_deque.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_errors.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
204514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace panda::ecmascript {
234514f5e3Sopenharmony_ciusing ContainerError = containers::ContainerError;
244514f5e3Sopenharmony_ciusing ErrorFlag = containers::ErrorFlag;
254514f5e3Sopenharmony_ciclass JSAPIDequelterator;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_civoid JSAPIDeque::InsertFront(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value)
284514f5e3Sopenharmony_ci{
294514f5e3Sopenharmony_ci    JSHandle<TaggedArray> elements(thread, deque->GetElements());
304514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
314514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
324514f5e3Sopenharmony_ci    uint32_t first = deque->GetFirst();
334514f5e3Sopenharmony_ci    uint32_t last = deque->GetLast();
344514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
354514f5e3Sopenharmony_ci    if ((first + capacity - 1) % capacity == last) {
364514f5e3Sopenharmony_ci        elements = GrowCapacity(thread, deque, capacity, first, last);
374514f5e3Sopenharmony_ci        ASSERT(!elements->IsDictionaryMode());
384514f5e3Sopenharmony_ci        deque->SetLast(capacity - 1);
394514f5e3Sopenharmony_ci        first = 0;
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    capacity = elements->GetLength();
424514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
434514f5e3Sopenharmony_ci    first = (first + capacity - 1) % capacity;
444514f5e3Sopenharmony_ci    elements->Set(thread, first, value);
454514f5e3Sopenharmony_ci    deque->SetFirst(first);
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_civoid JSAPIDeque::InsertEnd(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value)
494514f5e3Sopenharmony_ci{
504514f5e3Sopenharmony_ci    JSHandle<TaggedArray> elements(thread, deque->GetElements());
514514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
524514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
534514f5e3Sopenharmony_ci    uint32_t first = deque->GetFirst();
544514f5e3Sopenharmony_ci    uint32_t last = deque->GetLast();
554514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
564514f5e3Sopenharmony_ci    if (first == (last + 1) % capacity) {
574514f5e3Sopenharmony_ci        elements = GrowCapacity(thread, deque, capacity, first, last);
584514f5e3Sopenharmony_ci        ASSERT(!elements->IsDictionaryMode());
594514f5e3Sopenharmony_ci        deque->SetFirst(0);
604514f5e3Sopenharmony_ci        last = capacity - 1;
614514f5e3Sopenharmony_ci    }
624514f5e3Sopenharmony_ci    elements->Set(thread, last, value);
634514f5e3Sopenharmony_ci    capacity = elements->GetLength();
644514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
654514f5e3Sopenharmony_ci    last = (last + 1) % capacity;
664514f5e3Sopenharmony_ci    deque->SetLast(last);
674514f5e3Sopenharmony_ci}
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::GetFront()
704514f5e3Sopenharmony_ci{
714514f5e3Sopenharmony_ci    if (JSAPIDeque::IsEmpty()) {
724514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
764514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
774514f5e3Sopenharmony_ci    return elements->Get(GetFirst());
784514f5e3Sopenharmony_ci}
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::GetTail()
814514f5e3Sopenharmony_ci{
824514f5e3Sopenharmony_ci    if (JSAPIDeque::IsEmpty()) {
834514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
864514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
874514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
884514f5e3Sopenharmony_ci    uint32_t last = GetLast();
894514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
904514f5e3Sopenharmony_ci    return elements->Get((last + capacity - 1) % capacity);
914514f5e3Sopenharmony_ci}
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ciJSHandle<TaggedArray> JSAPIDeque::GrowCapacity(JSThread *thread, const JSHandle<JSAPIDeque> &deque,
944514f5e3Sopenharmony_ci                                               uint32_t oldCapacity, uint32_t first, uint32_t last)
954514f5e3Sopenharmony_ci{
964514f5e3Sopenharmony_ci    JSHandle<TaggedArray> oldElements(thread, deque->GetElements());
974514f5e3Sopenharmony_ci    ASSERT(!oldElements->IsDictionaryMode());
984514f5e3Sopenharmony_ci    uint32_t newCapacity = ComputeCapacity(oldCapacity);
994514f5e3Sopenharmony_ci    uint32_t size = deque->GetSize();
1004514f5e3Sopenharmony_ci    JSHandle<TaggedArray> newElements =
1014514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, size, first, last);
1024514f5e3Sopenharmony_ci    deque->SetElements(thread, newElements);
1034514f5e3Sopenharmony_ci    return newElements;
1044514f5e3Sopenharmony_ci}
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::PopFirst()
1074514f5e3Sopenharmony_ci{
1084514f5e3Sopenharmony_ci    if (JSAPIDeque::IsEmpty()) {
1094514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
1104514f5e3Sopenharmony_ci    }
1114514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1124514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1134514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
1144514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1154514f5e3Sopenharmony_ci    JSTaggedValue firstElement = elements->Get(first);
1164514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
1174514f5e3Sopenharmony_ci    first = (first + 1) % capacity;
1184514f5e3Sopenharmony_ci    SetFirst(first);
1194514f5e3Sopenharmony_ci    return firstElement;
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::PopLast()
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    if (JSAPIDeque::IsEmpty()) {
1254514f5e3Sopenharmony_ci        return JSTaggedValue::Undefined();
1264514f5e3Sopenharmony_ci    }
1274514f5e3Sopenharmony_ci    uint32_t last = GetLast();
1284514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1294514f5e3Sopenharmony_ci    ASSERT(!elements->IsDictionaryMode());
1304514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1314514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
1324514f5e3Sopenharmony_ci    last = (last + capacity - 1) % capacity;
1334514f5e3Sopenharmony_ci    JSTaggedValue lastElement = elements->Get(last);
1344514f5e3Sopenharmony_ci    SetLast(last);
1354514f5e3Sopenharmony_ci    return lastElement;
1364514f5e3Sopenharmony_ci}
1374514f5e3Sopenharmony_ci
1384514f5e3Sopenharmony_cibool JSAPIDeque::IsEmpty()
1394514f5e3Sopenharmony_ci{
1404514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1414514f5e3Sopenharmony_ci    uint32_t last = GetLast();
1424514f5e3Sopenharmony_ci    return first == last;
1434514f5e3Sopenharmony_ci}
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ciuint32_t JSAPIDeque::GetSize() const
1464514f5e3Sopenharmony_ci{
1474514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1484514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1494514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1504514f5e3Sopenharmony_ci    uint32_t last = GetLast();
1514514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
1524514f5e3Sopenharmony_ci    return (last - first + capacity) % capacity;
1534514f5e3Sopenharmony_ci}
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::Get(const uint32_t index)
1564514f5e3Sopenharmony_ci{
1574514f5e3Sopenharmony_ci    ASSERT(index < GetSize());
1584514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1594514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1604514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1614514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
1624514f5e3Sopenharmony_ci    uint32_t curIndex = (first + index) % capacity;
1634514f5e3Sopenharmony_ci    return elements->Get(curIndex);
1644514f5e3Sopenharmony_ci}
1654514f5e3Sopenharmony_ci
1664514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::Set(JSThread *thread, const uint32_t index, JSTaggedValue value)
1674514f5e3Sopenharmony_ci{
1684514f5e3Sopenharmony_ci    uint32_t length = static_cast<uint32_t>(GetSize());
1694514f5e3Sopenharmony_ci    if (index < 0 || index >= length) {
1704514f5e3Sopenharmony_ci        return JSTaggedValue::False();
1714514f5e3Sopenharmony_ci    }
1724514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1734514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1744514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1754514f5e3Sopenharmony_ci    ASSERT(capacity != 0);
1764514f5e3Sopenharmony_ci    uint32_t curIndex = (first + index) % capacity;
1774514f5e3Sopenharmony_ci    elements->Set(thread, curIndex, value);
1784514f5e3Sopenharmony_ci    return JSTaggedValue::True();
1794514f5e3Sopenharmony_ci}
1804514f5e3Sopenharmony_ci
1814514f5e3Sopenharmony_cibool JSAPIDeque::Has(JSTaggedValue value) const
1824514f5e3Sopenharmony_ci{
1834514f5e3Sopenharmony_ci    uint32_t first = GetFirst();
1844514f5e3Sopenharmony_ci    uint32_t last = GetLast();
1854514f5e3Sopenharmony_ci    TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
1864514f5e3Sopenharmony_ci    uint32_t capacity = elements->GetLength();
1874514f5e3Sopenharmony_ci    uint32_t index = first;
1884514f5e3Sopenharmony_ci    while (index != last) {
1894514f5e3Sopenharmony_ci        if (JSTaggedValue::SameValue(elements->Get(index), value)) {
1904514f5e3Sopenharmony_ci            return true;
1914514f5e3Sopenharmony_ci        }
1924514f5e3Sopenharmony_ci        ASSERT(capacity != 0);
1934514f5e3Sopenharmony_ci        index = (index + 1) % capacity;
1944514f5e3Sopenharmony_ci    }
1954514f5e3Sopenharmony_ci    return false;
1964514f5e3Sopenharmony_ci}
1974514f5e3Sopenharmony_ci
1984514f5e3Sopenharmony_ciJSHandle<TaggedArray> JSAPIDeque::OwnKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
1994514f5e3Sopenharmony_ci{
2004514f5e3Sopenharmony_ci    uint32_t length = deque->GetSize();
2014514f5e3Sopenharmony_ci
2024514f5e3Sopenharmony_ci    JSHandle<TaggedArray> oldElements(thread, deque->GetElements());
2034514f5e3Sopenharmony_ci    ASSERT(!oldElements->IsDictionaryMode());
2044514f5e3Sopenharmony_ci    uint32_t oldCapacity = oldElements->GetLength();
2054514f5e3Sopenharmony_ci    uint32_t newCapacity = ComputeCapacity(oldCapacity);
2064514f5e3Sopenharmony_ci    uint32_t firstIndex = deque->GetFirst();
2074514f5e3Sopenharmony_ci    uint32_t lastIndex = deque->GetLast();
2084514f5e3Sopenharmony_ci    JSHandle<TaggedArray> newElements =
2094514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, length, firstIndex, lastIndex);
2104514f5e3Sopenharmony_ci    deque->SetFirst(0);
2114514f5e3Sopenharmony_ci    deque->SetLast(length);
2124514f5e3Sopenharmony_ci    deque->SetElements(thread, newElements);
2134514f5e3Sopenharmony_ci
2144514f5e3Sopenharmony_ci    return JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(deque));
2154514f5e3Sopenharmony_ci}
2164514f5e3Sopenharmony_ci
2174514f5e3Sopenharmony_ciJSHandle<TaggedArray> JSAPIDeque::OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
2184514f5e3Sopenharmony_ci{
2194514f5e3Sopenharmony_ci    uint32_t length = deque->GetSize();
2204514f5e3Sopenharmony_ci
2214514f5e3Sopenharmony_ci    JSHandle<TaggedArray> oldElements(thread, deque->GetElements());
2224514f5e3Sopenharmony_ci    ASSERT(!oldElements->IsDictionaryMode());
2234514f5e3Sopenharmony_ci    uint32_t oldCapacity = oldElements->GetLength();
2244514f5e3Sopenharmony_ci    uint32_t newCapacity = ComputeCapacity(oldCapacity);
2254514f5e3Sopenharmony_ci    uint32_t firstIndex = deque->GetFirst();
2264514f5e3Sopenharmony_ci    uint32_t lastIndex = deque->GetLast();
2274514f5e3Sopenharmony_ci    JSHandle<TaggedArray> newElements =
2284514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, length, firstIndex, lastIndex);
2294514f5e3Sopenharmony_ci    deque->SetFirst(0);
2304514f5e3Sopenharmony_ci    deque->SetLast(length);
2314514f5e3Sopenharmony_ci    deque->SetElements(thread, newElements);
2324514f5e3Sopenharmony_ci
2334514f5e3Sopenharmony_ci    return JSObject::GetOwnEnumPropertyKeys(thread, JSHandle<JSObject>::Cast(deque));
2344514f5e3Sopenharmony_ci}
2354514f5e3Sopenharmony_ci
2364514f5e3Sopenharmony_cibool JSAPIDeque::GetOwnProperty(JSThread *thread, const JSHandle<JSAPIDeque> &deque,
2374514f5e3Sopenharmony_ci                                const JSHandle<JSTaggedValue> &key)
2384514f5e3Sopenharmony_ci{
2394514f5e3Sopenharmony_ci    uint32_t index = 0;
2404514f5e3Sopenharmony_ci    if (UNLIKELY(!JSTaggedValue::ToElementIndex(key.GetTaggedValue(), &index))) {
2414514f5e3Sopenharmony_ci        JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
2424514f5e3Sopenharmony_ci        RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
2434514f5e3Sopenharmony_ci        CString errorMsg =
2444514f5e3Sopenharmony_ci            "The type of \"key\" can not obtain attributes of no-number type. Received value is: "
2454514f5e3Sopenharmony_ci            + ConvertToString(*result);
2464514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
2474514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
2484514f5e3Sopenharmony_ci    }
2494514f5e3Sopenharmony_ci
2504514f5e3Sopenharmony_ci    uint32_t length = deque->GetSize();
2514514f5e3Sopenharmony_ci    if (index >= length) {
2524514f5e3Sopenharmony_ci        ASSERT(length > 0);
2534514f5e3Sopenharmony_ci        std::ostringstream oss;
2544514f5e3Sopenharmony_ci        oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
2554514f5e3Sopenharmony_ci            << ". Received value is: " << index;
2564514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
2574514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
2584514f5e3Sopenharmony_ci    }
2594514f5e3Sopenharmony_ci
2604514f5e3Sopenharmony_ci    deque->Get(index);
2614514f5e3Sopenharmony_ci    return true;
2624514f5e3Sopenharmony_ci}
2634514f5e3Sopenharmony_ci
2644514f5e3Sopenharmony_ciJSTaggedValue JSAPIDeque::GetIteratorObj(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
2654514f5e3Sopenharmony_ci{
2664514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2674514f5e3Sopenharmony_ci    JSHandle<JSAPIDequeIterator> iter(factory->NewJSAPIDequeIterator(deque));
2684514f5e3Sopenharmony_ci
2694514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
2704514f5e3Sopenharmony_ci}
2714514f5e3Sopenharmony_ci
2724514f5e3Sopenharmony_ciOperationResult JSAPIDeque::GetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj,
2734514f5e3Sopenharmony_ci                                        const JSHandle<JSTaggedValue> &key)
2744514f5e3Sopenharmony_ci{
2754514f5e3Sopenharmony_ci    int length = static_cast<int>(obj->GetSize());
2764514f5e3Sopenharmony_ci    if (length == 0) {
2774514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
2784514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
2794514f5e3Sopenharmony_ci                                                                        JSTaggedValue::Exception(),
2804514f5e3Sopenharmony_ci                                                                        PropertyMetaData(false)));
2814514f5e3Sopenharmony_ci    }
2824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> indexKey = key;
2834514f5e3Sopenharmony_ci    if (indexKey->IsDouble()) {
2844514f5e3Sopenharmony_ci        // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
2854514f5e3Sopenharmony_ci        // For integer which is greater than INT32_MAX, it will remain TaggedDouble
2864514f5e3Sopenharmony_ci        indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
2874514f5e3Sopenharmony_ci    }
2884514f5e3Sopenharmony_ci    if (!indexKey->IsInt()) {
2894514f5e3Sopenharmony_ci        CString errorMsg = "The type of \"index\" must be small integer.";
2904514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
2914514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
2924514f5e3Sopenharmony_ci                                         OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
2934514f5e3Sopenharmony_ci    }
2944514f5e3Sopenharmony_ci
2954514f5e3Sopenharmony_ci    int index = indexKey->GetInt();
2964514f5e3Sopenharmony_ci    if (index < 0 || index >= length) {
2974514f5e3Sopenharmony_ci        std::ostringstream oss;
2984514f5e3Sopenharmony_ci        oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
2994514f5e3Sopenharmony_ci            << ". Received value is: " << index;
3004514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
3014514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
3024514f5e3Sopenharmony_ci                                                                        JSTaggedValue::Exception(),
3034514f5e3Sopenharmony_ci                                                                        PropertyMetaData(false)));
3044514f5e3Sopenharmony_ci    }
3054514f5e3Sopenharmony_ci
3064514f5e3Sopenharmony_ci    return OperationResult(thread, obj->Get(index), PropertyMetaData(false));
3074514f5e3Sopenharmony_ci}
3084514f5e3Sopenharmony_ci
3094514f5e3Sopenharmony_cibool JSAPIDeque::SetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj,
3104514f5e3Sopenharmony_ci                             const JSHandle<JSTaggedValue> &key,
3114514f5e3Sopenharmony_ci                             const JSHandle<JSTaggedValue> &value)
3124514f5e3Sopenharmony_ci{
3134514f5e3Sopenharmony_ci    int length = static_cast<int>(obj->GetSize());
3144514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> indexKey = key;
3154514f5e3Sopenharmony_ci    if (indexKey->IsDouble()) {
3164514f5e3Sopenharmony_ci        // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
3174514f5e3Sopenharmony_ci        // For integer which is greater than INT32_MAX, it will remain TaggedDouble
3184514f5e3Sopenharmony_ci        indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
3194514f5e3Sopenharmony_ci    }
3204514f5e3Sopenharmony_ci    if (!indexKey->IsInt()) {
3214514f5e3Sopenharmony_ci        return false;
3224514f5e3Sopenharmony_ci    }
3234514f5e3Sopenharmony_ci
3244514f5e3Sopenharmony_ci    int index = indexKey->GetInt();
3254514f5e3Sopenharmony_ci    if (index < 0 || index >= length) {
3264514f5e3Sopenharmony_ci        return false;
3274514f5e3Sopenharmony_ci    }
3284514f5e3Sopenharmony_ci
3294514f5e3Sopenharmony_ci    obj->Set(thread, index, value.GetTaggedValue());
3304514f5e3Sopenharmony_ci    return true;
3314514f5e3Sopenharmony_ci}
3324514f5e3Sopenharmony_ci} // namespace panda::ecmascript
333