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_symbol.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 214514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 234514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 244514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 254514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h" 264514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h" 274514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 284514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 304514f5e3Sopenharmony_ci#include "ecmascript/symbol_table.h" 314514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 344514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_cinamespace panda::test { 374514f5e3Sopenharmony_ciusing Symbol = ecmascript::builtins::BuiltinsSymbol; 384514f5e3Sopenharmony_ciusing BuiltinsBase = panda::ecmascript::base::BuiltinsBase; 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_ciclass BuiltinsSymbolTest : public BaseTestWithScope<false> { 414514f5e3Sopenharmony_ci}; 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_cienum class AlgorithmType { 444514f5e3Sopenharmony_ci TO_STRING, 454514f5e3Sopenharmony_ci VALUE_OF, 464514f5e3Sopenharmony_ci KEY_FOR, 474514f5e3Sopenharmony_ci BUILTIN_VALUE_OF, 484514f5e3Sopenharmony_ci BUILTIN_FOR, 494514f5e3Sopenharmony_ci BUILTIN_KEY_FOR, 504514f5e3Sopenharmony_ci BUILTIN_TO_PRIMITIVE, 514514f5e3Sopenharmony_ci}; 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ciJSTaggedValue SymbolAlgorithm(JSThread *thread, JSTaggedValue thisArg, std::vector<JSTaggedValue>& args, 544514f5e3Sopenharmony_ci uint32_t argLen = 8, AlgorithmType type = AlgorithmType::TO_STRING) 554514f5e3Sopenharmony_ci{ 564514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfos = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen); 574514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetFunction(JSTaggedValue::Undefined()); 584514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetThis(thisArg); 594514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 604514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetCallArg(i, args[i]); 614514f5e3Sopenharmony_ci } 624514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfos); 634514f5e3Sopenharmony_ci JSTaggedValue result; 644514f5e3Sopenharmony_ci switch (type) { 654514f5e3Sopenharmony_ci case AlgorithmType::TO_STRING: 664514f5e3Sopenharmony_ci result = Symbol::ToString(ecmaRuntimeCallInfos); 674514f5e3Sopenharmony_ci break; 684514f5e3Sopenharmony_ci case AlgorithmType::VALUE_OF: 694514f5e3Sopenharmony_ci result = Symbol::ValueOf(ecmaRuntimeCallInfos); 704514f5e3Sopenharmony_ci break; 714514f5e3Sopenharmony_ci case AlgorithmType::KEY_FOR: 724514f5e3Sopenharmony_ci result = Symbol::KeyFor(ecmaRuntimeCallInfos); 734514f5e3Sopenharmony_ci break; 744514f5e3Sopenharmony_ci case AlgorithmType::BUILTIN_VALUE_OF: 754514f5e3Sopenharmony_ci result = BuiltinsSymbol::ValueOf(ecmaRuntimeCallInfos); 764514f5e3Sopenharmony_ci break; 774514f5e3Sopenharmony_ci case AlgorithmType::BUILTIN_FOR: 784514f5e3Sopenharmony_ci result = BuiltinsSymbol::For(ecmaRuntimeCallInfos); 794514f5e3Sopenharmony_ci break; 804514f5e3Sopenharmony_ci case AlgorithmType::BUILTIN_KEY_FOR: 814514f5e3Sopenharmony_ci result = BuiltinsSymbol::KeyFor(ecmaRuntimeCallInfos); 824514f5e3Sopenharmony_ci break; 834514f5e3Sopenharmony_ci case AlgorithmType::BUILTIN_TO_PRIMITIVE: 844514f5e3Sopenharmony_ci result = BuiltinsSymbol::ToPrimitive(ecmaRuntimeCallInfos); 854514f5e3Sopenharmony_ci break; 864514f5e3Sopenharmony_ci default: 874514f5e3Sopenharmony_ci break; 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 904514f5e3Sopenharmony_ci return result; 914514f5e3Sopenharmony_ci} 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci// new Symbol.toString() 944514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolNoParameterToString) 954514f5e3Sopenharmony_ci{ 964514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewJSSymbol(); 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 1014514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, symbol.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING); 1024514f5e3Sopenharmony_ci JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 1034514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_ci auto symbolValue = ecmaVM->GetFactory()->NewFromASCII("Symbol()"); 1064514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(ecmaVM, symbolValue, resultHandle), 0); 1074514f5e3Sopenharmony_ci 1084514f5e3Sopenharmony_ci // Undefined not Object 1094514f5e3Sopenharmony_ci result = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 4, AlgorithmType::TO_STRING); 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 1124514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 1134514f5e3Sopenharmony_ci thread->ClearException(); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci // No Symbol data 1164514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1174514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(factory->NewTaggedArray(3)); 1184514f5e3Sopenharmony_ci result = SymbolAlgorithm(thread, array.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING); 1194514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 1204514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 1214514f5e3Sopenharmony_ci thread->ClearException(); 1224514f5e3Sopenharmony_ci} 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci// new Symbol("aaa").toString() 1254514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolWithParameterToString) 1264514f5e3Sopenharmony_ci{ 1274514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewPublicSymbolWithChar("aaa"); 1304514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 1314514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, symbol.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING); 1324514f5e3Sopenharmony_ci JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 1334514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 1344514f5e3Sopenharmony_ci 1354514f5e3Sopenharmony_ci auto symbolValue = ecmaVM->GetFactory()->NewFromASCII("Symbol(aaa)"); 1364514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(ecmaVM, symbolValue, resultHandle), 0); 1374514f5e3Sopenharmony_ci} 1384514f5e3Sopenharmony_ci 1394514f5e3Sopenharmony_ci// new Symbol().valueOf() 1404514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolNoParameterValueOf) 1414514f5e3Sopenharmony_ci{ 1424514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 1434514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewJSSymbol(); 1464514f5e3Sopenharmony_ci 1474514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 1484514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, symbol.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_VALUE_OF); 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsSymbol()); 1514514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 1524514f5e3Sopenharmony_ci 1534514f5e3Sopenharmony_ci JSHandle<JSFunction> symbolObject(env->GetSymbolFunction()); 1544514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> symbolValue(symbol); 1554514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> symbolRef = ecmaVM->GetFactory()->NewJSPrimitiveRef(symbolObject, symbolValue); 1564514f5e3Sopenharmony_ci 1574514f5e3Sopenharmony_ci auto otherResult = SymbolAlgorithm(thread, symbolRef.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_VALUE_OF); 1584514f5e3Sopenharmony_ci EXPECT_TRUE(otherResult.IsSymbol()); 1594514f5e3Sopenharmony_ci ASSERT_EQ(otherResult.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ci // Undefined not Object 1624514f5e3Sopenharmony_ci result = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 4, AlgorithmType::VALUE_OF); 1634514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 1644514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 1654514f5e3Sopenharmony_ci thread->ClearException(); 1664514f5e3Sopenharmony_ci 1674514f5e3Sopenharmony_ci // No Symbol data 1684514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1694514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(factory->NewTaggedArray(3)); 1704514f5e3Sopenharmony_ci result = SymbolAlgorithm(thread, array.GetTaggedValue(), args, 4, AlgorithmType::VALUE_OF); 1714514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 1724514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 1734514f5e3Sopenharmony_ci thread->ClearException(); 1744514f5e3Sopenharmony_ci} 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ci// new Symbol("bbb").valueOf() 1774514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolWithParameterValueOf) 1784514f5e3Sopenharmony_ci{ 1794514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 1804514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 1814514f5e3Sopenharmony_ci 1824514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewPublicSymbolWithChar("bbb"); 1834514f5e3Sopenharmony_ci 1844514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 1854514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, symbol.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_VALUE_OF); 1864514f5e3Sopenharmony_ci 1874514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsSymbol()); 1884514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 1894514f5e3Sopenharmony_ci 1904514f5e3Sopenharmony_ci JSHandle<JSFunction> symbolObject(env->GetSymbolFunction()); 1914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> symbolValue(symbol); 1924514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> symbolRef = ecmaVM->GetFactory()->NewJSPrimitiveRef(symbolObject, symbolValue); 1934514f5e3Sopenharmony_ci 1944514f5e3Sopenharmony_ci auto otherResult = SymbolAlgorithm(thread, symbolRef.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_VALUE_OF); 1954514f5e3Sopenharmony_ci EXPECT_TRUE(otherResult.IsSymbol()); 1964514f5e3Sopenharmony_ci ASSERT_EQ(otherResult.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 1974514f5e3Sopenharmony_ci} 1984514f5e3Sopenharmony_ci 1994514f5e3Sopenharmony_ci// new Symbol().for 2004514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolWithParameterFor) 2014514f5e3Sopenharmony_ci{ 2024514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 2034514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 2044514f5e3Sopenharmony_ci 2054514f5e3Sopenharmony_ci JSHandle<SymbolTable> tableHandle(env->GetRegisterSymbols()); 2064514f5e3Sopenharmony_ci 2074514f5e3Sopenharmony_ci JSHandle<EcmaString> string = ecmaVM->GetFactory()->NewFromASCII("ccc"); 2084514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor(string).GetLength(), 3U); 2094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> string_handle(string); 2104514f5e3Sopenharmony_ci ASSERT_EQ(tableHandle->ContainsKey(string_handle.GetTaggedValue()), false); 2114514f5e3Sopenharmony_ci 2124514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewSymbolWithTableWithChar("ccc"); 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{string.GetTaggedValue()}; 2154514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 6, AlgorithmType::BUILTIN_FOR); 2164514f5e3Sopenharmony_ci ASSERT_EQ(tableHandle->ContainsKey(string_handle.GetTaggedValue()), true); 2174514f5e3Sopenharmony_ci 2184514f5e3Sopenharmony_ci JSTaggedValue target(*symbol); 2194514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData() == target.GetRawData(), true); 2204514f5e3Sopenharmony_ci} 2214514f5e3Sopenharmony_ci 2224514f5e3Sopenharmony_ci// Symbol.keyFor (sym) 2234514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolKeyFor) 2244514f5e3Sopenharmony_ci{ 2254514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 2264514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewPublicSymbolWithChar("bbb"); 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{symbol.GetTaggedValue()}; 2314514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 6, AlgorithmType::BUILTIN_KEY_FOR); 2324514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); 2334514f5e3Sopenharmony_ci 2344514f5e3Sopenharmony_ci JSHandle<EcmaString> string = ecmaVM->GetFactory()->NewFromASCII("ccc"); 2354514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor(string).GetLength(), 3U); 2364514f5e3Sopenharmony_ci 2374514f5e3Sopenharmony_ci args[0] = string.GetTaggedValue(); 2384514f5e3Sopenharmony_ci SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 6, AlgorithmType::BUILTIN_FOR); 2394514f5e3Sopenharmony_ci 2404514f5e3Sopenharmony_ci JSHandle<JSSymbol> otherSymbol = ecmaVM->GetFactory()->NewPublicSymbolWithChar("ccc"); 2414514f5e3Sopenharmony_ci args[0] = otherSymbol.GetTaggedValue(); 2424514f5e3Sopenharmony_ci auto otherResult = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 6, AlgorithmType::BUILTIN_KEY_FOR); 2434514f5e3Sopenharmony_ci ASSERT_TRUE(otherResult.IsString()); 2444514f5e3Sopenharmony_ci JSHandle<SymbolTable> tableHandle(env->GetRegisterSymbols()); 2454514f5e3Sopenharmony_ci JSTaggedValue stringValue(*string); 2464514f5e3Sopenharmony_ci ASSERT_EQ(tableHandle->ContainsKey(stringValue), true); 2474514f5e3Sopenharmony_ci 2484514f5e3Sopenharmony_ci // not symbol 2494514f5e3Sopenharmony_ci args[0] = JSTaggedValue::Undefined(); 2504514f5e3Sopenharmony_ci result = SymbolAlgorithm(thread, JSTaggedValue::Undefined(), args, 6, AlgorithmType::KEY_FOR); 2514514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 2524514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 2534514f5e3Sopenharmony_ci thread->ClearException(); 2544514f5e3Sopenharmony_ci} 2554514f5e3Sopenharmony_ci 2564514f5e3Sopenharmony_ci// Symbol.ToPrimitive() 2574514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolToPrimitive) 2584514f5e3Sopenharmony_ci{ 2594514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 2604514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 2614514f5e3Sopenharmony_ci 2624514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewJSSymbol(); 2634514f5e3Sopenharmony_ci 2644514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2654514f5e3Sopenharmony_ci auto result = SymbolAlgorithm(thread, symbol.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_TO_PRIMITIVE); 2664514f5e3Sopenharmony_ci 2674514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsSymbol()); 2684514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 2694514f5e3Sopenharmony_ci 2704514f5e3Sopenharmony_ci JSHandle<JSFunction> symbolObject(env->GetSymbolFunction()); 2714514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> symbolValue(symbol); 2724514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> symbolRef = ecmaVM->GetFactory()->NewJSPrimitiveRef(symbolObject, symbolValue); 2734514f5e3Sopenharmony_ci 2744514f5e3Sopenharmony_ci auto otherResult = 2754514f5e3Sopenharmony_ci SymbolAlgorithm(thread, symbolRef.GetTaggedValue(), args, 4, AlgorithmType::BUILTIN_TO_PRIMITIVE); 2764514f5e3Sopenharmony_ci EXPECT_TRUE(otherResult.IsSymbol()); 2774514f5e3Sopenharmony_ci ASSERT_EQ(otherResult.GetRawData() == (JSTaggedValue(*symbol)).GetRawData(), true); 2784514f5e3Sopenharmony_ci} 2794514f5e3Sopenharmony_ci 2804514f5e3Sopenharmony_ci// constructor 2814514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolConstructor) 2824514f5e3Sopenharmony_ci{ 2834514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 2844514f5e3Sopenharmony_ci 2854514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2864514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2874514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 2884514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); 2894514f5e3Sopenharmony_ci 2904514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2914514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSymbol::SymbolConstructor(ecmaRuntimeCallInfo); 2924514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2934514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsSymbol()); 2944514f5e3Sopenharmony_ci JSSymbol *sym = reinterpret_cast<JSSymbol *>(result.GetRawData()); 2954514f5e3Sopenharmony_ci ASSERT_EQ(sym->GetDescription().IsUndefined(), true); 2964514f5e3Sopenharmony_ci 2974514f5e3Sopenharmony_ci JSHandle<EcmaString> string = ecmaVM->GetFactory()->NewFromASCII("ddd"); 2984514f5e3Sopenharmony_ci 2994514f5e3Sopenharmony_ci auto otherEcmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3004514f5e3Sopenharmony_ci otherEcmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3014514f5e3Sopenharmony_ci otherEcmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3024514f5e3Sopenharmony_ci otherEcmaRuntimeCallInfo->SetCallArg(0, string.GetTaggedValue()); 3034514f5e3Sopenharmony_ci 3044514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, otherEcmaRuntimeCallInfo); 3054514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSymbol::SymbolConstructor(otherEcmaRuntimeCallInfo); 3064514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3074514f5e3Sopenharmony_ci JSHandle<EcmaString> resultString = JSTaggedValue::ToString( 3084514f5e3Sopenharmony_ci thread, JSHandle<JSTaggedValue>(thread, reinterpret_cast<JSSymbol *>(result1.GetRawData())->GetDescription())); 3094514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(ecmaVM, resultString, string), 0); 3104514f5e3Sopenharmony_ci} 3114514f5e3Sopenharmony_ci 3124514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSymbolTest, SymbolGetter) 3134514f5e3Sopenharmony_ci{ 3144514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 3154514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 3164514f5e3Sopenharmony_ci 3174514f5e3Sopenharmony_ci JSHandle<JSSymbol> symbol = ecmaVM->GetFactory()->NewPublicSymbolWithChar(""); 3184514f5e3Sopenharmony_ci JSHandle<EcmaString> string = ecmaVM->GetFactory()->NewFromASCII(""); 3194514f5e3Sopenharmony_ci 3204514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3214514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3224514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(symbol.GetTaggedValue()); 3234514f5e3Sopenharmony_ci 3244514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3254514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSymbol::DescriptionGetter(ecmaRuntimeCallInfo); 3264514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3274514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3284514f5e3Sopenharmony_ci EcmaString *resString = reinterpret_cast<EcmaString *>(result.GetRawData()); 3294514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor(resString).GetLength(), 0U); 3304514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::StringsAreEqual(resString, *string), true); 3314514f5e3Sopenharmony_ci 3324514f5e3Sopenharmony_ci // value is not symbol 3334514f5e3Sopenharmony_ci JSHandle<JSFunction> symbolObject(env->GetSymbolFunction()); 3344514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> symbolValue(symbol); 3354514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> symbolRef = ecmaVM->GetFactory()->NewJSPrimitiveRef(symbolObject, symbolValue); 3364514f5e3Sopenharmony_ci ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3374514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3384514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(symbolRef.GetTaggedValue()); 3394514f5e3Sopenharmony_ci 3404514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3414514f5e3Sopenharmony_ci result = BuiltinsSymbol::DescriptionGetter(ecmaRuntimeCallInfo); 3424514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3434514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3444514f5e3Sopenharmony_ci resString = reinterpret_cast<EcmaString *>(result.GetRawData()); 3454514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor(resString).GetLength(), 0U); 3464514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::StringsAreEqual(resString, *string), true); 3474514f5e3Sopenharmony_ci 3484514f5e3Sopenharmony_ci // Undefined 3494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3514514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3524514f5e3Sopenharmony_ci 3534514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3544514f5e3Sopenharmony_ci result = BuiltinsSymbol::DescriptionGetter(ecmaRuntimeCallInfo); 3554514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3564514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 3574514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue::Exception()); 3584514f5e3Sopenharmony_ci thread->ClearException(); 3594514f5e3Sopenharmony_ci} 3604514f5e3Sopenharmony_ci} // namespace panda::test 361