Skip to content

Commit 140c7b5

Browse files
committed
Fix Array length check
1 parent d96e131 commit 140c7b5

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@
187187
__chakraLibrary.arrayCreateDataPropertyOrThrow(compareArray, index, "" + array[index]);
188188
}
189189

190-
const fast = length < 1073741825;
191190
const buffer = [];
192191
const stringBuffer = [];
193-
194-
let i = 1, j = 0, k = 0, bucketSize = 4, position = 0;
192+
let i = 1, j = 0, k = 0, bucketSize = 4, lastSize = 2, position = 0;
195193

196194
// handle pairs first
197195
while (i < length) {
@@ -212,11 +210,11 @@
212210
while (position < length) {
213211
const left = position;
214212
let right = position + bucketSize;
215-
right < length ? right : length;
216-
const mid = fast ? left + right >> 1 : (left + right) / 2 |0;
213+
const mid = left + lastSize;
217214

218215
// perform a merge but only if it's necessary
219-
if (mid < length && compareArray[mid - 1] > compareArray[mid]) {
216+
if (mid < length && compareArray[mid] < compareArray[mid - 1]) {
217+
right = right < length ? right : length;
220218
i = left, j = 0, k = left;
221219
const currentLength = buffer.length;
222220

@@ -269,14 +267,14 @@
269267
position += bucketSize;
270268
}
271269
position = 0;
270+
lastSize = bucketSize;
272271
bucketSize *= 2;
273272
}
274273
});
275274

276275
platform.registerChakraLibraryFunction("MergeSort", function(array, length, compareFn) {
277-
const fast = length < 1073741825;
278276
const buffer = [];
279-
let i = 1, j = 0, k = 0, bucketSize = 4, position = 0;
277+
let i = 1, j = 0, k = 0, bucketSize = 4, lastSize = 2, position = 0;
280278

281279
// handle pairs first
282280
while (i < length) {
@@ -294,11 +292,11 @@
294292
while (position < length) {
295293
const left = position;
296294
let right = position + bucketSize;
297-
right < length ? right : length;
298-
const mid = fast ? left + right >> 1 : (left + right) / 2 |0;
295+
const mid = left + lastSize;
299296

300297
// perform a merge but only if it's necessary
301-
if (mid < length && compareFn(array[mid - 1], array[mid]) > 0) {
298+
if (mid < length && compareFn(array[mid], array[mid - 1]) < 0) {
299+
right = right < length ? right : length;
302300
i = left, j = 0, k = left;
303301
const currentLength = buffer.length;
304302

@@ -342,6 +340,7 @@
342340
position += bucketSize;
343341
}
344342
position = 0;
343+
lastSize = bucketSize;
345344
bucketSize *= 2;
346345
}
347346
});
@@ -376,35 +375,33 @@
376375
__chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.sort");
377376
}
378377

379-
const objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.sort");
380-
const array = objInfo.o;
381-
let len = objInfo.len;
378+
const {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.sort");
382379

383380
if (len < 2) { // early return if length < 2
384-
return array;
381+
return o;
385382
}
386383

387384
// check for if the array has any missing values
388385
// also pull in any values from the prototype
389-
let i = 0;
386+
let i = 0, length = len;
390387
while (i < len) {
391-
if (array[i] === undefined) {
392-
len = __chakraLibrary.FillArrayHoles(array, len, i);
388+
if (o[i] === undefined) {
389+
length = __chakraLibrary.FillArrayHoles(o, len, i);
393390
break;
394391
}
395-
array[i] = array[i++];
392+
o[i] = o[i++];
396393
}
397394

398-
if (len > 1)
395+
if (length > 1)
399396
{
400397
if (compareFn !== undefined) {
401-
__chakraLibrary.MergeSort(array, len, compareFn);
398+
__chakraLibrary.MergeSort(o, length, compareFn);
402399
} else {
403-
__chakraLibrary.DefaultMergeSort(array, len);
400+
__chakraLibrary.DefaultMergeSort(o, length);
404401
}
405402
}
406403

407-
return array;
404+
return o;
408405
});
409406

410407
platform.registerFunction(platform.FunctionKind.Array_filter, function (callbackfn, thisArg = undefined) {

0 commit comments

Comments
 (0)