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

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: 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 var length = %_TypedArrayGetLength(this);
131 var array = $innerArrayMap(predicate, thisArg, this, length);
132 return ConstructTypedArrayLike(this, array);
arv (Not doing code reviews) 2015/05/18 22:54:23 This one can be preallocated instead of using a te
dehrenberg 2015/05/19 00:13:45 Done.
133 }
134 %FunctionSetLength(TypedArrayMap, 1);
135
136
137 // ES6 draft 07-15-13, section 22.2.3.19
138 function TypedArrayReduce(callback, current) {
139 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
140
141 var length = %_TypedArrayGetLength(this);
142 return $innerArrayReduce(callback, current, this, length,
143 %_ArgumentsLength());
144 }
145 %FunctionSetLength(TypedArrayReduce, 1);
146
147
148 // ES6 draft 07-15-13, section 22.2.3.19
149 function TypedArrayReduceRight(callback, current) {
150 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
151
152 var length = %_TypedArrayGetLength(this);
153 return $innerArrayReduceRight(callback, current, this, length,
154 %_ArgumentsLength());
155 }
156 %FunctionSetLength(TypedArrayReduceRight, 1);
157
158
159 // ES6 draft 05-05-15, section 22.2.3.24
160 function TypedArraySome(f, receiver) {
161 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
162
163 var length = %_TypedArrayGetLength(this);
164
165 return $innerArraySome(f, receiver, this, length);
166 }
167 %FunctionSetLength(TypedArraySome, 1);
168
169
94 // ES6 draft 08-24-14, section 22.2.2.2 170 // ES6 draft 08-24-14, section 22.2.2.2
95 function TypedArrayOf() { 171 function TypedArrayOf() {
96 var length = %_ArgumentsLength(); 172 var length = %_ArgumentsLength();
97 var array = new this(length); 173 var array = new this(length);
98 for (var i = 0; i < length; i++) { 174 for (var i = 0; i < length; i++) {
99 array[i] = %_Arguments(i); 175 array[i] = %_Arguments(i);
100 } 176 }
101 return array; 177 return array;
102 } 178 }
103 179
(...skipping 26 matching lines...) Expand all
130 // Set up non-enumerable functions on the object. 206 // Set up non-enumerable functions on the object.
131 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ 207 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [
132 "from", TypedArrayFrom, 208 "from", TypedArrayFrom,
133 "of", TypedArrayOf 209 "of", TypedArrayOf
134 ]); 210 ]);
135 211
136 // Set up non-enumerable functions on the prototype object. 212 // Set up non-enumerable functions on the prototype object.
137 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ 213 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [
138 "copyWithin", TypedArrayCopyWithin, 214 "copyWithin", TypedArrayCopyWithin,
139 "every", TypedArrayEvery, 215 "every", TypedArrayEvery,
140 "forEach", TypedArrayForEach, 216 "fill", TypedArrayFill,
217 "filter", TypedArrayFilter,
141 "find", TypedArrayFind, 218 "find", TypedArrayFind,
142 "findIndex", TypedArrayFindIndex, 219 "findIndex", TypedArrayFindIndex,
143 "fill", TypedArrayFill 220 "forEach", TypedArrayForEach,
221 "map", TypedArrayMap,
222 "reduce", TypedArrayReduce,
223 "reduceRight", TypedArrayReduceRight,
224 "some", TypedArraySome
144 ]); 225 ]);
145 endmacro 226 endmacro
146 227
147 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) 228 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
148 229
149 }) 230 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698