• 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_hashmap_iterator.h"
17 
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/js_api/js_api_hashmap.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 JSAPIHashMapIterator::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 
37     if (!input->IsJSAPIHashMapIterator()) {
38         JSTaggedValue error = 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<JSAPIHashMapIterator> iter = JSHandle<JSAPIHashMapIterator>::Cast(input);
43     JSHandle<JSTaggedValue> iteratedHashMap(thread, iter->GetIteratedHashMap());
44     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
45     // If m is undefined, return undefinedIteratorResult.
46     if (iteratedHashMap->IsUndefined()) {
47         return globalConst->GetUndefinedIterResult();
48     }
49     JSHandle<TaggedHashArray> tableArr(thread, JSHandle<JSAPIHashMap>::Cast(iteratedHashMap)->GetTable());
50     uint32_t tableLength = tableArr->GetLength();
51     uint32_t index = iter->GetNextIndex();
52 
53     JSMutableHandle<TaggedQueue> queue(thread, iter->GetTaggedQueue());
54     JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
55     JSMutableHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue::Undefined());
56     JSMutableHandle<TaggedNode> currentNode(thread, JSTaggedValue::Undefined());
57 
58     IterationKind itemKind = iter->GetIterationKind();
59     while (index < tableLength) {
60         currentNode.Update(JSAPIHashMapIterator::FastGetCurrentNode(thread, iter, queue, tableArr));
61         if (!currentNode.GetTaggedValue().IsHole() && !currentNode.GetTaggedValue().IsUndefined()) {
62             JSTaggedValue key = currentNode->GetKey();
63             keyHandle.Update(key);
64             if (itemKind == IterationKind::KEY) {
65                 return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue();
66             }
67             valueHandle.Update(currentNode->GetValue());
68             if (itemKind == IterationKind::VALUE) {
69                 return JSIterator::CreateIterResultObject(thread, valueHandle, false).GetTaggedValue();
70             }
71             ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
72             JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
73             array->Set(thread, 0, keyHandle);
74             array->Set(thread, 1, valueHandle);
75             JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
76             return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
77         }
78         index++;
79         if (!currentNode.GetTaggedValue().IsRBTreeNode()) {
80             iter->SetNextIndex(index);
81         }
82     }
83     // Set [[IteratedMap]] to undefined.
84     iter->SetIteratedHashMap(thread, JSTaggedValue::Undefined());
85     return globalConst->GetUndefinedIterResult();
86 }
87 
FastGetCurrentNode(JSThread * thread,JSHandle<JSAPIHashMapIterator> & iter,JSMutableHandle<TaggedQueue> & queue,JSHandle<TaggedHashArray> & tableArr)88 JSHandle<JSTaggedValue> JSAPIHashMapIterator::FastGetCurrentNode(JSThread *thread,
89                                                                  JSHandle<JSAPIHashMapIterator> &iter,
90                                                                  JSMutableHandle<TaggedQueue> &queue,
91                                                                  JSHandle<TaggedHashArray> &tableArr)
92 {
93     JSHandle<JSTaggedValue> rootValue(thread, JSTaggedValue::Undefined());
94     uint32_t index = iter->GetNextIndex();
95     JSHandle<JSTaggedValue> prevNodeValue(thread, iter->GetCurrentNodeResult());
96     if (prevNodeValue->IsRBTreeNode()) {
97         return GetCurrentNode(thread, iter, queue, tableArr);
98     }
99     if (prevNodeValue->IsUndefined() || prevNodeValue->IsHole()) {
100         rootValue = JSHandle<JSTaggedValue>(thread, tableArr->Get(index));
101         iter->SetCurrentNodeResult(thread, rootValue);
102         return rootValue;
103     }
104     JSHandle<LinkedNode> prevNode = JSHandle<LinkedNode>::Cast(prevNodeValue);
105     if (!prevNode->GetNext().IsHole()) {
106         JSHandle<JSTaggedValue> next(thread, prevNode->GetNext());
107         iter->SetCurrentNodeResult(thread, next);
108         return next;
109     }
110     iter->SetCurrentNodeResult(thread, JSTaggedValue::Undefined());
111     return rootValue;
112 }
113 
114 // level traversal
GetCurrentNode(JSThread * thread,JSHandle<JSAPIHashMapIterator> & iter,JSMutableHandle<TaggedQueue> & queue,JSHandle<TaggedHashArray> & tableArr)115 JSHandle<JSTaggedValue> JSAPIHashMapIterator::GetCurrentNode(JSThread *thread,
116                                                              JSHandle<JSAPIHashMapIterator> &iter,
117                                                              JSMutableHandle<TaggedQueue> &queue,
118                                                              JSHandle<TaggedHashArray> &tableArr)
119 {
120     JSHandle<JSTaggedValue> rootValue(thread, JSTaggedValue::Undefined());
121     uint32_t index = iter->GetNextIndex();
122     if (queue->Empty()) {
123         rootValue = JSHandle<JSTaggedValue>(thread, tableArr->Get(index));
124         if (rootValue->IsHole()) {
125             iter->SetNextIndex(++index);
126             return rootValue;
127         }
128     } else {
129         rootValue = JSHandle<JSTaggedValue>(thread, queue->Pop(thread));
130     }
131     if (rootValue->IsRBTreeNode()) {
132         JSHandle<RBTreeNode> root = JSHandle<RBTreeNode>::Cast(rootValue);
133         if (!root->GetLeft().IsHole()) {
134             JSHandle<JSTaggedValue> left(thread, root->GetLeft());
135             queue.Update(JSTaggedValue(TaggedQueue::Push(thread, queue, left)));
136         }
137         if (!root->GetRight().IsHole()) {
138             JSHandle<JSTaggedValue> right(thread, root->GetRight());
139             queue.Update(JSTaggedValue(TaggedQueue::Push(thread, queue, right)));
140         }
141     }
142     iter->SetTaggedQueue(thread, queue.GetTaggedValue());
143     if (queue->Empty()) {
144         iter->SetNextIndex(++index);
145     }
146     return rootValue;
147 }
148 
CreateHashMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)149 JSHandle<JSTaggedValue> JSAPIHashMapIterator::CreateHashMapIterator(JSThread *thread,
150                                                                     const JSHandle<JSTaggedValue> &obj,
151                                                                     IterationKind kind)
152 {
153     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
154     if (!obj->IsJSAPIHashMap()) {
155         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
156                                                             "The Symbol.iterator method cannot be bound");
157         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
158     }
159     JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashMapIterator(JSHandle<JSAPIHashMap>(obj), kind));
160     return iter;
161 }
162 }  // namespace panda::ecmascript