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 constexpr uint32_t NEW_ARRAY_LENGTH = 2; // 2 means the length of array
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 == IterationKind::KEY) {
61 return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue();
62 }
63 JSHandle<JSTaggedValue> value(thread, map->GetValue(index));
64 if (itemKind == IterationKind::VALUE) {
65 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
66 }
67 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
68 JSHandle<TaggedArray> array(factory->NewTaggedArray(NEW_ARRAY_LENGTH));
69 array->Set(thread, 0, keyHandle);
70 array->Set(thread, 1, value);
71 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
72 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
73 }
74 index++;
75 }
76 iter->SetIteratedMap(thread, JSTaggedValue::Undefined());
77 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
78 }
79
CreateMapIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)80 JSHandle<JSTaggedValue> JSSharedMapIterator::CreateMapIterator(JSThread *thread,
81 const JSHandle<JSTaggedValue> &obj, IterationKind kind)
82 {
83 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
84 if (!obj->IsJSSharedMap()) {
85 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
86 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not SharedMap", undefinedHandle);
87 }
88 JSHandle<JSTaggedValue> iter(factory->NewJSMapIterator(JSHandle<JSSharedMap>(obj), kind));
89 return iter;
90 }
91 } // namespace panda::ecmascript
92