Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: src/harmony-typedarray.js

Issue 1132163011: Add TypedArray.from method (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: improving style Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/harmony-array.js ('k') | test/mjsunit/harmony/typedarray-from.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // TODO(littledan): Investigate if there is a receiver which could be
122 // faster to accumulate on than Array, e.g., a TypedVector.
123 var array = %_CallFunction(GlobalArray, source, mapfn, thisArg, $arrayFrom);
124 return ConstructTypedArray(this, array);
125 }
126 %FunctionSetLength(TypedArrayFrom, 1);
127
128 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085).
103 macro EXTEND_TYPED_ARRAY(NAME) 129 macro EXTEND_TYPED_ARRAY(NAME)
104 // Set up non-enumerable functions on the object. 130 // Set up non-enumerable functions on the object.
105 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ 131 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [
132 "from", TypedArrayFrom,
106 "of", TypedArrayOf 133 "of", TypedArrayOf
107 ]); 134 ]);
108 135
109 // Set up non-enumerable functions on the prototype object. 136 // Set up non-enumerable functions on the prototype object.
110 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ 137 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [
111 "copyWithin", TypedArrayCopyWithin, 138 "copyWithin", TypedArrayCopyWithin,
112 "every", TypedArrayEvery, 139 "every", TypedArrayEvery,
113 "forEach", TypedArrayForEach, 140 "forEach", TypedArrayForEach,
114 "find", TypedArrayFind, 141 "find", TypedArrayFind,
115 "findIndex", TypedArrayFindIndex, 142 "findIndex", TypedArrayFindIndex,
116 "fill", TypedArrayFill 143 "fill", TypedArrayFill
117 ]); 144 ]);
118 endmacro 145 endmacro
119 146
120 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) 147 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
121 148
122 }) 149 })
OLDNEW
« no previous file with comments | « src/harmony-array.js ('k') | test/mjsunit/harmony/typedarray-from.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698