!8187 修正Stringify时的异常

Merge pull request !8187 from 张鸿雨/master
This commit is contained in:
openharmony_ci 2024-07-18 02:43:45 +00:00 committed by Gitee
commit 66cb954d49
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 28 additions and 5 deletions

View File

@ -292,6 +292,8 @@ JSTaggedValue JsonStringifier::SerializeJSONProperty(const JSHandle<JSTaggedValu
}
case JSType::JS_API_LINKED_LIST: {
JSHandle listHandle = JSHandle<JSAPILinkedList>(thread_, tagValue);
CheckStackPushSameValue(valHandle);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
valHandle = JSHandle<JSTaggedValue>(thread_, JSAPILinkedList::ConvertToArray(thread_, listHandle));
SerializeJSONObject(valHandle, replacer);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
@ -299,6 +301,8 @@ JSTaggedValue JsonStringifier::SerializeJSONProperty(const JSHandle<JSTaggedValu
}
case JSType::JS_API_LIST: {
JSHandle listHandle = JSHandle<JSAPIList>(thread_, tagValue);
CheckStackPushSameValue(valHandle);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
valHandle = JSHandle<JSTaggedValue>(thread_, JSAPIList::ConvertToArray(thread_, listHandle));
SerializeJSONObject(valHandle, replacer);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
@ -346,6 +350,8 @@ JSTaggedValue JsonStringifier::SerializeJSONProperty(const JSHandle<JSTaggedValu
SerializeJSProxy(valHandle, replacer);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
} else {
CheckStackPushSameValue(valHandle);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
SerializeJSONObject(valHandle, replacer);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread_);
}
@ -406,11 +412,6 @@ void JsonStringifier::PopValue()
bool JsonStringifier::SerializeJSONObject(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer)
{
bool isContain = PushValue(value);
if (isContain) {
THROW_TYPE_ERROR_AND_RETURN(thread_, "stack contains value, usually caused by circular structure", true);
}
CString stepback = indent_;
indent_ += gap_;
@ -939,4 +940,14 @@ bool JsonStringifier::AppendJsonString(const JSHandle<JSObject> &obj, const JSHa
}
return hasContent;
}
bool JsonStringifier::CheckStackPushSameValue(JSHandle<JSTaggedValue> value)
{
bool isContain = PushValue(value);
if (isContain) {
THROW_TYPE_ERROR_AND_RETURN(thread_, "stack contains value, usually caused by circular structure", true);
}
return false;
}
} // namespace panda::ecmascript::base

View File

@ -70,6 +70,7 @@ private:
bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent);
JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object,
const JSHandle<JSTaggedValue> &value);
bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value);
CString gap_;
CString result_;
CString indent_;

View File

@ -41,3 +41,4 @@ true
{"a":"a","b":"b"}
TypeError
true
TypeError: stack contains value, usually caused by circular structure

View File

@ -247,3 +247,13 @@ try {
print(err.name);
print(err.message.includes("circular structure"));
}
try {
let arkPrivate = globalThis.ArkPrivate;
var List = arkPrivate.Load(arkPrivate.List);
const v10 = new List();
v10.add(v10);
print(JSON.stringify(v10));
} catch (err) {
print(err);
}