| 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 (!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 | |
| 55 function TypedArrayCopyWithin(target, start, end) { | 33 function TypedArrayCopyWithin(target, start, end) { |
| 56 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 34 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 57 | 35 |
| 58 var length = %_TypedArrayGetLength(this); | 36 var length = %_TypedArrayGetLength(this); |
| 59 | 37 |
| 60 // TODO(littledan): Replace with a memcpy for better performance | 38 // TODO(littledan): Replace with a memcpy for better performance |
| 61 return $innerArrayCopyWithin(target, start, end, this, length); | 39 return $innerArrayCopyWithin(target, start, end, this, length); |
| 62 } | 40 } |
| 63 %FunctionSetLength(TypedArrayCopyWithin, 2); | 41 %FunctionSetLength(TypedArrayCopyWithin, 2); |
| 64 | 42 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 76 function TypedArrayForEach(f, receiver) { | 54 function TypedArrayForEach(f, receiver) { |
| 77 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 55 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 78 | 56 |
| 79 var length = %_TypedArrayGetLength(this); | 57 var length = %_TypedArrayGetLength(this); |
| 80 | 58 |
| 81 $innerArrayForEach(f, receiver, this, length); | 59 $innerArrayForEach(f, receiver, this, length); |
| 82 } | 60 } |
| 83 %FunctionSetLength(TypedArrayForEach, 1); | 61 %FunctionSetLength(TypedArrayForEach, 1); |
| 84 | 62 |
| 85 // ES6 draft 04-05-14 section 22.2.3.8 | 63 // ES6 draft 04-05-14 section 22.2.3.8 |
| 86 function TypedArrayFill(value, start, end) { | 64 function TypedArrayFill(value, start , end) { |
| 87 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 65 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 88 | 66 |
| 89 var length = %_TypedArrayGetLength(this); | 67 var length = %_TypedArrayGetLength(this); |
| 90 | 68 |
| 91 return $innerArrayFill(value, start, end, this, length); | 69 return $innerArrayFill(value, start, end, this, length); |
| 92 } | 70 } |
| 93 %FunctionSetLength(TypedArrayFill, 1); | 71 %FunctionSetLength(TypedArrayFill, 1); |
| 94 | 72 |
| 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 | |
| 105 // ES6 draft 07-15-13, section 22.2.3.10 | 73 // ES6 draft 07-15-13, section 22.2.3.10 |
| 106 function TypedArrayFind(predicate, thisArg) { | 74 function TypedArrayFind(predicate, thisArg) { |
| 107 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 75 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 108 | 76 |
| 109 var length = %_TypedArrayGetLength(this); | 77 var length = %_TypedArrayGetLength(this); |
| 110 | 78 |
| 111 return $innerArrayFind(predicate, thisArg, this, length); | 79 return $innerArrayFind(predicate, thisArg, this, length); |
| 112 } | 80 } |
| 113 %FunctionSetLength(TypedArrayFind, 1); | 81 %FunctionSetLength(TypedArrayFind, 1); |
| 114 | 82 |
| 115 // ES6 draft 07-15-13, section 22.2.3.11 | 83 // ES6 draft 07-15-13, section 22.2.3.11 |
| 116 function TypedArrayFindIndex(predicate, thisArg) { | 84 function TypedArrayFindIndex(predicate, thisArg) { |
| 117 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 85 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| 118 | 86 |
| 119 var length = %_TypedArrayGetLength(this); | 87 var length = %_TypedArrayGetLength(this); |
| 120 | 88 |
| 121 return $innerArrayFindIndex(predicate, thisArg, this, length); | 89 return $innerArrayFindIndex(predicate, thisArg, this, length); |
| 122 } | 90 } |
| 123 %FunctionSetLength(TypedArrayFindIndex, 1); | 91 %FunctionSetLength(TypedArrayFindIndex, 1); |
| 124 | 92 |
| 125 | 93 |
| 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 // TODO(littledan): Preallocate rather than making an intermediate | |
| 131 // array, for better performance. | |
| 132 var length = %_TypedArrayGetLength(this); | |
| 133 var array = $innerArrayMap(predicate, thisArg, this, length); | |
| 134 return ConstructTypedArrayLike(this, array); | |
| 135 } | |
| 136 %FunctionSetLength(TypedArrayMap, 1); | |
| 137 | |
| 138 | |
| 139 // ES6 draft 07-15-13, section 22.2.3.19 | |
| 140 function TypedArrayReduce(callback, current) { | |
| 141 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
| 142 | |
| 143 var length = %_TypedArrayGetLength(this); | |
| 144 return $innerArrayReduce(callback, current, this, length, | |
| 145 %_ArgumentsLength()); | |
| 146 } | |
| 147 %FunctionSetLength(TypedArrayReduce, 1); | |
| 148 | |
| 149 | |
| 150 // ES6 draft 07-15-13, section 22.2.3.19 | |
| 151 function TypedArrayReduceRight(callback, current) { | |
| 152 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
| 153 | |
| 154 var length = %_TypedArrayGetLength(this); | |
| 155 return $innerArrayReduceRight(callback, current, this, length, | |
| 156 %_ArgumentsLength()); | |
| 157 } | |
| 158 %FunctionSetLength(TypedArrayReduceRight, 1); | |
| 159 | |
| 160 | |
| 161 // ES6 draft 05-05-15, section 22.2.3.24 | |
| 162 function TypedArraySome(f, receiver) { | |
| 163 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
| 164 | |
| 165 var length = %_TypedArrayGetLength(this); | |
| 166 | |
| 167 return $innerArraySome(f, receiver, this, length); | |
| 168 } | |
| 169 %FunctionSetLength(TypedArraySome, 1); | |
| 170 | |
| 171 | |
| 172 // ES6 draft 08-24-14, section 22.2.2.2 | 94 // ES6 draft 08-24-14, section 22.2.2.2 |
| 173 function TypedArrayOf() { | 95 function TypedArrayOf() { |
| 174 var length = %_ArgumentsLength(); | 96 var length = %_ArgumentsLength(); |
| 175 var array = new this(length); | 97 var array = new this(length); |
| 176 for (var i = 0; i < length; i++) { | 98 for (var i = 0; i < length; i++) { |
| 177 array[i] = %_Arguments(i); | 99 array[i] = %_Arguments(i); |
| 178 } | 100 } |
| 179 return array; | 101 return array; |
| 180 } | 102 } |
| 181 | 103 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 208 // Set up non-enumerable functions on the object. | 130 // Set up non-enumerable functions on the object. |
| 209 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ | 131 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
| 210 "from", TypedArrayFrom, | 132 "from", TypedArrayFrom, |
| 211 "of", TypedArrayOf | 133 "of", TypedArrayOf |
| 212 ]); | 134 ]); |
| 213 | 135 |
| 214 // Set up non-enumerable functions on the prototype object. | 136 // Set up non-enumerable functions on the prototype object. |
| 215 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ | 137 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
| 216 "copyWithin", TypedArrayCopyWithin, | 138 "copyWithin", TypedArrayCopyWithin, |
| 217 "every", TypedArrayEvery, | 139 "every", TypedArrayEvery, |
| 218 "fill", TypedArrayFill, | 140 "forEach", TypedArrayForEach, |
| 219 "filter", TypedArrayFilter, | |
| 220 "find", TypedArrayFind, | 141 "find", TypedArrayFind, |
| 221 "findIndex", TypedArrayFindIndex, | 142 "findIndex", TypedArrayFindIndex, |
| 222 "forEach", TypedArrayForEach, | 143 "fill", TypedArrayFill |
| 223 "map", TypedArrayMap, | |
| 224 "reduce", TypedArrayReduce, | |
| 225 "reduceRight", TypedArrayReduceRight, | |
| 226 "some", TypedArraySome | |
| 227 ]); | 144 ]); |
| 228 endmacro | 145 endmacro |
| 229 | 146 |
| 230 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 147 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
| 231 | 148 |
| 232 }) | 149 }) |
| OLD | NEW |