1 /*
2 * Copyright (c) 2021 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 "js_map_iterator.h"
17 #include "builtins/builtins_errors.h"
18 #include "js_array.h"
19 #include "js_map.h"
20 #include "linked_hash_table-inl.h"
21 #include "object_factory.h"
22
23 namespace panda::ecmascript {
24 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)25 JSTaggedValue JSMapIterator::Next(EcmaRuntimeCallInfo *argv)
26 {
27 ASSERT(argv);
28 JSThread *thread = argv->GetThread();
29 [[maybe_unused]] EcmaHandleScope handleScope(thread);
30 // 1.Let O be the this value
31 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
32
33 // 3.If O does not have all of the internal slots of a Map Iterator Instance (23.1.5.3), throw a TypeError
34 // exception.
35 if (!input->IsJSMapIterator()) {
36 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a map iterator", JSTaggedValue::Exception());
37 }
38 JSHandle<JSMapIterator> iter(input);
39 iter->Update(thread);
40 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
41 // 4.Let m be O.[[IteratedMap]].
42 JSHandle<JSTaggedValue> iteratedMap(thread, iter->GetIteratedMap());
43
44 // 5.Let index be O.[[MapNextIndex]].
45 int index = iter->GetNextIndex();
46 IterationKind itemKind = iter->GetIterationKind();
47 // 7.If m is undefined, return CreateIterResultObject(undefined, true).
48 if (iteratedMap->IsUndefined()) {
49 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
50 };
51 JSHandle<LinkedHashMap> map(iteratedMap);
52 int totalElements = map->NumberOfElements() + map->NumberOfDeletedElements();
53
54 JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
55 while (index < totalElements) {
56 JSTaggedValue key = map->GetKey(index);
57 if (!key.IsHole()) {
58 iter->SetNextIndex(index + 1);
59 keyHandle.Update(key);
60 // If itemKind is key, let result be e.[[Key]]
61 if (itemKind == IterationKind::KEY) {
62 return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue();
63 }
64 JSHandle<JSTaggedValue> value(thread, map->GetValue(index));
65 // Else if itemKind is value, let result be e.[[Value]].
66 if (itemKind == IterationKind::VALUE) {
67 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
68 }
69 // Else
70 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
71 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
72 array->Set(thread, 0, keyHandle);
73 array->Set(thread, 1, value);
74 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
75 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
76 }
77 index++;
78 }
79 // 13.Set O.[[IteratedMap]] to undefined.
80 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
81 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
82 }
83
Update(const JSThread * thread)84 void JSMapIterator::Update(const JSThread *thread)
85 {
86 [[maybe_unused]] DisallowGarbageCollection noGc;
87 JSTaggedValue iteratedMap = GetIteratedMap();
88 if (iteratedMap.IsUndefined()) {
89 return;
90 }
91 LinkedHashMap *map = LinkedHashMap::Cast(iteratedMap.GetTaggedObject());
92 if (map->GetNextTable().IsHole()) {
93 return;
94 }
95 int index = GetNextIndex();
96 JSTaggedValue nextTable = map->GetNextTable();
97 while (!nextTable.IsHole()) {
98 index -= map->GetDeletedElementsAt(index);
99 map = LinkedHashMap::Cast(nextTable.GetTaggedObject());
100 nextTable = map->GetNextTable();
101 }
102 SetIteratedMap(thread, JSTaggedValue(map));
103 SetNextIndex(index);
104 }
105
CreateMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)106 JSHandle<JSTaggedValue> JSMapIterator::CreateMapIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
107 IterationKind kind)
108 {
109 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
110 if (!obj->IsJSMap()) {
111 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
112 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSMap", undefinedHandle);
113 }
114 JSHandle<JSTaggedValue> iter(factory->NewJSMapIterator(JSHandle<JSMap>(obj), kind));
115 return iter;
116 }
117 } // namespace panda::ecmascript
118