Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: test/mjsunit/harmony/typedarrays-foreach.js

Issue 583723002: Implement .forEach() on typed arrays (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Improve tests, remove harmony_typedarrays flag Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-arrays --allow-natives-syntax
6
7 var typedArrayConstructors = [
8 Uint8Array,
9 Int8Array,
10 Uint16Array,
11 Int16Array,
12 Uint32Array,
13 Int32Array,
14 Uint8ClampedArray,
15 Float32Array,
16 Float64Array];
17
18 function TestTypedArrayForEach(constructor) {
19 assertEquals(1, constructor.prototype.forEach.length);
20
21 var a = new constructor(2);
22 a[0] = 0;
23 a[1] = 1;
24
25 var count = 0;
26 a.forEach(function (n) { count++; });
27 assertEquals(2, count);
28
29 // Use specified object as this object when calling the function.
30 var o = { value: 42 };
31 var result = [];
32 a.forEach(function (n, index, array) { result.push(this.value); }, o);
33 assertArrayEquals([42, 42], result);
34
35 // Modify the original array.
36 count = 0;
37 a.forEach(function (n, index, array) { array[index] = n + 1; count++ });
38 assertEquals(2, count);
39 assertArrayEquals([1, 2], a);
40
41 // Throw before completing iteration, only the first element
42 // should be modified when thorwing mid-way.
43 count = 0;
44 a[0] = 42;
45 a[1] = 42;
46 try {
47 a.forEach(function (n, index, array) {
48 if (count > 0) throw "meh";
49 array[index] = n + 1;
50 count++;
51 });
52 } catch (e) {
53 }
54 assertEquals(1, count);
55 assertEquals(43, a[0]);
56 assertEquals(42, a[1]);
57
58 // Neutering the buffer backing the typed array mid-way should
59 // still make .forEach() finish, and the array should keep being
60 // empty after neutering it.
61 count = 0;
62 a.forEach(function (n, index, array) {
63 if (count > 0) %ArrayBufferNeuter(array.buffer);
64 array[index] = n + 1;
65 count++;
66 });
67 assertEquals(0, a.length);
68 assertEquals(undefined, a[0]);
Dmitry Lomov (no reviews) 2014/10/14 16:10:25 Assert how many times you have executed the functi
aperez 2014/10/15 15:54:48 Acknowledged.
69
70 // The method must work for typed arrays created from ArrayBuffer.
71 // The length of the ArrayBuffer is chosen so it is a multiple of
72 // all lengths of the typed array items.
73 a = new constructor(new ArrayBuffer(64));
74 count = 0;
75 a.forEach(function (n) { count++ });
76 assertEquals(a.length, count);
77
78 // Externalizing the array mid-way accessing the .buffer property
79 // should work.
80 a = new constructor(2);
81 count = 0;
82 var buffer = undefined;
83 a.forEach(function (n, index, array) {
84 if (count++ > 0)
85 buffer = array.buffer;
86 });
87 assertEquals(2, count);
88 assertTrue(!!buffer);
89 assertEquals("ArrayBuffer", %_ClassOf(buffer));
90 assertSame(buffer, a.buffer);
91
92 // The %TypedArray%.forEach() method should not work when
93 // transplanted to objects that are not typed arrays.
94 assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], funct ion (x) {}) }, TypeError);
95 assertThrows(function () { constructor.prototype.forEach.call("abc", function (x) {}) }, TypeError);
96 assertThrows(function () { constructor.prototype.forEach.call({}, function (x) {}) }, TypeError);
97 assertThrows(function () { constructor.prototype.forEach.call(0, function (x) {}) }, TypeError);
98
99 // Method must be useable on instances of other typed arrays.
100 for (var i = 0; i < typedArrayConstructors.length; i++) {
101 count = 0;
102 a = new typedArrayConstructors[i](4);
103 constructor.prototype.forEach.call(a, function (x) { count++ });
104 assertEquals(a.length, count);
105 }
106 }
107
108 for (i = 0; i < typedArrayConstructors.length; i++) {
109 TestTypedArrayForEach(typedArrayConstructors[i]);
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698