1 /*
2 * Copyright (c) 2022 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_api/js_api_hashset_iterator.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/js_api/js_api_hashset.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/tagged_node.h"
24 #include "ecmascript/tagged_queue.h"
25
26 namespace panda::ecmascript {
27 using BuiltinsBase = base::BuiltinsBase;
28 using ContainerError = containers::ContainerError;
29 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)30 JSTaggedValue JSAPIHashSetIterator::Next(EcmaRuntimeCallInfo *argv)
31 {
32 ASSERT(argv);
33 JSThread *thread = argv->GetThread();
34 [[maybe_unused]] EcmaHandleScope handleScope(thread);
35 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
36 if (!input->IsJSAPIHashSetIterator()) {
37 JSTaggedValue error =
38 ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
39 "The Symbol.iterator method cannot be bound");
40 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
41 }
42 JSHandle<JSAPIHashSetIterator> iter = JSHandle<JSAPIHashSetIterator>::Cast(input);
43 JSHandle<JSTaggedValue> iteratedHashSet(thread, iter->GetIteratedHashSet());
44 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
45 if (iteratedHashSet->IsUndefined()) {
46 return globalConst->GetUndefinedIterResult();
47 }
48 JSHandle<JSAPIHashSet> hashSet = JSHandle<JSAPIHashSet>::Cast(iteratedHashSet);
49 JSHandle<TaggedHashArray> tableArr(thread, hashSet->GetTable());
50 uint32_t tableLength = tableArr->GetLength();
51 uint32_t tableIndex = iter->GetTableIndex();
52 uint32_t index = iter->GetNextIndex();
53 uint32_t size = hashSet->GetSize();
54 JSMutableHandle<TaggedQueue> queue(thread, iter->GetTaggedQueue());
55 JSMutableHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue::Undefined());
56 JSMutableHandle<TaggedNode> currentNode(thread, JSTaggedValue::Undefined());
57 IterationKind itemKind = iter->GetIterationKind();
58 while (tableIndex < tableLength && index < size) {
59 currentNode.Update(FastGetCurrentNode(thread, iter, queue, tableArr));
60 if (!currentNode.GetTaggedValue().IsHole() && !currentNode.GetTaggedValue().IsUndefined()) {
61 iter->SetNextIndex(++index);
62 valueHandle.Update(currentNode->GetKey());
63 if (itemKind == IterationKind::VALUE) {
64 return JSIterator::CreateIterResultObject(thread, valueHandle, false).GetTaggedValue();
65 }
66 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
68 array->Set(thread, 0, JSTaggedValue(--index));
69 array->Set(thread, 1, valueHandle);
70 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
71 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
72 }
73 tableIndex++;
74 if (!currentNode.GetTaggedValue().IsRBTreeNode()) {
75 iter->SetTableIndex(tableIndex);
76 }
77 }
78 // Set O.[[IteratedMap]] to undefined.
79 iter->SetIteratedHashSet(thread, JSTaggedValue::Undefined());
80 return globalConst->GetUndefinedIterResult();
81 }
82
FastGetCurrentNode(JSThread * thread,JSHandle<JSAPIHashSetIterator> & iter,JSMutableHandle<TaggedQueue> & queue,JSHandle<TaggedHashArray> & tableArr)83 JSHandle<JSTaggedValue> JSAPIHashSetIterator::FastGetCurrentNode(JSThread *thread,
84 JSHandle<JSAPIHashSetIterator> &iter,
85 JSMutableHandle<TaggedQueue> &queue,
86 JSHandle<TaggedHashArray> &tableArr)
87 {
88 JSHandle<JSTaggedValue> rootValue(thread, JSTaggedValue::Undefined());
89 uint32_t index = iter->GetTableIndex();
90 JSHandle<JSTaggedValue> prevNodeValue(thread, iter->GetCurrentNodeResult());
91 if (prevNodeValue->IsRBTreeNode()) {
92 return GetCurrentNode(thread, iter, queue, tableArr);
93 }
94 if (prevNodeValue->IsUndefined() || prevNodeValue->IsHole()) {
95 rootValue = JSHandle<JSTaggedValue>(thread, tableArr->Get(index));
96 iter->SetCurrentNodeResult(thread, rootValue);
97 return rootValue;
98 }
99 JSHandle<LinkedNode> prevNode = JSHandle<LinkedNode>::Cast(prevNodeValue);
100 if (!prevNode->GetNext().IsHole()) {
101 JSHandle<JSTaggedValue> next(thread, prevNode->GetNext());
102 iter->SetCurrentNodeResult(thread, next);
103 return next;
104 }
105 iter->SetCurrentNodeResult(thread, JSTaggedValue::Undefined());
106 return rootValue;
107 }
108
109 // level traversal
GetCurrentNode(JSThread * thread,JSHandle<JSAPIHashSetIterator> & iter,JSMutableHandle<TaggedQueue> & queue,JSHandle<TaggedHashArray> & tableArr)110 JSHandle<JSTaggedValue> JSAPIHashSetIterator::GetCurrentNode(JSThread *thread, JSHandle<JSAPIHashSetIterator> &iter,
111 JSMutableHandle<TaggedQueue> &queue,
112 JSHandle<TaggedHashArray> &tableArr)
113 {
114 JSHandle<JSTaggedValue> rootValue(thread, JSTaggedValue::Undefined());
115 uint32_t index = iter->GetTableIndex();
116 if (queue->Empty()) {
117 rootValue = JSHandle<JSTaggedValue>(thread, tableArr->Get(index));
118 if (rootValue->IsHole()) {
119 iter->SetTableIndex(++index);
120 return rootValue;
121 }
122 } else {
123 rootValue = JSHandle<JSTaggedValue>(thread, queue->Pop(thread));
124 }
125 if (rootValue->IsRBTreeNode()) {
126 JSHandle<RBTreeNode> root = JSHandle<RBTreeNode>::Cast(rootValue);
127 if (!root->GetLeft().IsHole()) {
128 JSHandle<JSTaggedValue> left(thread, root->GetLeft());
129 queue.Update(JSTaggedValue(TaggedQueue::Push(thread, queue, left)));
130 }
131 if (!root->GetRight().IsHole()) {
132 JSHandle<JSTaggedValue> right(thread, root->GetRight());
133 queue.Update(JSTaggedValue(TaggedQueue::Push(thread, queue, right)));
134 }
135 }
136 iter->SetTaggedQueue(thread, queue.GetTaggedValue());
137 if (queue->Empty()) {
138 iter->SetTableIndex(++index);
139 }
140 return rootValue;
141 }
142
CreateHashSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)143 JSHandle<JSTaggedValue> JSAPIHashSetIterator::CreateHashSetIterator(JSThread *thread,
144 const JSHandle<JSTaggedValue> &obj,
145 IterationKind kind)
146 {
147 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
148 if (!obj->IsJSAPIHashSet()) {
149 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
150 "The Symbol.iterator method cannot be bound");
151 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
152 }
153 JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashSetIterator(JSHandle<JSAPIHashSet>(obj), kind));
154 return iter;
155 }
156 } // namespace panda::ecmascript