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

Side by Side Diff: src/harmony-typedarray.js

Issue 1139663005: Implement %TypedArray%.prototype.{map,filter,some,reduce,reduceRight} (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixes Created 5 years, 7 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
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, exports) { 5 (function(global, exports) {
6 6
7 "use strict"; 7 "use strict";
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
(...skipping 12 matching lines...) Expand all
23 23
24 macro DECLARE_GLOBALS(NAME) 24 macro DECLARE_GLOBALS(NAME)
25 var GlobalNAME = global.NAME; 25 var GlobalNAME = global.NAME;
26 endmacro 26 endmacro
27 27
28 TYPED_ARRAYS(DECLARE_GLOBALS) 28 TYPED_ARRAYS(DECLARE_GLOBALS)
29 DECLARE_GLOBALS(Array) 29 DECLARE_GLOBALS(Array)
30 30
31 // ------------------------------------------------------------------- 31 // -------------------------------------------------------------------
32 32
33 function ConstructTypedArray(constructor, array) {
34 // TODO(littledan): This is an approximation of the spec, which requires
35 // that only real TypedArray classes should be accepted (22.2.2.1.1)
36 if (!IS_SPEC_OBJECT(constructor) || IS_UNDEFINED(constructor.prototype) ||
37 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) {
38 throw MakeTypeError(kNotTypedArray);
39 }
40
41 // TODO(littledan): The spec requires that, rather than directly calling
42 // the constructor, a TypedArray is created with the proper proto and
43 // underlying size and element size, and elements are put in one by one.
44 // By contrast, this would allow subclasses to make a radically different
45 // constructor with different semantics.
46 return new constructor(array);
47 }
48
49 function ConstructTypedArrayLike(typedArray, arrayContents) {
50 // TODO(littledan): The spec requires that we actuallly use
51 // typedArray.constructor[Symbol.species] (bug v8:4093)
52 return new typedArray.constructor(arrayContents);
53 }
54
33 function TypedArrayCopyWithin(target, start, end) { 55 function TypedArrayCopyWithin(target, start, end) {
34 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 56 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
35 57
36 var length = %_TypedArrayGetLength(this); 58 var length = %_TypedArrayGetLength(this);
37 59
38 // TODO(littledan): Replace with a memcpy for better performance 60 // TODO(littledan): Replace with a memcpy for better performance
39 return $innerArrayCopyWithin(target, start, end, this, length); 61 return $innerArrayCopyWithin(target, start, end, this, length);
40 } 62 }
41 %FunctionSetLength(TypedArrayCopyWithin, 2); 63 %FunctionSetLength(TypedArrayCopyWithin, 2);
42 64
(...skipping 11 matching lines...) Expand all
54 function TypedArrayForEach(f, receiver) { 76 function TypedArrayForEach(f, receiver) {
55 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 77 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
56 78
57 var length = %_TypedArrayGetLength(this); 79 var length = %_TypedArrayGetLength(this);
58 80
59 $innerArrayForEach(f, receiver, this, length); 81 $innerArrayForEach(f, receiver, this, length);
60 } 82 }
61 %FunctionSetLength(TypedArrayForEach, 1); 83 %FunctionSetLength(TypedArrayForEach, 1);
62 84
63 // ES6 draft 04-05-14 section 22.2.3.8 85 // ES6 draft 04-05-14 section 22.2.3.8
64 function TypedArrayFill(value, start , end) { 86 function TypedArrayFill(value, start, end) {
65 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 87 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
66 88
67 var length = %_TypedArrayGetLength(this); 89 var length = %_TypedArrayGetLength(this);
68 90
69 return $innerArrayFill(value, start, end, this, length); 91 return $innerArrayFill(value, start, end, this, length);
70 } 92 }
71 %FunctionSetLength(TypedArrayFill, 1); 93 %FunctionSetLength(TypedArrayFill, 1);
72 94
95 // ES6 draft 07-15-13, section 22.2.3.9
96 function TypedArrayFilter(predicate, thisArg) {
97 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
98
99 var length = %_TypedArrayGetLength(this);
100 var array = $innerArrayFilter(predicate, thisArg, this, length);
101 return ConstructTypedArrayLike(this, array);
102 }
103 %FunctionSetLength(TypedArrayFilter, 1);
104
73 // ES6 draft 07-15-13, section 22.2.3.10 105 // ES6 draft 07-15-13, section 22.2.3.10
74 function TypedArrayFind(predicate, thisArg) { 106 function TypedArrayFind(predicate, thisArg) {
75 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 107 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
76 108
77 var length = %_TypedArrayGetLength(this); 109 var length = %_TypedArrayGetLength(this);
78 110
79 return $innerArrayFind(predicate, thisArg, this, length); 111 return $innerArrayFind(predicate, thisArg, this, length);
80 } 112 }
81 %FunctionSetLength(TypedArrayFind, 1); 113 %FunctionSetLength(TypedArrayFind, 1);
82 114
83 // ES6 draft 07-15-13, section 22.2.3.11 115 // ES6 draft 07-15-13, section 22.2.3.11
84 function TypedArrayFindIndex(predicate, thisArg) { 116 function TypedArrayFindIndex(predicate, thisArg) {
85 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 117 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
86 118
87 var length = %_TypedArrayGetLength(this); 119 var length = %_TypedArrayGetLength(this);
88 120
89 return $innerArrayFindIndex(predicate, thisArg, this, length); 121 return $innerArrayFindIndex(predicate, thisArg, this, length);
90 } 122 }
91 %FunctionSetLength(TypedArrayFindIndex, 1); 123 %FunctionSetLength(TypedArrayFindIndex, 1);
92 124
93 125
126 // ES6 draft 07-15-13, section 22.2.3.18
127 function TypedArrayMap(predicate, thisArg) {
128 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
129
130 // TODO(littledan): Preallocate rather than making an intermediate
131 // array, for better performance.
132 var length = %_TypedArrayGetLength(this);
133 var array = $innerArrayMap(predicate, thisArg, this, length);
134 return ConstructTypedArrayLike(this, array);
135 }
136 %FunctionSetLength(TypedArrayMap, 1);
137
138
139 // ES6 draft 07-15-13, section 22.2.3.19
140 function TypedArrayReduce(callback, current) {
141 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
142
143 var length = %_TypedArrayGetLength(this);
144 return $innerArrayReduce(callback, current, this, length,
145 %_ArgumentsLength());
146 }
147 %FunctionSetLength(TypedArrayReduce, 1);
148
149
150 // ES6 draft 07-15-13, section 22.2.3.19
151 function TypedArrayReduceRight(callback, current) {
152 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
153
154 var length = %_TypedArrayGetLength(this);
155 return $innerArrayReduceRight(callback, current, this, length,
156 %_ArgumentsLength());
157 }
158 %FunctionSetLength(TypedArrayReduceRight, 1);
159
160
161 // ES6 draft 05-05-15, section 22.2.3.24
162 function TypedArraySome(f, receiver) {
163 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
164
165 var length = %_TypedArrayGetLength(this);
166
167 return $innerArraySome(f, receiver, this, length);
168 }
169 %FunctionSetLength(TypedArraySome, 1);
170
171
94 // ES6 draft 08-24-14, section 22.2.2.2 172 // ES6 draft 08-24-14, section 22.2.2.2
95 function TypedArrayOf() { 173 function TypedArrayOf() {
96 var length = %_ArgumentsLength(); 174 var length = %_ArgumentsLength();
97 var array = new this(length); 175 var array = new this(length);
98 for (var i = 0; i < length; i++) { 176 for (var i = 0; i < length; i++) {
99 array[i] = %_Arguments(i); 177 array[i] = %_Arguments(i);
100 } 178 }
101 return array; 179 return array;
102 } 180 }
103 181
(...skipping 26 matching lines...) Expand all
130 // Set up non-enumerable functions on the object. 208 // Set up non-enumerable functions on the object.
131 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ 209 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [
132 "from", TypedArrayFrom, 210 "from", TypedArrayFrom,
133 "of", TypedArrayOf 211 "of", TypedArrayOf
134 ]); 212 ]);
135 213
136 // Set up non-enumerable functions on the prototype object. 214 // Set up non-enumerable functions on the prototype object.
137 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ 215 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [
138 "copyWithin", TypedArrayCopyWithin, 216 "copyWithin", TypedArrayCopyWithin,
139 "every", TypedArrayEvery, 217 "every", TypedArrayEvery,
140 "forEach", TypedArrayForEach, 218 "fill", TypedArrayFill,
219 "filter", TypedArrayFilter,
141 "find", TypedArrayFind, 220 "find", TypedArrayFind,
142 "findIndex", TypedArrayFindIndex, 221 "findIndex", TypedArrayFindIndex,
143 "fill", TypedArrayFill 222 "forEach", TypedArrayForEach,
223 "map", TypedArrayMap,
224 "reduce", TypedArrayReduce,
225 "reduceRight", TypedArrayReduceRight,
226 "some", TypedArraySome
144 ]); 227 ]);
145 endmacro 228 endmacro
146 229
147 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) 230 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
148 231
149 }) 232 })
OLDNEW
« src/array.js ('K') | « src/array.js ('k') | test/mjsunit/harmony/typedarray-iteration.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698