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_tree_set_iterator.h"
17
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/js_api/js_api_tree_set.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/tagged_array-inl.h"
24 #include "ecmascript/tagged_tree.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 JSAPITreeSetIterator::Next(EcmaRuntimeCallInfo *argv)
31 {
32 ASSERT(argv);
33 JSThread *thread = argv->GetThread();
34 [[maybe_unused]] EcmaHandleScope handleScope(thread);
35 // Let input be the this value
36 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
37
38 if (!input->IsJSAPITreeSetIterator()) {
39 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
40 "The Symbol.iterator method cannot be bound");
41 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
42 }
43 JSHandle<JSAPITreeSetIterator> iter(input);
44 // Let it be [[IteratedSet]].
45 JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
46
47 // If it is undefined, return undefinedIteratorResult.
48 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
49 if (iteratedSet->IsUndefined()) {
50 return globalConst->GetUndefinedIterResult();
51 }
52 JSHandle<TaggedTreeSet> set(thread, JSHandle<JSAPITreeSet>::Cast(iteratedSet)->GetTreeSet());
53 uint32_t elements = static_cast<uint32_t>(set->NumberOfElements());
54
55 JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries());
56 if ((iter->GetEntries().IsHole()) || (elements != entries->GetLength())) {
57 entries.Update(TaggedTreeSet::GetArrayFromSet(thread, set).GetTaggedValue());
58 iter->SetEntries(thread, entries);
59 }
60
61 // Let index be Set.[[NextIndex]].
62 uint32_t index = static_cast<uint32_t>(iter->GetNextIndex());
63 if (index < elements) {
64 IterationKind itemKind = IterationKind(iter->GetIterationKind());
65
66 int keyIndex = entries->Get(index).GetInt();
67 iter->SetNextIndex(index + 1);
68
69 JSHandle<JSTaggedValue> key(thread, set->GetKey(keyIndex));
70 // If itemKind is key or value, let result be e.[[Key]].
71 if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
72 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
73 }
74 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
75 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
76 array->Set(thread, 0, key);
77 array->Set(thread, 1, key);
78 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
79 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
80 }
81
82 // Set [[IteratedSet]] to undefined.
83 iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
84 return globalConst->GetUndefinedIterResult();
85 }
86
CreateTreeSetIterator(JSThread * thread,JSHandle<JSTaggedValue> & obj,IterationKind kind)87 JSHandle<JSTaggedValue> JSAPITreeSetIterator::CreateTreeSetIterator(JSThread *thread,
88 JSHandle<JSTaggedValue> &obj,
89 IterationKind kind)
90 {
91 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
92 if (!obj->IsJSAPITreeSet()) {
93 if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget().IsJSAPITreeSet()) {
94 obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget());
95 } else {
96 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
97 "The Symbol.iterator method cannot be bound");
98 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
99 JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
100 }
101 }
102 JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeSetIterator(JSHandle<JSAPITreeSet>(obj), kind));
103 return iter;
104 }
105 } // namespace panda::ecmascript
106