1 /*
2 * Copyright (c) 2021-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_map_iterator.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/js_map.h"
21 #include "ecmascript/linked_hash_table.h"
22 #include "ecmascript/object_factory.h"
23
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)26 JSTaggedValue JSMapIterator::Next(EcmaRuntimeCallInfo *argv)
27 {
28 ASSERT(argv);
29 JSThread *thread = argv->GetThread();
30 [[maybe_unused]] EcmaHandleScope handleScope(thread);
31 // 1.Let O be the this value
32 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
33
34 // 3.If O does not have all of the internal slots of a Map Iterator Instance (23.1.5.3), throw a TypeError
35 // exception.
36 if (!input->IsJSMapIterator()) {
37 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a map iterator", JSTaggedValue::Exception());
38 }
39 JSHandle<JSMapIterator> iter(input);
40 iter->Update(thread);
41 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
42 // 4.Let m be O.[[IteratedMap]].
43 JSHandle<JSTaggedValue> iteratedMap(thread, iter->GetIteratedMap());
44
45 // 5.Let index be O.[[MapNextIndex]].
46 int index = static_cast<int>(iter->GetNextIndex());
47 IterationKind itemKind = iter->GetIterationKind();
48 // 7.If m is undefined, return CreateIterResultObject(undefined, true).
49 if (iteratedMap->IsUndefined()) {
50 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
51 };
52 JSHandle<LinkedHashMap> map(iteratedMap);
53 int totalElements = map->NumberOfElements() + map->NumberOfDeletedElements();
54
55 JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
56 while (index < totalElements) {
57 JSTaggedValue key = map->GetKey(index);
58 if (!key.IsHole()) {
59 iter->SetNextIndex(index + 1);
60 keyHandle.Update(key);
61 // If itemKind is key, let result be e.[[Key]]
62 if (itemKind == IterationKind::KEY) {
63 return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue();
64 }
65 JSHandle<JSTaggedValue> value(thread, map->GetValue(index));
66 // Else if itemKind is value, let result be e.[[Value]].
67 if (itemKind == IterationKind::VALUE) {
68 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
69 }
70 // Else
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, value);
75 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
76 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
77 }
78 index++;
79 }
80 // 13.Set O.[[IteratedMap]] to undefined.
81 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
82 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
83 }
84
Update(const JSThread * thread)85 void JSMapIterator::Update(const JSThread *thread)
86 {
87 [[maybe_unused]] DisallowGarbageCollection noGc;
88 JSTaggedValue iteratedMap = GetIteratedMap();
89 if (iteratedMap.IsUndefined()) {
90 return;
91 }
92 LinkedHashMap *map = LinkedHashMap::Cast(iteratedMap.GetTaggedObject());
93 if (map->GetNextTable().IsHole()) {
94 return;
95 }
96 int index = static_cast<int>(GetNextIndex());
97 JSTaggedValue nextTable = map->GetNextTable();
98 while (!nextTable.IsHole()) {
99 index -= map->GetDeletedElementsAt(index);
100 map = LinkedHashMap::Cast(nextTable.GetTaggedObject());
101 nextTable = map->GetNextTable();
102 }
103 SetIteratedMap(thread, JSTaggedValue(map));
104 SetNextIndex(index);
105 }
106
CreateMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)107 JSHandle<JSTaggedValue> JSMapIterator::CreateMapIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
108 IterationKind kind)
109 {
110 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
111 if (!obj->IsJSMap()) {
112 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
113 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", undefinedHandle);
114 }
115 JSHandle<JSTaggedValue> iter(factory->NewJSMapIterator(JSHandle<JSMap>(obj), kind));
116 return iter;
117 }
118
MapIteratorToList(JSThread * thread,JSHandle<JSTaggedValue> & items,JSHandle<JSTaggedValue> & method)119 JSTaggedValue JSMapIterator::MapIteratorToList(JSThread *thread, JSHandle<JSTaggedValue> &items,
120 JSHandle<JSTaggedValue> &method)
121 {
122 JSTaggedValue newArray = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue();
123 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
124 JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
125 JSHandle<JSObject> newArrayHandle(thread, newArray);
126 JSHandle<JSTaggedValue> iterator = JSIterator::GetIterator(thread, items, method);
127 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
128
129 JSHandle<JSMapIterator> iter(iterator);
130 JSHandle<JSTaggedValue> iteratedMap(thread, iter->GetIteratedMap());
131 if (iteratedMap->IsUndefined()) {
132 return newArrayHandle.GetTaggedValue();
133 }
134 IterationKind itemKind = iter->GetIterationKind();
135 JSHandle<LinkedHashMap> map(iteratedMap);
136 int totalElements = map->NumberOfElements() + map->NumberOfDeletedElements();
137 int index = static_cast<int>(iter->GetNextIndex());
138 int k = 0;
139
140 JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
141 JSMutableHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue::Undefined());
142 JSMutableHandle<JSTaggedValue> indexHandle(thread, JSTaggedValue::Undefined());
143 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
144 while (index < totalElements) {
145 JSTaggedValue key = map->GetKey(index);
146 indexHandle.Update(JSTaggedValue(k));
147 if (!key.IsHole()) {
148 keyHandle.Update(key);
149 valueHandle.Update(map->GetValue(index));
150 if (itemKind == IterationKind::KEY) {
151 JSObject::CreateDataPropertyOrThrow(thread, newArrayHandle, indexHandle, keyHandle);
152 } else if (itemKind == IterationKind::VALUE) {
153 JSObject::CreateDataPropertyOrThrow(thread, newArrayHandle, indexHandle, valueHandle);
154 } else {
155 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
156 array->Set(thread, 0, keyHandle);
157 array->Set(thread, 1, valueHandle);
158 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
159 JSObject::CreateDataPropertyOrThrow(thread, newArrayHandle, indexHandle, keyAndValue);
160 }
161 k++;
162 }
163 index++;
164 }
165
166 indexHandle.Update(JSTaggedValue(k));
167 JSTaggedValue::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(newArrayHandle), lengthKey, indexHandle, true);
168 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
169 return newArrayHandle.GetTaggedValue();
170 }
171 } // namespace panda::ecmascript
172