/* * 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. */ #ifndef ECMA_CONTAINER_COMMON_H #define ECMA_CONTAINER_COMMON_H #include "ecmascript/containers/containers_private.h" #include "ecmascript/ecma_string-inl.h" #include "ecmascript/global_env.h" #include "ecmascript/js_api/js_api_arraylist.h" #include "ecmascript/js_api/js_api_arraylist_iterator.h" #include "ecmascript/js_api/js_api_deque.h" #include "ecmascript/js_api/js_api_deque_iterator.h" #include "ecmascript/js_api/js_api_hashmap.h" #include "ecmascript/js_api/js_api_hashmap_iterator.h" #include "ecmascript/js_api/js_api_hashset.h" #include "ecmascript/js_api/js_api_hashset_iterator.h" #include "ecmascript/js_api/js_api_lightweightmap.h" #include "ecmascript/js_api/js_api_lightweightmap_iterator.h" #include "ecmascript/js_api/js_api_lightweightset.h" #include "ecmascript/js_api/js_api_lightweightset_iterator.h" #include "ecmascript/js_api/js_api_linked_list.h" #include "ecmascript/js_api/js_api_linked_list_iterator.h" #include "ecmascript/js_api/js_api_list.h" #include "ecmascript/js_api/js_api_list_iterator.h" #include "ecmascript/js_api/js_api_plain_array.h" #include "ecmascript/js_api/js_api_plain_array_iterator.h" #include "ecmascript/js_api/js_api_queue.h" #include "ecmascript/js_api/js_api_queue_iterator.h" #include "ecmascript/js_api/js_api_stack.h" #include "ecmascript/js_api/js_api_tree_map.h" #include "ecmascript/js_date_time_format.h" #include "ecmascript/js_tagged_value.h" #include "ecmascript/object_factory.h" #include "ecmascript/tagged_tree.h" #include "ecmascript/tests/test_common.h" namespace panda::test { using namespace panda; using namespace panda::ecmascript; using ecmascript::base::BuiltinsBase; constexpr uint32_t g_defaultSize = 8; class EcmaContainerCommon { public: static JSObject *JSObjectTestCreate(JSThread *thread) { [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread); EcmaVM *ecmaVM = thread->GetEcmaVM(); auto globalEnv = ecmaVM->GetGlobalEnv(); JSHandle jsFunc = globalEnv->GetObjectFunction(); JSHandle newObj = ecmaVM->GetFactory()->NewJSObjectByConstructor(JSHandle(jsFunc), jsFunc); return *newObj; } static JSAPIArrayList *CreateArrayList(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::ArrayList); JSHandle constructor(thread, result); JSHandle arrayList( factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle taggedArray = factory->NewTaggedArray(JSAPIArrayList::DEFAULT_CAPACITY_LENGTH); arrayList->SetElements(thread, taggedArray); return *arrayList; } static JSAPIPlainArray *CreatePlainArray(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::PlainArray); JSHandle constructor(thread, result); JSHandle plainArray( factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle keyArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); JSHandle valueArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); plainArray->SetKeys(thread, keyArray); plainArray->SetValues(thread, valueArray); return *plainArray; } static JSAPIDeque *CreateJSApiDeque(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::Deque); JSHandle constructor(thread, result); JSHandle deque(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle newElements = factory->NewTaggedArray(JSAPIDeque::DEFAULT_CAPACITY_LENGTH); deque->SetElements(thread, newElements); return *deque; } static JSAPIHashMap *CreateHashMap(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::HashMap); JSHandle constructor(thread, result); JSHandle map(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSTaggedValue hashMapArray = TaggedHashArray::Create(thread); map->SetTable(thread, hashMapArray); map->SetSize(0); return *map; } static JSAPIHashSet *CreateHashSet(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::HashSet); JSHandle constructor(thread, result); JSHandle set(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSTaggedValue hashSetArray = TaggedHashArray::Create(thread); set->SetTable(thread, hashSetArray); set->SetSize(0); return *set; } static JSAPILightWeightMap *CreateLightWeightMap(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::LightWeightMap); JSHandle constructor(thread, result); JSHandle lightWeightMap = JSHandle::Cast( factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle hashArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); JSHandle keyArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); JSHandle valueArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); lightWeightMap->SetHashes(thread, hashArray); lightWeightMap->SetKeys(thread, keyArray); lightWeightMap->SetValues(thread, valueArray); lightWeightMap->SetLength(0); return *lightWeightMap; } static JSAPILightWeightSet *CreateLightWeightSet(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::LightWeightSet); JSHandle constructor(thread, result); JSHandle lightweightSet = JSHandle::Cast( factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle hashArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); JSHandle valueArray = JSHandle(factory->NewTaggedArray(g_defaultSize)); lightweightSet->SetHashes(thread, hashArray); lightweightSet->SetValues(thread, valueArray); lightweightSet->SetLength(0); // 0 means the value return *lightweightSet; } static JSAPILinkedList *CreateLinkedList(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::LinkedList); JSHandle contianer(thread, result); JSHandle linkedList = JSHandle::Cast( factory->NewJSObjectByConstructor(JSHandle(contianer), contianer)); JSTaggedValue doubleList = TaggedDoubleList::Create(thread); linkedList->SetDoubleList(thread, doubleList); return *linkedList; } static JSAPIList *CreateList(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::List); JSHandle constructor(thread, result); JSHandle list(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSTaggedValue singleList = TaggedSingleList::Create(thread); list->SetSingleList(thread, singleList); return *list; } static JSHandle CreateQueue(JSThread *thread, int capacaty) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::Queue); JSHandle constructor(thread, result); JSHandle jsQueue(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSHandle newElements = factory->NewTaggedArray(capacaty); jsQueue->SetElements(thread, newElements); jsQueue->SetLength(thread, JSTaggedValue(0)); jsQueue->SetFront(0); jsQueue->SetTail(0); return jsQueue; } static JSHandle CreateJSApiStack(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::Stack); JSHandle constructor(thread, result); JSHandle jsStack(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); jsStack->SetTop(-1); return jsStack; } static JSHandle CreateTreeMap(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::TreeMap); JSHandle constructor(thread, result); JSHandle jsTreeMap( factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); JSTaggedValue internal = TaggedTreeMap::Create(thread); jsTreeMap->SetTreeMap(thread, internal); return jsTreeMap; } }; }; #endif