14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 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_lightweightmap.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_lightweightmap.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_lightweightmap_iterator.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript::containers { 254514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::LightWeightMapConstructor(EcmaRuntimeCallInfo *argv) 264514f5e3Sopenharmony_ci{ 274514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 284514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, 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 LightWeightMap'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<JSAPILightWeightMap> lwMap = JSHandle<JSAPILightWeightMap>::Cast(obj); 434514f5e3Sopenharmony_ci JSHandle<TaggedArray> hashArray = factory->NewTaggedArray(JSAPILightWeightMap::DEFAULT_CAPACITY_LENGTH); 444514f5e3Sopenharmony_ci JSHandle<TaggedArray> keyArray = factory->NewTaggedArray(JSAPILightWeightMap::DEFAULT_CAPACITY_LENGTH); 454514f5e3Sopenharmony_ci JSHandle<TaggedArray> valueArray = factory->NewTaggedArray(JSAPILightWeightMap::DEFAULT_CAPACITY_LENGTH); 464514f5e3Sopenharmony_ci lwMap->SetHashes(thread, hashArray.GetTaggedValue()); 474514f5e3Sopenharmony_ci lwMap->SetKeys(thread, keyArray.GetTaggedValue()); 484514f5e3Sopenharmony_ci lwMap->SetValues(thread, valueArray.GetTaggedValue()); 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci return lwMap.GetTaggedValue(); 514514f5e3Sopenharmony_ci} 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Length(EcmaRuntimeCallInfo *argv) 544514f5e3Sopenharmony_ci{ 554514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 564514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 574514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Length); 584514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 594514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 624514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 634514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 644514f5e3Sopenharmony_ci } else { 654514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 664514f5e3Sopenharmony_ci "The getLength method cannot be bound"); 674514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 684514f5e3Sopenharmony_ci } 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci return JSTaggedValue(JSHandle<JSAPILightWeightMap>::Cast(self)->GetLength()); 724514f5e3Sopenharmony_ci} 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::HasAll(EcmaRuntimeCallInfo *argv) 754514f5e3Sopenharmony_ci{ 764514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 774514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 784514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, HasAll); 794514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 834514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 844514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 854514f5e3Sopenharmony_ci } else { 864514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 874514f5e3Sopenharmony_ci "The hasAll method cannot be bound"); 884514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci } 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lightWeightMap(GetCallArg(argv, 0)); 934514f5e3Sopenharmony_ci if (!lightWeightMap->IsJSAPILightWeightMap()) { 944514f5e3Sopenharmony_ci if (lightWeightMap->IsJSProxy() && 954514f5e3Sopenharmony_ci JSHandle<JSProxy>::Cast(lightWeightMap)->GetTarget().IsJSAPILightWeightMap()) { 964514f5e3Sopenharmony_ci lightWeightMap = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(lightWeightMap)->GetTarget()); 974514f5e3Sopenharmony_ci } else { 984514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, lightWeightMap.GetTaggedValue()); 994514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1004514f5e3Sopenharmony_ci CString errorMsg = 1014514f5e3Sopenharmony_ci "The type of \"map\" must be LightWeightMap. Received value is: " + ConvertToString(*result); 1024514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 1034514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1044514f5e3Sopenharmony_ci } 1054514f5e3Sopenharmony_ci } 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci return JSAPILightWeightMap::HasAll(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 1084514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap>::Cast(lightWeightMap)); 1094514f5e3Sopenharmony_ci} 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::HasKey(EcmaRuntimeCallInfo *argv) 1124514f5e3Sopenharmony_ci{ 1134514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1144514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1154514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, HasKey); 1164514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1184514f5e3Sopenharmony_ci 1194514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 1204514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 1214514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1224514f5e3Sopenharmony_ci } else { 1234514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1244514f5e3Sopenharmony_ci "The hasKey method cannot be bound"); 1254514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1264514f5e3Sopenharmony_ci } 1274514f5e3Sopenharmony_ci } 1284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci return JSAPILightWeightMap::HasKey(thread, JSHandle<JSAPILightWeightMap>::Cast(self), key); 1314514f5e3Sopenharmony_ci} 1324514f5e3Sopenharmony_ci 1334514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::HasValue(EcmaRuntimeCallInfo *argv) 1344514f5e3Sopenharmony_ci{ 1354514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1364514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1374514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, HasValue); 1384514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 1424514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 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 hasValue method cannot be bound"); 1474514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1484514f5e3Sopenharmony_ci } 1494514f5e3Sopenharmony_ci } 1504514f5e3Sopenharmony_ci 1514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 1524514f5e3Sopenharmony_ci return JSAPILightWeightMap::HasValue(thread, JSHandle<JSAPILightWeightMap>::Cast(self), value); 1534514f5e3Sopenharmony_ci} 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::IncreaseCapacityTo(EcmaRuntimeCallInfo *argv) 1564514f5e3Sopenharmony_ci{ 1574514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1584514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1594514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, IncreaseCapacityTo); 1604514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1614514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 1644514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 1654514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1664514f5e3Sopenharmony_ci } else { 1674514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1684514f5e3Sopenharmony_ci "The increaseCapacityTo method cannot be bound"); 1694514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1704514f5e3Sopenharmony_ci } 1714514f5e3Sopenharmony_ci } 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 1744514f5e3Sopenharmony_ci 1754514f5e3Sopenharmony_ci // for case like Math.foor(1.3), it gives double 1.0; 1764514f5e3Sopenharmony_ci if (index->IsDouble()) { 1774514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 1784514f5e3Sopenharmony_ci } 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci if (!index->IsInt()) { 1814514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index); 1824514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1834514f5e3Sopenharmony_ci CString errorMsg = 1844514f5e3Sopenharmony_ci "The type of \"minimumCapacity\" must be small integer. Received value is: " + ConvertToString(*result); 1854514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 1864514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1874514f5e3Sopenharmony_ci } 1884514f5e3Sopenharmony_ci JSAPILightWeightMap::IncreaseCapacityTo(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 1894514f5e3Sopenharmony_ci index->GetInt()); 1904514f5e3Sopenharmony_ci 1914514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1924514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 1934514f5e3Sopenharmony_ci} 1944514f5e3Sopenharmony_ci 1954514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Entries(EcmaRuntimeCallInfo *argv) 1964514f5e3Sopenharmony_ci{ 1974514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 1984514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1994514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Entries); 2004514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2014514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = 2034514f5e3Sopenharmony_ci JSAPILightWeightMapIterator::CreateLightWeightMapIterator(thread, self, IterationKind::KEY_AND_VALUE); 2044514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 2054514f5e3Sopenharmony_ci} 2064514f5e3Sopenharmony_ci 2074514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Get(EcmaRuntimeCallInfo *argv) 2084514f5e3Sopenharmony_ci{ 2094514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2104514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2114514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Get); 2124514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2134514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2144514f5e3Sopenharmony_ci 2154514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 2164514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 2174514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2184514f5e3Sopenharmony_ci } else { 2194514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2204514f5e3Sopenharmony_ci "The get method cannot be bound"); 2214514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2224514f5e3Sopenharmony_ci } 2234514f5e3Sopenharmony_ci } 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 2264514f5e3Sopenharmony_ci 2274514f5e3Sopenharmony_ci return JSAPILightWeightMap::Get(thread, JSHandle<JSAPILightWeightMap>::Cast(self), key); 2284514f5e3Sopenharmony_ci} 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::GetIndexOfKey(EcmaRuntimeCallInfo *argv) 2314514f5e3Sopenharmony_ci{ 2324514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2334514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2344514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, GetIndexOfKey); 2354514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2364514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 2394514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 2404514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2414514f5e3Sopenharmony_ci } else { 2424514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2434514f5e3Sopenharmony_ci "The getIndexOfKey method cannot be bound"); 2444514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2454514f5e3Sopenharmony_ci } 2464514f5e3Sopenharmony_ci } 2474514f5e3Sopenharmony_ci 2484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_ci int32_t index = JSAPILightWeightMap::GetIndexOfKey(thread, JSHandle<JSAPILightWeightMap>::Cast(self), key); 2514514f5e3Sopenharmony_ci return JSTaggedValue(index); 2524514f5e3Sopenharmony_ci} 2534514f5e3Sopenharmony_ci 2544514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::GetIndexOfValue(EcmaRuntimeCallInfo *argv) 2554514f5e3Sopenharmony_ci{ 2564514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2574514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2584514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, GetIndexOfValue); 2594514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2614514f5e3Sopenharmony_ci 2624514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 2634514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 2644514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2654514f5e3Sopenharmony_ci } else { 2664514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2674514f5e3Sopenharmony_ci "The getIndexOfValue method cannot be bound"); 2684514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2694514f5e3Sopenharmony_ci } 2704514f5e3Sopenharmony_ci } 2714514f5e3Sopenharmony_ci 2724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 0)); 2734514f5e3Sopenharmony_ci 2744514f5e3Sopenharmony_ci int32_t index = JSAPILightWeightMap::GetIndexOfValue(thread, JSHandle<JSAPILightWeightMap>::Cast(self), value); 2754514f5e3Sopenharmony_ci return JSTaggedValue(index); 2764514f5e3Sopenharmony_ci} 2774514f5e3Sopenharmony_ci 2784514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::IsEmpty(EcmaRuntimeCallInfo *argv) 2794514f5e3Sopenharmony_ci{ 2804514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 2814514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2824514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, IsEmpty); 2834514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2854514f5e3Sopenharmony_ci 2864514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 2874514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 2884514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2894514f5e3Sopenharmony_ci } else { 2904514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2914514f5e3Sopenharmony_ci "The isEmpty method cannot be bound"); 2924514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2934514f5e3Sopenharmony_ci } 2944514f5e3Sopenharmony_ci } 2954514f5e3Sopenharmony_ci return JSHandle<JSAPILightWeightMap>::Cast(self)->IsEmpty(); 2964514f5e3Sopenharmony_ci} 2974514f5e3Sopenharmony_ci 2984514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::GetKeyAt(EcmaRuntimeCallInfo *argv) 2994514f5e3Sopenharmony_ci{ 3004514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3014514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3024514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, GetKeyAt); 3034514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3044514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3054514f5e3Sopenharmony_ci 3064514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 3074514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 3084514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3094514f5e3Sopenharmony_ci } else { 3104514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3114514f5e3Sopenharmony_ci "The getKeyAt method cannot be bound"); 3124514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3134514f5e3Sopenharmony_ci } 3144514f5e3Sopenharmony_ci } 3154514f5e3Sopenharmony_ci 3164514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 3174514f5e3Sopenharmony_ci 3184514f5e3Sopenharmony_ci if (index->IsDouble()) { 3194514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 3204514f5e3Sopenharmony_ci } 3214514f5e3Sopenharmony_ci if (!index->IsInt()) { 3224514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index); 3234514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3244514f5e3Sopenharmony_ci CString errorMsg = 3254514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 3264514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3274514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3284514f5e3Sopenharmony_ci } 3294514f5e3Sopenharmony_ci 3304514f5e3Sopenharmony_ci return JSAPILightWeightMap::GetKeyAt(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 3314514f5e3Sopenharmony_ci index->GetInt()); 3324514f5e3Sopenharmony_ci} 3334514f5e3Sopenharmony_ci 3344514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Keys(EcmaRuntimeCallInfo *argv) 3354514f5e3Sopenharmony_ci{ 3364514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3374514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3384514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Keys); 3394514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = 3424514f5e3Sopenharmony_ci JSAPILightWeightMapIterator::CreateLightWeightMapIterator(thread, self, IterationKind::KEY); 3434514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 3444514f5e3Sopenharmony_ci} 3454514f5e3Sopenharmony_ci 3464514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::SetAll(EcmaRuntimeCallInfo *argv) 3474514f5e3Sopenharmony_ci{ 3484514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3494514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3504514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, SetAll); 3514514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3534514f5e3Sopenharmony_ci 3544514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 3554514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 3564514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3574514f5e3Sopenharmony_ci } else { 3584514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3594514f5e3Sopenharmony_ci "The setAll method cannot be bound"); 3604514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3614514f5e3Sopenharmony_ci } 3624514f5e3Sopenharmony_ci } 3634514f5e3Sopenharmony_ci 3644514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lightWeightMap(GetCallArg(argv, 0)); 3654514f5e3Sopenharmony_ci 3664514f5e3Sopenharmony_ci if (!lightWeightMap->IsJSAPILightWeightMap()) { 3674514f5e3Sopenharmony_ci if (lightWeightMap->IsJSProxy() && 3684514f5e3Sopenharmony_ci JSHandle<JSProxy>::Cast(lightWeightMap)->GetTarget().IsJSAPILightWeightMap()) { 3694514f5e3Sopenharmony_ci lightWeightMap = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(lightWeightMap)->GetTarget()); 3704514f5e3Sopenharmony_ci } else { 3714514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, lightWeightMap.GetTaggedValue()); 3724514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3734514f5e3Sopenharmony_ci CString errorMsg = 3744514f5e3Sopenharmony_ci "The type of \"map\" must be LightWeightMap. Received value is: " + ConvertToString(*result); 3754514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3764514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3774514f5e3Sopenharmony_ci } 3784514f5e3Sopenharmony_ci } 3794514f5e3Sopenharmony_ci 3804514f5e3Sopenharmony_ci JSAPILightWeightMap::SetAll(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 3814514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap>::Cast(lightWeightMap)); 3824514f5e3Sopenharmony_ci return JSTaggedValue::True(); 3834514f5e3Sopenharmony_ci} 3844514f5e3Sopenharmony_ci 3854514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Set(EcmaRuntimeCallInfo *argv) 3864514f5e3Sopenharmony_ci{ 3874514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 3884514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3894514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Set); 3904514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3924514f5e3Sopenharmony_ci 3934514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 3944514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 3954514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3964514f5e3Sopenharmony_ci } else { 3974514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3984514f5e3Sopenharmony_ci "The set method cannot be bound"); 3994514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4004514f5e3Sopenharmony_ci } 4014514f5e3Sopenharmony_ci } 4024514f5e3Sopenharmony_ci 4034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 4044514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 1)); 4054514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap> lightWeightMap = JSHandle<JSAPILightWeightMap>::Cast(self); 4064514f5e3Sopenharmony_ci JSAPILightWeightMap::Set(thread, lightWeightMap, key, value); 4074514f5e3Sopenharmony_ci 4084514f5e3Sopenharmony_ci return lightWeightMap.GetTaggedValue(); 4094514f5e3Sopenharmony_ci} 4104514f5e3Sopenharmony_ci 4114514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Remove(EcmaRuntimeCallInfo *argv) 4124514f5e3Sopenharmony_ci{ 4134514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4144514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4154514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Remove); 4164514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4184514f5e3Sopenharmony_ci 4194514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 4204514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 4214514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4224514f5e3Sopenharmony_ci } else { 4234514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4244514f5e3Sopenharmony_ci "The remove method cannot be bound"); 4254514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4264514f5e3Sopenharmony_ci } 4274514f5e3Sopenharmony_ci } 4284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(GetCallArg(argv, 0)); 4294514f5e3Sopenharmony_ci 4304514f5e3Sopenharmony_ci return JSAPILightWeightMap::Remove(thread, JSHandle<JSAPILightWeightMap>::Cast(self), key); 4314514f5e3Sopenharmony_ci} 4324514f5e3Sopenharmony_ci 4334514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::RemoveAt(EcmaRuntimeCallInfo *argv) 4344514f5e3Sopenharmony_ci{ 4354514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4364514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4374514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, RemoveAt); 4384514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4404514f5e3Sopenharmony_ci 4414514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 4424514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 4434514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4444514f5e3Sopenharmony_ci } else { 4454514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4464514f5e3Sopenharmony_ci "The removeAt method cannot be bound"); 4474514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4484514f5e3Sopenharmony_ci } 4494514f5e3Sopenharmony_ci } 4504514f5e3Sopenharmony_ci 4514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 4524514f5e3Sopenharmony_ci 4534514f5e3Sopenharmony_ci if (index->IsDouble()) { 4544514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 4554514f5e3Sopenharmony_ci } 4564514f5e3Sopenharmony_ci if (!index->IsInt()) { 4574514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index); 4584514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 4594514f5e3Sopenharmony_ci CString errorMsg = 4604514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 4614514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 4624514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4634514f5e3Sopenharmony_ci } 4644514f5e3Sopenharmony_ci 4654514f5e3Sopenharmony_ci return JSAPILightWeightMap::RemoveAt(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 4664514f5e3Sopenharmony_ci index->GetInt()); 4674514f5e3Sopenharmony_ci} 4684514f5e3Sopenharmony_ci 4694514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Clear(EcmaRuntimeCallInfo *argv) 4704514f5e3Sopenharmony_ci{ 4714514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4724514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4734514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Clear); 4744514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4764514f5e3Sopenharmony_ci 4774514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 4784514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 4794514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4804514f5e3Sopenharmony_ci } else { 4814514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4824514f5e3Sopenharmony_ci "The clear method cannot be bound"); 4834514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4844514f5e3Sopenharmony_ci } 4854514f5e3Sopenharmony_ci } 4864514f5e3Sopenharmony_ci 4874514f5e3Sopenharmony_ci JSAPILightWeightMap::Clear(thread, JSHandle<JSAPILightWeightMap>::Cast(self)); 4884514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 4894514f5e3Sopenharmony_ci} 4904514f5e3Sopenharmony_ci 4914514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::SetValueAt(EcmaRuntimeCallInfo *argv) 4924514f5e3Sopenharmony_ci{ 4934514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 4944514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 4954514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, SetValueAt); 4964514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 4974514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 4984514f5e3Sopenharmony_ci 4994514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 5004514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 5014514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5024514f5e3Sopenharmony_ci } else { 5034514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5044514f5e3Sopenharmony_ci "The setValueAt method cannot be bound"); 5054514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5064514f5e3Sopenharmony_ci } 5074514f5e3Sopenharmony_ci } 5084514f5e3Sopenharmony_ci 5094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 5104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(GetCallArg(argv, 1)); 5114514f5e3Sopenharmony_ci if (index->IsDouble()) { 5124514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 5134514f5e3Sopenharmony_ci } 5144514f5e3Sopenharmony_ci if (!index->IsInt()) { 5154514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index); 5164514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5174514f5e3Sopenharmony_ci CString errorMsg = 5184514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 5194514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 5204514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5214514f5e3Sopenharmony_ci } 5224514f5e3Sopenharmony_ci 5234514f5e3Sopenharmony_ci return JSAPILightWeightMap::SetValueAt(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 5244514f5e3Sopenharmony_ci index->GetInt(), value); 5254514f5e3Sopenharmony_ci} 5264514f5e3Sopenharmony_ci 5274514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::ForEach(EcmaRuntimeCallInfo *argv) 5284514f5e3Sopenharmony_ci{ 5294514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5304514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5314514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, ForEach); 5324514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5334514f5e3Sopenharmony_ci // get and check lightweightmap object 5344514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5354514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 5364514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 5374514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5384514f5e3Sopenharmony_ci } else { 5394514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5404514f5e3Sopenharmony_ci "The forEach method cannot be bound"); 5414514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5424514f5e3Sopenharmony_ci } 5434514f5e3Sopenharmony_ci } 5444514f5e3Sopenharmony_ci // get and check callback function 5454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> func(GetCallArg(argv, 0)); 5464514f5e3Sopenharmony_ci if (!func->IsCallable()) { 5474514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, func.GetTaggedValue()); 5484514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5494514f5e3Sopenharmony_ci CString errorMsg = 5504514f5e3Sopenharmony_ci "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result); 5514514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 5524514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5534514f5e3Sopenharmony_ci } 5544514f5e3Sopenharmony_ci // If thisArg was supplied, let T be thisArg; else let T be undefined. 5554514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1); 5564514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap> tmap = JSHandle<JSAPILightWeightMap>::Cast(self); 5574514f5e3Sopenharmony_ci JSMutableHandle<TaggedArray> keys(thread, tmap->GetKeys()); 5584514f5e3Sopenharmony_ci JSMutableHandle<TaggedArray> values(thread, tmap->GetValues()); 5594514f5e3Sopenharmony_ci 5604514f5e3Sopenharmony_ci uint32_t index = 0; 5614514f5e3Sopenharmony_ci uint32_t length = tmap->GetSize(); 5624514f5e3Sopenharmony_ci const uint32_t argsLength = 3; 5634514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 5644514f5e3Sopenharmony_ci while (index < length) { 5654514f5e3Sopenharmony_ci // ignore the hash value is required to determine the true index 5664514f5e3Sopenharmony_ci // Let funcResult be Call(callbackfn, T, «e, e, S»). 5674514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, func, thisArg, undefined, argsLength); 5684514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 5694514f5e3Sopenharmony_ci info->SetCallArg(values->Get(index), keys->Get(index), self.GetTaggedValue()); 5704514f5e3Sopenharmony_ci JSTaggedValue ret = JSFunction::Call(info); 5714514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret); 5724514f5e3Sopenharmony_ci 5734514f5e3Sopenharmony_ci // check entries should be update, size will be update in tmap set or remove. 5744514f5e3Sopenharmony_ci if (tmap->GetSize() != length) { 5754514f5e3Sopenharmony_ci keys.Update(tmap->GetKeys()); 5764514f5e3Sopenharmony_ci values.Update(tmap->GetValues()); 5774514f5e3Sopenharmony_ci length = tmap->GetSize(); 5784514f5e3Sopenharmony_ci } 5794514f5e3Sopenharmony_ci index++; 5804514f5e3Sopenharmony_ci } 5814514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 5824514f5e3Sopenharmony_ci} 5834514f5e3Sopenharmony_ci 5844514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::ToString(EcmaRuntimeCallInfo *argv) 5854514f5e3Sopenharmony_ci{ 5864514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 5874514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 5884514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, ToString); 5894514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 5904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 5914514f5e3Sopenharmony_ci 5924514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 5934514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 5944514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 5954514f5e3Sopenharmony_ci } else { 5964514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 5974514f5e3Sopenharmony_ci "The toString method cannot be bound"); 5984514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 5994514f5e3Sopenharmony_ci } 6004514f5e3Sopenharmony_ci } 6014514f5e3Sopenharmony_ci 6024514f5e3Sopenharmony_ci return JSAPILightWeightMap::ToString(thread, JSHandle<JSAPILightWeightMap>::Cast(self)); 6034514f5e3Sopenharmony_ci} 6044514f5e3Sopenharmony_ci 6054514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::GetValueAt(EcmaRuntimeCallInfo *argv) 6064514f5e3Sopenharmony_ci{ 6074514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 6084514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 6094514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, GetValueAt); 6104514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 6114514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 6124514f5e3Sopenharmony_ci 6134514f5e3Sopenharmony_ci if (!self->IsJSAPILightWeightMap()) { 6144514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILightWeightMap()) { 6154514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 6164514f5e3Sopenharmony_ci } else { 6174514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 6184514f5e3Sopenharmony_ci "The getValueAt method cannot be bound"); 6194514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 6204514f5e3Sopenharmony_ci } 6214514f5e3Sopenharmony_ci } 6224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> index(GetCallArg(argv, 0)); 6234514f5e3Sopenharmony_ci if (index->IsDouble()) { 6244514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 6254514f5e3Sopenharmony_ci } 6264514f5e3Sopenharmony_ci if (!index->IsInt()) { 6274514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index); 6284514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 6294514f5e3Sopenharmony_ci CString errorMsg = 6304514f5e3Sopenharmony_ci "The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result); 6314514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 6324514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 6334514f5e3Sopenharmony_ci } 6344514f5e3Sopenharmony_ci 6354514f5e3Sopenharmony_ci if (index->IsDouble()) { 6364514f5e3Sopenharmony_ci index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble())); 6374514f5e3Sopenharmony_ci } 6384514f5e3Sopenharmony_ci return JSAPILightWeightMap::GetValueAt(thread, JSHandle<JSAPILightWeightMap>::Cast(self), 6394514f5e3Sopenharmony_ci index->GetInt()); 6404514f5e3Sopenharmony_ci} 6414514f5e3Sopenharmony_ci 6424514f5e3Sopenharmony_ciJSTaggedValue ContainersLightWeightMap::Values(EcmaRuntimeCallInfo *argv) 6434514f5e3Sopenharmony_ci{ 6444514f5e3Sopenharmony_ci ASSERT(argv != nullptr); 6454514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 6464514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, LightWeightMap, Keys); 6474514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 6484514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 6494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = 6504514f5e3Sopenharmony_ci JSAPILightWeightMapIterator::CreateLightWeightMapIterator(thread, self, IterationKind::VALUE); 6514514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 6524514f5e3Sopenharmony_ci} 6534514f5e3Sopenharmony_ci} // namespace panda::ecmascript::containers 654