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