!5153 Bugfix on JSArray.sort when this be modified to string which length is 1

Merge pull request !5153 from chenjingxiang/bugfix_js_array_sort
This commit is contained in:
openharmony_ci 2023-11-07 02:28:54 +00:00 committed by Gitee
commit 7e8d47b43a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 25 additions and 6 deletions

View File

@ -397,6 +397,10 @@ JSTaggedValue JSArray::Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj
int64_t len = ArrayHelper::GetArrayLength(thread, obj);
// ReturnIfAbrupt(len).
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
// If len is 0 or 1, no need to sort
if (len == 0 || len == 1) {
return obj.GetTaggedValue();
}
// 4. Let SortCompare be a new Abstract Closure with parameters (x, y) that captures comparefn and performs
// the following steps when called:

View File

@ -207,9 +207,21 @@ Object.defineProperty(Array.prototype, "tt", {
writable:false,
});
var arr = new Array(5);
arr[0] = 93;
arr[2] = -45;
arr[3] = "djs";
arr[4] = 0;
print(arr.sort());
var arr6 = new Array(5);
arr6[0] = 93;
arr6[2] = -45;
arr6[3] = "djs";
arr6[4] = 0;
print(arr6.sort());
var arr7 = [1];
print(arr7.sort());
var res1 = Array.prototype.sort.call("m", Uint8Array);
print(res1);
try {
Array.prototype.sort.call("mm", Uint8Array);
} catch (e) {
print(e.name);
}

View File

@ -41,3 +41,6 @@ false
930,93,44,0,-45
1,3,4
-45,0,93,djs,
1
m
TypeError