From 5df01fd442032162e24ee2ad8f0fb8e50d38ff35 Mon Sep 17 00:00:00 2001 From: lhc Date: Tue, 9 Sep 2025 11:10:09 +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: I746425b00b22bad7bfa4e382bf17b02b24aef189 --- ecmascript/base/json_stringifier.cpp | 3 +-- ecmascript/base/json_stringifier_optimized.cpp | 3 +-- ecmascript/js_object.cpp | 4 ++-- test/moduletest/jsonstringifier/jsonstringifier.js | 5 +++++ test/sharedtest/sharedJSON/expect_output.txt | 2 ++ test/sharedtest/sharedJSON/sharedJSON.ts | 12 ++++++++++++ 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ecmascript/base/json_stringifier.cpp b/ecmascript/base/json_stringifier.cpp index 151d767392..12a0619c67 100644 --- a/ecmascript/base/json_stringifier.cpp +++ b/ecmascript/base/json_stringifier.cpp @@ -894,8 +894,7 @@ 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())); - sortArr.emplace_back(JSHandle(thread_, numberKey)); + sortArr.emplace_back(JSHandle(thread_, key)); } } } diff --git a/ecmascript/base/json_stringifier_optimized.cpp b/ecmascript/base/json_stringifier_optimized.cpp index 8a2f42c335..1b2277f70a 100644 --- a/ecmascript/base/json_stringifier_optimized.cpp +++ b/ecmascript/base/json_stringifier_optimized.cpp @@ -872,8 +872,7 @@ 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())); - sortArr.emplace_back(JSHandle(thread_, numberKey)); + sortArr.emplace_back(JSHandle(thread_, key)); } } } diff --git a/ecmascript/js_object.cpp b/ecmascript/js_object.cpp index 72bddac71c..aaff940ba4 100644 --- a/ecmascript/js_object.cpp +++ b/ecmascript/js_object.cpp @@ -494,7 +494,7 @@ bool JSObject::AddElementInternal(JSThread *thread, const JSHandle &re TaggedArray *elements = TaggedArray::Cast(receiver->GetElements(thread).GetTaggedObject()); if (isDictionary) { ASSERT(elements->IsDictionaryMode()); - JSHandle keyHandle(thread, JSTaggedValue(static_cast(index))); + JSHandle keyHandle(thread, JSTaggedValue(index)); JSHandle newDict = NumberDictionary::Put(thread, JSHandle(thread, elements), keyHandle, value, attr); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false); @@ -507,7 +507,7 @@ bool JSObject::AddElementInternal(JSThread *thread, const JSHandle &re if (!receiver->IsJSSArray() && (ShouldTransToDict(capacity, index) || !attr.IsDefaultAttributes())) { JSObject::ElementsToDictionary(thread, receiver); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false); - JSHandle keyHandle(thread, JSTaggedValue(static_cast(index))); + JSHandle keyHandle(thread, JSTaggedValue(index)); JSHandle dict(thread, receiver->GetElements(thread)); JSHandle newKey = NumberDictionary::Put(thread, dict, keyHandle, value, attr); receiver->SetElements(thread, newKey); 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..b190d2c83e 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