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