OLD | NEW |
---|---|
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 Loading... | |
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) { | |
adamk
2015/05/20 18:50:07
Where is this called?
arv (Not doing code reviews)
2015/05/20 19:22:31
Also, this function is defined twice... was there
dehrenberg
2015/05/20 23:39:01
Yes. OK, I learned my lesson, not to do git confli
| |
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) || | |
adamk
2015/05/20 18:50:07
Can you use %IsConstructor here? These checks look
dehrenberg
2015/05/20 23:39:01
The check is supposed to be an approximation of ch
| |
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 Loading... | |
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 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 | 179 |
148 // ES6 section 22.2.3.16 | 180 // ES6 section 22.2.3.16 |
149 function TypedArrayLastIndexOf(element, index) { | 181 function TypedArrayLastIndexOf(element, index) { |
150 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 182 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
151 | 183 |
152 var length = %_TypedArrayGetLength(this); | 184 var length = %_TypedArrayGetLength(this); |
153 | 185 |
154 return %_CallFunction(this, element, index, length, | 186 return %_CallFunction(this, element, index, length, |
155 %_ArgumentsLength(), $innerArrayLastIndexOf); | 187 %_ArgumentsLength(), $innerArrayLastIndexOf); |
156 } | 188 } |
157 %FunctionSetLength(TypedArrayLastIndexOf, 1); | 189 |
190 | |
191 // ES6 draft 07-15-13, section 22.2.3.18 | |
192 function TypedArrayMap(predicate, thisArg) { | |
193 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
194 | |
195 // TODO(littledan): Preallocate rather than making an intermediate | |
196 // array, for better performance. | |
197 var length = %_TypedArrayGetLength(this); | |
198 var array = $innerArrayMap(predicate, thisArg, this, length); | |
199 return ConstructTypedArrayLike(this, array); | |
200 } | |
201 %FunctionSetLength(TypedArrayMap, 1); | |
202 | |
203 | |
204 // ES6 draft 05-05-15, section 22.2.3.24 | |
205 function TypedArraySome(f, receiver) { | |
206 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
207 | |
208 var length = %_TypedArrayGetLength(this); | |
209 | |
210 return $innerArraySome(f, receiver, this, length); | |
211 } | |
212 %FunctionSetLength(TypedArraySome, 1); | |
158 | 213 |
159 | 214 |
160 // ES6 draft 08-24-14, section 22.2.2.2 | 215 // ES6 draft 08-24-14, section 22.2.2.2 |
161 function TypedArrayOf() { | 216 function TypedArrayOf() { |
162 var length = %_ArgumentsLength(); | 217 var length = %_ArgumentsLength(); |
163 var array = new this(length); | 218 var array = new this(length); |
164 for (var i = 0; i < length; i++) { | 219 for (var i = 0; i < length; i++) { |
165 array[i] = %_Arguments(i); | 220 array[i] = %_Arguments(i); |
166 } | 221 } |
167 return array; | 222 return array; |
(...skipping 28 matching lines...) Expand all Loading... | |
196 // Set up non-enumerable functions on the object. | 251 // Set up non-enumerable functions on the object. |
197 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ | 252 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
198 "from", TypedArrayFrom, | 253 "from", TypedArrayFrom, |
199 "of", TypedArrayOf | 254 "of", TypedArrayOf |
200 ]); | 255 ]); |
201 | 256 |
202 // Set up non-enumerable functions on the prototype object. | 257 // Set up non-enumerable functions on the prototype object. |
203 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ | 258 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
204 "copyWithin", TypedArrayCopyWithin, | 259 "copyWithin", TypedArrayCopyWithin, |
205 "every", TypedArrayEvery, | 260 "every", TypedArrayEvery, |
206 "forEach", TypedArrayForEach, | 261 "fill", TypedArrayFill, |
262 "filter", TypedArrayFilter, | |
207 "find", TypedArrayFind, | 263 "find", TypedArrayFind, |
208 "findIndex", TypedArrayFindIndex, | 264 "findIndex", TypedArrayFindIndex, |
209 "fill", TypedArrayFill, | 265 "fill", TypedArrayFill, |
adamk
2015/05/20 18:50:07
This is a dup of the sorted line above.
dehrenberg
2015/05/20 23:39:01
Done.
| |
210 "indexOf", TypedArrayIndexOf, | 266 "indexOf", TypedArrayIndexOf, |
211 "lastIndexOf", TypedArrayLastIndexOf, | 267 "lastIndexOf", TypedArrayLastIndexOf, |
268 "forEach", TypedArrayForEach, | |
269 "map", TypedArrayMap, | |
212 "reverse", TypedArrayReverse, | 270 "reverse", TypedArrayReverse, |
271 "some", TypedArraySome | |
213 "sort", TypedArraySort | 272 "sort", TypedArraySort |
214 ]); | 273 ]); |
215 endmacro | 274 endmacro |
216 | 275 |
217 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 276 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
218 | 277 |
219 }) | 278 }) |
OLD | NEW |