/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/shared_objects/js_shared_map_iterator.h" #include "ecmascript/js_array.h" #include "ecmascript/linked_hash_table.h" #include "ecmascript/shared_objects/concurrent_api_scope.h" #include "ecmascript/shared_objects/js_shared_map.h" namespace panda::ecmascript { using BuiltinsBase = base::BuiltinsBase; JSTaggedValue JSSharedMapIterator::Next(EcmaRuntimeCallInfo *argv) { ASSERT(argv); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); JSHandle thisObj(BuiltinsBase::GetThis(argv)); return NextInternal(thread, thisObj); } JSTaggedValue JSSharedMapIterator::NextInternal(JSThread *thread, JSHandle thisObj) { if (!thisObj->IsJSSharedMapIterator()) { THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a map iterator", JSTaggedValue::Exception()); } JSHandle iter(thisObj); JSHandle undefinedHandle(thread, JSTaggedValue::Undefined()); if (iter->GetIteratedMap(thread).IsUndefined()) { return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue(); } JSHandle iteratedMap(thread, iter->GetIteratedMap(thread)); [[maybe_unused]] ConcurrentApiScope scope(thread, JSHandle::Cast(iteratedMap)); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception()); JSHandle map(thread, iteratedMap->GetLinkedMap(thread)); int index = static_cast(iter->GetNextIndex()); IterationKind itemKind = iter->GetIterationKind(); int totalElements = map->NumberOfElements() + map->NumberOfDeletedElements(); constexpr uint32_t NEW_ARRAY_LENGTH = 2; // 2 means the length of array JSMutableHandle keyHandle(thread, JSTaggedValue::Undefined()); while (index < totalElements) { JSTaggedValue key = map->GetKey(thread, index); if (!key.IsHole()) { iter->SetNextIndex(index + 1); keyHandle.Update(key); if (itemKind == IterationKind::KEY) { return JSIterator::CreateIterResultObject(thread, keyHandle, false).GetTaggedValue(); } JSHandle value(thread, map->GetValue(thread, index)); if (itemKind == IterationKind::VALUE) { return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue(); } ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle array(factory->NewTaggedArray(NEW_ARRAY_LENGTH)); array->Set(thread, 0, keyHandle); array->Set(thread, 1, value); JSHandle keyAndValue(JSArray::CreateArrayFromList(thread, array)); return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue(); } index++; } iter->SetIteratedMap(thread, JSTaggedValue::Undefined()); return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue(); } JSHandle JSSharedMapIterator::CreateMapIterator(JSThread *thread, const JSHandle &obj, IterationKind kind) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); if (!obj->IsJSSharedMap()) { JSHandle undefinedHandle(thread, JSTaggedValue::Undefined()); THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not SharedMap", undefinedHandle); } JSHandle iter(factory->NewJSMapIterator(JSHandle(obj), kind)); return iter; } } // namespace panda::ecmascript