| 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) { |
| 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 (!%IsConstructor(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 ConstructTypedArray(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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 %FunctionSetLength(TypedArrayLastIndexOf, 1); |
| 158 | 190 |
| 159 | 191 |
| 192 // ES6 draft 07-15-13, section 22.2.3.18 |
| 193 function TypedArrayMap(predicate, thisArg) { |
| 194 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 195 |
| 196 // TODO(littledan): Preallocate rather than making an intermediate |
| 197 // InternalArray, for better performance. |
| 198 var length = %_TypedArrayGetLength(this); |
| 199 var array = $innerArrayMap(predicate, thisArg, this, length); |
| 200 return ConstructTypedArrayLike(this, array); |
| 201 } |
| 202 %FunctionSetLength(TypedArrayMap, 1); |
| 203 |
| 204 |
| 205 // ES6 draft 05-05-15, section 22.2.3.24 |
| 206 function TypedArraySome(f, receiver) { |
| 207 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 208 |
| 209 var length = %_TypedArrayGetLength(this); |
| 210 |
| 211 return $innerArraySome(f, receiver, this, length); |
| 212 } |
| 213 %FunctionSetLength(TypedArraySome, 1); |
| 214 |
| 215 |
| 160 // ES6 draft 08-24-14, section 22.2.2.2 | 216 // ES6 draft 08-24-14, section 22.2.2.2 |
| 161 function TypedArrayOf() { | 217 function TypedArrayOf() { |
| 162 var length = %_ArgumentsLength(); | 218 var length = %_ArgumentsLength(); |
| 163 var array = new this(length); | 219 var array = new this(length); |
| 164 for (var i = 0; i < length; i++) { | 220 for (var i = 0; i < length; i++) { |
| 165 array[i] = %_Arguments(i); | 221 array[i] = %_Arguments(i); |
| 166 } | 222 } |
| 167 return array; | 223 return array; |
| 168 } | 224 } |
| 169 | 225 |
| 170 function ConstructTypedArray(constructor, array) { | |
| 171 // TODO(littledan): This is an approximation of the spec, which requires | |
| 172 // that only real TypedArray classes should be accepted (22.2.2.1.1) | |
| 173 if (!IS_SPEC_OBJECT(constructor) || IS_UNDEFINED(constructor.prototype) || | |
| 174 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { | |
| 175 throw MakeTypeError(kNotTypedArray); | |
| 176 } | |
| 177 | |
| 178 // TODO(littledan): The spec requires that, rather than directly calling | |
| 179 // the constructor, a TypedArray is created with the proper proto and | |
| 180 // underlying size and element size, and elements are put in one by one. | |
| 181 // By contrast, this would allow subclasses to make a radically different | |
| 182 // constructor with different semantics. | |
| 183 return new constructor(array); | |
| 184 } | |
| 185 | 226 |
| 186 function TypedArrayFrom(source, mapfn, thisArg) { | 227 function TypedArrayFrom(source, mapfn, thisArg) { |
| 187 // TODO(littledan): Investigate if there is a receiver which could be | 228 // TODO(littledan): Investigate if there is a receiver which could be |
| 188 // faster to accumulate on than Array, e.g., a TypedVector. | 229 // faster to accumulate on than Array, e.g., a TypedVector. |
| 189 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom); | 230 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom); |
| 190 return ConstructTypedArray(this, array); | 231 return ConstructTypedArray(this, array); |
| 191 } | 232 } |
| 192 %FunctionSetLength(TypedArrayFrom, 1); | 233 %FunctionSetLength(TypedArrayFrom, 1); |
| 193 | 234 |
| 194 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). | 235 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). |
| 195 macro EXTEND_TYPED_ARRAY(NAME) | 236 macro EXTEND_TYPED_ARRAY(NAME) |
| 196 // Set up non-enumerable functions on the object. | 237 // Set up non-enumerable functions on the object. |
| 197 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ | 238 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
| 198 "from", TypedArrayFrom, | 239 "from", TypedArrayFrom, |
| 199 "of", TypedArrayOf | 240 "of", TypedArrayOf |
| 200 ]); | 241 ]); |
| 201 | 242 |
| 202 // Set up non-enumerable functions on the prototype object. | 243 // Set up non-enumerable functions on the prototype object. |
| 203 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ | 244 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
| 204 "copyWithin", TypedArrayCopyWithin, | 245 "copyWithin", TypedArrayCopyWithin, |
| 205 "every", TypedArrayEvery, | 246 "every", TypedArrayEvery, |
| 206 "forEach", TypedArrayForEach, | 247 "fill", TypedArrayFill, |
| 248 "filter", TypedArrayFilter, |
| 207 "find", TypedArrayFind, | 249 "find", TypedArrayFind, |
| 208 "findIndex", TypedArrayFindIndex, | 250 "findIndex", TypedArrayFindIndex, |
| 209 "fill", TypedArrayFill, | |
| 210 "indexOf", TypedArrayIndexOf, | 251 "indexOf", TypedArrayIndexOf, |
| 211 "lastIndexOf", TypedArrayLastIndexOf, | 252 "lastIndexOf", TypedArrayLastIndexOf, |
| 253 "forEach", TypedArrayForEach, |
| 254 "map", TypedArrayMap, |
| 212 "reverse", TypedArrayReverse, | 255 "reverse", TypedArrayReverse, |
| 256 "some", TypedArraySome, |
| 213 "sort", TypedArraySort | 257 "sort", TypedArraySort |
| 214 ]); | 258 ]); |
| 215 endmacro | 259 endmacro |
| 216 | 260 |
| 217 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 261 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
| 218 | 262 |
| 219 }) | 263 }) |
| OLD | NEW |