14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_weak_map.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 194514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 204514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 214514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 224514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 234514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 244514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 254514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 264514f5e3Sopenharmony_ci#include "ecmascript/js_map_iterator.h" 274514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h" 284514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 294514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 304514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h" 314514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 324514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h" 334514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 344514f5e3Sopenharmony_ci 354514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 364514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_cinamespace panda::test { 394514f5e3Sopenharmony_ciusing BuiltinsWeakMap = ecmascript::builtins::BuiltinsWeakMap; 404514f5e3Sopenharmony_ciusing JSWeakMap = ecmascript::JSWeakMap; 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_ciclass BuiltinsWeakMapTest : public BaseTestWithScope<false> { 434514f5e3Sopenharmony_ci}; 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_cistatic JSObject *JSObjectTestCreate(JSThread *thread) 464514f5e3Sopenharmony_ci{ 474514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 484514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 494514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 504514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> jsFunc = globalEnv->GetObjectFunction(); 524514f5e3Sopenharmony_ci return *factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc); 534514f5e3Sopenharmony_ci} 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ciJSWeakMap *CreateBuiltinsWeakMap(JSThread *thread) 564514f5e3Sopenharmony_ci{ 574514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 584514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetBuiltinsWeakMapFunction()); 594514f5e3Sopenharmony_ci // 4 : test case 604514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, newTarget.GetTaggedValue(), 4); 614514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 624514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 654514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsWeakMap::WeakMapConstructor(ecmaRuntimeCallInfo); 664514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsECMAObject()); 694514f5e3Sopenharmony_ci return JSWeakMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData())); 704514f5e3Sopenharmony_ci} 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci// new Map("abrupt").toString() 734514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakMapTest, CreateAndGetSize) 744514f5e3Sopenharmony_ci{ 754514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 764514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 774514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetBuiltinsWeakMapFunction()); 784514f5e3Sopenharmony_ci JSHandle<JSWeakMap> map(thread, CreateBuiltinsWeakMap(thread)); 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(factory->NewTaggedArray(1)); 814514f5e3Sopenharmony_ci JSHandle<TaggedArray> internalArray(factory->NewTaggedArray(2)); 824514f5e3Sopenharmony_ci JSTaggedValue value(JSObjectTestCreate(thread)); 834514f5e3Sopenharmony_ci internalArray->Set(thread, 0, value); 844514f5e3Sopenharmony_ci internalArray->Set(thread, 1, JSTaggedValue(0)); 854514f5e3Sopenharmony_ci auto result = JSArray::CreateArrayFromList(thread, internalArray); 864514f5e3Sopenharmony_ci array->Set(thread, 0, result); 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array); 894514f5e3Sopenharmony_ci 904514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 914514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 924514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue()); 934514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, values.GetTaggedValue()); 944514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetNewTarget(newTarget.GetTaggedValue()); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsWeakMap::WeakMapConstructor(ecmaRuntimeCallInfo); 994514f5e3Sopenharmony_ci JSHandle<JSWeakMap> weakMap(thread, JSWeakMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))); 1004514f5e3Sopenharmony_ci EXPECT_EQ(weakMap->GetSize(), 1); 1014514f5e3Sopenharmony_ci} 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakMapTest, SetAndHas) 1044514f5e3Sopenharmony_ci{ 1054514f5e3Sopenharmony_ci // create jsWeakMap 1064514f5e3Sopenharmony_ci JSHandle<JSWeakMap> weakMap(thread, CreateBuiltinsWeakMap(thread)); 1074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, JSObjectTestCreate(thread)); 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1104514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1114514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(weakMap.GetTaggedValue()); 1124514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue()); 1134514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1))); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci { 1164514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1174514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsWeakMap::Has(ecmaRuntimeCallInfo); 1184514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1194514f5e3Sopenharmony_ci 1204514f5e3Sopenharmony_ci EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData()); 1214514f5e3Sopenharmony_ci } 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci // test Set() 1244514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsWeakMap::Set(ecmaRuntimeCallInfo); 1254514f5e3Sopenharmony_ci EXPECT_TRUE(result2.IsECMAObject()); 1264514f5e3Sopenharmony_ci JSWeakMap *jsWeakMap = JSWeakMap::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData())); 1274514f5e3Sopenharmony_ci EXPECT_EQ(jsWeakMap->GetSize(), 1); 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci // test Has() 1304514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1314514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 1324514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, key.GetTaggedValue()); 1334514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1))); 1344514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(jsWeakMap)); 1354514f5e3Sopenharmony_ci { 1364514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 1374514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsWeakMap::Has(ecmaRuntimeCallInfo1); 1384514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1394514f5e3Sopenharmony_ci 1404514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 1414514f5e3Sopenharmony_ci } 1424514f5e3Sopenharmony_ci} 1434514f5e3Sopenharmony_ci 1444514f5e3Sopenharmony_civoid KeySetCommon(JSThread* thread, JSHandle<JSWeakMap>& weakMap, JSHandle<JSTaggedValue>& key, int32_t val) 1454514f5e3Sopenharmony_ci{ 1464514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1474514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1484514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(weakMap.GetTaggedValue()); 1494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue()); 1504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(val)); 1514514f5e3Sopenharmony_ci 1524514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1534514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsWeakMap::Set(ecmaRuntimeCallInfo); 1544514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1554514f5e3Sopenharmony_ci 1564514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsECMAObject()); 1574514f5e3Sopenharmony_ci JSWeakMap *jsWeakMap = JSWeakMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())); 1584514f5e3Sopenharmony_ci EXPECT_EQ(jsWeakMap->GetSize(), static_cast<int>(val) + 1); 1594514f5e3Sopenharmony_ci} 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakMapTest, DeleteAndRemove) 1624514f5e3Sopenharmony_ci{ 1634514f5e3Sopenharmony_ci // create jsWeakMap 1644514f5e3Sopenharmony_ci JSHandle<JSWeakMap> weakMap(thread, CreateBuiltinsWeakMap(thread)); 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci // add 40 keys 1674514f5e3Sopenharmony_ci JSTaggedValue lastKey(JSTaggedValue::Undefined()); 1684514f5e3Sopenharmony_ci for (int i = 0; i < 40; i++) { 1694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, JSObjectTestCreate(thread)); 1704514f5e3Sopenharmony_ci KeySetCommon(thread, weakMap, key, static_cast<int32_t>(i)); 1714514f5e3Sopenharmony_ci lastKey = key.GetTaggedValue(); 1724514f5e3Sopenharmony_ci } 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ci // whether jsWeakMap has delete lastKey 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1774514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 1784514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(weakMap.GetTaggedValue()); 1794514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, lastKey); 1804514f5e3Sopenharmony_ci 1814514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 1824514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsWeakMap::Has(ecmaRuntimeCallInfo1); 1834514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1844514f5e3Sopenharmony_ci 1854514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData()); 1864514f5e3Sopenharmony_ci 1874514f5e3Sopenharmony_ci // delete 1884514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsWeakMap::Delete(ecmaRuntimeCallInfo1); 1894514f5e3Sopenharmony_ci 1904514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_ci // check deleteKey is deleted 1934514f5e3Sopenharmony_ci JSTaggedValue result4 = BuiltinsWeakMap::Has(ecmaRuntimeCallInfo1); 1944514f5e3Sopenharmony_ci 1954514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData()); 1964514f5e3Sopenharmony_ci} 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakMapTest, SymbolKey) 1994514f5e3Sopenharmony_ci{ 2004514f5e3Sopenharmony_ci // create jsWeakMap 2014514f5e3Sopenharmony_ci JSHandle<JSWeakMap> weakMap(thread, CreateBuiltinsWeakMap(thread)); 2024514f5e3Sopenharmony_ci 2034514f5e3Sopenharmony_ci // add 2 symbol keys 2044514f5e3Sopenharmony_ci JSTaggedValue lastKey(JSTaggedValue::Undefined()); 2054514f5e3Sopenharmony_ci for (int i = 0; i < 2; i++) { 2064514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbolKey = thread->GetEcmaVM()->GetFactory()->NewJSSymbol(); 2074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(symbolKey); 2084514f5e3Sopenharmony_ci KeySetCommon(thread, weakMap, key, static_cast<int32_t>(i)); 2094514f5e3Sopenharmony_ci lastKey = key.GetTaggedValue(); 2104514f5e3Sopenharmony_ci } 2114514f5e3Sopenharmony_ci 2124514f5e3Sopenharmony_ci // check whether jsWeakMap can get and delete lastKey 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = 2154514f5e3Sopenharmony_ci TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call arg 2164514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 2174514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(weakMap.GetTaggedValue()); 2184514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, lastKey); 2194514f5e3Sopenharmony_ci 2204514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 2214514f5e3Sopenharmony_ci // get 2224514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsWeakMap::Get(ecmaRuntimeCallInfo1); 2234514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2244514f5e3Sopenharmony_ci EXPECT_EQ(result2, JSTaggedValue(1)); 2254514f5e3Sopenharmony_ci 2264514f5e3Sopenharmony_ci // delete 2274514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsWeakMap::Delete(ecmaRuntimeCallInfo1); 2284514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ci // check deleteKey is deleted 2314514f5e3Sopenharmony_ci JSTaggedValue result4 = BuiltinsWeakMap::Has(ecmaRuntimeCallInfo1); 2324514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData()); 2334514f5e3Sopenharmony_ci} 2344514f5e3Sopenharmony_ci} // namespace panda::test 235