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_map_iterator.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_api/js_api_tree_map.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/tagged_tree.h"
23
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
26 using ContainerError = containers::ContainerError;
27 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)28 JSTaggedValue JSAPITreeMapIterator::Next(EcmaRuntimeCallInfo *argv)
29 {
30 ASSERT(argv);
31 JSThread *thread = argv->GetThread();
32 [[maybe_unused]] EcmaHandleScope handleScope(thread);
33 // Let input be the this value
34 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
35
36 if (!input->IsJSAPITreeMapIterator()) {
37 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
38 "The Symbol.iterator method cannot be bound");
39 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
40 }
41 JSHandle<JSAPITreeMapIterator> iter(input);
42 // Let it be [[IteratedMap]].
43 JSHandle<JSTaggedValue> iteratedMap(thread, iter->GetIteratedMap(thread));
44
45 // If it is undefined, return undefinedIteratorResult.
46 if (iteratedMap->IsUndefined()) {
47 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
48 return env->GetUndefinedIteratorResult().GetTaggedValue();
49 }
50 JSHandle<TaggedTreeMap> map(thread, JSHandle<JSAPITreeMap>::Cast(iteratedMap)->GetTreeMap(thread));
51 uint32_t elements = static_cast<uint32_t>(map->NumberOfElements());
52
53 JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries(thread));
54 if ((iter->GetEntries(thread).IsHole()) || (elements != entries->GetLength())) {
55 entries.Update(TaggedTreeMap::GetArrayFromMap(thread, map).GetTaggedValue());
56 iter->SetEntries(thread, entries);
57 }
58
59 // Let index be Map.[[NextIndex]].
60 uint32_t index = static_cast<uint32_t>(iter->GetNextIndex());
61 if (index < elements) {
62 IterationKind itemKind = IterationKind(iter->GetIterationKind());
63
64 int keyIndex = entries->Get(thread, index).GetInt();
65 iter->SetNextIndex(index + 1);
66
67 JSHandle<JSTaggedValue> key(thread, map->GetKey(thread, keyIndex));
68 // If itemKind is key, let result be e.[[Key]]
69 if (itemKind == IterationKind::KEY) {
70 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
71 }
72 JSHandle<JSTaggedValue> value(thread, map->GetValue(thread, keyIndex));
73 // Else if itemKind is value, let result be e.[[Value]].
74 if (itemKind == IterationKind::VALUE) {
75 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
76 }
77 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
78 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
79 array->Set(thread, 0, key);
80 array->Set(thread, 1, value);
81 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
82 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
83 }
84
85 // Set [[IteratedMap]] to undefined.
86 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
87 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
88 return env->GetUndefinedIteratorResult().GetTaggedValue();
89 }
90
CreateTreeMapIterator(JSThread * thread,JSHandle<JSTaggedValue> & obj,IterationKind kind)91 JSHandle<JSTaggedValue> JSAPITreeMapIterator::CreateTreeMapIterator(JSThread *thread,
92 JSHandle<JSTaggedValue> &obj,
93 IterationKind kind)
94 {
95 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
96 if (!obj->IsJSAPITreeMap()) {
97 if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget(thread).IsJSAPITreeMap()) {
98 obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget(thread));
99 } else {
100 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
101 "The Symbol.iterator method cannot be bound");
102 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
103 JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
104 }
105 }
106 JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeMapIterator(JSHandle<JSAPITreeMap>(obj), kind));
107 return iter;
108 }
109 } // namespace panda::ecmascript
110