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_treeset.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_set.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_tree_set_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 ContainersTreeSet::TreeSetConstructor(EcmaRuntimeCallInfo *argv) 274514f5e3Sopenharmony_ci{ 284514f5e3Sopenharmony_ci ASSERT(argv); 294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, 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 TreeSet's constructor cannot be directly invoked"); 394514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 404514f5e3Sopenharmony_ci } 414514f5e3Sopenharmony_ci // new TreeSet 424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 434514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 444514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_ci // Set set’s internal slot with a new empty List. 474514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(obj); 484514f5e3Sopenharmony_ci JSTaggedValue internal = TaggedTreeSet::Create(thread); 494514f5e3Sopenharmony_ci set->SetTreeSet(thread, internal); 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci // If comparefn was supplied, let compare be comparefn; else let compare be hole. 524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> compareFn(GetCallArg(argv, 0)); 534514f5e3Sopenharmony_ci if (compareFn->IsUndefined() || compareFn->IsNull()) { 544514f5e3Sopenharmony_ci return set.GetTaggedValue(); 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci if (!compareFn->IsCallable()) { 574514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, compareFn.GetTaggedValue()); 584514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 594514f5e3Sopenharmony_ci CString errorMsg = 604514f5e3Sopenharmony_ci "The type of \"comparefn\" must be callable. Received value is: " + ConvertToString(*result); 614514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 624514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 634514f5e3Sopenharmony_ci } 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ci TaggedTreeSet::Cast(internal.GetTaggedObject())->SetCompare(thread, compareFn.GetTaggedValue()); 664514f5e3Sopenharmony_ci return set.GetTaggedValue(); 674514f5e3Sopenharmony_ci} 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Add(EcmaRuntimeCallInfo *argv) 704514f5e3Sopenharmony_ci{ 714514f5e3Sopenharmony_ci ASSERT(argv); 724514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Add); 734514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 744514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 754514f5e3Sopenharmony_ci // get and check this set 764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 774514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 784514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 794514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 804514f5e3Sopenharmony_ci } else { 814514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 824514f5e3Sopenharmony_ci "The add method cannot be bound"); 834514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 844514f5e3Sopenharmony_ci } 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 884514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 894514f5e3Sopenharmony_ci JSAPITreeSet::Add(thread, set, value); 904514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 914514f5e3Sopenharmony_ci return JSTaggedValue::True(); 924514f5e3Sopenharmony_ci} 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Remove(EcmaRuntimeCallInfo *argv) 954514f5e3Sopenharmony_ci{ 964514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Remove); 974514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 984514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 994514f5e3Sopenharmony_ci // get and check this set 1004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 1014514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 1024514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 1034514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1044514f5e3Sopenharmony_ci } else { 1054514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1064514f5e3Sopenharmony_ci "The remove method cannot be bound"); 1074514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1084514f5e3Sopenharmony_ci } 1094514f5e3Sopenharmony_ci } 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 1124514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = GetCallArg(argv, 0); 1134514f5e3Sopenharmony_ci return GetTaggedBoolean(JSAPITreeSet::Delete(thread, set, key)); 1144514f5e3Sopenharmony_ci} 1154514f5e3Sopenharmony_ci 1164514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Has(EcmaRuntimeCallInfo *argv) 1174514f5e3Sopenharmony_ci{ 1184514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Has); 1194514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1204514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1214514f5e3Sopenharmony_ci // get and check this set 1224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 1234514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 1244514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 1254514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1264514f5e3Sopenharmony_ci } else { 1274514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1284514f5e3Sopenharmony_ci "The has method cannot be bound"); 1294514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1304514f5e3Sopenharmony_ci } 1314514f5e3Sopenharmony_ci } 1324514f5e3Sopenharmony_ci 1334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = GetCallArg(argv, 0); 1344514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 1354514f5e3Sopenharmony_ci 1364514f5e3Sopenharmony_ci bool flag = JSAPITreeSet::Has(thread, JSHandle<JSAPITreeSet>::Cast(set), key); 1374514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1384514f5e3Sopenharmony_ci return GetTaggedBoolean(flag); 1394514f5e3Sopenharmony_ci} 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::GetFirstValue(EcmaRuntimeCallInfo *argv) 1424514f5e3Sopenharmony_ci{ 1434514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, GetFirstValue); 1444514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1454514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1464514f5e3Sopenharmony_ci // get and check this set 1474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 1484514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 1494514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 1504514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1514514f5e3Sopenharmony_ci } else { 1524514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1534514f5e3Sopenharmony_ci "The getFirstValue method cannot be bound"); 1544514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1554514f5e3Sopenharmony_ci } 1564514f5e3Sopenharmony_ci } 1574514f5e3Sopenharmony_ci 1584514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 1594514f5e3Sopenharmony_ci return TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject())->GetFirstKey(); 1604514f5e3Sopenharmony_ci} 1614514f5e3Sopenharmony_ci 1624514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::GetLastValue(EcmaRuntimeCallInfo *argv) 1634514f5e3Sopenharmony_ci{ 1644514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, GetLastValue); 1654514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1664514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1674514f5e3Sopenharmony_ci // get and check this set 1684514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 1694514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 1704514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 1714514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1724514f5e3Sopenharmony_ci } else { 1734514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1744514f5e3Sopenharmony_ci "The getLastValue method cannot be bound"); 1754514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1764514f5e3Sopenharmony_ci } 1774514f5e3Sopenharmony_ci } 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 1804514f5e3Sopenharmony_ci return TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject())->GetLastKey(); 1814514f5e3Sopenharmony_ci} 1824514f5e3Sopenharmony_ci 1834514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Clear(EcmaRuntimeCallInfo *argv) 1844514f5e3Sopenharmony_ci{ 1854514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Clear); 1864514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1874514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 1884514f5e3Sopenharmony_ci // get and check this set 1894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 1904514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 1914514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 1924514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 1934514f5e3Sopenharmony_ci } else { 1944514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 1954514f5e3Sopenharmony_ci "The clear method cannot be bound"); 1964514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 1974514f5e3Sopenharmony_ci } 1984514f5e3Sopenharmony_ci } 1994514f5e3Sopenharmony_ci 2004514f5e3Sopenharmony_ci JSAPITreeSet::Clear(thread, JSHandle<JSAPITreeSet>::Cast(self)); 2014514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 2024514f5e3Sopenharmony_ci} 2034514f5e3Sopenharmony_ci 2044514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::GetLowerValue(EcmaRuntimeCallInfo *argv) 2054514f5e3Sopenharmony_ci{ 2064514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, GetLowerValue); 2074514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2084514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2094514f5e3Sopenharmony_ci // get and check this set 2104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 2114514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 2124514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 2134514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2144514f5e3Sopenharmony_ci } else { 2154514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2164514f5e3Sopenharmony_ci "The getLowerValue method cannot be bound"); 2174514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2184514f5e3Sopenharmony_ci } 2194514f5e3Sopenharmony_ci } 2204514f5e3Sopenharmony_ci 2214514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 2224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = GetCallArg(argv, 0); 2234514f5e3Sopenharmony_ci JSHandle<TaggedTreeSet> tset(thread, set->GetTreeSet()); 2244514f5e3Sopenharmony_ci return TaggedTreeSet::GetLowerKey(thread, tset, key); 2254514f5e3Sopenharmony_ci} 2264514f5e3Sopenharmony_ci 2274514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::GetHigherValue(EcmaRuntimeCallInfo *argv) 2284514f5e3Sopenharmony_ci{ 2294514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, GetHigherValue); 2304514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2314514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2324514f5e3Sopenharmony_ci // get and check this set 2334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 2344514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 2354514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 2364514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2374514f5e3Sopenharmony_ci } else { 2384514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2394514f5e3Sopenharmony_ci "The getHigherValue method cannot be bound"); 2404514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2414514f5e3Sopenharmony_ci } 2424514f5e3Sopenharmony_ci } 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 2454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key = GetCallArg(argv, 0); 2464514f5e3Sopenharmony_ci JSHandle<TaggedTreeSet> tset(thread, set->GetTreeSet()); 2474514f5e3Sopenharmony_ci return TaggedTreeSet::GetHigherKey(thread, tset, key); 2484514f5e3Sopenharmony_ci} 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::PopFirst(EcmaRuntimeCallInfo *argv) 2514514f5e3Sopenharmony_ci{ 2524514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, PopFirst); 2534514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2544514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2554514f5e3Sopenharmony_ci // get and check this set 2564514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 2574514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 2584514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 2594514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2604514f5e3Sopenharmony_ci } else { 2614514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2624514f5e3Sopenharmony_ci "The popFirst method cannot be bound"); 2634514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2644514f5e3Sopenharmony_ci } 2654514f5e3Sopenharmony_ci } 2664514f5e3Sopenharmony_ci 2674514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 2684514f5e3Sopenharmony_ci return JSAPITreeSet::PopFirst(thread, set); 2694514f5e3Sopenharmony_ci} 2704514f5e3Sopenharmony_ci 2714514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::PopLast(EcmaRuntimeCallInfo *argv) 2724514f5e3Sopenharmony_ci{ 2734514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, PopLast); 2744514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2754514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2764514f5e3Sopenharmony_ci // get and check this set 2774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 2784514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 2794514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 2804514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 2814514f5e3Sopenharmony_ci } else { 2824514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 2834514f5e3Sopenharmony_ci "The popLast method cannot be bound"); 2844514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 2854514f5e3Sopenharmony_ci } 2864514f5e3Sopenharmony_ci } 2874514f5e3Sopenharmony_ci 2884514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 2894514f5e3Sopenharmony_ci return JSAPITreeSet::PopLast(thread, set); 2904514f5e3Sopenharmony_ci} 2914514f5e3Sopenharmony_ci 2924514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::IsEmpty(EcmaRuntimeCallInfo *argv) 2934514f5e3Sopenharmony_ci{ 2944514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, IsEmpty); 2954514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 2964514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 2974514f5e3Sopenharmony_ci // get and check this set 2984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 2994514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 3004514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 3014514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 3024514f5e3Sopenharmony_ci } else { 3034514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 3044514f5e3Sopenharmony_ci "The isEmpty method cannot be bound"); 3054514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3064514f5e3Sopenharmony_ci } 3074514f5e3Sopenharmony_ci } 3084514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> set = JSHandle<JSAPITreeSet>::Cast(self); 3094514f5e3Sopenharmony_ci return GetTaggedBoolean(set->GetSize() == 0); 3104514f5e3Sopenharmony_ci} 3114514f5e3Sopenharmony_ci 3124514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Values(EcmaRuntimeCallInfo *argv) 3134514f5e3Sopenharmony_ci{ 3144514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Values); 3154514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3164514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = JSAPITreeSetIterator::CreateTreeSetIterator(thread, self, IterationKind::KEY); 3194514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 3204514f5e3Sopenharmony_ci} 3214514f5e3Sopenharmony_ci 3224514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::Entries(EcmaRuntimeCallInfo *argv) 3234514f5e3Sopenharmony_ci{ 3244514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, Entries); 3254514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3264514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3274514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> iter = 3294514f5e3Sopenharmony_ci JSAPITreeSetIterator::CreateTreeSetIterator(thread, self, IterationKind::KEY_AND_VALUE); 3304514f5e3Sopenharmony_ci return iter.GetTaggedValue(); 3314514f5e3Sopenharmony_ci} 3324514f5e3Sopenharmony_ci 3334514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::ForEach(EcmaRuntimeCallInfo *argv) 3344514f5e3Sopenharmony_ci{ 3354514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, ForEach); 3364514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3374514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3384514f5e3Sopenharmony_ci // get and check TreeSet object 3394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self = GetThis(argv); 3404514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 3414514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 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 forEach method cannot be bound"); 3464514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3474514f5e3Sopenharmony_ci } 3484514f5e3Sopenharmony_ci } 3494514f5e3Sopenharmony_ci // get and check callback function 3504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> func(GetCallArg(argv, 0)); 3514514f5e3Sopenharmony_ci if (!func->IsCallable()) { 3524514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, func.GetTaggedValue()); 3534514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3544514f5e3Sopenharmony_ci CString errorMsg = 3554514f5e3Sopenharmony_ci "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result); 3564514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str()); 3574514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 3584514f5e3Sopenharmony_ci } 3594514f5e3Sopenharmony_ci // If thisArg was supplied, let T be thisArg; else let T be undefined. 3604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1); 3614514f5e3Sopenharmony_ci JSHandle<JSAPITreeSet> tset = JSHandle<JSAPITreeSet>::Cast(self); 3624514f5e3Sopenharmony_ci JSMutableHandle<TaggedTreeSet> iteratedSet(thread, tset->GetTreeSet()); 3634514f5e3Sopenharmony_ci uint32_t elements = iteratedSet->NumberOfElements(); 3644514f5e3Sopenharmony_ci JSHandle<TaggedArray> entries = TaggedTreeSet::GetArrayFromSet(thread, iteratedSet); 3654514f5e3Sopenharmony_ci uint32_t index = 0; 3664514f5e3Sopenharmony_ci size_t length = entries->GetLength(); 3674514f5e3Sopenharmony_ci const uint32_t argsLength = 3; 3684514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 3694514f5e3Sopenharmony_ci JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined()); 3704514f5e3Sopenharmony_ci while (index < elements) { 3714514f5e3Sopenharmony_ci int entriesIndex = entries->Get(index).GetInt(); 3724514f5e3Sopenharmony_ci key.Update(iteratedSet->GetKey(entriesIndex)); 3734514f5e3Sopenharmony_ci // Let funcResult be Call(callbackfn, T, «e, e, S»). 3744514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, func, thisArg, undefined, argsLength); 3754514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 3764514f5e3Sopenharmony_ci info->SetCallArg(key.GetTaggedValue(), key.GetTaggedValue(), self.GetTaggedValue()); 3774514f5e3Sopenharmony_ci JSTaggedValue ret = JSFunction::Call(info); 3784514f5e3Sopenharmony_ci RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret); 3794514f5e3Sopenharmony_ci // check entries should be update, size will be update by set add and remove. 3804514f5e3Sopenharmony_ci if (tset->GetSize() != static_cast<int>(length)) { 3814514f5e3Sopenharmony_ci iteratedSet.Update(tset->GetTreeSet()); 3824514f5e3Sopenharmony_ci entries = TaggedTreeSet::GetArrayFromSet(thread, iteratedSet); 3834514f5e3Sopenharmony_ci elements = iteratedSet->NumberOfElements(); 3844514f5e3Sopenharmony_ci length = entries->GetLength(); 3854514f5e3Sopenharmony_ci } 3864514f5e3Sopenharmony_ci index++; 3874514f5e3Sopenharmony_ci } 3884514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 3894514f5e3Sopenharmony_ci} 3904514f5e3Sopenharmony_ci 3914514f5e3Sopenharmony_ciJSTaggedValue ContainersTreeSet::GetLength(EcmaRuntimeCallInfo *argv) 3924514f5e3Sopenharmony_ci{ 3934514f5e3Sopenharmony_ci ASSERT(argv); 3944514f5e3Sopenharmony_ci BUILTINS_API_TRACE(argv->GetThread(), TreeSet, GetLength); 3954514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 3964514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope handleScope(thread); 3974514f5e3Sopenharmony_ci // get and check this set 3984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> self(GetThis(argv)); 3994514f5e3Sopenharmony_ci if (!self->IsJSAPITreeSet()) { 4004514f5e3Sopenharmony_ci if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPITreeSet()) { 4014514f5e3Sopenharmony_ci self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget()); 4024514f5e3Sopenharmony_ci } else { 4034514f5e3Sopenharmony_ci JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR, 4044514f5e3Sopenharmony_ci "The getLength method cannot be bound"); 4054514f5e3Sopenharmony_ci THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 4064514f5e3Sopenharmony_ci } 4074514f5e3Sopenharmony_ci } 4084514f5e3Sopenharmony_ci 4094514f5e3Sopenharmony_ci int count = JSHandle<JSAPITreeSet>::Cast(self)->GetSize(); 4104514f5e3Sopenharmony_ci return JSTaggedValue(count); 4114514f5e3Sopenharmony_ci} 4124514f5e3Sopenharmony_ci} // namespace panda::ecmascript::containers 413