Files
ark_js_runtime/ecmascript/js_api_tree_map_iterator.cpp
T
xliu 0bb3e25edc Description
To ensure the high performance of container classes, TreeMap and
  TreeSet is provided in ark.
  Add test cases for TaggedTree.
Related issue
  #I4PQ1G:add TreeMap and TreeSet

Change-Id: I5cda72d06a71380711374109a87e971af6a8c5b7
Signed-off-by: xliu <liuxin259@huawei.com>
2022-02-10 11:28:34 +08:00

98 lines
4.3 KiB
C++

/*
* 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<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
if (!input->IsJSAPITreeMapIterator()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a tree map iterator", JSTaggedValue::Exception());
}
JSHandle<JSAPITreeMapIterator> iter(input);
// Let it be [[IteratedMap]].
JSHandle<JSTaggedValue> 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<TaggedTreeMap> map(thread, JSHandle<JSAPITreeMap>::Cast(iteratedMap)->GetTreeMap());
int elements = map->NumberOfElements();
JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries());
if (elements != static_cast<int>(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<JSTaggedValue> 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<JSTaggedValue> 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<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
array->Set(thread, 0, key);
array->Set(thread, 1, value);
JSHandle<JSTaggedValue> 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<JSTaggedValue> JSAPITreeMapIterator::CreateTreeMapIterator(JSThread *thread,
const JSHandle<JSTaggedValue> &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<JSTaggedValue> iter(factory->NewJSAPITreeMapIterator(JSHandle<JSAPITreeMap>(obj), kind));
return iter;
}
} // namespace panda::ecmascript