1 /*
2 * Copyright (c) 2021 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/js_set_iterator.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/js_array.h"
21 #include "ecmascript/js_set.h"
22 #include "ecmascript/linked_hash_table.h"
23 #include "ecmascript/object_factory.h"
24
25 namespace panda::ecmascript {
26 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)27 JSTaggedValue JSSetIterator::Next(EcmaRuntimeCallInfo *argv)
28 {
29 ASSERT(argv);
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope handleScope(thread);
32 // 1.If Type(O) is not Object, throw a TypeError exception.
33 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
34
35 // 3.If O does not have all of the internal slots of a Set Iterator Instance (23.2.5.3), throw a TypeError
36 // exception.
37 if (!input->IsJSSetIterator()) {
38 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a set iterator", JSTaggedValue::Exception());
39 }
40 JSHandle<JSSetIterator> iter(input);
41 iter->Update(thread);
42 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
43 // 4.Let s be O.[[IteratedSet]].
44 JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
45
46 // 5.Let index be O.[[SetNextIndex]].
47 int index = static_cast<int>(iter->GetNextIndex());
48 IterationKind itemKind = iter->GetIterationKind();
49 // 7.If s is undefined, return CreateIterResultObject(undefined, true).
50 if (iteratedSet->IsUndefined()) {
51 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
52 }
53 JSHandle<LinkedHashSet> set(iteratedSet);
54 int totalElements = set->NumberOfElements() + set->NumberOfDeletedElements();
55
56 while (index < totalElements) {
57 if (!set->GetKey(index).IsHole()) {
58 iter->SetNextIndex(index + 1);
59 JSHandle<JSTaggedValue> key(thread, set->GetKey(index));
60 // If itemKind is value
61 if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
62 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
63 }
64 // If itemKind is key+value, then
65 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2: key and value pair
67 array->Set(thread, 0, key);
68 array->Set(thread, 1, key);
69 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
70 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
71 }
72 index++;
73 }
74 // 13.Set O.[[IteratedSet]] to undefined.
75 iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
76 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
77 }
78
Update(const JSThread * thread)79 void JSSetIterator::Update(const JSThread *thread)
80 {
81 [[maybe_unused]] DisallowGarbageCollection noGc;
82 JSTaggedValue iteratedSet = GetIteratedSet();
83 if (iteratedSet.IsUndefined()) {
84 return;
85 }
86 LinkedHashSet *set = LinkedHashSet::Cast(iteratedSet.GetTaggedObject());
87 if (set->GetNextTable().IsHole()) {
88 return;
89 }
90 int index = static_cast<int>(GetNextIndex());
91 JSTaggedValue nextTable = set->GetNextTable();
92 while (!nextTable.IsHole()) {
93 index -= set->GetDeletedElementsAt(index);
94 set = LinkedHashSet::Cast(nextTable.GetTaggedObject());
95 nextTable = set->GetNextTable();
96 }
97 SetIteratedSet(thread, JSTaggedValue(set));
98 SetNextIndex(index);
99 }
100
CreateSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)101 JSHandle<JSTaggedValue> JSSetIterator::CreateSetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
102 IterationKind kind)
103 {
104 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
105 if (!obj->IsJSSet()) {
106 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
107 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", undefinedHandle);
108 }
109 JSHandle<JSTaggedValue> iter(factory->NewJSSetIterator(JSHandle<JSSet>(obj), kind));
110 return iter;
111 }
112 } // namespace panda::ecmascript
113