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 |
11 macro TYPED_ARRAYS(FUNCTION) | 11 macro TYPED_ARRAYS(FUNCTION) |
12 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 12 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
13 FUNCTION(Uint8Array) | 13 FUNCTION(Uint8Array) |
14 FUNCTION(Int8Array) | 14 FUNCTION(Int8Array) |
15 FUNCTION(Uint16Array) | 15 FUNCTION(Uint16Array) |
16 FUNCTION(Int16Array) | 16 FUNCTION(Int16Array) |
17 FUNCTION(Uint32Array) | 17 FUNCTION(Uint32Array) |
18 FUNCTION(Int32Array) | 18 FUNCTION(Int32Array) |
19 FUNCTION(Float32Array) | 19 FUNCTION(Float32Array) |
20 FUNCTION(Float64Array) | 20 FUNCTION(Float64Array) |
21 FUNCTION(Uint8ClampedArray) | 21 FUNCTION(Uint8ClampedArray) |
22 endmacro | 22 endmacro |
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 | 30 |
30 // ------------------------------------------------------------------- | 31 // ------------------------------------------------------------------- |
31 | 32 |
32 function TypedArrayCopyWithin(target, start, end) { | 33 function TypedArrayCopyWithin(target, start, end) { |
33 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 34 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
34 | 35 |
35 var length = %_TypedArrayGetLength(this); | 36 var length = %_TypedArrayGetLength(this); |
36 | 37 |
37 // TODO(dehrenberg): Replace with a memcpy for better performance | 38 // TODO(littledan): Replace with a memcpy for better performance |
38 return $innerArrayCopyWithin(target, start, end, this, length); | 39 return $innerArrayCopyWithin(target, start, end, this, length); |
39 } | 40 } |
40 %FunctionSetLength(TypedArrayCopyWithin, 2); | 41 %FunctionSetLength(TypedArrayCopyWithin, 2); |
41 | 42 |
42 // ES6 draft 05-05-15, section 22.2.3.7 | 43 // ES6 draft 05-05-15, section 22.2.3.7 |
43 function TypedArrayEvery(f, receiver) { | 44 function TypedArrayEvery(f, receiver) { |
44 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | 45 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
45 | 46 |
46 var length = %_TypedArrayGetLength(this); | 47 var length = %_TypedArrayGetLength(this); |
47 | 48 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 // ES6 draft 08-24-14, section 22.2.2.2 | 94 // ES6 draft 08-24-14, section 22.2.2.2 |
94 function TypedArrayOf() { | 95 function TypedArrayOf() { |
95 var length = %_ArgumentsLength(); | 96 var length = %_ArgumentsLength(); |
96 var array = new this(length); | 97 var array = new this(length); |
97 for (var i = 0; i < length; i++) { | 98 for (var i = 0; i < length; i++) { |
98 array[i] = %_Arguments(i); | 99 array[i] = %_Arguments(i); |
99 } | 100 } |
100 return array; | 101 return array; |
101 } | 102 } |
102 | 103 |
104 function ConstructTypedArray(constructor, array) { | |
105 // TODO(littledan): This is an approximation of the spec, which requires | |
106 // that only real TypedArray classes should be accepted (22.2.2.1.1) | |
107 if (!IS_SPEC_OBJECT(constructor) || IS_UNDEFINED(constructor.prototype) || | |
108 !%HasOwnProperty(constructor.prototype, "BYTES_PER_ELEMENT")) { | |
109 throw MakeTypeError(kNotTypedArray); | |
110 } | |
111 | |
112 // TODO(littledan): The spec requires that, rather than directly calling | |
113 // the constructor, a TypedArray is created with the proper proto and | |
114 // underlying size and element size, and elements are put in one by one. | |
115 // By contrast, this would allow subclasses to make a radically different | |
116 // constructor with different semantics. | |
117 return new constructor(array); | |
118 } | |
119 | |
120 function TypedArrayFrom(source, mapfn, thisArg) { | |
121 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom); | |
arv (Not doing code reviews)
2015/05/16 18:23:45
Add todo. It would surely be better to not have a
dehrenberg
2015/05/18 17:56:50
The problem is that, if the source is iterable, th
| |
122 return ConstructTypedArray(this, array); | |
123 } | |
124 %FunctionSetLength(TypedArrayFrom, 1); | |
125 | |
126 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). | |
103 macro EXTEND_TYPED_ARRAY(NAME) | 127 macro EXTEND_TYPED_ARRAY(NAME) |
104 // Set up non-enumerable functions on the object. | 128 // Set up non-enumerable functions on the object. |
105 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ | 129 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
130 "from", TypedArrayFrom, | |
106 "of", TypedArrayOf | 131 "of", TypedArrayOf |
107 ]); | 132 ]); |
108 | 133 |
109 // Set up non-enumerable functions on the prototype object. | 134 // Set up non-enumerable functions on the prototype object. |
110 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ | 135 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
111 "copyWithin", TypedArrayCopyWithin, | 136 "copyWithin", TypedArrayCopyWithin, |
112 "every", TypedArrayEvery, | 137 "every", TypedArrayEvery, |
113 "forEach", TypedArrayForEach, | 138 "forEach", TypedArrayForEach, |
114 "find", TypedArrayFind, | 139 "find", TypedArrayFind, |
115 "findIndex", TypedArrayFindIndex, | 140 "findIndex", TypedArrayFindIndex, |
116 "fill", TypedArrayFill | 141 "fill", TypedArrayFill |
117 ]); | 142 ]); |
118 endmacro | 143 endmacro |
119 | 144 |
120 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 145 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
121 | 146 |
122 }) | 147 }) |
OLD | NEW |