Sort function can get unstable array. (#30)

Signed-off-by: seonghyun kim <sh8281.kim@samsung.com>
This commit is contained in:
김승현/Web Platform Lab(S/W센터)/Engineer/삼성전자 2017-11-25 13:33:56 +09:00 committed by 최영일/Web Platform Lab(S/W센터)/Principal Engineer/삼성전자
commit 91d02bc621
5 changed files with 193 additions and 6 deletions

View file

@ -18,6 +18,7 @@
#include "ArrayObject.h"
#include "Context.h"
#include "VMInstance.h"
#include "util/Util.h"
namespace Escargot {
@ -186,7 +187,7 @@ void ArrayObject::enumeration(ExecutionState& state, bool (*callback)(ExecutionS
Object::enumeration(state, callback, data);
}
void ArrayObject::sort(ExecutionState& state, std::function<bool(const Value& a, const Value& b)> comp)
void ArrayObject::sort(ExecutionState& state, const std::function<bool(const Value& a, const Value& b)>& comp)
{
if (isFastModeArray()) {
if (getArrayLength(state)) {
@ -197,7 +198,16 @@ void ArrayObject::sort(ExecutionState& state, std::function<bool(const Value& a,
tempBuffer[i] = m_fastModeData[i];
}
std::sort(tempBuffer, tempBuffer + orgLength, comp);
if (orgLength) {
TightVector<Value, GCUtil::gc_malloc_ignore_off_page_allocator<Value>> tempSpace;
tempSpace.resizeWithUninitializedValues(orgLength);
mergeSort(tempBuffer, orgLength, tempSpace.data(), [&](const Value& a, const Value& b, bool* lessOrEqualp) -> bool {
*lessOrEqualp = comp(a, b);
return true;
});
}
if (getArrayLength(state) != orgLength) {
setArrayLength(state, orgLength);
}