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_api_tree_map_iterator.h"
17
18 #include "base/builtins_base.h"
19 #include "js_api_tree_map.h"
20 #include "js_array.h"
21 #include "tagged_tree-inl.h"
22 #include "object_factory.h"
23
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)26 JSTaggedValue JSAPITreeMapIterator::Next(EcmaRuntimeCallInfo *argv)
27 {
28 ASSERT(argv);
29 JSThread *thread = argv->GetThread();
30 [[maybe_unused]] EcmaHandleScope handleScope(thread);
31 // Let input be the this value
32 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
33
34 if (!input->IsJSAPITreeMapIterator()) {
35 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a tree map iterator", JSTaggedValue::Exception());
36 }
37 JSHandle<JSAPITreeMapIterator> iter(input);
38 // Let it be [[IteratedMap]].
39 JSHandle<JSTaggedValue> iteratedMap(thread, iter->GetIteratedMap());
40
41 // If it is undefined, return CreateIterResultObject(undefined, true).
42 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
43 if (iteratedMap->IsUndefined()) {
44 return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue();
45 }
46 JSHandle<TaggedTreeMap> map(thread, JSHandle<JSAPITreeMap>::Cast(iteratedMap)->GetTreeMap());
47 int elements = map->NumberOfElements();
48
49 JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries());
50 if (elements != static_cast<int>(entries->GetLength())) {
51 entries.Update(TaggedTreeMap::GetArrayFromMap(thread, map).GetTaggedValue());
52 iter->SetEntries(thread, entries);
53 }
54
55 // Let index be Map.[[NextIndex]].
56 int index = iter->GetNextIndex().GetInt();
57 if (index < elements) {
58 IterationKind itemKind = IterationKind(iter->GetIterationKind().GetInt());
59
60 int keyIndex = entries->Get(index).GetInt();
61 iter->SetNextIndex(thread, JSTaggedValue(index + 1));
62
63 JSHandle<JSTaggedValue> key(thread, map->GetKey(keyIndex));
64 // If itemKind is key, let result be e.[[Key]]
65 if (itemKind == IterationKind::KEY) {
66 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
67 }
68 JSHandle<JSTaggedValue> value(thread, map->GetValue(keyIndex));
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 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
74 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
75 array->Set(thread, 0, key);
76 array->Set(thread, 1, value);
77 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
78 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
79 }
80
81 // Set [[IteratedMap]] to undefined.
82 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
83 return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue();
84 }
85
CreateTreeMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)86 JSHandle<JSTaggedValue> JSAPITreeMapIterator::CreateTreeMapIterator(JSThread *thread,
87 const JSHandle<JSTaggedValue> &obj,
88 IterationKind kind)
89 {
90 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91 if (!obj->IsJSAPITreeMap()) {
92 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not TreeMap", thread->GlobalConstants()->GetHandledUndefined());
93 }
94 JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeMapIterator(JSHandle<JSAPITreeMap>(obj), kind));
95 return iter;
96 }
97 } // namespace panda::ecmascript
98