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