/* * Copyright (c) 2021 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 "js_api_tree_map_iterator.h" #include "base/builtins_base.h" #include "js_api_tree_map.h" #include "js_array.h" #include "tagged_tree-inl.h" #include "object_factory.h" namespace panda::ecmascript { using BuiltinsBase = base::BuiltinsBase; JSTaggedValue JSAPITreeMapIterator::Next(EcmaRuntimeCallInfo *argv) { ASSERT(argv); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); // Let input be the this value JSHandle input(BuiltinsBase::GetThis(argv)); if (!input->IsJSAPITreeMapIterator()) { THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a tree map iterator", JSTaggedValue::Exception()); } JSHandle iter(input); // Let it be [[IteratedMap]]. JSHandle iteratedMap(thread, iter->GetIteratedMap()); // If it is undefined, return CreateIterResultObject(undefined, true). const GlobalEnvConstants *globalConst = thread->GlobalConstants(); if (iteratedMap->IsUndefined()) { return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue(); } JSHandle map(thread, JSHandle::Cast(iteratedMap)->GetTreeMap()); int elements = map->NumberOfElements(); JSMutableHandle entries(thread, iter->GetEntries()); if (elements != static_cast(entries->GetLength())) { entries.Update(TaggedTreeMap::GetArrayFromMap(thread, map).GetTaggedValue()); iter->SetEntries(thread, entries); } // Let index be Map.[[NextIndex]]. int index = iter->GetNextIndex().GetInt(); if (index < elements) { IterationKind itemKind = IterationKind(iter->GetIterationKind().GetInt()); int keyIndex = entries->Get(index).GetInt(); iter->SetNextIndex(thread, JSTaggedValue(index + 1)); JSHandle key(thread, map->GetKey(keyIndex)); // If itemKind is key, let result be e.[[Key]] if (itemKind == IterationKind::KEY) { return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue(); } JSHandle value(thread, map->GetValue(keyIndex)); // Else if itemKind is value, let result be e.[[Value]]. if (itemKind == IterationKind::VALUE) { return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue(); } ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle array(factory->NewTaggedArray(2)); // 2 means the length of array array->Set(thread, 0, key); array->Set(thread, 1, value); JSHandle keyAndValue(JSArray::CreateArrayFromList(thread, array)); return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue(); } // Set [[IteratedMap]] to undefined. iter->SetIteratedMap(thread, JSTaggedValue::Undefined()); return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue(); } JSHandle JSAPITreeMapIterator::CreateTreeMapIterator(JSThread *thread, const JSHandle &obj, IterationKind kind) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); if (!obj->IsJSAPITreeMap()) { THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not TreeMap", thread->GlobalConstants()->GetHandledUndefined()); } JSHandle iter(factory->NewJSAPITreeMapIterator(JSHandle(obj), kind)); return iter; } } // namespace panda::ecmascript