1 /*
2 * Copyright (c) 2024 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/shared_objects/js_shared_map_iterator.h"
17
18 #include "ecmascript/js_array.h"
19 #include "ecmascript/linked_hash_table.h"
20 #include "ecmascript/shared_objects/concurrent_api_scope.h"
21 #include "ecmascript/shared_objects/js_shared_map.h"
22
23 namespace panda::ecmascript {
24 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)25 JSTaggedValue JSSharedMapIterator::Next(EcmaRuntimeCallInfo *argv)
26 {
27 ASSERT(argv);
28 JSThread *thread = argv->GetThread();
29 [[maybe_unused]] EcmaHandleScope handleScope(thread);
30 JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
31 return NextInternal(thread, thisObj);
32 }
33
NextInternal(JSThread * thread,JSHandle<JSTaggedValue> thisObj)34 JSTaggedValue JSSharedMapIterator::NextInternal(JSThread *thread, JSHandle<JSTaggedValue> thisObj)
35 {
36 if (!thisObj->IsJSSharedMapIterator()) {
37 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a map iterator", JSTaggedValue::Exception());
38 }
39 JSHandle<JSSharedMapIterator> iter(thisObj);
40 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
41 if (iter->GetIteratedMap().IsUndefined()) {
42 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
43 };
44 JSHandle<JSSharedMap> iteratedMap(thread, iter->GetIteratedMap());
45 [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(iteratedMap));
46 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception());
47 JSHandle<LinkedHashMap> map(thread, iteratedMap->GetLinkedMap());
48
49 int index = static_cast<int>(iter->GetNextIndex());
50 IterationKind itemKind = iter->GetIterationKind();
51 int totalElements = map->NumberOfElements() + map->NumberOfDeletedElements();
52
53 JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
54 while (index < totalElements) {
55 JSTaggedValue key = map->GetKey(index);
56 if (!key.IsHole()) {
57 iter->SetNextIndex(index + 1);
58 keyHandle.Update(key);
59 if (itemKind == IterationKind::KEY) {
60 return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue();
61 }
62 JSHandle<JSTaggedValue> value(thread, map->GetValue(index));
63 if (itemKind == IterationKind::VALUE) {
64 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
65 }
66 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
68 array->Set(thread, 0, keyHandle);
69 array->Set(thread, 1, value);
70 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
71 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
72 }
73 index++;
74 }
75 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
76 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
77 }
78
CreateMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)79 JSHandle<JSTaggedValue> JSSharedMapIterator::CreateMapIterator(JSThread *thread,
80 const JSHandle<JSTaggedValue> &obj, IterationKind kind)
81 {
82 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
83 if (!obj->IsJSSharedMap()) {
84 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
85 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not SharedMap", undefinedHandle);
86 }
87 JSHandle<JSTaggedValue> iter(factory->NewJSMapIterator(JSHandle<JSSharedMap>(obj), kind));
88 return iter;
89 }
90 } // namespace panda::ecmascript
91