From d4615f36b7ab8d412c79857107d667a0ee0bb6a2 Mon Sep 17 00:00:00 2001 From: chenjx-huawei Date: Tue, 4 Jun 2024 23:12:42 +0800 Subject: [PATCH] Bugfix JSON.stringify when value type is JSNativePointer Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I9V17F?from=project-issue Signed-off-by: chenjx-huawei Change-Id: I5b6ee80f2ed5dc34195f1e90e8899b4cec3c7f6f --- ecmascript/base/json_stringifier.cpp | 4 ++ .../base/tests/json_stringifier_test.cpp | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/ecmascript/base/json_stringifier.cpp b/ecmascript/base/json_stringifier.cpp index dec1078bc5..c0d0994a83 100644 --- a/ecmascript/base/json_stringifier.cpp +++ b/ecmascript/base/json_stringifier.cpp @@ -326,6 +326,10 @@ JSTaggedValue JsonStringifier::SerializeJSONProperty(const JSHandleGetClass(); diff --git a/ecmascript/base/tests/json_stringifier_test.cpp b/ecmascript/base/tests/json_stringifier_test.cpp index 6e7b5b03ac..6f212d734e 100644 --- a/ecmascript/base/tests/json_stringifier_test.cpp +++ b/ecmascript/base/tests/json_stringifier_test.cpp @@ -321,4 +321,62 @@ HWTEST_F_L0(JsonStringifierTest, Stringify_008) JSHandle handleEcmaStr(resultString); EXPECT_STREQ("\"\\\"\\\\\\b\\f\\n\\r\\t\"", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } + +static void* Detach(void *param1, void *param2, void *hint, void *detachData) +{ + GTEST_LOG_(INFO) << "detach is running"; + if (param1 == nullptr && param2 == nullptr) { + GTEST_LOG_(INFO) << "detach: two params is nullptr"; + } + if (hint == nullptr && detachData) { + GTEST_LOG_(INFO) << "detach: hint is nullptr"; + } + return nullptr; +} + +static void* Attach([[maybe_unused]] void *enginePointer, [[maybe_unused]] void *buffer, [[maybe_unused]] void *hint, + [[maybe_unused]] void *attachData) +{ + GTEST_LOG_(INFO) << "attach is running"; + return nullptr; +} + +static panda::JSNApi::NativeBindingInfo* CreateNativeBindingInfo(void* attach, void* detach) +{ + GTEST_LOG_(INFO) << "CreateNativeBindingInfo"; + auto info = panda::JSNApi::NativeBindingInfo::CreateNewInstance(); + info->attachFunc = attach; + info->detachFunc = detach; + return info; +} + +HWTEST_F_L0(JsonStringifierTest, Stringify_009) +{ + ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + JsonStringifier stringifier(thread); + + EcmaVM *ecmaVM = thread->GetEcmaVM(); + JSHandle globalEnv = ecmaVM->GetGlobalEnv(); + JSHandle objectFunc(globalEnv->GetObjectFunction()); + JSHandle jsObject(factory->NewJSObjectByConstructor(JSHandle(objectFunc), objectFunc)); + EXPECT_TRUE(*jsObject != nullptr); + + JSHandle key1(factory->NewFromASCII("key1")); + auto info = CreateNativeBindingInfo(reinterpret_cast(Attach), reinterpret_cast(Detach)); + JSHandle value1(factory->NewJSNativePointer(reinterpret_cast(info))); + JSObject::SetProperty(thread, JSHandle(jsObject), key1, value1); + + JSHandle key2(factory->NewFromASCII("key2")); + JSHandle value2(factory->NewFromASCII("abc")); + JSObject::SetProperty(thread, JSHandle(jsObject), key2, value2); + + JSHandle handleValue(thread, jsObject.GetTaggedValue()); + JSHandle handleReplacer(thread, JSTaggedValue::Undefined()); + JSHandle handleGap(thread, JSTaggedValue::Undefined()); + + JSHandle resultString = stringifier.Stringify(handleValue, handleReplacer, handleGap); + EXPECT_TRUE(resultString->IsString()); + JSHandle handleEcmaStr(resultString); + EXPECT_STREQ("{\"key1\":{},\"key2\":\"abc\"}", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); +} } // namespace panda::test