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 } // namespace panda::ecmascript
119