diff --git a/ecmascript/js_stable_array.cpp b/ecmascript/js_stable_array.cpp index 56a400f49c1b62c566d83a846d286018377898e5..07e1f73eb919ef04e092f8e6bbab6673b4d40d5c 100644 --- a/ecmascript/js_stable_array.cpp +++ b/ecmascript/js_stable_array.cpp @@ -1745,7 +1745,7 @@ JSHandle JSStableArray::SortIndexedProperties(JSThread *thread, con } JSTaggedValue JSStableArray::CopySortedListToReceiver(JSThread *thread, const JSHandle &thisObjVal, - JSHandle sortedList, uint32_t len) + JSHandle sortedList) { // 6. Let itemCount be the number of elements in sortedList. uint32_t itemCount = sortedList->GetLength(); @@ -1774,7 +1774,7 @@ JSTaggedValue JSStableArray::CopySortedListToReceiver(JSThread *thread, const JS // a. Perform ? DeletePropertyOrThrow(obj, ! ToString((j))). // b. Set j to j + 1. valueHandle.Update(JSTaggedValue::Hole()); - for (uint32_t j = itemCount; j < len; j++) { + for (uint32_t j = itemCount; j < newLength; j++) { ElementAccessor::Set(thread, thisObj, j, valueHandle, needTransition); } JSHandle::Cast(thisObj)->SetArrayLength(thread, newLength); @@ -1800,7 +1800,7 @@ JSTaggedValue JSStableArray::Sort(JSThread *thread, const JSHandleIsStableJSArray(thread)) { - CopySortedListToReceiver(thread, thisObjVal, sortedList, len); + CopySortedListToReceiver(thread, thisObjVal, sortedList); } else { JSArray::CopySortedListToReceiver(thread, thisObjVal, sortedList, len); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); diff --git a/ecmascript/js_stable_array.h b/ecmascript/js_stable_array.h index fc9d9a5fbef945f34f2e3d6b524cc2e5a8bdba7a..2e0dd30d2439c004234aadae0b97b2eaeb67378a 100644 --- a/ecmascript/js_stable_array.h +++ b/ecmascript/js_stable_array.h @@ -114,7 +114,7 @@ public: static JSTaggedValue Sort(JSThread *thread, const JSHandle &thisObjVal, const JSHandle &callbackFnHandle); static JSTaggedValue CopySortedListToReceiver(JSThread *thread, const JSHandle &thisObjVal, - JSHandle sortedList, uint32_t len); + JSHandle sortedList); static JSTaggedValue Fill(JSThread *thread, const JSHandle &thisObj, const JSHandle &value, int64_t start, int64_t end); diff --git a/test/moduletest/arraysort/arraysort.js b/test/moduletest/arraysort/arraysort.js index 9eabf8b60cd35089b3a384f7fa9060c6054d4776..4119c6b527e9eb97e14034bdec6ded23cb598029 100644 --- a/test/moduletest/arraysort/arraysort.js +++ b/test/moduletest/arraysort/arraysort.js @@ -846,3 +846,11 @@ print(sortNumber7[6]); print(arr.toSorted()); print("---------------test for string array sort end-----------------------------") } + +{ + let arr = [0, , 1]; + arr.sort((v1, v2) => { + arr.shift(); + }); + print("test for array sort with shift Success!"); +} diff --git a/test/moduletest/arraysort/expect_output.txt b/test/moduletest/arraysort/expect_output.txt index 9d5f1a5d9df3845fc816bfb4696ccdc5fa745224..22533304ff96878107ee0805a42e6cbf999e28be 100644 --- a/test/moduletest/arraysort/expect_output.txt +++ b/test/moduletest/arraysort/expect_output.txt @@ -119,3 +119,4 @@ Fri Dec 13 2019 14:48:00 GMT+0800,Sat Dec 14 2019 14:48:00 GMT+0800,Thu Dec 12 2 ---------------test for string array sort--------------------------------- a,aa,ab,aba,abb,abc,ac,b,c ---------------test for string array sort end----------------------------- +test for array sort with shift Success!