• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/shared_objects/js_shared_set_iterator.h"
17 
18 #include "ecmascript/js_array.h"
19 #include "ecmascript/linked_hash_table.h"
20 #include "ecmascript/shared_objects/concurrent_api_scope.h"
21 
22 namespace panda::ecmascript {
23 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)24 JSTaggedValue JSSharedSetIterator::Next(EcmaRuntimeCallInfo *argv)
25 {
26     ASSERT(argv);
27     JSThread *thread = argv->GetThread();
28     [[maybe_unused]] EcmaHandleScope handleScope(thread);
29     JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
30     return NextInternal(thread, thisObj);
31 }
32 
NextInternal(JSThread * thread,JSHandle<JSTaggedValue> thisObj)33 JSTaggedValue JSSharedSetIterator::NextInternal(JSThread *thread, JSHandle<JSTaggedValue> thisObj)
34 {
35     if (!thisObj->IsJSSharedSetIterator()) {
36         THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a shared-set iterator", JSTaggedValue::Exception());
37     }
38     JSHandle<JSSharedSetIterator> iter(thisObj);
39     JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
40     if (iter->GetIteratedSet(thread).IsUndefined()) {
41         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
42     }
43     JSHandle<JSSharedSet> iteratedSet(thread, iter->GetIteratedSet(thread));
44     [[maybe_unused]] ConcurrentApiScope<JSSharedSet> scope(thread, JSHandle<JSTaggedValue>::Cast(iteratedSet));
45     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception());
46     JSHandle<LinkedHashSet> set(thread, iteratedSet->GetLinkedSet(thread));
47 
48     int index = static_cast<int>(iter->GetNextIndex());
49     IterationKind itemKind = iter->GetIterationKind();
50     int totalElements = set->NumberOfElements() + set->NumberOfDeletedElements();
51 
52     while (index < totalElements) {
53         if (!set->GetKey(thread, index).IsHole()) {
54             iter->SetNextIndex(index + 1);
55             JSHandle<JSTaggedValue> key(thread, set->GetKey(thread, index));
56             if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
57                 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
58             }
59             ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
60             JSHandle<TaggedArray> array(factory->NewTaggedArray(2));  // 2: key and value pair
61             array->Set(thread, 0, key);
62             array->Set(thread, 1, key);
63             JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
64             return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
65         }
66         index++;
67     }
68     iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
69     return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
70 }
71 
CreateSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)72 JSHandle<JSTaggedValue> JSSharedSetIterator::CreateSetIterator(JSThread *thread,
73     const JSHandle<JSTaggedValue> &obj, IterationKind kind)
74 {
75     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
76     if (!obj->IsJSSharedSet()) {
77         JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
78         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not SharedSet", undefinedHandle);
79     }
80     JSHandle<JSTaggedValue> iter(factory->NewJSSetIterator(JSHandle<JSSharedSet>(obj), kind));
81     return iter;
82 }
83 }  // namespace panda::ecmascript
84