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