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_plainarray.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_errors.h" 194514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_plain_array.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_iterator.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_cinamespace panda::ecmascript::containers { 234514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::PlainArrayConstructor(EcmaRuntimeCallInfo *argv) 244514f5e3Sopenharmony_ci{ 254514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 264514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 274514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Constructor); 284514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 294514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 304514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 314514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 324514f5e3Sopenharmony_ci JSTaggedValue error = 334514f5e3Sopenharmony_ci ContainerError::BusinessError(thread, ErrorFlag::IS_NULL_ERROR, 344514f5e3Sopenharmony_ci "The PlainArray's constructor cannot be directly invoked"); 354514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 384514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 394514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 404514f5e3Sopenharmony_ci JSHandle<JSAPIPlainArray> plainArray = JSHandle<JSAPIPlainArray>::Cast(obj); 414514f5e3Sopenharmony_ci JSHandle<TaggedArray> keys = 424514f5e3Sopenharmony_ci JSAPIPlainArray::CreateSlot(thread, JSAPIPlainArray::DEFAULT_CAPACITY_LENGTH); 434514f5e3Sopenharmony_ci JSHandle<TaggedArray> values = 444514f5e3Sopenharmony_ci JSAPIPlainArray::CreateSlot(thread, JSAPIPlainArray::DEFAULT_CAPACITY_LENGTH); 454514f5e3Sopenharmony_ci plainArray->SetKeys(thread, keys); 464514f5e3Sopenharmony_ci plainArray->SetValues(thread, values); 474514f5e3Sopenharmony_ci return obj.GetTaggedValue(); 484514f5e3Sopenharmony_ci} 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Add(EcmaRuntimeCallInfo *argv) 514514f5e3Sopenharmony_ci{ 524514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 534514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 544514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Add); 554514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 564514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 574514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 584514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 594514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 604514f5e3Sopenharmony_ci } else { 614514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 624514f5e3Sopenharmony_ci "The add method cannot be bound"); 634514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci } 664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 1)); 684514f5e3Sopenharmony_ci if (key->IsDouble()) { 694514f5e3Sopenharmony_ci key = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(key->GetDouble())); 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci if (!key->IsInt()) { 724514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue()); 734514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 744514f5e3Sopenharmony_ci CString errorMsg = 754514f5e3Sopenharmony_ci "The type of \"key\" must be small integer. Received value is: " + ConvertToString(*result); 764514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 774514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 784514f5e3Sopenharmony_ci } 794514f5e3Sopenharmony_ci JSAPIPlainArray::Add(thread, JSHandle<JSAPIPlainArray>::Cast(self), key, value); 804514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 814514f5e3Sopenharmony_ci} 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Clear(EcmaRuntimeCallInfo *argv) 844514f5e3Sopenharmony_ci{ 854514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 864514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 874514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Clear); 884514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 904514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 914514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 924514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 934514f5e3Sopenharmony_ci } else { 944514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 954514f5e3Sopenharmony_ci "The clear method cannot be bound"); 964514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 974514f5e3Sopenharmony_ci } 984514f5e3Sopenharmony_ci } 994514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 1004514f5e3Sopenharmony_ci array->Clear(thread); 1014514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 1024514f5e3Sopenharmony_ci} 1034514f5e3Sopenharmony_ci 1044514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Clone(EcmaRuntimeCallInfo *argv) 1054514f5e3Sopenharmony_ci{ 1064514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1074514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1084514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Clone); 1094514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1114514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 1124514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 1134514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1144514f5e3Sopenharmony_ci } else { 1154514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1164514f5e3Sopenharmony_ci "The clone method cannot be bound"); 1174514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1184514f5e3Sopenharmony_ci } 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci JSHandle<JSAPIPlainArray> newPlainArray = 1214514f5e3Sopenharmony_ci JSAPIPlainArray::Clone(thread, JSHandle<JSAPIPlainArray>::Cast(self)); 1224514f5e3Sopenharmony_ci return newPlainArray.GetTaggedValue(); 1234514f5e3Sopenharmony_ci} 1244514f5e3Sopenharmony_ci 1254514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Has(EcmaRuntimeCallInfo *argv) 1264514f5e3Sopenharmony_ci{ 1274514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1284514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Has); 1304514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1314514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1324514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 1334514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 1344514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1354514f5e3Sopenharmony_ci } else { 1364514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1374514f5e3Sopenharmony_ci "The has method cannot be bound"); 1384514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1394514f5e3Sopenharmony_ci } 1404514f5e3Sopenharmony_ci } 1414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 1424514f5e3Sopenharmony_ci if (value->IsDouble()) { 1434514f5e3Sopenharmony_ci value = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(value->GetDouble())); 1444514f5e3Sopenharmony_ci } 1454514f5e3Sopenharmony_ci if (!value->IsInt()) { 1464514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue()); 1474514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1484514f5e3Sopenharmony_ci CString errorMsg = 1494514f5e3Sopenharmony_ci "The type of \"key\" must be small integer. Received value is: " + ConvertToString(*result); 1504514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 1514514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1524514f5e3Sopenharmony_ci } 1534514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 1544514f5e3Sopenharmony_ci int32_t key = value->GetNumber(); 1554514f5e3Sopenharmony_ci bool result = array->Has(key); 1564514f5e3Sopenharmony_ci return JSTaggedValue(result); 1574514f5e3Sopenharmony_ci} 1584514f5e3Sopenharmony_ci 1594514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Get(EcmaRuntimeCallInfo *argv) 1604514f5e3Sopenharmony_ci{ 1614514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1624514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1634514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Get); 1644514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1654514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1664514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 1674514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 1684514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1694514f5e3Sopenharmony_ci } else { 1704514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1714514f5e3Sopenharmony_ci "The get method cannot be bound"); 1724514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1734514f5e3Sopenharmony_ci } 1744514f5e3Sopenharmony_ci } 1754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 1764514f5e3Sopenharmony_ci if (key->IsDouble()) { 1774514f5e3Sopenharmony_ci key = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(key->GetDouble())); 1784514f5e3Sopenharmony_ci } 1794514f5e3Sopenharmony_ci if (!key->IsInt()) { 1804514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue()); 1814514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1824514f5e3Sopenharmony_ci CString errorMsg = 1834514f5e3Sopenharmony_ci "The type of \"key\" must be small integer. Received value is: " + ConvertToString(*result); 1844514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 1854514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1864514f5e3Sopenharmony_ci } 1874514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 1884514f5e3Sopenharmony_ci JSTaggedValue value = array->Get(key.GetTaggedValue()); 1894514f5e3Sopenharmony_ci 1904514f5e3Sopenharmony_ci return value; 1914514f5e3Sopenharmony_ci} 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetIteratorObj(EcmaRuntimeCallInfo *argv) 1944514f5e3Sopenharmony_ci{ 1954514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1964514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1974514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetIteratorObj); 1984514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1994514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2004514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 2014514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 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 Symbol.iterator method cannot be bound"); 2064514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2074514f5e3Sopenharmony_ci } 2084514f5e3Sopenharmony_ci } 2094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = 2104514f5e3Sopenharmony_ci JSAPIPlainArray::GetIteratorObj(thread, JSHandle<JSAPIPlainArray>::Cast(self), IterationKind::KEY_AND_VALUE); 2114514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 2124514f5e3Sopenharmony_ci} 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::ForEach(EcmaRuntimeCallInfo *argv) 2154514f5e3Sopenharmony_ci{ 2164514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2174514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2184514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, ForEach); 2194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisHandle = GetThis(argv); 2204514f5e3Sopenharmony_ci if (!thisHandle->IsJSAPIPlainArray()) { 2214514f5e3Sopenharmony_ci if (thisHandle->IsJSProxy() && JSHandle<JSProxy>::Cast(thisHandle)->GetTarget().IsJSAPIPlainArray()) { 2224514f5e3Sopenharmony_ci thisHandle = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(thisHandle)->GetTarget()); 2234514f5e3Sopenharmony_ci } else { 2244514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2254514f5e3Sopenharmony_ci "The forEach method cannot be bound"); 2264514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2274514f5e3Sopenharmony_ci } 2284514f5e3Sopenharmony_ci } 2294514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> callbackFnHandle = GetCallArg(argv, 0); 2304514f5e3Sopenharmony_ci if (!callbackFnHandle->IsCallable()) { 2314514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, callbackFnHandle.GetTaggedValue()); 2324514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 2334514f5e3Sopenharmony_ci CString errorMsg = 2344514f5e3Sopenharmony_ci "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result); 2354514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 2364514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2374514f5e3Sopenharmony_ci } 2384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1); 2394514f5e3Sopenharmony_ci return JSAPIPlainArray::ForEach(thread, thisHandle, callbackFnHandle, thisArgHandle); 2404514f5e3Sopenharmony_ci} 2414514f5e3Sopenharmony_ci 2424514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::ToString(EcmaRuntimeCallInfo *argv) 2434514f5e3Sopenharmony_ci{ 2444514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2454514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2464514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, ToString); 2474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2484514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 2494514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 2504514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2514514f5e3Sopenharmony_ci } else { 2524514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2534514f5e3Sopenharmony_ci "The toString method cannot be bound"); 2544514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2554514f5e3Sopenharmony_ci } 2564514f5e3Sopenharmony_ci } 2574514f5e3Sopenharmony_ci JSTaggedValue value = JSAPIPlainArray::ToString(thread, JSHandle<JSAPIPlainArray>::Cast(self)); 2584514f5e3Sopenharmony_ci return value; 2594514f5e3Sopenharmony_ci} 2604514f5e3Sopenharmony_ci 2614514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetIndexOfKey(EcmaRuntimeCallInfo *argv) 2624514f5e3Sopenharmony_ci{ 2634514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2644514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2654514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetIndexOfKey); 2664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2674514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 2684514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 2694514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2704514f5e3Sopenharmony_ci } else { 2714514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2724514f5e3Sopenharmony_ci "The getIndexOfKey method cannot be bound"); 2734514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2744514f5e3Sopenharmony_ci } 2754514f5e3Sopenharmony_ci } 2764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 2774514f5e3Sopenharmony_ci if (value->IsDouble()) { 2784514f5e3Sopenharmony_ci value = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(value->GetDouble())); 2794514f5e3Sopenharmony_ci } 2804514f5e3Sopenharmony_ci if (!value->IsInt()) { 2814514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue()); 2824514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 2834514f5e3Sopenharmony_ci CString errorMsg = 2844514f5e3Sopenharmony_ci "The type of \"key\" must be small integer. Received value is: " + ConvertToString(*result); 2854514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 2864514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2874514f5e3Sopenharmony_ci } 2884514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 2894514f5e3Sopenharmony_ci int32_t key = value->GetNumber(); 2904514f5e3Sopenharmony_ci JSTaggedValue result = array->GetIndexOfKey(key); 2914514f5e3Sopenharmony_ci return result; 2924514f5e3Sopenharmony_ci} 2934514f5e3Sopenharmony_ci 2944514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetIndexOfValue(EcmaRuntimeCallInfo *argv) 2954514f5e3Sopenharmony_ci{ 2964514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2974514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2984514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetIndexOfValue); 2994514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3004514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 3014514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 3024514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3034514f5e3Sopenharmony_ci } else { 3044514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3054514f5e3Sopenharmony_ci "The getIndexOfValue method cannot be bound"); 3064514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3074514f5e3Sopenharmony_ci } 3084514f5e3Sopenharmony_ci } 3094514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 3104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 3114514f5e3Sopenharmony_ci JSTaggedValue jsValue = array->GetIndexOfValue(value.GetTaggedValue()); 3124514f5e3Sopenharmony_ci return jsValue; 3134514f5e3Sopenharmony_ci} 3144514f5e3Sopenharmony_ci 3154514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::IsEmpty(EcmaRuntimeCallInfo *argv) 3164514f5e3Sopenharmony_ci{ 3174514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3184514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3194514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, IsEmpty); 3204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3214514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 3224514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 3234514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3244514f5e3Sopenharmony_ci } else { 3254514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3264514f5e3Sopenharmony_ci "The isEmpty method cannot be bound"); 3274514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3284514f5e3Sopenharmony_ci } 3294514f5e3Sopenharmony_ci } 3304514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 3314514f5e3Sopenharmony_ci bool ret = array->IsEmpty(); 3324514f5e3Sopenharmony_ci return JSTaggedValue(ret); 3334514f5e3Sopenharmony_ci} 3344514f5e3Sopenharmony_ci 3354514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetKeyAt(EcmaRuntimeCallInfo *argv) 3364514f5e3Sopenharmony_ci{ 3374514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3384514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3394514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetKeyAt); 3404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3414514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 3424514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 3434514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3444514f5e3Sopenharmony_ci } else { 3454514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3464514f5e3Sopenharmony_ci "The getKeyAt method cannot be bound"); 3474514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3484514f5e3Sopenharmony_ci } 3494514f5e3Sopenharmony_ci } 3504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 3514514f5e3Sopenharmony_ci if (value->IsDouble()) { 3524514f5e3Sopenharmony_ci value = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(value->GetDouble())); 3534514f5e3Sopenharmony_ci } 3544514f5e3Sopenharmony_ci if (!value->IsInt()) { 3554514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue()); 3564514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3574514f5e3Sopenharmony_ci CString errorMsg = 3584514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 3594514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3604514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3614514f5e3Sopenharmony_ci } 3624514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 3634514f5e3Sopenharmony_ci int32_t index = value->GetNumber(); 3644514f5e3Sopenharmony_ci JSTaggedValue result = array->GetKeyAt(index); 3654514f5e3Sopenharmony_ci return result; 3664514f5e3Sopenharmony_ci} 3674514f5e3Sopenharmony_ci 3684514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::Remove(EcmaRuntimeCallInfo *argv) 3694514f5e3Sopenharmony_ci{ 3704514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3714514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3724514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, Remove); 3734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3744514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 3754514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 3764514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3774514f5e3Sopenharmony_ci } else { 3784514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3794514f5e3Sopenharmony_ci "The remove method cannot be bound"); 3804514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3814514f5e3Sopenharmony_ci } 3824514f5e3Sopenharmony_ci } 3834514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 3844514f5e3Sopenharmony_ci if (key->IsDouble()) { 3854514f5e3Sopenharmony_ci key = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(key->GetDouble())); 3864514f5e3Sopenharmony_ci } 3874514f5e3Sopenharmony_ci if (!key->IsInt()) { 3884514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue()); 3894514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3904514f5e3Sopenharmony_ci CString errorMsg = 3914514f5e3Sopenharmony_ci "The type of \"key\" must be small integer. Received value is: " + ConvertToString(*result); 3924514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3934514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3944514f5e3Sopenharmony_ci } 3954514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 3964514f5e3Sopenharmony_ci JSTaggedValue value = array->Remove(thread, key.GetTaggedValue()); 3974514f5e3Sopenharmony_ci return value; 3984514f5e3Sopenharmony_ci} 3994514f5e3Sopenharmony_ci 4004514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::RemoveAt(EcmaRuntimeCallInfo *argv) 4014514f5e3Sopenharmony_ci{ 4024514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4034514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4044514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, RemoveAt); 4054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4064514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 4074514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 4084514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4094514f5e3Sopenharmony_ci } else { 4104514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4114514f5e3Sopenharmony_ci "The removeAt method cannot be bound"); 4124514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4134514f5e3Sopenharmony_ci } 4144514f5e3Sopenharmony_ci } 4154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 4164514f5e3Sopenharmony_ci if (index->IsDouble()) { 4174514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 4184514f5e3Sopenharmony_ci } 4194514f5e3Sopenharmony_ci if (!index->IsInt()) { 4204514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue()); 4214514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4224514f5e3Sopenharmony_ci CString errorMsg = 4234514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 4244514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 4254514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4264514f5e3Sopenharmony_ci } 4274514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 4284514f5e3Sopenharmony_ci JSTaggedValue value = array->RemoveAt(thread, index.GetTaggedValue()); 4294514f5e3Sopenharmony_ci return value; 4304514f5e3Sopenharmony_ci} 4314514f5e3Sopenharmony_ci 4324514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::RemoveRangeFrom(EcmaRuntimeCallInfo *argv) 4334514f5e3Sopenharmony_ci{ 4344514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4354514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4364514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, RemoveRangeFrom); 4374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4384514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 4394514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 4404514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4414514f5e3Sopenharmony_ci } else { 4424514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4434514f5e3Sopenharmony_ci "The removeRangeFrom method cannot be bound"); 4444514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4454514f5e3Sopenharmony_ci } 4464514f5e3Sopenharmony_ci } 4474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueIndex(GetCallArg(argv, 0)); 4484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueSize(GetCallArg(argv, 1)); 4494514f5e3Sopenharmony_ci if (valueIndex->IsDouble()) { 4504514f5e3Sopenharmony_ci valueIndex = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(valueIndex->GetDouble())); 4514514f5e3Sopenharmony_ci } 4524514f5e3Sopenharmony_ci if (valueSize->IsDouble()) { 4534514f5e3Sopenharmony_ci valueSize = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(valueSize->GetDouble())); 4544514f5e3Sopenharmony_ci } 4554514f5e3Sopenharmony_ci if (!valueIndex->IsInt()) { 4564514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, valueIndex.GetTaggedValue()); 4574514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4584514f5e3Sopenharmony_ci CString errorMsg = 4594514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 4604514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 4614514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4624514f5e3Sopenharmony_ci } 4634514f5e3Sopenharmony_ci if (!valueSize->IsInt()) { 4644514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, valueSize.GetTaggedValue()); 4654514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4664514f5e3Sopenharmony_ci CString errorMsg = 4674514f5e3Sopenharmony_ci "The type of \"size\" must be small integer. Received value is: " + ConvertToString(*result); 4684514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 4694514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4704514f5e3Sopenharmony_ci } 4714514f5e3Sopenharmony_ci int32_t index = valueIndex->GetNumber(); 4724514f5e3Sopenharmony_ci int32_t size = valueSize->GetNumber(); 4734514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 4744514f5e3Sopenharmony_ci JSTaggedValue value = array->RemoveRangeFrom(thread, index, size); 4754514f5e3Sopenharmony_ci return value; 4764514f5e3Sopenharmony_ci} 4774514f5e3Sopenharmony_ci 4784514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::SetValueAt(EcmaRuntimeCallInfo *argv) 4794514f5e3Sopenharmony_ci{ 4804514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4814514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4824514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, SetValueAt); 4834514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4854514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 4864514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 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 setValueAt 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> value(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 JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 5084514f5e3Sopenharmony_ci array->SetValueAt(thread, index.GetTaggedValue(), value.GetTaggedValue()); 5094514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 5104514f5e3Sopenharmony_ci} 5114514f5e3Sopenharmony_ci 5124514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetValueAt(EcmaRuntimeCallInfo *argv) 5134514f5e3Sopenharmony_ci{ 5144514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5154514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5164514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetValueAt); 5174514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5194514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 5204514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 5214514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5224514f5e3Sopenharmony_ci } else { 5234514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5244514f5e3Sopenharmony_ci "The getValueAt method cannot be bound"); 5254514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5264514f5e3Sopenharmony_ci } 5274514f5e3Sopenharmony_ci } 5284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> idx(GetCallArg(argv, 0)); 5294514f5e3Sopenharmony_ci if (idx->IsDouble()) { 5304514f5e3Sopenharmony_ci idx = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(idx->GetDouble())); 5314514f5e3Sopenharmony_ci } 5324514f5e3Sopenharmony_ci if (!idx->IsInt()) { 5334514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, idx.GetTaggedValue()); 5344514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5354514f5e3Sopenharmony_ci CString errorMsg = 5364514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 5374514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 5384514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5394514f5e3Sopenharmony_ci } 5404514f5e3Sopenharmony_ci JSAPIPlainArray *array = JSAPIPlainArray::Cast(self->GetTaggedObject()); 5414514f5e3Sopenharmony_ci int32_t index = idx->GetNumber(); 5424514f5e3Sopenharmony_ci JSTaggedValue value = array->GetValueAt(thread, index); 5434514f5e3Sopenharmony_ci return value; 5444514f5e3Sopenharmony_ci} 5454514f5e3Sopenharmony_ci 5464514f5e3Sopenharmony_ciJSTaggedValue ContainersPlainArray::GetSize(EcmaRuntimeCallInfo *argv) 5474514f5e3Sopenharmony_ci{ 5484514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5494514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5504514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PlainArray, GetSize); 5514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5524514f5e3Sopenharmony_ci if (!self->IsJSAPIPlainArray()) { 5534514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIPlainArray()) { 5544514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5554514f5e3Sopenharmony_ci } else { 5564514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5574514f5e3Sopenharmony_ci "The getLength method cannot be bound"); 5584514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5594514f5e3Sopenharmony_ci } 5604514f5e3Sopenharmony_ci } 5614514f5e3Sopenharmony_ci uint32_t length = JSHandle<JSAPIPlainArray>::Cast(self)->GetSize(); 5624514f5e3Sopenharmony_ci return JSTaggedValue(length); 5634514f5e3Sopenharmony_ci} 5644514f5e3Sopenharmony_ci} // namespace panda::ecmascript::containers 565