From 268cc240598213b05ed76e2da76189114980a8d9 Mon Sep 17 00:00:00 2001 From: lhc Date: Wed, 3 Sep 2025 09:46:25 +0800 Subject: [PATCH] Fix stringify of Ason bug Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICW36K?from=project-issue Signed-off-by: lhc Change-Id: I472eaca17235ee198b43365cfac513c064ac624d --- ecmascript/base/json_stringifier.cpp | 3 ++- ecmascript/base/json_stringifier_optimized.cpp | 3 ++- test/moduletest/jsonstringifier/jsonstringifier.js | 5 +++++ test/sharedtest/sharedJSON/expect_output.txt | 2 ++ test/sharedtest/sharedJSON/sharedJSON.ts | 12 ++++++++++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ecmascript/base/json_stringifier.cpp b/ecmascript/base/json_stringifier.cpp index 151d767392..c280b71a46 100644 --- a/ecmascript/base/json_stringifier.cpp +++ b/ecmascript/base/json_stringifier.cpp @@ -894,7 +894,8 @@ bool JsonStringifier::SerializeElements(const JSHandle &obj, const JSH if (!key.IsUndefined() && !key.IsHole()) { PropertyAttributes attr = numberDic->GetAttributes(thread_, hashIndex); if (attr.IsEnumerable()) { - JSTaggedValue numberKey = JSTaggedValue(static_cast(key.GetInt())); + JSTaggedValue numberKey = key.IsInt() ? JSTaggedValue(static_cast(key.GetInt())) : + JSTaggedValue(key.GetDouble()); sortArr.emplace_back(JSHandle(thread_, numberKey)); } } diff --git a/ecmascript/base/json_stringifier_optimized.cpp b/ecmascript/base/json_stringifier_optimized.cpp index 8a2f42c335..381fb95fcb 100644 --- a/ecmascript/base/json_stringifier_optimized.cpp +++ b/ecmascript/base/json_stringifier_optimized.cpp @@ -872,7 +872,8 @@ bool JsonStringifier::SerializeElements(const JSHandle &obj, const JSH if (!key.IsUndefined() && !key.IsHole()) { PropertyAttributes attr = numberDic->GetAttributes(thread_, hashIndex); if (attr.IsEnumerable()) { - JSTaggedValue numberKey = JSTaggedValue(static_cast(key.GetInt())); + JSTaggedValue numberKey = key.IsInt() ? JSTaggedValue(static_cast(key.GetInt())) : + JSTaggedValue(key.GetDouble()); sortArr.emplace_back(JSHandle(thread_, numberKey)); } } diff --git a/test/moduletest/jsonstringifier/jsonstringifier.js b/test/moduletest/jsonstringifier/jsonstringifier.js index cec5ef7425..139716367c 100644 --- a/test/moduletest/jsonstringifier/jsonstringifier.js +++ b/test/moduletest/jsonstringifier/jsonstringifier.js @@ -38,6 +38,11 @@ var obj = { } assert_equal(JSON.stringify(obj),'{"2147483648":2289}'); +var obj = { + 6958012010: 2289 +} +assert_equal(JSON.stringify(obj),'{"6958012010":2289}'); + const a = new Uint32Array(0x10); let b = a.__proto__; b[1073741823] = {} diff --git a/test/sharedtest/sharedJSON/expect_output.txt b/test/sharedtest/sharedJSON/expect_output.txt index 4d3e75f1f2..5a809b9333 100644 --- a/test/sharedtest/sharedJSON/expect_output.txt +++ b/test/sharedtest/sharedJSON/expect_output.txt @@ -137,3 +137,5 @@ true ["a1",null,1,true] ["a1",null,1,true] ["a1",1,null,true] +{"2300004177":{"number":"2300004177"}} +{"100":{"number":"100"}} diff --git a/test/sharedtest/sharedJSON/sharedJSON.ts b/test/sharedtest/sharedJSON/sharedJSON.ts index de80d84d53..396b325603 100644 --- a/test/sharedtest/sharedJSON/sharedJSON.ts +++ b/test/sharedtest/sharedJSON/sharedJSON.ts @@ -553,6 +553,17 @@ function testASONStringifyMapSetAddUndefined() { print(str6); } +function testOverInt32Key() { + let jsonText = '{"2300004177":{"number":"2300004177"}}'; + let obj = JSON.parseSendable(jsonText); + let str = JSON.stringify(obj); + print(str) + let jsonText1 = '{"100":{"number":"100"}}'; + let obj1 = JSON.parseSendable(jsonText1); + let str1 = JSON.stringify(obj1); + print(str1) +} + testJSONParseSendable(); jsonRepeatCall(); testASONBigInt(); @@ -567,3 +578,4 @@ testASONStringifyMapAndSet(); testASONStringifyMapAndSetAndObj(); testASONStringifyAfterClearMapAndSet(); testASONStringifyMapSetAddUndefined(); +testOverInt32Key(); -- Gitee