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/global_env.h"
20 #include "ecmascript/js_api/js_api_hasharray_iterator.h"
21 #include "ecmascript/js_api/js_api_hashset.h"
22 namespace panda::ecmascript {
23 using BuiltinsBase = base::BuiltinsBase;
24 using ContainerError = containers::ContainerError;
25 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)26 JSTaggedValue JSAPIHashSetIterator::Next(EcmaRuntimeCallInfo *argv)
27 {
28 ASSERT(argv);
29 JSThread *thread = argv->GetThread();
30 [[maybe_unused]] EcmaHandleScope handleScope(thread);
31 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
32 if (!input->IsJSAPIHashSetIterator()) {
33 JSTaggedValue error =
34 ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
35 "The Symbol.iterator method cannot be bound");
36 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
37 }
38 JSHandle<JSAPIHashSetIterator> iter = JSHandle<JSAPIHashSetIterator>::Cast(input);
39 JSHandle<JSTaggedValue> iteratedHashSet(thread, iter->GetIteratedHashSet(thread));
40 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
41 if (iteratedHashSet->IsUndefined()) {
42 return env->GetUndefinedIteratorResult().GetTaggedValue();
43 }
44 JSHandle<JSAPIHashSet> hashSet = JSHandle<JSAPIHashSet>::Cast(iteratedHashSet);
45 JSHandle<TaggedHashArray> tableArr(thread, hashSet->GetTable(thread));
46 uint32_t tableLength = tableArr->GetLength();
47
48 uint32_t index = iter->GetNextIndex();
49
50 JSMutableHandle<TaggedQueue> queue(thread, iter->GetTaggedQueue(thread));
51 JSMutableHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue::Undefined());
52 JSMutableHandle<TaggedNode> currentNode(thread, JSTaggedValue::Undefined());
53 IterationKind itemKind = iter->GetIterationKind();
54 while (index < tableLength) {
55 currentNode.Update(JSAPIHashArrayIterator::GetCurrentNode<JSAPIHashSetIterator>(thread, iter, queue, tableArr));
56 if (currentNode.GetTaggedValue().IsHole()) {
57 iter->SetNextIndex(++index);
58 continue;
59 }
60 valueHandle.Update(currentNode->GetKey(thread));
61 if (itemKind == IterationKind::VALUE) {
62 return JSIterator::CreateIterResultObject(thread, valueHandle, false).GetTaggedValue();
63 }
64 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
66 array->Set(thread, 0, valueHandle);
67 array->Set(thread, 1, valueHandle);
68 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
69 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
70 }
71 // Set O.[[IteratedMap]] to undefined.
72 iter->SetIteratedHashSet(thread, JSTaggedValue::Undefined());
73 return env->GetUndefinedIteratorResult().GetTaggedValue();
74 }
75
CreateHashSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)76 JSHandle<JSTaggedValue> JSAPIHashSetIterator::CreateHashSetIterator(JSThread *thread,
77 const JSHandle<JSTaggedValue> &obj,
78 IterationKind kind)
79 {
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 if (!obj->IsJSAPIHashSet()) {
82 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
83 "The Symbol.iterator method cannot be bound");
84 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
85 }
86 JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashSetIterator(JSHandle<JSAPIHashSet>(obj), kind));
87 return iter;
88 }
89 } // namespace panda::ecmascript
90