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