var $arrayUnshift;
var $innerArrayForEach;
var $innerArrayEvery;
+var $innerArrayReverse;
(function(global, shared, exports) {
}
-function ArrayReverse() {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reverse");
-
- var array = TO_OBJECT_INLINE(this);
- var len = TO_UINT32(array.length);
-
- if (UseSparseVariant(array, len, IS_ARRAY(array), len)) {
- %NormalizeElements(array);
- SparseReverse(array, len);
- return array;
- }
-
+function InnerArrayReverse(array, len) {
var j = len - 1;
for (var i = 0; i < j; i++, j--) {
var current_i = array[i];
}
+function ArrayReverse() {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reverse");
+
+ var array = TO_OBJECT_INLINE(this);
+ var len = TO_UINT32(array.length);
+
+ if (UseSparseVariant(array, len, IS_ARRAY(array), len)) {
+ %NormalizeElements(array);
+ SparseReverse(array, len);
+ return array;
+ }
+
+ return InnerArrayReverse(array, len);
+}
+
+
function ObservedArrayShift(len) {
var first = this[0];
$innerArrayForEach = InnerArrayForEach;
$innerArrayEvery = InnerArrayEvery;
+$innerArrayReverse = InnerArrayReverse;
});
}
%FunctionSetLength(TypedArrayFindIndex, 1);
+// ES6 draft 05-18-15, section 22.2.3.21
+function TypedArrayReverse() {
+ if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
+
+ var length = %_TypedArrayGetLength(this);
+
+ return $innerArrayReverse(this, length);
+}
+
// ES6 draft 08-24-14, section 22.2.2.2
function TypedArrayOf() {
"forEach", TypedArrayForEach,
"find", TypedArrayFind,
"findIndex", TypedArrayFindIndex,
- "fill", TypedArrayFill
+ "fill", TypedArrayFill,
+ "reverse", TypedArrayReverse
]);
endmacro
--- /dev/null
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-arrays
+
+function ArrayMaker(x) {
+ return x;
+}
+ArrayMaker.prototype = Array.prototype;
+
+var arrayConstructors = [
+ Uint8Array,
+ Int8Array,
+ Uint16Array,
+ Int16Array,
+ Uint32Array,
+ Int32Array,
+ Uint8ClampedArray,
+ Float32Array,
+ Float64Array,
+ ArrayMaker // Also test arrays
+];
+
+function assertArrayLikeEquals(value, expected, type) {
+ assertEquals(value.__proto__, type.prototype);
+ assertEquals(expected.length, value.length);
+ for (var i = 0; i < value.length; ++i) {
+ assertEquals(expected[i], value[i]);
+ }
+}
+
+for (var constructor of arrayConstructors) {
+ // Test reversing both even and odd length arrays
+ var a = new constructor([1, 2, 3]);
+ assertArrayLikeEquals(a.reverse(), [3, 2, 1], constructor);
+ assertArrayLikeEquals(a, [3, 2, 1], constructor);
+
+ a = new constructor([1, 2, 3, 4]);
+ assertArrayLikeEquals(a.reverse(), [4, 3, 2, 1], constructor);
+ assertArrayLikeEquals(a, [4, 3, 2, 1], constructor);
+
+ if (constructor != ArrayMaker) {
+ // Cannot be called on objects which are not TypedArrays
+ assertThrows(function () { a.reverse.call({ length: 0 }); }, TypeError);
+ } else {
+ // Array.reverse works on array-like objects
+ var x = { length: 2, 1: 5 };
+ a.reverse.call(x);
+ assertEquals(2, x.length);
+ assertFalse(Object.hasOwnProperty(x, '1'));
+ assertEquals(5, x[0]);
+ }
+
+ assertEquals(0, a.reverse.length);
+}