/* * 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 "ecmascript/builtins/builtins_json.h" #include #include #include #include "ecmascript/base/builtins_base.h" #include "ecmascript/builtins/builtins_bigint.h" #include "ecmascript/builtins/builtins_errors.h" #include "ecmascript/builtins/builtins_proxy.h" #include "ecmascript/builtins/builtins_typedarray.h" #include "ecmascript/ecma_runtime_call_info.h" #include "ecmascript/ecma_string-inl.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/js_array.h" #include "ecmascript/js_function.h" #include "ecmascript/js_handle.h" #include "ecmascript/js_object-inl.h" #include "ecmascript/js_primitive_ref.h" #include "ecmascript/js_tagged_value-inl.h" #include "ecmascript/js_tagged_value.h" #include "ecmascript/js_thread.h" #include "ecmascript/object_factory.h" #include "ecmascript/tests/test_helper.h" using namespace panda::ecmascript; using namespace panda::ecmascript::builtins; namespace panda::test { class BuiltinsJsonTest : public testing::Test { public: static void SetUpTestCase() { GTEST_LOG_(INFO) << "SetUpTestCase"; } static void TearDownTestCase() { GTEST_LOG_(INFO) << "TearDownCase"; } void SetUp() override { TestHelper::CreateEcmaVMWithScope(instance, thread, scope); } void TearDown() override { TestHelper::DestroyEcmaVMWithScope(instance, scope); } EcmaVM *instance {nullptr}; EcmaHandleScope *scope {nullptr}; JSThread *thread {nullptr}; class TestClass : public base::BuiltinsBase { public: static JSTaggedValue TestForParse(EcmaRuntimeCallInfo *argv) { uint32_t argc = argv->GetArgsNumber(); if (argc > 0) { } JSTaggedValue key = GetCallArg(argv, 0).GetTaggedValue(); if (key.IsUndefined()) { return JSTaggedValue::Undefined(); } JSTaggedValue value = GetCallArg(argv, 1).GetTaggedValue(); if (value.IsUndefined()) { return JSTaggedValue::Undefined(); } return JSTaggedValue(value); } static JSTaggedValue TestForParse1(EcmaRuntimeCallInfo *argv) { uint32_t argc = argv->GetArgsNumber(); if (argc > 0) { } return JSTaggedValue::Undefined(); } static JSTaggedValue TestForStringfy(EcmaRuntimeCallInfo *argv) { uint32_t argc = argv->GetArgsNumber(); if (argc > 0) { JSTaggedValue key = GetCallArg(argv, 0).GetTaggedValue(); if (key.IsUndefined()) { return JSTaggedValue::Undefined(); } JSTaggedValue value = GetCallArg(argv, 1).GetTaggedValue(); if (value.IsUndefined()) { return JSTaggedValue::Undefined(); } return JSTaggedValue(value); } return JSTaggedValue::Undefined(); } }; }; JSTaggedValue CreateBuiltinJSObject1(JSThread *thread, const CString keyCStr) { EcmaVM *ecmaVM = thread->GetEcmaVM(); JSHandle globalEnv = ecmaVM->GetGlobalEnv(); ObjectFactory *factory = ecmaVM->GetFactory(); JSHandle objectFunc(globalEnv->GetObjectFunction()); JSHandle jsobject(factory->NewJSObjectByConstructor(JSHandle(objectFunc), objectFunc)); EXPECT_TRUE(*jsobject != nullptr); JSHandle key(factory->NewFromASCII(&keyCStr[0])); JSHandle value(thread, JSTaggedValue(1)); JSObject::SetProperty(thread, JSHandle(jsobject), key, value); CString str2 = "y"; JSHandle key2(factory->NewFromASCII(str2)); JSHandle value2(thread, JSTaggedValue(2.5)); // 2.5 : test case JSObject::SetProperty(thread, JSHandle(jsobject), key2, value2); CString str3 = "z"; JSHandle key3(factory->NewFromASCII(str3)); JSHandle value3(factory->NewFromASCII("abc")); JSObject::SetProperty(thread, JSHandle(jsobject), key3, value3); return jsobject.GetTaggedValue(); } // Math.abs(-10) HWTEST_F_L0(BuiltinsJsonTest, Parse10) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle msg(factory->NewFromASCII( "\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 " "\n,\t\r \nnull\t\r \n,123.456\t\r \n] \t\r \n}\t\r \n")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsECMAObject()); } HWTEST_F_L0(BuiltinsJsonTest, Parse21) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle msg(factory->NewFromASCII("[100,2.5,\"abc\"]")); JSHandle handleFunc = factory->NewJSFunction(env, reinterpret_cast(TestClass::TestForParse)); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsECMAObject()); } HWTEST_F_L0(BuiltinsJsonTest, Parse) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle lengthKeyHandle = thread->GlobalConstants()->GetHandledLengthString(); JSHandle msg(factory->NewFromASCII("[100,2.5,\"abc\"]")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); JSTaggedValue value(static_cast(result.GetRawData())); ASSERT_TRUE(value.IsECMAObject()); JSHandle valueHandle(thread, value); JSHandle lenResult = JSObject::GetProperty(thread, JSHandle(valueHandle), lengthKeyHandle).GetValue(); uint32_t length = JSTaggedValue::ToLength(thread, lenResult).ToUint32(); EXPECT_EQ(length, 3U); } HWTEST_F_L0(BuiltinsJsonTest, Parse2) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle msg(factory->NewFromASCII("{\"epf\":100,\"key1\":200}")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); JSTaggedValue value(static_cast(result.GetRawData())); ASSERT_TRUE(value.IsECMAObject()); JSHandle valueHandle(thread, value); JSHandle nameList(JSObject::EnumerableOwnNames(thread, valueHandle)); JSHandle nameResult = JSArray::CreateArrayFromList(thread, nameList); JSHandle handleKey(nameResult); JSHandle lengthKey(factory->NewFromASCII("length")); JSHandle lenResult = JSObject::GetProperty(thread, handleKey, lengthKey).GetValue(); uint32_t length = JSTaggedValue::ToLength(thread, lenResult).ToUint32(); ASSERT_EQ(length, 2U); } HWTEST_F_L0(BuiltinsJsonTest, Parse3) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle str = factory->NewFromStdString("\"\\u0000\""); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); uint32_t length = EcmaStringAccessor(result).GetLength(); ASSERT_EQ(length, 1U); } HWTEST_F_L0(BuiltinsJsonTest, Parse4) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle str = factory->NewFromStdString("{\n\t\"on\":\t0\n}"); JSHandle key = factory->NewFromStdString("on"); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Parse(ecmaRuntimeCallInfo); JSHandle value = JSTaggedValue::GetProperty(thread, JSHandle(thread, result), JSHandle(key)) .GetValue(); int32_t number = JSTaggedValue::ToInt32(thread, value); ASSERT_EQ(number, 0); } HWTEST_F_L0(BuiltinsJsonTest, Stringify11) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle obj = JSHandle(thread, CreateBuiltinJSObject1(thread, "x")); JSHandle handleFunc = factory->NewJSFunction(env, reinterpret_cast(TestClass::TestForStringfy)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify12) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle obj = JSHandle(thread, CreateBuiltinJSObject1(thread, "x")); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle handleFunc = factory->NewJSFunction(env, reinterpret_cast(TestClass::TestForStringfy)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(static_cast(10))); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify13) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle obj = JSHandle(thread, CreateBuiltinJSObject1(thread, "x")); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle handleFunc = factory->NewJSFunction(env, reinterpret_cast(TestClass::TestForStringfy)); JSHandle msg(factory->NewFromASCII("tttt")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(2, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify14) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle obj = JSHandle(thread, CreateBuiltinJSObject1(thread, "x")); JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); JSHandle obj1(thread, arr); JSHandle key0(thread, JSTaggedValue(0)); JSHandle value0(factory->NewFromASCII("x")); JSObject::SetProperty(thread, JSHandle(obj), key0, value0); JSHandle key1(thread, JSTaggedValue(1)); JSHandle value1(factory->NewFromASCII("z")); JSObject::SetProperty(thread, JSHandle(obj), key1, value1); JSHandle msg(factory->NewFromASCII("tttt")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, obj1.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(2, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify) { JSHandle obj = JSHandle(thread, CreateBuiltinJSObject1(thread, "x")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify1) { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); JSHandle env = ecmaVM->GetGlobalEnv(); JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); EXPECT_TRUE(arr != nullptr); JSHandle obj(thread, arr); JSHandle key0(thread, JSTaggedValue(0)); JSHandle value(factory->NewFromASCII("def")); JSObject::SetProperty(thread, JSHandle(obj), key0, value); JSHandle key1(thread, JSTaggedValue(1)); PropertyDescriptor desc1(thread, JSHandle(thread, JSTaggedValue(200)), true, true, true); JSArray::DefineOwnProperty(thread, obj, key1, desc1); JSHandle key2(thread, JSTaggedValue(2)); JSHandle value2(factory->NewFromASCII("abc")); JSObject::SetProperty(thread, JSHandle(obj), key2, value2); JSHandle handleFunc = factory->NewJSFunction(env, reinterpret_cast(TestClass::TestForStringfy)); JSHandle msg(factory->NewFromASCII("tttt")); JSHandle str(JSTaggedValue::ToString(thread, msg)); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handleFunc.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(2, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify2) { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); JSArray *arr = JSArray::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetTaggedValue().GetTaggedObject()); EXPECT_TRUE(arr != nullptr); JSHandle obj(thread, arr); JSHandle key0(thread, JSTaggedValue(0)); PropertyDescriptor desc0(thread, JSHandle(thread, JSTaggedValue(1)), true, true, true); JSArray::DefineOwnProperty(thread, obj, key0, desc0); JSHandle key1(thread, JSTaggedValue(1)); // 2.5 : test case PropertyDescriptor desc1(thread, JSHandle(thread, JSTaggedValue(2.5)), true, true, true); JSArray::DefineOwnProperty(thread, obj, key1, desc1); // 2 : test case JSHandle key2(thread, JSTaggedValue(2)); JSHandle value2(factory->NewFromASCII("abc")); JSObject::SetProperty(thread, JSHandle(obj), key2, value2); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(result.IsString()); } HWTEST_F_L0(BuiltinsJsonTest, Stringify3) { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); uint16_t data[1]; data[0] = 0; JSHandle str = factory->NewFromUtf16(data, 1); JSHandle test = factory->NewFromStdString("\"\\u0000\""); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); } JSHandle CreateJSObject(JSThread *thread) { EcmaVM *ecmaVM = thread->GetEcmaVM(); JSHandle globalEnv = ecmaVM->GetGlobalEnv(); JSHandle objFun = globalEnv->GetObjectFunction(); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle obj(factory->NewJSObjectByConstructor(JSHandle(objFun), objFun)); JSHandle key(factory->NewFromStdString("x")); JSHandle value(thread, JSTaggedValue(1)); JSObject::SetProperty(thread, obj, key, value); return obj; } JSHandle CreateProxy(JSThread *thread) { JSHandle target = CreateJSObject(thread); JSHandle handler = CreateJSObject(thread); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Null(), 8); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsProxy::ProxyConstructor(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); return JSHandle(thread, result); } HWTEST_F_L0(BuiltinsJsonTest, Stringify4) // Test for proxy object { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); JSHandle proxy = CreateProxy(thread); JSHandle test = factory->NewFromStdString("{\"x\":1}"); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, proxy.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); TestHelper::TearDownFrame(thread, prev); } HWTEST_F_L0(BuiltinsJsonTest, Stringify5) // Test for typedarray object { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); [[maybe_unused]] JSHandle array(factory->NewTaggedArray(3)); array->Set(thread, 0, JSTaggedValue(2)); array->Set(thread, 1, JSTaggedValue(3)); array->Set(thread, 2, JSTaggedValue(4)); JSHandle env = ecmaVM->GetGlobalEnv(); JSHandle jsArray(JSArray::CreateArrayFromList(thread, array)); JSHandle int8Func(env->GetInt8ArrayFunction()); JSHandle globalObject(thread, env->GetGlobalObject()); auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo1->SetNewTarget(JSTaggedValue(*int8Func)); ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(*globalObject)); ecmaRuntimeCallInfo1->SetCallArg(0, jsArray.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSHandle int8Array(thread, BuiltinsTypedArray::Int8ArrayConstructor(ecmaRuntimeCallInfo1)); TestHelper::TearDownFrame(thread, prev); JSHandle test = factory->NewFromStdString("{\"0\":2,\"1\":3,\"2\":4}"); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, int8Array.GetTaggedValue()); prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); ASSERT_TRUE(result.IsString()); ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*test, EcmaString::Cast(result.GetTaggedObject()))); } HWTEST_F_L0(BuiltinsJsonTest, Stringify6) // Test for bigint object { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle numericValue(factory->NewFromASCII("123456789123456789")); auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo1->SetCallArg(0, numericValue.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSTaggedValue result1 = BuiltinsBigInt::BigIntConstructor(ecmaRuntimeCallInfo1); TestHelper::TearDownFrame(thread, prev); JSHandle bigIntHandle(thread, result1); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, bigIntHandle.GetTaggedValue()); prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); [[maybe_unused]] JSTaggedValue result = BuiltinsJson::Stringify(ecmaRuntimeCallInfo); bool hasPendingException = false; if (thread->HasPendingException()) { hasPendingException = true; thread->ClearException(); } ASSERT_TRUE(hasPendingException); } HWTEST_F_L0(BuiltinsJsonTest, StringifyAndParse) { auto ecmaVM = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVM->GetFactory(); JSHandle obj = CreateJSObject(thread); JSHandle ykey(factory->NewFromASCII("y")); JSHandle yvalue(thread, JSTaggedValue(2.2)); // 2.2: use to test double value JSObject::SetProperty(thread, obj, ykey, yvalue); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue()); JSMutableHandle result(thread, JSTaggedValue::Hole()); { [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); result.Update(BuiltinsJson::Stringify(ecmaRuntimeCallInfo)); TestHelper::TearDownFrame(thread, prev); } { ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, result.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); result.Update(BuiltinsJson::Parse(ecmaRuntimeCallInfo)); TestHelper::TearDownFrame(thread, prev); } ASSERT_TRUE(result->IsECMAObject()); JSHandle resultObj(result); JSHandle key(factory->NewFromASCII("x")); JSHandle res = JSObject::GetProperty(thread, resultObj, key).GetValue(); ASSERT_TRUE(res->IsInt()); ASSERT_EQ(res->GetInt(), 1); res = JSObject::GetProperty(thread, resultObj, ykey).GetValue(); ASSERT_TRUE(res->IsDouble()); ASSERT_EQ(res->GetDouble(), 2.2); // 2.2:use to test double value } } // namespace panda::test