diff --git a/src/runtime/runtime-array.cc b/src/runtime/runtime-array.cc index 9b1f446669..648606a274 100644 --- a/src/runtime/runtime-array.cc +++ b/src/runtime/runtime-array.cc @@ -149,7 +149,8 @@ Object* PrepareElementsForSort(Handle object, uint32_t limit) { JSObject::ValidateElements(*object); } else if (object->HasFixedTypedArrayElements()) { // Typed arrays cannot have holes or undefined elements. - return Smi::FromInt(FixedArrayBase::cast(object->elements())->length()); + int array_length = FixedArrayBase::cast(object->elements())->length(); + return Smi::FromInt(Min(limit, static_cast(array_length))); } else if (!object->HasDoubleElements()) { JSObject::EnsureWritableFastElements(object); } diff --git a/test/mjsunit/array-sort.js b/test/mjsunit/array-sort.js index acc96117be..0454da8cae 100644 --- a/test/mjsunit/array-sort.js +++ b/test/mjsunit/array-sort.js @@ -503,6 +503,19 @@ function TestSortOnNonExtensible() { } TestSortOnNonExtensible(); +function TestSortOnTypedArray() { + var array = new Int8Array([10,9,8,7,6,5,4,3,2,1]); + Object.defineProperty(array, "length", {value: 5}); + Array.prototype.sort.call(array); + assertEquals(array, new Int8Array([10,6,7,8,9,5,4,3,2,1])); + + var array = new Int8Array([10,9,8,7,6,5,4,3,2,1]); + Object.defineProperty(array, "length", {value: 15}); + Array.prototype.sort.call(array); + assertEquals(array, new Int8Array([1,10,2,3,4,5,6,7,8,9])); +} +TestSortOnTypedArray(); + // Test special prototypes (function testSortSpecialPrototypes() {