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, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | |
12 // Imports | |
13 | |
14 macro TYPED_ARRAYS(FUNCTION) | 11 macro TYPED_ARRAYS(FUNCTION) |
15 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 12 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
16 FUNCTION(Uint8Array) | 13 FUNCTION(Uint8Array) |
17 FUNCTION(Int8Array) | 14 FUNCTION(Int8Array) |
18 FUNCTION(Uint16Array) | 15 FUNCTION(Uint16Array) |
19 FUNCTION(Int16Array) | 16 FUNCTION(Int16Array) |
20 FUNCTION(Uint32Array) | 17 FUNCTION(Uint32Array) |
21 FUNCTION(Int32Array) | 18 FUNCTION(Int32Array) |
22 FUNCTION(Float32Array) | 19 FUNCTION(Float32Array) |
23 FUNCTION(Float64Array) | 20 FUNCTION(Float64Array) |
24 FUNCTION(Uint8ClampedArray) | 21 FUNCTION(Uint8ClampedArray) |
25 endmacro | 22 endmacro |
26 | 23 |
27 macro DECLARE_GLOBALS(NAME) | 24 macro DECLARE_GLOBALS(NAME) |
28 var GlobalNAME = global.NAME; | 25 var GlobalNAME = global.NAME; |
29 endmacro | 26 endmacro |
30 | 27 |
31 TYPED_ARRAYS(DECLARE_GLOBALS) | 28 TYPED_ARRAYS(DECLARE_GLOBALS) |
32 DECLARE_GLOBALS(Array) | 29 DECLARE_GLOBALS(Array) |
33 | 30 |
34 var ArrayFrom; | |
35 var InnerArrayCopyWithin; | |
36 var InnerArrayEvery; | |
37 var InnerArrayFill; | |
38 var InnerArrayFilter; | |
39 var InnerArrayFind; | |
40 var InnerArrayFindIndex; | |
41 var InnerArrayForEach; | |
42 var InnerArrayIndexOf; | |
43 var InnerArrayLastIndexOf; | |
44 var InnerArrayMap; | |
45 var InnerArrayReverse; | |
46 var InnerArraySome; | |
47 var InnerArraySort; | |
48 var IsNaN; | |
49 | |
50 utils.Import(function(from) { | |
51 ArrayFrom = from.ArrayFrom; | |
52 InnerArrayCopyWithin = from.InnerArrayCopyWithin; | |
53 InnerArrayEvery = from.InnerArrayEvery; | |
54 InnerArrayFill = from.InnerArrayFill; | |
55 InnerArrayFilter = from.InnerArrayFilter; | |
56 InnerArrayFind = from.InnerArrayFind; | |
57 InnerArrayFindIndex = from.InnerArrayFindIndex; | |
58 InnerArrayForEach = from.InnerArrayForEach; | |
59 InnerArrayIndexOf = from.InnerArrayIndexOf; | |
60 InnerArrayLastIndexOf = from.InnerArrayLastIndexOf; | |
61 InnerArrayMap = from.InnerArrayMap; | |
62 InnerArrayReverse = from.InnerArrayReverse; | |
63 InnerArraySome = from.InnerArraySome; | |
64 InnerArraySort = from.InnerArraySort; | |
65 IsNaN = from.IsNaN; | |
66 }); | |
67 | |
68 // ------------------------------------------------------------------- | 31 // ------------------------------------------------------------------- |
69 | 32 |
70 function ConstructTypedArray(constructor, array) { | 33 function ConstructTypedArray(constructor, array) { |
71 // TODO(littledan): This is an approximation of the spec, which requires | 34 // TODO(littledan): This is an approximation of the spec, which requires |
72 // that only real TypedArray classes should be accepted (22.2.2.1.1) | 35 // that only real TypedArray classes should be accepted (22.2.2.1.1) |
73 if (!%IsConstructor(constructor) || IS_UNDEFINED(constructor.prototype) || | 36 if (!%IsConstructor(constructor) || IS_UNDEFINED(constructor.prototype) || |
74 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { | 37 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { |
75 throw MakeTypeError(kNotTypedArray); | 38 throw MakeTypeError(kNotTypedArray); |
76 } | 39 } |
77 | 40 |
(...skipping 10 matching lines...) Expand all Loading... |
88 // typedArray.constructor[Symbol.species] (bug v8:4093) | 51 // typedArray.constructor[Symbol.species] (bug v8:4093) |
89 return ConstructTypedArray(typedArray.constructor, arrayContents); | 52 return ConstructTypedArray(typedArray.constructor, arrayContents); |
90 } | 53 } |
91 | 54 |
92 function TypedArrayCopyWithin(target, start, end) { | 55 function TypedArrayCopyWithin(target, start, end) { |
93 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 56 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
94 | 57 |
95 var length = %_TypedArrayGetLength(this); | 58 var length = %_TypedArrayGetLength(this); |
96 | 59 |
97 // TODO(littledan): Replace with a memcpy for better performance | 60 // TODO(littledan): Replace with a memcpy for better performance |
98 return InnerArrayCopyWithin(target, start, end, this, length); | 61 return $innerArrayCopyWithin(target, start, end, this, length); |
99 } | 62 } |
100 %FunctionSetLength(TypedArrayCopyWithin, 2); | 63 %FunctionSetLength(TypedArrayCopyWithin, 2); |
101 | 64 |
102 // ES6 draft 05-05-15, section 22.2.3.7 | 65 // ES6 draft 05-05-15, section 22.2.3.7 |
103 function TypedArrayEvery(f, receiver) { | 66 function TypedArrayEvery(f, receiver) { |
104 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 67 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
105 | 68 |
106 var length = %_TypedArrayGetLength(this); | 69 var length = %_TypedArrayGetLength(this); |
107 | 70 |
108 return InnerArrayEvery(f, receiver, this, length); | 71 return $innerArrayEvery(f, receiver, this, length); |
109 } | 72 } |
110 %FunctionSetLength(TypedArrayEvery, 1); | 73 %FunctionSetLength(TypedArrayEvery, 1); |
111 | 74 |
112 // ES6 draft 08-24-14, section 22.2.3.12 | 75 // ES6 draft 08-24-14, section 22.2.3.12 |
113 function TypedArrayForEach(f, receiver) { | 76 function TypedArrayForEach(f, receiver) { |
114 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 77 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
115 | 78 |
116 var length = %_TypedArrayGetLength(this); | 79 var length = %_TypedArrayGetLength(this); |
117 | 80 |
118 InnerArrayForEach(f, receiver, this, length); | 81 $innerArrayForEach(f, receiver, this, length); |
119 } | 82 } |
120 %FunctionSetLength(TypedArrayForEach, 1); | 83 %FunctionSetLength(TypedArrayForEach, 1); |
121 | 84 |
122 // ES6 draft 04-05-14 section 22.2.3.8 | 85 // ES6 draft 04-05-14 section 22.2.3.8 |
123 function TypedArrayFill(value, start, end) { | 86 function TypedArrayFill(value, start, end) { |
124 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 87 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
125 | 88 |
126 var length = %_TypedArrayGetLength(this); | 89 var length = %_TypedArrayGetLength(this); |
127 | 90 |
128 return InnerArrayFill(value, start, end, this, length); | 91 return $innerArrayFill(value, start, end, this, length); |
129 } | 92 } |
130 %FunctionSetLength(TypedArrayFill, 1); | 93 %FunctionSetLength(TypedArrayFill, 1); |
131 | 94 |
132 // ES6 draft 07-15-13, section 22.2.3.9 | 95 // ES6 draft 07-15-13, section 22.2.3.9 |
133 function TypedArrayFilter(predicate, thisArg) { | 96 function TypedArrayFilter(predicate, thisArg) { |
134 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 97 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
135 | 98 |
136 var length = %_TypedArrayGetLength(this); | 99 var length = %_TypedArrayGetLength(this); |
137 var array = InnerArrayFilter(predicate, thisArg, this, length); | 100 var array = $innerArrayFilter(predicate, thisArg, this, length); |
138 return ConstructTypedArrayLike(this, array); | 101 return ConstructTypedArrayLike(this, array); |
139 } | 102 } |
140 %FunctionSetLength(TypedArrayFilter, 1); | 103 %FunctionSetLength(TypedArrayFilter, 1); |
141 | 104 |
142 // ES6 draft 07-15-13, section 22.2.3.10 | 105 // ES6 draft 07-15-13, section 22.2.3.10 |
143 function TypedArrayFind(predicate, thisArg) { | 106 function TypedArrayFind(predicate, thisArg) { |
144 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 107 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
145 | 108 |
146 var length = %_TypedArrayGetLength(this); | 109 var length = %_TypedArrayGetLength(this); |
147 | 110 |
148 return InnerArrayFind(predicate, thisArg, this, length); | 111 return $innerArrayFind(predicate, thisArg, this, length); |
149 } | 112 } |
150 %FunctionSetLength(TypedArrayFind, 1); | 113 %FunctionSetLength(TypedArrayFind, 1); |
151 | 114 |
152 // ES6 draft 07-15-13, section 22.2.3.11 | 115 // ES6 draft 07-15-13, section 22.2.3.11 |
153 function TypedArrayFindIndex(predicate, thisArg) { | 116 function TypedArrayFindIndex(predicate, thisArg) { |
154 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 117 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
155 | 118 |
156 var length = %_TypedArrayGetLength(this); | 119 var length = %_TypedArrayGetLength(this); |
157 | 120 |
158 return InnerArrayFindIndex(predicate, thisArg, this, length); | 121 return $innerArrayFindIndex(predicate, thisArg, this, length); |
159 } | 122 } |
160 %FunctionSetLength(TypedArrayFindIndex, 1); | 123 %FunctionSetLength(TypedArrayFindIndex, 1); |
161 | 124 |
162 // ES6 draft 05-18-15, section 22.2.3.21 | 125 // ES6 draft 05-18-15, section 22.2.3.21 |
163 function TypedArrayReverse() { | 126 function TypedArrayReverse() { |
164 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 127 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
165 | 128 |
166 var length = %_TypedArrayGetLength(this); | 129 var length = %_TypedArrayGetLength(this); |
167 | 130 |
168 return InnerArrayReverse(this, length); | 131 return $innerArrayReverse(this, length); |
169 } | 132 } |
170 | 133 |
171 | 134 |
172 function TypedArrayComparefn(x, y) { | 135 function TypedArrayComparefn(x, y) { |
173 if (IsNaN(x) && IsNaN(y)) { | 136 if ($isNaN(x) && $isNaN(y)) { |
174 return IsNaN(y) ? 0 : 1; | 137 return $isNaN(y) ? 0 : 1; |
175 } | 138 } |
176 if (IsNaN(x)) { | 139 if ($isNaN(x)) { |
177 return 1; | 140 return 1; |
178 } | 141 } |
179 if (x === 0 && x === y) { | 142 if (x === 0 && x === y) { |
180 if (%_IsMinusZero(x)) { | 143 if (%_IsMinusZero(x)) { |
181 if (!%_IsMinusZero(y)) { | 144 if (!%_IsMinusZero(y)) { |
182 return -1; | 145 return -1; |
183 } | 146 } |
184 } else if (%_IsMinusZero(y)) { | 147 } else if (%_IsMinusZero(y)) { |
185 return 1; | 148 return 1; |
186 } | 149 } |
187 } | 150 } |
188 return x - y; | 151 return x - y; |
189 } | 152 } |
190 | 153 |
191 | 154 |
192 // ES6 draft 05-18-15, section 22.2.3.25 | 155 // ES6 draft 05-18-15, section 22.2.3.25 |
193 function TypedArraySort(comparefn) { | 156 function TypedArraySort(comparefn) { |
194 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 157 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
195 | 158 |
196 var length = %_TypedArrayGetLength(this); | 159 var length = %_TypedArrayGetLength(this); |
197 | 160 |
198 if (IS_UNDEFINED(comparefn)) { | 161 if (IS_UNDEFINED(comparefn)) { |
199 comparefn = TypedArrayComparefn; | 162 comparefn = TypedArrayComparefn; |
200 } | 163 } |
201 | 164 |
202 return %_CallFunction(this, length, comparefn, InnerArraySort); | 165 return %_CallFunction(this, length, comparefn, $innerArraySort); |
203 } | 166 } |
204 | 167 |
205 | 168 |
206 // ES6 section 22.2.3.13 | 169 // ES6 section 22.2.3.13 |
207 function TypedArrayIndexOf(element, index) { | 170 function TypedArrayIndexOf(element, index) { |
208 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 171 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
209 | 172 |
210 var length = %_TypedArrayGetLength(this); | 173 var length = %_TypedArrayGetLength(this); |
211 | 174 |
212 return %_CallFunction(this, element, index, length, InnerArrayIndexOf); | 175 return %_CallFunction(this, element, index, length, $innerArrayIndexOf); |
213 } | 176 } |
214 %FunctionSetLength(TypedArrayIndexOf, 1); | 177 %FunctionSetLength(TypedArrayIndexOf, 1); |
215 | 178 |
216 | 179 |
217 // ES6 section 22.2.3.16 | 180 // ES6 section 22.2.3.16 |
218 function TypedArrayLastIndexOf(element, index) { | 181 function TypedArrayLastIndexOf(element, index) { |
219 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 182 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
220 | 183 |
221 var length = %_TypedArrayGetLength(this); | 184 var length = %_TypedArrayGetLength(this); |
222 | 185 |
223 return %_CallFunction(this, element, index, length, | 186 return %_CallFunction(this, element, index, length, |
224 %_ArgumentsLength(), InnerArrayLastIndexOf); | 187 %_ArgumentsLength(), $innerArrayLastIndexOf); |
225 } | 188 } |
226 %FunctionSetLength(TypedArrayLastIndexOf, 1); | 189 %FunctionSetLength(TypedArrayLastIndexOf, 1); |
227 | 190 |
228 | 191 |
229 // ES6 draft 07-15-13, section 22.2.3.18 | 192 // ES6 draft 07-15-13, section 22.2.3.18 |
230 function TypedArrayMap(predicate, thisArg) { | 193 function TypedArrayMap(predicate, thisArg) { |
231 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 194 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
232 | 195 |
233 // TODO(littledan): Preallocate rather than making an intermediate | 196 // TODO(littledan): Preallocate rather than making an intermediate |
234 // InternalArray, for better performance. | 197 // InternalArray, for better performance. |
235 var length = %_TypedArrayGetLength(this); | 198 var length = %_TypedArrayGetLength(this); |
236 var array = InnerArrayMap(predicate, thisArg, this, length); | 199 var array = $innerArrayMap(predicate, thisArg, this, length); |
237 return ConstructTypedArrayLike(this, array); | 200 return ConstructTypedArrayLike(this, array); |
238 } | 201 } |
239 %FunctionSetLength(TypedArrayMap, 1); | 202 %FunctionSetLength(TypedArrayMap, 1); |
240 | 203 |
241 | 204 |
242 // ES6 draft 05-05-15, section 22.2.3.24 | 205 // ES6 draft 05-05-15, section 22.2.3.24 |
243 function TypedArraySome(f, receiver) { | 206 function TypedArraySome(f, receiver) { |
244 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 207 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
245 | 208 |
246 var length = %_TypedArrayGetLength(this); | 209 var length = %_TypedArrayGetLength(this); |
247 | 210 |
248 return InnerArraySome(f, receiver, this, length); | 211 return $innerArraySome(f, receiver, this, length); |
249 } | 212 } |
250 %FunctionSetLength(TypedArraySome, 1); | 213 %FunctionSetLength(TypedArraySome, 1); |
251 | 214 |
252 | 215 |
253 // ES6 draft 08-24-14, section 22.2.2.2 | 216 // ES6 draft 08-24-14, section 22.2.2.2 |
254 function TypedArrayOf() { | 217 function TypedArrayOf() { |
255 var length = %_ArgumentsLength(); | 218 var length = %_ArgumentsLength(); |
256 var array = new this(length); | 219 var array = new this(length); |
257 for (var i = 0; i < length; i++) { | 220 for (var i = 0; i < length; i++) { |
258 array[i] = %_Arguments(i); | 221 array[i] = %_Arguments(i); |
259 } | 222 } |
260 return array; | 223 return array; |
261 } | 224 } |
262 | 225 |
263 | 226 |
264 function TypedArrayFrom(source, mapfn, thisArg) { | 227 function TypedArrayFrom(source, mapfn, thisArg) { |
265 // TODO(littledan): Investigate if there is a receiver which could be | 228 // TODO(littledan): Investigate if there is a receiver which could be |
266 // faster to accumulate on than Array, e.g., a TypedVector. | 229 // faster to accumulate on than Array, e.g., a TypedVector. |
267 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, ArrayFrom); | 230 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom); |
268 return ConstructTypedArray(this, array); | 231 return ConstructTypedArray(this, array); |
269 } | 232 } |
270 %FunctionSetLength(TypedArrayFrom, 1); | 233 %FunctionSetLength(TypedArrayFrom, 1); |
271 | 234 |
272 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). | 235 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). |
273 macro EXTEND_TYPED_ARRAY(NAME) | 236 macro EXTEND_TYPED_ARRAY(NAME) |
274 // Set up non-enumerable functions on the object. | 237 // Set up non-enumerable functions on the object. |
275 utils.InstallFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ | 238 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
276 "from", TypedArrayFrom, | 239 "from", TypedArrayFrom, |
277 "of", TypedArrayOf | 240 "of", TypedArrayOf |
278 ]); | 241 ]); |
279 | 242 |
280 // Set up non-enumerable functions on the prototype object. | 243 // Set up non-enumerable functions on the prototype object. |
281 utils.InstallFunctions(GlobalNAME.prototype, DONT_ENUM, [ | 244 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
282 "copyWithin", TypedArrayCopyWithin, | 245 "copyWithin", TypedArrayCopyWithin, |
283 "every", TypedArrayEvery, | 246 "every", TypedArrayEvery, |
284 "fill", TypedArrayFill, | 247 "fill", TypedArrayFill, |
285 "filter", TypedArrayFilter, | 248 "filter", TypedArrayFilter, |
286 "find", TypedArrayFind, | 249 "find", TypedArrayFind, |
287 "findIndex", TypedArrayFindIndex, | 250 "findIndex", TypedArrayFindIndex, |
288 "indexOf", TypedArrayIndexOf, | 251 "indexOf", TypedArrayIndexOf, |
289 "lastIndexOf", TypedArrayLastIndexOf, | 252 "lastIndexOf", TypedArrayLastIndexOf, |
290 "forEach", TypedArrayForEach, | 253 "forEach", TypedArrayForEach, |
291 "map", TypedArrayMap, | 254 "map", TypedArrayMap, |
292 "reverse", TypedArrayReverse, | 255 "reverse", TypedArrayReverse, |
293 "some", TypedArraySome, | 256 "some", TypedArraySome, |
294 "sort", TypedArraySort | 257 "sort", TypedArraySort |
295 ]); | 258 ]); |
296 endmacro | 259 endmacro |
297 | 260 |
298 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 261 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
299 | 262 |
300 }) | 263 }) |
OLD | NEW |