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