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_treemap.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_tree_map.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_tree_map_iterator.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_function.h"
234514f5e3Sopenharmony_ci#include "ecmascript/tagged_tree.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::ecmascript::containers {
264514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::TreeMapConstructor(EcmaRuntimeCallInfo *argv)
274514f5e3Sopenharmony_ci{
284514f5e3Sopenharmony_ci    ASSERT(argv);
294514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Constructor);
304514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
314514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
324514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
354514f5e3Sopenharmony_ci    if (newTarget->IsUndefined()) {
364514f5e3Sopenharmony_ci        JSTaggedValue error =
374514f5e3Sopenharmony_ci            ContainerError::BusinessError(thread, ErrorFlag::IS_NULL_ERROR,
384514f5e3Sopenharmony_ci                                          "The TreeMap's constructor cannot be directly invoked");
394514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    // new TreeMap
434514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
444514f5e3Sopenharmony_ci    JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
454514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    // Set map’s internal slot with a new empty List.
484514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(obj);
494514f5e3Sopenharmony_ci    JSTaggedValue internal = TaggedTreeMap::Create(thread);
504514f5e3Sopenharmony_ci    map->SetTreeMap(thread, internal);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    // If comparefn was supplied, let compare be comparefn; else let compare be hole.
534514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> compareFn(GetCallArg(argv, 0));
544514f5e3Sopenharmony_ci    if (compareFn->IsUndefined() || compareFn->IsNull()) {
554514f5e3Sopenharmony_ci        return map.GetTaggedValue();
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci    if (!compareFn->IsCallable()) {
584514f5e3Sopenharmony_ci        JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, compareFn.GetTaggedValue());
594514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
604514f5e3Sopenharmony_ci        CString errorMsg =
614514f5e3Sopenharmony_ci            "The type of \"comparefn\" must be callable. Received value is: " + ConvertToString(*result);
624514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
634514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    TaggedTreeMap::Cast(internal.GetTaggedObject())->SetCompare(thread, compareFn.GetTaggedValue());
674514f5e3Sopenharmony_ci    return map.GetTaggedValue();
684514f5e3Sopenharmony_ci}
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Set(EcmaRuntimeCallInfo *argv)
714514f5e3Sopenharmony_ci{
724514f5e3Sopenharmony_ci    ASSERT(argv);
734514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Set);
744514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
754514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
764514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
774514f5e3Sopenharmony_ci    // get and check this map
784514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
794514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
804514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
814514f5e3Sopenharmony_ci        } else {
824514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
834514f5e3Sopenharmony_ci                                                                "The set method cannot be bound");
844514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
854514f5e3Sopenharmony_ci        }
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
894514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
924514f5e3Sopenharmony_ci    JSAPITreeMap::Set(thread, map, key, value);
934514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
944514f5e3Sopenharmony_ci    return map.GetTaggedValue();
954514f5e3Sopenharmony_ci}
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Get(EcmaRuntimeCallInfo *argv)
984514f5e3Sopenharmony_ci{
994514f5e3Sopenharmony_ci    ASSERT(argv);
1004514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Get);
1014514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1024514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1034514f5e3Sopenharmony_ci    // get and check this map
1044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1054514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
1064514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
1074514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
1084514f5e3Sopenharmony_ci        } else {
1094514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
1104514f5e3Sopenharmony_ci                                                                "The get method cannot be bound");
1114514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
1124514f5e3Sopenharmony_ci        }
1134514f5e3Sopenharmony_ci    }
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
1164514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1174514f5e3Sopenharmony_ci    return JSAPITreeMap::Get(thread, map, key);
1184514f5e3Sopenharmony_ci}
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Remove(EcmaRuntimeCallInfo *argv)
1214514f5e3Sopenharmony_ci{
1224514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Remove);
1234514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1244514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1254514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
1264514f5e3Sopenharmony_ci    // get and check this map
1274514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
1284514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
1294514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
1304514f5e3Sopenharmony_ci        } else {
1314514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
1324514f5e3Sopenharmony_ci                                                                "The remove method cannot be bound");
1334514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
1344514f5e3Sopenharmony_ci        }
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
1384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1394514f5e3Sopenharmony_ci    return JSAPITreeMap::Delete(thread, map, key);
1404514f5e3Sopenharmony_ci}
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::HasKey(EcmaRuntimeCallInfo *argv)
1434514f5e3Sopenharmony_ci{
1444514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, HasKey);
1454514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1464514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1474514f5e3Sopenharmony_ci    // get and check this map
1484514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1494514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
1504514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
1514514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
1524514f5e3Sopenharmony_ci        } else {
1534514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
1544514f5e3Sopenharmony_ci                                                                "The hasKey method cannot be bound");
1554514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
1564514f5e3Sopenharmony_ci        }
1574514f5e3Sopenharmony_ci    }
1584514f5e3Sopenharmony_ci
1594514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
1604514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_ci    bool flag = JSAPITreeMap::HasKey(thread, JSHandle<JSAPITreeMap>::Cast(map), key);
1634514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1644514f5e3Sopenharmony_ci    return GetTaggedBoolean(flag);
1654514f5e3Sopenharmony_ci}
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::HasValue(EcmaRuntimeCallInfo *argv)
1684514f5e3Sopenharmony_ci{
1694514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, HasValue);
1704514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1714514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1724514f5e3Sopenharmony_ci    // get and check this map
1734514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1744514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
1754514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
1764514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
1774514f5e3Sopenharmony_ci        } else {
1784514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
1794514f5e3Sopenharmony_ci                                                                "The hasValue method cannot be bound");
1804514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
1814514f5e3Sopenharmony_ci        }
1824514f5e3Sopenharmony_ci    }
1834514f5e3Sopenharmony_ci
1844514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
1854514f5e3Sopenharmony_ci    bool flag = map->HasValue(thread, GetCallArg(argv, 0));
1864514f5e3Sopenharmony_ci    return GetTaggedBoolean(flag);
1874514f5e3Sopenharmony_ci}
1884514f5e3Sopenharmony_ci
1894514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::GetFirstKey(EcmaRuntimeCallInfo *argv)
1904514f5e3Sopenharmony_ci{
1914514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, GetFirstKey);
1924514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1934514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
1944514f5e3Sopenharmony_ci    // get and check this map
1954514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
1964514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
1974514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
1984514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
1994514f5e3Sopenharmony_ci        } else {
2004514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
2014514f5e3Sopenharmony_ci                                                                "The getFirstKey method cannot be bound");
2024514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
2034514f5e3Sopenharmony_ci        }
2044514f5e3Sopenharmony_ci    }
2054514f5e3Sopenharmony_ci
2064514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
2074514f5e3Sopenharmony_ci    return TaggedTreeMap::Cast(map->GetTreeMap().GetTaggedObject())->GetFirstKey();
2084514f5e3Sopenharmony_ci}
2094514f5e3Sopenharmony_ci
2104514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::GetLastKey(EcmaRuntimeCallInfo *argv)
2114514f5e3Sopenharmony_ci{
2124514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, GetLastKey);
2134514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2144514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2154514f5e3Sopenharmony_ci    // get and check this map
2164514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
2174514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
2184514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
2194514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
2204514f5e3Sopenharmony_ci        } else {
2214514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
2224514f5e3Sopenharmony_ci                                                                "The getLastKey method cannot be bound");
2234514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
2244514f5e3Sopenharmony_ci        }
2254514f5e3Sopenharmony_ci    }
2264514f5e3Sopenharmony_ci
2274514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
2284514f5e3Sopenharmony_ci    return TaggedTreeMap::Cast(map->GetTreeMap().GetTaggedObject())->GetLastKey();
2294514f5e3Sopenharmony_ci}
2304514f5e3Sopenharmony_ci
2314514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::SetAll(EcmaRuntimeCallInfo *argv)
2324514f5e3Sopenharmony_ci{
2334514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, SetAll);
2344514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2354514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2364514f5e3Sopenharmony_ci    // get and check this map
2374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
2384514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
2394514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
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 setAll 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> obj = GetCallArg(argv, 0);
2494514f5e3Sopenharmony_ci    if (!obj->IsJSAPITreeMap()) {
2504514f5e3Sopenharmony_ci        if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget().IsJSAPITreeMap()) {
2514514f5e3Sopenharmony_ci            obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget());
2524514f5e3Sopenharmony_ci        } else {
2534514f5e3Sopenharmony_ci            JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, obj.GetTaggedValue());
2544514f5e3Sopenharmony_ci            RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
2554514f5e3Sopenharmony_ci            CString errorMsg =
2564514f5e3Sopenharmony_ci                "The type of \"map\" must be TreeMap. Received value is: " + ConvertToString(*result);
2574514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
2584514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
2594514f5e3Sopenharmony_ci        }
2604514f5e3Sopenharmony_ci    }
2614514f5e3Sopenharmony_ci
2624514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> dst = JSHandle<JSAPITreeMap>::Cast(self);
2634514f5e3Sopenharmony_ci    JSHandle<TaggedTreeMap> dmap(thread, dst->GetTreeMap());
2644514f5e3Sopenharmony_ci    JSHandle<TaggedTreeMap> smap(thread, JSHandle<JSAPITreeMap>::Cast(obj)->GetTreeMap());
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_ci    if (JSHandle<JSAPITreeMap>::Cast(obj)->GetSize() > 0) {
2674514f5e3Sopenharmony_ci        JSTaggedValue tmap = TaggedTreeMap::SetAll(thread, dmap, smap);
2684514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
2694514f5e3Sopenharmony_ci        dst->SetTreeMap(thread, tmap);
2704514f5e3Sopenharmony_ci    }
2714514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
2724514f5e3Sopenharmony_ci}
2734514f5e3Sopenharmony_ci
2744514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Clear(EcmaRuntimeCallInfo *argv)
2754514f5e3Sopenharmony_ci{
2764514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Clear);
2774514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2784514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
2794514f5e3Sopenharmony_ci    // get and check this map
2804514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
2814514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
2824514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
2834514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
2844514f5e3Sopenharmony_ci        } else {
2854514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
2864514f5e3Sopenharmony_ci                                                                "The clear method cannot be bound");
2874514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
2884514f5e3Sopenharmony_ci        }
2894514f5e3Sopenharmony_ci    }
2904514f5e3Sopenharmony_ci
2914514f5e3Sopenharmony_ci    JSAPITreeMap::Clear(thread, JSHandle<JSAPITreeMap>::Cast(self));
2924514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
2934514f5e3Sopenharmony_ci}
2944514f5e3Sopenharmony_ci
2954514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::GetLowerKey(EcmaRuntimeCallInfo *argv)
2964514f5e3Sopenharmony_ci{
2974514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, GetLowerKey);
2984514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
2994514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3004514f5e3Sopenharmony_ci    // get and check this map
3014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
3024514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
3034514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
3044514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
3054514f5e3Sopenharmony_ci        } else {
3064514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
3074514f5e3Sopenharmony_ci                                                                "The getLowerKey method cannot be bound");
3084514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
3094514f5e3Sopenharmony_ci        }
3104514f5e3Sopenharmony_ci    }
3114514f5e3Sopenharmony_ci
3124514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
3134514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
3144514f5e3Sopenharmony_ci
3154514f5e3Sopenharmony_ci    JSHandle<TaggedTreeMap> tmap(thread, map->GetTreeMap());
3164514f5e3Sopenharmony_ci    return TaggedTreeMap::GetLowerKey(thread, tmap, key);
3174514f5e3Sopenharmony_ci}
3184514f5e3Sopenharmony_ci
3194514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::GetHigherKey(EcmaRuntimeCallInfo *argv)
3204514f5e3Sopenharmony_ci{
3214514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, GetHigherKey);
3224514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
3234514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3244514f5e3Sopenharmony_ci    // get and check this map
3254514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
3264514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
3274514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
3284514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
3294514f5e3Sopenharmony_ci        } else {
3304514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
3314514f5e3Sopenharmony_ci                                                                "The getHigherKey method cannot be bound");
3324514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
3334514f5e3Sopenharmony_ci        }
3344514f5e3Sopenharmony_ci    }
3354514f5e3Sopenharmony_ci
3364514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
3374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
3384514f5e3Sopenharmony_ci
3394514f5e3Sopenharmony_ci    JSHandle<TaggedTreeMap> tmap(thread, map->GetTreeMap());
3404514f5e3Sopenharmony_ci    return TaggedTreeMap::GetHigherKey(thread, tmap, key);
3414514f5e3Sopenharmony_ci}
3424514f5e3Sopenharmony_ci
3434514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Replace(EcmaRuntimeCallInfo *argv)
3444514f5e3Sopenharmony_ci{
3454514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Replace);
3464514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
3474514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3484514f5e3Sopenharmony_ci    // get and check this map
3494514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
3504514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
3514514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
3524514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
3534514f5e3Sopenharmony_ci        } else {
3544514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
3554514f5e3Sopenharmony_ci                                                                "The replace method cannot be bound");
3564514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
3574514f5e3Sopenharmony_ci        }
3584514f5e3Sopenharmony_ci    }
3594514f5e3Sopenharmony_ci
3604514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
3614514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
3624514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
3634514f5e3Sopenharmony_ci
3644514f5e3Sopenharmony_ci    bool success = JSAPITreeMap::Replace(thread, map, key, value);
3654514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
3664514f5e3Sopenharmony_ci    return GetTaggedBoolean(success);
3674514f5e3Sopenharmony_ci}
3684514f5e3Sopenharmony_ci
3694514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Keys(EcmaRuntimeCallInfo *argv)
3704514f5e3Sopenharmony_ci{
3714514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Keys);
3724514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
3734514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3744514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
3754514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter = JSAPITreeMapIterator::CreateTreeMapIterator(thread, self, IterationKind::KEY);
3764514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
3774514f5e3Sopenharmony_ci}
3784514f5e3Sopenharmony_ci
3794514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Values(EcmaRuntimeCallInfo *argv)
3804514f5e3Sopenharmony_ci{
3814514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Values);
3824514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
3834514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
3854514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter = JSAPITreeMapIterator::CreateTreeMapIterator(thread, self, IterationKind::VALUE);
3864514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
3874514f5e3Sopenharmony_ci}
3884514f5e3Sopenharmony_ci
3894514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::Entries(EcmaRuntimeCallInfo *argv)
3904514f5e3Sopenharmony_ci{
3914514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, Entries);
3924514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
3934514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
3944514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
3954514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iter =
3964514f5e3Sopenharmony_ci        JSAPITreeMapIterator::CreateTreeMapIterator(thread, self, IterationKind::KEY_AND_VALUE);
3974514f5e3Sopenharmony_ci    return iter.GetTaggedValue();
3984514f5e3Sopenharmony_ci}
3994514f5e3Sopenharmony_ci
4004514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::ForEach(EcmaRuntimeCallInfo *argv)
4014514f5e3Sopenharmony_ci{
4024514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, ForEach);
4034514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
4044514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
4054514f5e3Sopenharmony_ci    // get and check TreeMap object
4064514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
4074514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
4084514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
4094514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
4104514f5e3Sopenharmony_ci        } else {
4114514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
4124514f5e3Sopenharmony_ci                                                                "The forEach method cannot be bound");
4134514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
4144514f5e3Sopenharmony_ci        }
4154514f5e3Sopenharmony_ci    }
4164514f5e3Sopenharmony_ci    // get and check callback function
4174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> func(GetCallArg(argv, 0));
4184514f5e3Sopenharmony_ci    if (!func->IsCallable()) {
4194514f5e3Sopenharmony_ci        JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, func.GetTaggedValue());
4204514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
4214514f5e3Sopenharmony_ci        CString errorMsg =
4224514f5e3Sopenharmony_ci            "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result);
4234514f5e3Sopenharmony_ci        JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
4244514f5e3Sopenharmony_ci        THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
4254514f5e3Sopenharmony_ci    }
4264514f5e3Sopenharmony_ci    // If thisArg was supplied, let T be thisArg; else let T be undefined.
4274514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1);
4284514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> tmap = JSHandle<JSAPITreeMap>::Cast(self);
4294514f5e3Sopenharmony_ci    JSMutableHandle<TaggedTreeMap> iteratedMap(thread, tmap->GetTreeMap());
4304514f5e3Sopenharmony_ci    uint32_t elements = iteratedMap->NumberOfElements();
4314514f5e3Sopenharmony_ci    JSHandle<TaggedArray> entries = TaggedTreeMap::GetArrayFromMap(thread, iteratedMap);
4324514f5e3Sopenharmony_ci    uint32_t index = 0;
4334514f5e3Sopenharmony_ci    size_t length = entries->GetLength();
4344514f5e3Sopenharmony_ci    const uint32_t argsLength = 3;
4354514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
4364514f5e3Sopenharmony_ci    JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
4374514f5e3Sopenharmony_ci    JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
4384514f5e3Sopenharmony_ci    while (index < elements) {
4394514f5e3Sopenharmony_ci        int entriesIndex = entries->Get(index).GetInt();
4404514f5e3Sopenharmony_ci        key.Update(iteratedMap->GetKey(entriesIndex));
4414514f5e3Sopenharmony_ci        value.Update(iteratedMap->GetValue(entriesIndex));
4424514f5e3Sopenharmony_ci        // Let funcResult be Call(callbackfn, T, «e, e, S»).
4434514f5e3Sopenharmony_ci        EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, func, thisArg, undefined, argsLength);
4444514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
4454514f5e3Sopenharmony_ci        info->SetCallArg(value.GetTaggedValue(), key.GetTaggedValue(), self.GetTaggedValue());
4464514f5e3Sopenharmony_ci        JSTaggedValue ret = JSFunction::Call(info);
4474514f5e3Sopenharmony_ci        RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
4484514f5e3Sopenharmony_ci        // check entries should be update, size will be update in tmap set or remove.
4494514f5e3Sopenharmony_ci        if (tmap->GetSize() != static_cast<int>(length)) {
4504514f5e3Sopenharmony_ci            iteratedMap.Update(tmap->GetTreeMap());
4514514f5e3Sopenharmony_ci            entries = TaggedTreeMap::GetArrayFromMap(thread, iteratedMap);
4524514f5e3Sopenharmony_ci            elements = iteratedMap->NumberOfElements();
4534514f5e3Sopenharmony_ci            length = entries->GetLength();
4544514f5e3Sopenharmony_ci        }
4554514f5e3Sopenharmony_ci        index++;
4564514f5e3Sopenharmony_ci    }
4574514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
4584514f5e3Sopenharmony_ci}
4594514f5e3Sopenharmony_ci
4604514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::GetLength(EcmaRuntimeCallInfo *argv)
4614514f5e3Sopenharmony_ci{
4624514f5e3Sopenharmony_ci    ASSERT(argv);
4634514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, GetLength);
4644514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
4654514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
4664514f5e3Sopenharmony_ci    // get and check this map
4674514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self(GetThis(argv));
4684514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
4694514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
4704514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
4714514f5e3Sopenharmony_ci        } else {
4724514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
4734514f5e3Sopenharmony_ci                                                                "The getLength method cannot be bound");
4744514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
4754514f5e3Sopenharmony_ci        }
4764514f5e3Sopenharmony_ci    }
4774514f5e3Sopenharmony_ci    int count = JSHandle<JSAPITreeMap>::Cast(self)->GetSize();
4784514f5e3Sopenharmony_ci    return JSTaggedValue(count);
4794514f5e3Sopenharmony_ci}
4804514f5e3Sopenharmony_ci
4814514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeMap::IsEmpty(EcmaRuntimeCallInfo *argv)
4824514f5e3Sopenharmony_ci{
4834514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), TreeMap, IsEmpty);
4844514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
4854514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
4864514f5e3Sopenharmony_ci    // get and check this map
4874514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> self = GetThis(argv);
4884514f5e3Sopenharmony_ci    if (!self->IsJSAPITreeMap()) {
4894514f5e3Sopenharmony_ci        if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeMap()) {
4904514f5e3Sopenharmony_ci            self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
4914514f5e3Sopenharmony_ci        } else {
4924514f5e3Sopenharmony_ci            JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
4934514f5e3Sopenharmony_ci                                                                "The isEmpty method cannot be bound");
4944514f5e3Sopenharmony_ci            THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
4954514f5e3Sopenharmony_ci        }
4964514f5e3Sopenharmony_ci    }
4974514f5e3Sopenharmony_ci    JSHandle<JSAPITreeMap> map = JSHandle<JSAPITreeMap>::Cast(self);
4984514f5e3Sopenharmony_ci    return GetTaggedBoolean(map->GetSize() == 0);
4994514f5e3Sopenharmony_ci}
5004514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::containers
501