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_json.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include <algorithm> 194514f5e3Sopenharmony_ci#include <iomanip> 204514f5e3Sopenharmony_ci#include <sstream> 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 234514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_bigint.h" 244514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_errors.h" 254514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_proxy.h" 264514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_typedarray.h" 274514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 284514f5e3Sopenharmony_ci#include "ecmascript/ecma_string-inl.h" 294514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 304514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 314514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 324514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 334514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 344514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h" 354514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h" 364514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 374514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 384514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 394514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 404514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 434514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_cinamespace panda::test { 464514f5e3Sopenharmony_ciclass BuiltinsJsonTest : public BaseTestWithScope<false> { 474514f5e3Sopenharmony_cipublic: 484514f5e3Sopenharmony_ci class TestClass : public base::BuiltinsBase { 494514f5e3Sopenharmony_ci public: 504514f5e3Sopenharmony_ci static JSTaggedValue TestForCommon(EcmaRuntimeCallInfo *argv) 514514f5e3Sopenharmony_ci { 524514f5e3Sopenharmony_ci JSTaggedValue key = GetCallArg(argv, 0).GetTaggedValue(); 534514f5e3Sopenharmony_ci if (key.IsUndefined()) { 544514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci JSTaggedValue value = GetCallArg(argv, 1).GetTaggedValue(); 574514f5e3Sopenharmony_ci if (value.IsUndefined()) { 584514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci return JSTaggedValue(value); 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci static JSTaggedValue TestForParse(EcmaRuntimeCallInfo *argv) 654514f5e3Sopenharmony_ci { 664514f5e3Sopenharmony_ci return TestForCommon(argv); 674514f5e3Sopenharmony_ci } 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ci static JSTaggedValue TestForParse1(EcmaRuntimeCallInfo *argv) 704514f5e3Sopenharmony_ci { 714514f5e3Sopenharmony_ci (void)argv; 724514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 734514f5e3Sopenharmony_ci } 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ci static JSTaggedValue TestForStringfy(EcmaRuntimeCallInfo *argv) 764514f5e3Sopenharmony_ci { 774514f5e3Sopenharmony_ci uint32_t argc = argv->GetArgsNumber(); 784514f5e3Sopenharmony_ci if (argc > 0) { 794514f5e3Sopenharmony_ci return TestForCommon(argv); 804514f5e3Sopenharmony_ci } 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 834514f5e3Sopenharmony_ci } 844514f5e3Sopenharmony_ci }; 854514f5e3Sopenharmony_ci}; 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ciJSTaggedValue CreateBuiltinJSObject1(JSThread *thread, const CString keyCStr) 884514f5e3Sopenharmony_ci{ 894514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 904514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 914514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 924514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objectFunc(globalEnv->GetObjectFunction()); 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci JSHandle<JSObject> jsobject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objectFunc), objectFunc)); 954514f5e3Sopenharmony_ci EXPECT_TRUE(*jsobject != nullptr); 964514f5e3Sopenharmony_ci 974514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII(&keyCStr[0])); 984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1)); 994514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsobject), key, value); 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci CString str2 = "y"; 1024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(factory->NewFromASCII(str2)); 1034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2.5)); // 2.5 : test case 1044514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsobject), key2, value2); 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci CString str3 = "z"; 1074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key3(factory->NewFromASCII(str3)); 1084514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value3(factory->NewFromASCII("abc")); 1094514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsobject), key3, value3); 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci return jsobject.GetTaggedValue(); 1124514f5e3Sopenharmony_ci} 1134514f5e3Sopenharmony_ci// Math.abs(-10) 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse10) 1164514f5e3Sopenharmony_ci{ 1174514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1184514f5e3Sopenharmony_ci 1194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII( 1204514f5e3Sopenharmony_ci "\t\r \n{\t\r \n \"property\"\t\r \n:\t\r \n{\t\r \n}\t\r \n,\t\r \n \"prop2\"\t\r \n:\t\r \n [\t\r \ntrue\t\r " 1214514f5e3Sopenharmony_ci "\n,\t\r \nnull\t\r \n,123.456\t\r \n] \t\r \n}\t\r \n")); 1224514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1254514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1264514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1274514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1304514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 1314514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 1324514f5e3Sopenharmony_ci} 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse21) 1354514f5e3Sopenharmony_ci{ 1364514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1374514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1384514f5e3Sopenharmony_ci 1394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("[100,2.5,\"abc\"]")); 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForParse)); 1424514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 1434514f5e3Sopenharmony_ci 1444514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1454514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1464514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1474514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); 1484514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1514514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 1524514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 1534514f5e3Sopenharmony_ci} 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse) 1564514f5e3Sopenharmony_ci{ 1574514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1584514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lengthKeyHandle = thread->GlobalConstants()->GetHandledLengthString(); 1594514f5e3Sopenharmony_ci 1604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("[100,2.5,\"abc\"]")); 1614514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 1624514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{str.GetTaggedValue()}; 1634514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6); 1644514f5e3Sopenharmony_ci 1654514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1664514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 1674514f5e3Sopenharmony_ci JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData())); 1684514f5e3Sopenharmony_ci ASSERT_TRUE(value.IsECMAObject()); 1694514f5e3Sopenharmony_ci JSHandle<JSObject> valueHandle(thread, value); 1704514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lenResult = 1714514f5e3Sopenharmony_ci JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(valueHandle), lengthKeyHandle).GetValue(); 1724514f5e3Sopenharmony_ci uint32_t length = JSTaggedValue::ToLength(thread, lenResult).ToUint32(); 1734514f5e3Sopenharmony_ci EXPECT_EQ(length, 3U); 1744514f5e3Sopenharmony_ci} 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse2) 1774514f5e3Sopenharmony_ci{ 1784514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("{\"epf\":100,\"key1\":200}")); 1804514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 1814514f5e3Sopenharmony_ci 1824514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{str.GetTaggedValue()}; 1834514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6); 1844514f5e3Sopenharmony_ci 1854514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1864514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 1874514f5e3Sopenharmony_ci JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData())); 1884514f5e3Sopenharmony_ci ASSERT_TRUE(value.IsECMAObject()); 1894514f5e3Sopenharmony_ci JSHandle<JSObject> valueHandle(thread, value); 1904514f5e3Sopenharmony_ci 1914514f5e3Sopenharmony_ci JSHandle<TaggedArray> nameList(JSObject::EnumerableOwnNames(thread, valueHandle)); 1924514f5e3Sopenharmony_ci JSHandle<JSArray> nameResult = JSArray::CreateArrayFromList(thread, nameList); 1934514f5e3Sopenharmony_ci 1944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleKey(nameResult); 1954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lengthKey(factory->NewFromASCII("length")); 1964514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> lenResult = JSObject::GetProperty(thread, handleKey, lengthKey).GetValue(); 1974514f5e3Sopenharmony_ci uint32_t length = JSTaggedValue::ToLength(thread, lenResult).ToUint32(); 1984514f5e3Sopenharmony_ci ASSERT_EQ(length, 2U); 1994514f5e3Sopenharmony_ci} 2004514f5e3Sopenharmony_ci 2014514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse3) 2024514f5e3Sopenharmony_ci{ 2034514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2044514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromStdString("\"\\u0000\""); 2054514f5e3Sopenharmony_ci 2064514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2074514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2084514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 2094514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2124514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 2134514f5e3Sopenharmony_ci uint32_t length = EcmaStringAccessor(result).GetLength(); 2144514f5e3Sopenharmony_ci ASSERT_EQ(length, 1U); 2154514f5e3Sopenharmony_ci} 2164514f5e3Sopenharmony_ci 2174514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Parse4) 2184514f5e3Sopenharmony_ci{ 2194514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2204514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromStdString("{\n\t\"on\":\t0\n}"); 2214514f5e3Sopenharmony_ci JSHandle<EcmaString> key = factory->NewFromStdString("on"); 2224514f5e3Sopenharmony_ci 2234514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2244514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2254514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 2264514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2294514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); 2304514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = 2314514f5e3Sopenharmony_ci JSTaggedValue::GetProperty(thread, JSHandle<JSTaggedValue>(thread, result), JSHandle<JSTaggedValue>(key)) 2324514f5e3Sopenharmony_ci .GetValue(); 2334514f5e3Sopenharmony_ci int32_t number = JSTaggedValue::ToInt32(thread, value); 2344514f5e3Sopenharmony_ci ASSERT_EQ(number, 0); 2354514f5e3Sopenharmony_ci} 2364514f5e3Sopenharmony_ci 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify11) 2394514f5e3Sopenharmony_ci{ 2404514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2414514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, CreateBuiltinJSObject1(thread, "x")); 2434514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc = 2444514f5e3Sopenharmony_ci factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForStringfy)); 2454514f5e3Sopenharmony_ci 2464514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 2474514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2484514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 2494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 2504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); 2514514f5e3Sopenharmony_ci 2524514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2534514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 2544514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 2554514f5e3Sopenharmony_ci} 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify12) 2584514f5e3Sopenharmony_ci{ 2594514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, CreateBuiltinJSObject1(thread, "x")); 2614514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2624514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc = 2634514f5e3Sopenharmony_ci factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForStringfy)); 2644514f5e3Sopenharmony_ci 2654514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); 2664514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2674514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 2684514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 2694514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); 2704514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(static_cast<int32_t>(10))); 2714514f5e3Sopenharmony_ci 2724514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2734514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 2744514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 2754514f5e3Sopenharmony_ci} 2764514f5e3Sopenharmony_ci 2774514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify13) 2784514f5e3Sopenharmony_ci{ 2794514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, CreateBuiltinJSObject1(thread, "x")); 2814514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2824514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc = 2834514f5e3Sopenharmony_ci factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForStringfy)); 2844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("tttt")); 2854514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 2864514f5e3Sopenharmony_ci 2874514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{obj.GetTaggedValue(), handleFunc.GetTaggedValue(), str.GetTaggedValue()}; 2884514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 10); 2894514f5e3Sopenharmony_ci 2904514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2914514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 2924514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 2934514f5e3Sopenharmony_ci} 2944514f5e3Sopenharmony_ci 2954514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify14) 2964514f5e3Sopenharmony_ci{ 2974514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, CreateBuiltinJSObject1(thread, "x")); 2994514f5e3Sopenharmony_ci JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 3004514f5e3Sopenharmony_ci 3014514f5e3Sopenharmony_ci JSHandle<JSObject> obj1(thread, arr); 3024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0)); 3034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value0(factory->NewFromASCII("x")); 3044514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key0, value0); 3054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1)); 3064514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value1(factory->NewFromASCII("z")); 3074514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key1, value1); 3084514f5e3Sopenharmony_ci 3094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("tttt")); 3104514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 3114514f5e3Sopenharmony_ci 3124514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); 3134514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3144514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 3164514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, obj1.GetTaggedValue()); 3174514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(2, str.GetTaggedValue()); 3184514f5e3Sopenharmony_ci 3194514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3204514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 3214514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3224514f5e3Sopenharmony_ci} 3234514f5e3Sopenharmony_ci 3244514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify) 3254514f5e3Sopenharmony_ci{ 3264514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(thread, CreateBuiltinJSObject1(thread, "x")); 3274514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3284514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3294514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3304514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 3314514f5e3Sopenharmony_ci 3324514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3334514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 3344514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3354514f5e3Sopenharmony_ci} 3364514f5e3Sopenharmony_ci 3374514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify1) 3384514f5e3Sopenharmony_ci{ 3394514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 3404514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 3414514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 3424514f5e3Sopenharmony_ci 3434514f5e3Sopenharmony_ci JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 3444514f5e3Sopenharmony_ci 3454514f5e3Sopenharmony_ci EXPECT_TRUE(arr != nullptr); 3464514f5e3Sopenharmony_ci JSHandle<JSObject> obj(thread, arr); 3474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0)); 3484514f5e3Sopenharmony_ci 3494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(factory->NewFromASCII("def")); 3504514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key0, value); 3514514f5e3Sopenharmony_ci 3524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1)); 3534514f5e3Sopenharmony_ci PropertyDescriptor desc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(200)), true, true, true); 3544514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, obj, key1, desc1); 3554514f5e3Sopenharmony_ci 3564514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2)); 3574514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(factory->NewFromASCII("abc")); 3584514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key2, value2); 3594514f5e3Sopenharmony_ci 3604514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunc = 3614514f5e3Sopenharmony_ci factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForStringfy)); 3624514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> msg(factory->NewFromASCII("tttt")); 3634514f5e3Sopenharmony_ci JSHandle<EcmaString> str(JSTaggedValue::ToString(thread, msg)); 3644514f5e3Sopenharmony_ci 3654514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{obj.GetTaggedValue(), handleFunc.GetTaggedValue(), str.GetTaggedValue()}; 3664514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 10); 3674514f5e3Sopenharmony_ci 3684514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3694514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 3704514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3714514f5e3Sopenharmony_ci} 3724514f5e3Sopenharmony_ci 3734514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify2) 3744514f5e3Sopenharmony_ci{ 3754514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 3764514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 3774514f5e3Sopenharmony_ci 3784514f5e3Sopenharmony_ci JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); 3794514f5e3Sopenharmony_ci EXPECT_TRUE(arr != nullptr); 3804514f5e3Sopenharmony_ci JSHandle<JSObject> obj(thread, arr); 3814514f5e3Sopenharmony_ci 3824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0)); 3834514f5e3Sopenharmony_ci PropertyDescriptor desc0(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), true, true, true); 3844514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, obj, key0, desc0); 3854514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1)); 3864514f5e3Sopenharmony_ci // 2.5 : test case 3874514f5e3Sopenharmony_ci PropertyDescriptor desc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(2.5)), true, true, true); 3884514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, obj, key1, desc1); 3894514f5e3Sopenharmony_ci // 2 : test case 3904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2)); 3914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value2(factory->NewFromASCII("abc")); 3924514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key2, value2); 3934514f5e3Sopenharmony_ci 3944514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3954514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3964514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3974514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 3984514f5e3Sopenharmony_ci 3994514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4004514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 4014514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 4024514f5e3Sopenharmony_ci} 4034514f5e3Sopenharmony_ci 4044514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify3) 4054514f5e3Sopenharmony_ci{ 4064514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 4074514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 4084514f5e3Sopenharmony_ci 4094514f5e3Sopenharmony_ci uint16_t data[1]; 4104514f5e3Sopenharmony_ci data[0] = 0; 4114514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromUtf16(data, 1); 4124514f5e3Sopenharmony_ci JSHandle<EcmaString> test = factory->NewFromStdString("\"\\u0000\""); 4134514f5e3Sopenharmony_ci 4144514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4164514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4174514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); 4184514f5e3Sopenharmony_ci 4194514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4204514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 4214514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); 4224514f5e3Sopenharmony_ci} 4234514f5e3Sopenharmony_ci 4244514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> CreateJSObject(JSThread *thread) 4254514f5e3Sopenharmony_ci{ 4264514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 4274514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 4284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = globalEnv->GetObjectFunction(); 4294514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 4304514f5e3Sopenharmony_ci 4314514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun)); 4324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromStdString("x")); 4334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1)); 4344514f5e3Sopenharmony_ci JSObject::SetProperty(thread, obj, key, value); 4354514f5e3Sopenharmony_ci return obj; 4364514f5e3Sopenharmony_ci} 4374514f5e3Sopenharmony_ci 4384514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> CreateProxy(JSThread *thread) 4394514f5e3Sopenharmony_ci{ 4404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> target = CreateJSObject(thread); 4414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handler = CreateJSObject(thread); 4424514f5e3Sopenharmony_ci 4434514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Null(), 8); 4444514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4454514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4464514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue()); 4474514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue()); 4484514f5e3Sopenharmony_ci 4494514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4504514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsProxy::ProxyConstructor(ecmaRuntimeCallInfo); 4514514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4524514f5e3Sopenharmony_ci return JSHandle<JSTaggedValue>(thread, result); 4534514f5e3Sopenharmony_ci} 4544514f5e3Sopenharmony_ci 4554514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify4) // Test for proxy object 4564514f5e3Sopenharmony_ci{ 4574514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 4584514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 4594514f5e3Sopenharmony_ci 4604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> proxy = CreateProxy(thread); 4614514f5e3Sopenharmony_ci JSHandle<EcmaString> test = factory->NewFromStdString("{\"x\":1}"); 4624514f5e3Sopenharmony_ci 4634514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4644514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4654514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4664514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, proxy.GetTaggedValue()); 4674514f5e3Sopenharmony_ci 4684514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4694514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 4704514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); 4714514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4724514f5e3Sopenharmony_ci} 4734514f5e3Sopenharmony_ci 4744514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify5) // Test for typedarray object 4754514f5e3Sopenharmony_ci{ 4764514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 4774514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 4784514f5e3Sopenharmony_ci [[maybe_unused]] JSHandle<TaggedArray> array(factory->NewTaggedArray(3)); 4794514f5e3Sopenharmony_ci array->Set(thread, 0, JSTaggedValue(2)); 4804514f5e3Sopenharmony_ci array->Set(thread, 1, JSTaggedValue(3)); 4814514f5e3Sopenharmony_ci array->Set(thread, 2, JSTaggedValue(4)); 4824514f5e3Sopenharmony_ci 4834514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 4844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> jsArray(JSArray::CreateArrayFromList(thread, array)); 4854514f5e3Sopenharmony_ci JSHandle<JSFunction> int8Func(env->GetInt8ArrayFunction()); 4864514f5e3Sopenharmony_ci JSHandle<JSObject> globalObject(thread, env->GetGlobalObject()); 4874514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4884514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetNewTarget(JSTaggedValue(*int8Func)); 4894514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(*globalObject)); 4904514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, jsArray.GetTaggedValue()); 4914514f5e3Sopenharmony_ci 4924514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 4934514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> int8Array(thread, BuiltinsTypedArray::Int8ArrayConstructor(ecmaRuntimeCallInfo1)); 4944514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4954514f5e3Sopenharmony_ci 4964514f5e3Sopenharmony_ci JSHandle<EcmaString> test = factory->NewFromStdString("{\"0\":2,\"1\":3,\"2\":4}"); 4974514f5e3Sopenharmony_ci 4984514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4994514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 5004514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 5014514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, int8Array.GetTaggedValue()); 5024514f5e3Sopenharmony_ci 5034514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 5044514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 5054514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 5064514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 5074514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); 5084514f5e3Sopenharmony_ci} 5094514f5e3Sopenharmony_ci 5104514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, Stringify6) // Test for bigint object 5114514f5e3Sopenharmony_ci{ 5124514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 5134514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericValue(factory->NewFromASCII("123456789123456789")); 5144514f5e3Sopenharmony_ci 5154514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 5164514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 5174514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined()); 5184514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, numericValue.GetTaggedValue()); 5194514f5e3Sopenharmony_ci 5204514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 5214514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsBigInt::BigIntConstructor(ecmaRuntimeCallInfo1); 5224514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 5234514f5e3Sopenharmony_ci 5244514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> bigIntHandle(thread, result1); 5254514f5e3Sopenharmony_ci 5264514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 5274514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 5284514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 5294514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, bigIntHandle.GetTaggedValue()); 5304514f5e3Sopenharmony_ci 5314514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 5324514f5e3Sopenharmony_ci [[maybe_unused]] JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); 5334514f5e3Sopenharmony_ci bool hasPendingException = false; 5344514f5e3Sopenharmony_ci if (thread->HasPendingException()) { 5354514f5e3Sopenharmony_ci hasPendingException = true; 5364514f5e3Sopenharmony_ci thread->ClearException(); 5374514f5e3Sopenharmony_ci } 5384514f5e3Sopenharmony_ci ASSERT_TRUE(hasPendingException); 5394514f5e3Sopenharmony_ci} 5404514f5e3Sopenharmony_ci 5414514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsJsonTest, StringifyAndParse) 5424514f5e3Sopenharmony_ci{ 5434514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 5444514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVM->GetFactory(); 5454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> obj = CreateJSObject(thread); 5464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> ykey(factory->NewFromASCII("y")); 5474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> yvalue(thread, JSTaggedValue(2.2)); // 2.2: use to test double value 5484514f5e3Sopenharmony_ci JSObject::SetProperty(thread, obj, ykey, yvalue); 5494514f5e3Sopenharmony_ci 5504514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 5514514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 5524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 5534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); 5544514f5e3Sopenharmony_ci JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Hole()); 5554514f5e3Sopenharmony_ci { 5564514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 5574514f5e3Sopenharmony_ci result.Update(BuiltinsJson::Stringify(ecmaRuntimeCallInfo)); 5584514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 5594514f5e3Sopenharmony_ci } 5604514f5e3Sopenharmony_ci { 5614514f5e3Sopenharmony_ci ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 5624514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 5634514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 5644514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, result.GetTaggedValue()); 5654514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 5664514f5e3Sopenharmony_ci result.Update(BuiltinsJson::Parse(ecmaRuntimeCallInfo)); 5674514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 5684514f5e3Sopenharmony_ci } 5694514f5e3Sopenharmony_ci ASSERT_TRUE(result->IsECMAObject()); 5704514f5e3Sopenharmony_ci 5714514f5e3Sopenharmony_ci JSHandle<JSObject> resultObj(result); 5724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII("x")); 5734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> res = JSObject::GetProperty(thread, resultObj, key).GetValue(); 5744514f5e3Sopenharmony_ci ASSERT_TRUE(res->IsInt()); 5754514f5e3Sopenharmony_ci ASSERT_EQ(res->GetInt(), 1); 5764514f5e3Sopenharmony_ci 5774514f5e3Sopenharmony_ci res = JSObject::GetProperty(thread, resultObj, ykey).GetValue(); 5784514f5e3Sopenharmony_ci ASSERT_TRUE(res->IsDouble()); 5794514f5e3Sopenharmony_ci ASSERT_EQ(res->GetDouble(), 2.2); // 2.2:use to test double value 5804514f5e3Sopenharmony_ci} 5814514f5e3Sopenharmony_ci} // namespace panda::test 582