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/containers/containers_linked_list.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_errors.h" 194514f5e3Sopenharmony_ci#include "ecmascript/interpreter/interpreter.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_linked_list.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_linked_list_iterator.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript::containers { 254514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::LinkedListConstructor(EcmaRuntimeCallInfo *argv) 264514f5e3Sopenharmony_ci{ 274514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 284514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Constructor); 304514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 314514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 334514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 344514f5e3Sopenharmony_ci JSTaggedValue error = 354514f5e3Sopenharmony_ci ContainerError::BusinessError(thread, ErrorFlag::IS_NULL_ERROR, 364514f5e3Sopenharmony_ci "The LinkedList's constructor cannot be directly invoked"); 374514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 384514f5e3Sopenharmony_ci } 394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 404514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 414514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 424514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> linkedList = JSHandle<JSAPILinkedList>::Cast(obj); 434514f5e3Sopenharmony_ci JSTaggedValue doubleList = TaggedDoubleList::Create(thread); 444514f5e3Sopenharmony_ci linkedList->SetDoubleList(thread, doubleList); 454514f5e3Sopenharmony_ci return linkedList.GetTaggedValue(); 464514f5e3Sopenharmony_ci} 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Add(EcmaRuntimeCallInfo *argv) 494514f5e3Sopenharmony_ci{ 504514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 514514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 524514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Add); 534514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 544514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 554514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 564514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 574514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 584514f5e3Sopenharmony_ci } else { 594514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 604514f5e3Sopenharmony_ci "The add method cannot be bound"); 614514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci } 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 664514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 674514f5e3Sopenharmony_ci JSAPILinkedList::Add(thread, jsAPILinkedList, value); 684514f5e3Sopenharmony_ci return JSTaggedValue::True(); 694514f5e3Sopenharmony_ci} 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::AddFirst(EcmaRuntimeCallInfo *argv) 724514f5e3Sopenharmony_ci{ 734514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 744514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 754514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, AddFirst); 764514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 784514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 794514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 804514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 814514f5e3Sopenharmony_ci } else { 824514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 834514f5e3Sopenharmony_ci "The addFirst method cannot be bound"); 844514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci } 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 894514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 904514f5e3Sopenharmony_ci JSAPILinkedList::AddFirst(thread, jsAPILinkedList, value); 914514f5e3Sopenharmony_ci return JSTaggedValue::True(); 924514f5e3Sopenharmony_ci} 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::GetFirst(EcmaRuntimeCallInfo *argv) 954514f5e3Sopenharmony_ci{ 964514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 974514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 984514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, GetFirst); 994514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1014514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 1024514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 1034514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1044514f5e3Sopenharmony_ci } else { 1054514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1064514f5e3Sopenharmony_ci "The getFirst method cannot be bound"); 1074514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1084514f5e3Sopenharmony_ci } 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 1114514f5e3Sopenharmony_ci return jsAPILinkedList->GetFirst(); 1124514f5e3Sopenharmony_ci} 1134514f5e3Sopenharmony_ci 1144514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::GetLast(EcmaRuntimeCallInfo *argv) 1154514f5e3Sopenharmony_ci{ 1164514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1174514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1184514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, GetLast); 1194514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1214514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 1224514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 1234514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1244514f5e3Sopenharmony_ci } else { 1254514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1264514f5e3Sopenharmony_ci "The getLast method cannot be bound"); 1274514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci } 1304514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 1314514f5e3Sopenharmony_ci return jsAPILinkedList->GetLast(); 1324514f5e3Sopenharmony_ci} 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Length(EcmaRuntimeCallInfo *argv) 1354514f5e3Sopenharmony_ci{ 1364514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1374514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1384514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Length); 1394514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1414514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 1424514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 1434514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1444514f5e3Sopenharmony_ci } else { 1454514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1464514f5e3Sopenharmony_ci "The getLength method cannot be bound"); 1474514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1484514f5e3Sopenharmony_ci } 1494514f5e3Sopenharmony_ci } 1504514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 1514514f5e3Sopenharmony_ci return JSTaggedValue(jsAPILinkedList->Length()); 1524514f5e3Sopenharmony_ci} 1534514f5e3Sopenharmony_ci 1544514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Insert(EcmaRuntimeCallInfo *argv) 1554514f5e3Sopenharmony_ci{ 1564514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1574514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1584514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Insert); 1594514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1614514f5e3Sopenharmony_ci 1624514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 1634514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 1644514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1654514f5e3Sopenharmony_ci } else { 1664514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1674514f5e3Sopenharmony_ci "The insert method cannot be bound"); 1684514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1694514f5e3Sopenharmony_ci } 1704514f5e3Sopenharmony_ci } 1714514f5e3Sopenharmony_ci 1724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 1); 1734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index = GetCallArg(argv, 0); 1744514f5e3Sopenharmony_ci 1754514f5e3Sopenharmony_ci if (index->IsDouble()) { 1764514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 1774514f5e3Sopenharmony_ci } 1784514f5e3Sopenharmony_ci if (!index->IsInt()) { 1794514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue()); 1804514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1814514f5e3Sopenharmony_ci CString errorMsg = 1824514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 1834514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 1844514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1854514f5e3Sopenharmony_ci } 1864514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 1874514f5e3Sopenharmony_ci JSTaggedValue result = 1884514f5e3Sopenharmony_ci JSAPILinkedList::Insert(thread, jsAPILinkedList, value, index->GetInt()); 1894514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1904514f5e3Sopenharmony_ci return result; 1914514f5e3Sopenharmony_ci} 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Clear(EcmaRuntimeCallInfo *argv) 1944514f5e3Sopenharmony_ci{ 1954514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1964514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1974514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Clear); 1984514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1994514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2004514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 2014514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 2024514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2034514f5e3Sopenharmony_ci } else { 2044514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2054514f5e3Sopenharmony_ci "The clear method cannot be bound"); 2064514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2074514f5e3Sopenharmony_ci } 2084514f5e3Sopenharmony_ci } 2094514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 2104514f5e3Sopenharmony_ci jsAPILinkedList->Clear(thread); 2114514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 2124514f5e3Sopenharmony_ci} 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Clone(EcmaRuntimeCallInfo *argv) 2154514f5e3Sopenharmony_ci{ 2164514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2174514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2184514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Clone); 2194514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2214514f5e3Sopenharmony_ci 2224514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 2234514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 2244514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2254514f5e3Sopenharmony_ci } else { 2264514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2274514f5e3Sopenharmony_ci "The clone method cannot be bound"); 2284514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2294514f5e3Sopenharmony_ci } 2304514f5e3Sopenharmony_ci } 2314514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 2324514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> newLinkedList = JSAPILinkedList::Clone(thread, jsAPILinkedList); 2334514f5e3Sopenharmony_ci return newLinkedList.GetTaggedValue(); 2344514f5e3Sopenharmony_ci} 2354514f5e3Sopenharmony_ci 2364514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Has(EcmaRuntimeCallInfo *argv) 2374514f5e3Sopenharmony_ci{ 2384514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2394514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2404514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Has); 2414514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2434514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 2444514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 2454514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2464514f5e3Sopenharmony_ci } else { 2474514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2484514f5e3Sopenharmony_ci "The has method cannot be bound"); 2494514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2504514f5e3Sopenharmony_ci } 2514514f5e3Sopenharmony_ci } 2524514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 2534514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 0); 2544514f5e3Sopenharmony_ci return GetTaggedBoolean(jsAPILinkedList->Has(element.GetTaggedValue())); 2554514f5e3Sopenharmony_ci} 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Get(EcmaRuntimeCallInfo *argv) 2584514f5e3Sopenharmony_ci{ 2594514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2604514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2614514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Get); 2624514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2634514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2644514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 2654514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 2664514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2674514f5e3Sopenharmony_ci } else { 2684514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2694514f5e3Sopenharmony_ci "The get method cannot be bound"); 2704514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2714514f5e3Sopenharmony_ci } 2724514f5e3Sopenharmony_ci } 2734514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 2744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index = GetCallArg(argv, 0); 2754514f5e3Sopenharmony_ci if (index->IsDouble()) { 2764514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 2774514f5e3Sopenharmony_ci } 2784514f5e3Sopenharmony_ci if (!index->IsInt()) { 2794514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue()); 2804514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 2814514f5e3Sopenharmony_ci CString errorMsg = 2824514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 2834514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 2844514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2854514f5e3Sopenharmony_ci } 2864514f5e3Sopenharmony_ci return jsAPILinkedList->Get(index->GetInt()); 2874514f5e3Sopenharmony_ci} 2884514f5e3Sopenharmony_ci 2894514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::GetIndexOf(EcmaRuntimeCallInfo *argv) 2904514f5e3Sopenharmony_ci{ 2914514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2924514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2934514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, GetIndexOf); 2944514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2964514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 2974514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 2984514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2994514f5e3Sopenharmony_ci } else { 3004514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3014514f5e3Sopenharmony_ci "The getIndexOf method cannot be bound"); 3024514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3034514f5e3Sopenharmony_ci } 3044514f5e3Sopenharmony_ci } 3054514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 3064514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 0); 3074514f5e3Sopenharmony_ci return jsAPILinkedList->GetIndexOf(element.GetTaggedValue()); 3084514f5e3Sopenharmony_ci} 3094514f5e3Sopenharmony_ci 3104514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::GetLastIndexOf(EcmaRuntimeCallInfo *argv) 3114514f5e3Sopenharmony_ci{ 3124514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3134514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3144514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, GetLastIndexOf); 3154514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3164514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3174514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 3184514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 3194514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3204514f5e3Sopenharmony_ci } else { 3214514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3224514f5e3Sopenharmony_ci "The getLastIndexOf method cannot be bound"); 3234514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3244514f5e3Sopenharmony_ci } 3254514f5e3Sopenharmony_ci } 3264514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 3274514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element(GetCallArg(argv, 0)); 3284514f5e3Sopenharmony_ci return jsAPILinkedList->GetLastIndexOf(element.GetTaggedValue()); 3294514f5e3Sopenharmony_ci} 3304514f5e3Sopenharmony_ci 3314514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::RemoveByIndex(EcmaRuntimeCallInfo *argv) 3324514f5e3Sopenharmony_ci{ 3334514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3344514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3354514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, RemoveByIndex); 3364514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3384514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 3394514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 3404514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3414514f5e3Sopenharmony_ci } else { 3424514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3434514f5e3Sopenharmony_ci "The removeByIndex method cannot be bound"); 3444514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3454514f5e3Sopenharmony_ci } 3464514f5e3Sopenharmony_ci } 3474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index = GetCallArg(argv, 0); 3484514f5e3Sopenharmony_ci if (index->IsDouble()) { 3494514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 3504514f5e3Sopenharmony_ci } 3514514f5e3Sopenharmony_ci if (!index->IsInt()) { 3524514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue()); 3534514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3544514f5e3Sopenharmony_ci CString errorMsg = 3554514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 3564514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3574514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3584514f5e3Sopenharmony_ci } 3594514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 3604514f5e3Sopenharmony_ci JSTaggedValue nodeData = 3614514f5e3Sopenharmony_ci JSAPILinkedList::RemoveByIndex(thread, jsAPILinkedList, index->GetInt()); 3624514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3634514f5e3Sopenharmony_ci return nodeData; 3644514f5e3Sopenharmony_ci} 3654514f5e3Sopenharmony_ci 3664514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Remove(EcmaRuntimeCallInfo *argv) 3674514f5e3Sopenharmony_ci{ 3684514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3694514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3704514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Remove); 3714514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3734514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 3744514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 3754514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3764514f5e3Sopenharmony_ci } else { 3774514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3784514f5e3Sopenharmony_ci "The remove method cannot be bound"); 3794514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3804514f5e3Sopenharmony_ci } 3814514f5e3Sopenharmony_ci } 3824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 0); 3834514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 3844514f5e3Sopenharmony_ci return jsAPILinkedList->Remove(thread, element.GetTaggedValue()); 3854514f5e3Sopenharmony_ci} 3864514f5e3Sopenharmony_ci 3874514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::RemoveFirst(EcmaRuntimeCallInfo *argv) 3884514f5e3Sopenharmony_ci{ 3894514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3904514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3914514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, RemoveFirst); 3924514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3934514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3944514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 3954514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 3964514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3974514f5e3Sopenharmony_ci } else { 3984514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3994514f5e3Sopenharmony_ci "The removeFirst method cannot be bound"); 4004514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4014514f5e3Sopenharmony_ci } 4024514f5e3Sopenharmony_ci } 4034514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 4044514f5e3Sopenharmony_ci JSTaggedValue lastValue = JSAPILinkedList::RemoveFirst(thread, jsAPILinkedList); 4054514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4064514f5e3Sopenharmony_ci return lastValue; 4074514f5e3Sopenharmony_ci} 4084514f5e3Sopenharmony_ci 4094514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::RemoveFirstFound(EcmaRuntimeCallInfo *argv) 4104514f5e3Sopenharmony_ci{ 4114514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4124514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4134514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, RemoveFirstFound); 4144514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4164514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 4174514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 4184514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4194514f5e3Sopenharmony_ci } else { 4204514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4214514f5e3Sopenharmony_ci "The removeFirstFound method cannot be bound"); 4224514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4234514f5e3Sopenharmony_ci } 4244514f5e3Sopenharmony_ci } 4254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 0); 4264514f5e3Sopenharmony_ci 4274514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 4284514f5e3Sopenharmony_ci JSTaggedValue result = JSAPILinkedList::RemoveFirstFound(thread, jsAPILinkedList, element.GetTaggedValue()); 4294514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4304514f5e3Sopenharmony_ci return result; 4314514f5e3Sopenharmony_ci} 4324514f5e3Sopenharmony_ci 4334514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::RemoveLast(EcmaRuntimeCallInfo *argv) 4344514f5e3Sopenharmony_ci{ 4354514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4364514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4374514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, RemoveLast); 4384514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4404514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 4414514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 4424514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4434514f5e3Sopenharmony_ci } else { 4444514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4454514f5e3Sopenharmony_ci "The removeLast method cannot be bound"); 4464514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4474514f5e3Sopenharmony_ci } 4484514f5e3Sopenharmony_ci } 4494514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 4504514f5e3Sopenharmony_ci JSTaggedValue lastValue = JSAPILinkedList::RemoveLast(thread, jsAPILinkedList); 4514514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4524514f5e3Sopenharmony_ci return lastValue; 4534514f5e3Sopenharmony_ci} 4544514f5e3Sopenharmony_ci 4554514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::RemoveLastFound(EcmaRuntimeCallInfo *argv) 4564514f5e3Sopenharmony_ci{ 4574514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4584514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4594514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, RemoveLastFound); 4604514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4614514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4624514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 4634514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 4644514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4654514f5e3Sopenharmony_ci } else { 4664514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4674514f5e3Sopenharmony_ci "The removeLastFound method cannot be bound"); 4684514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4694514f5e3Sopenharmony_ci } 4704514f5e3Sopenharmony_ci } 4714514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 0); 4724514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 4734514f5e3Sopenharmony_ci JSTaggedValue result = JSAPILinkedList::RemoveLastFound(thread, jsAPILinkedList, element.GetTaggedValue()); 4744514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4754514f5e3Sopenharmony_ci return result; 4764514f5e3Sopenharmony_ci} 4774514f5e3Sopenharmony_ci 4784514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::Set(EcmaRuntimeCallInfo *argv) 4794514f5e3Sopenharmony_ci{ 4804514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4814514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4824514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, Set); 4834514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4854514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 4864514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 4874514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4884514f5e3Sopenharmony_ci } else { 4894514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4904514f5e3Sopenharmony_ci "The set method cannot be bound"); 4914514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4924514f5e3Sopenharmony_ci } 4934514f5e3Sopenharmony_ci } 4944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index = GetCallArg(argv, 0); 4954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> element = GetCallArg(argv, 1); 4964514f5e3Sopenharmony_ci if (index->IsDouble()) { 4974514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 4984514f5e3Sopenharmony_ci } 4994514f5e3Sopenharmony_ci if (!index->IsInt()) { 5004514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue()); 5014514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5024514f5e3Sopenharmony_ci CString errorMsg = 5034514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 5044514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 5054514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5064514f5e3Sopenharmony_ci } 5074514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 5084514f5e3Sopenharmony_ci JSTaggedValue oldValue = 5094514f5e3Sopenharmony_ci JSAPILinkedList::Set(thread, jsAPILinkedList, index->GetInt(), element); 5104514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5114514f5e3Sopenharmony_ci return oldValue; 5124514f5e3Sopenharmony_ci} 5134514f5e3Sopenharmony_ci 5144514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::ConvertToArray(EcmaRuntimeCallInfo *argv) 5154514f5e3Sopenharmony_ci{ 5164514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5174514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5184514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, ConvertToArray); 5194514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5214514f5e3Sopenharmony_ci if (!self->IsJSAPILinkedList()) { 5224514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) { 5234514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5244514f5e3Sopenharmony_ci } else { 5254514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5264514f5e3Sopenharmony_ci "The convertToArray method cannot be bound"); 5274514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5284514f5e3Sopenharmony_ci } 5294514f5e3Sopenharmony_ci } 5304514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self); 5314514f5e3Sopenharmony_ci return JSAPILinkedList::ConvertToArray(thread, jsAPILinkedList); 5324514f5e3Sopenharmony_ci} 5334514f5e3Sopenharmony_ci 5344514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::ForEach(EcmaRuntimeCallInfo *argv) 5354514f5e3Sopenharmony_ci{ 5364514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5374514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5384514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, ForEach); 5394514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisHandle = GetThis(argv); 5414514f5e3Sopenharmony_ci if (!thisHandle->IsJSAPILinkedList()) { 5424514f5e3Sopenharmony_ci if (thisHandle->IsJSProxy() && JSHandle<JSProxy>::Cast(thisHandle)->GetTarget().IsJSAPILinkedList()) { 5434514f5e3Sopenharmony_ci thisHandle = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(thisHandle)->GetTarget()); 5444514f5e3Sopenharmony_ci } else { 5454514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5464514f5e3Sopenharmony_ci "The forEach method cannot be bound"); 5474514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5484514f5e3Sopenharmony_ci } 5494514f5e3Sopenharmony_ci } 5504514f5e3Sopenharmony_ci 5514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> callbackFnHandle(GetCallArg(argv, 0)); 5524514f5e3Sopenharmony_ci if (!callbackFnHandle->IsCallable()) { 5534514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, callbackFnHandle.GetTaggedValue()); 5544514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5554514f5e3Sopenharmony_ci CString errorMsg = 5564514f5e3Sopenharmony_ci "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result); 5574514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 5584514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5594514f5e3Sopenharmony_ci } 5604514f5e3Sopenharmony_ci 5614514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1); 5624514f5e3Sopenharmony_ci JSHandle<JSAPILinkedList> linkedList = JSHandle<JSAPILinkedList>::Cast(thisHandle); 5634514f5e3Sopenharmony_ci JSHandle<TaggedDoubleList> doubleList(thread, linkedList->GetDoubleList()); 5644514f5e3Sopenharmony_ci uint32_t length = linkedList->Length(); 5654514f5e3Sopenharmony_ci 5664514f5e3Sopenharmony_ci uint32_t index = 0; 5674514f5e3Sopenharmony_ci const uint32_t argsLength = 3; // 3: «kValue, k, O» 5684514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 5694514f5e3Sopenharmony_ci int valueNode = TaggedDoubleList::ELEMENTS_START_INDEX; 5704514f5e3Sopenharmony_ci while (index < length) { 5714514f5e3Sopenharmony_ci valueNode = doubleList->GetNextDataIndex(valueNode); 5724514f5e3Sopenharmony_ci JSTaggedValue value = doubleList->GetElement(valueNode); 5734514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *info = 5744514f5e3Sopenharmony_ci EcmaInterpreter::NewRuntimeCallInfo(thread, callbackFnHandle, thisArgHandle, undefined, argsLength); 5754514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5764514f5e3Sopenharmony_ci info->SetCallArg(value, JSTaggedValue(index), thisHandle.GetTaggedValue()); 5774514f5e3Sopenharmony_ci JSTaggedValue funcResult = JSFunction::Call(info); 5784514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, funcResult); 5794514f5e3Sopenharmony_ci index++; 5804514f5e3Sopenharmony_ci } 5814514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 5824514f5e3Sopenharmony_ci} 5834514f5e3Sopenharmony_ci 5844514f5e3Sopenharmony_ciJSTaggedValue ContainersLinkedList::GetIteratorObj(EcmaRuntimeCallInfo *argv) 5854514f5e3Sopenharmony_ci{ 5864514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5874514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5884514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LinkedList, GetIteratorObj); 5894514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = JSAPILinkedListIterator::CreateLinkedListIterator(thread, self); 5924514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 5934514f5e3Sopenharmony_ci} 5944514f5e3Sopenharmony_ci} // namespace panda::ecmascript::containers 595