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