Reviewers: arv,
Description:
Implement %TypedArray%.reverse
This patch adds the reverse method to TypedArrays, together with a
test. The test also runs for normal Arrays, since I didn't see a
test for reversing dense arrays.
BUG=v8:3578
LOG=Y
[email protected]
Please review this at https://codereview.chromium.org/1132723008/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+80, -7 lines):
M src/array.js
M src/harmony-typedarray.js
A test/mjsunit/harmony/typedarray-reverse.js
Index: src/array.js
diff --git a/src/array.js b/src/array.js
index
93378cfb00b1b14c0150e5b179c6de5f6a8852a2..0124b414bee21e6d7064b3565e3c6178644472b4
100644
--- a/src/array.js
+++ b/src/array.js
@@ -12,6 +12,7 @@ var $arraySplice;
var $arrayUnshift;
var $innerArrayForEach;
var $innerArrayEvery;
+var $innerArrayReverse;
(function(global, shared, exports) {
@@ -564,12 +565,7 @@ function SparseReverse(array, len) {
}
-function ArrayReverse() {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reverse");
-
- var array = TO_OBJECT_INLINE(this);
- var len = TO_UINT32(array.length);
-
+function InnerArrayReverse(array, len) {
if (UseSparseVariant(array, len, IS_ARRAY(array), len)) {
%NormalizeElements(array);
SparseReverse(array, len);
@@ -600,6 +596,16 @@ function ArrayReverse() {
}
+function ArrayReverse() {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reverse");
+
+ var array = TO_OBJECT_INLINE(this);
+ var len = TO_UINT32(array.length);
+
+ return InnerArrayReverse(array, len);
+}
+
+
function ObservedArrayShift(len) {
var first = this[0];
@@ -1609,5 +1615,6 @@ $arrayUnshift = ArrayUnshift;
$innerArrayForEach = InnerArrayForEach;
$innerArrayEvery = InnerArrayEvery;
+$innerArrayReverse = InnerArrayReverse;
});
Index: src/harmony-typedarray.js
diff --git a/src/harmony-typedarray.js b/src/harmony-typedarray.js
index
90679e0c1fe1afffb6b6b7115e93719e02d7b0c1..59c9a79bd22144422d1bce9559c4cf1367dc37d8
100644
--- a/src/harmony-typedarray.js
+++ b/src/harmony-typedarray.js
@@ -90,6 +90,15 @@ function TypedArrayFindIndex(predicate, thisArg) {
}
%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() {
@@ -140,7 +149,8 @@ macro EXTEND_TYPED_ARRAY(NAME)
"forEach", TypedArrayForEach,
"find", TypedArrayFind,
"findIndex", TypedArrayFindIndex,
- "fill", TypedArrayFill
+ "fill", TypedArrayFill,
+ "reverse", TypedArrayReverse
]);
endmacro
Index: test/mjsunit/harmony/typedarray-reverse.js
diff --git a/test/mjsunit/harmony/typedarray-reverse.js
b/test/mjsunit/harmony/typedarray-reverse.js
new file mode 100644
index
0000000000000000000000000000000000000000..5945d336a69f301971c9d86a8b942bf52a11ad43
--- /dev/null
+++ b/test/mjsunit/harmony/typedarray-reverse.js
@@ -0,0 +1,56 @@
+// 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 id(x) {
+ return x;
+}
+id.prototype = Array.prototype;
+
+var typedArrayConstructors = [
+ Uint8Array,
+ Int8Array,
+ Uint16Array,
+ Int16Array,
+ Uint32Array,
+ Int32Array,
+ Uint8ClampedArray,
+ Float32Array,
+ Float64Array,
+ id // 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 typedArrayConstructors) {
+ // 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 != id) {
+ // 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);
+}
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.