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

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

Issue 1131113002: TypedArray.prototype.copyWithin method (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: 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
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() { 5 (function() {
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(1, Uint8Array, 1) 13 FUNCTION(Uint8Array)
14 FUNCTION(2, Int8Array, 1) 14 FUNCTION(Int8Array)
15 FUNCTION(3, Uint16Array, 2) 15 FUNCTION(Uint16Array)
16 FUNCTION(4, Int16Array, 2) 16 FUNCTION(Int16Array)
17 FUNCTION(5, Uint32Array, 4) 17 FUNCTION(Uint32Array)
18 FUNCTION(6, Int32Array, 4) 18 FUNCTION(Int32Array)
19 FUNCTION(7, Float32Array, 4) 19 FUNCTION(Float32Array)
20 FUNCTION(8, Float64Array, 8) 20 FUNCTION(Float64Array)
21 FUNCTION(9, Uint8ClampedArray, 1) 21 FUNCTION(Uint8ClampedArray)
22 endmacro 22 endmacro
23 23
24 macro DECLARE_GLOBALS(INDEX, NAME, SIZE) 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 29
30 // ------------------------------------------------------------------- 30 // ES6 draft 03-17-15, section 22.2.3.5
31 31 function TypedArrayCopyWithin(target, start, end) {
32 macro TYPED_ARRAY_HARMONY_ADDITIONS(ARRAY_ID, NAME, ELEMENT_SIZE)
33
34 // ES6 draft 05-05-15, section 22.2.3.7
35 function NAMEEvery(f /* thisArg */) { // length == 1
36 if (!%IsTypedArray(this)) { 32 if (!%IsTypedArray(this)) {
37 throw MakeTypeError('not_typed_array', []); 33 throw MakeTypeError('not_typed_array', []);
38 } 34 }
35
36 var length = %_TypedArrayGetLength(this);
37
38 target = TO_INTEGER(target);
39 var to;
40 if (target < 0) {
41 to = $max(length + target, 0);
42 } else {
43 to = $min(target, length);
44 }
45
46 start = TO_INTEGER(start);
47 var from;
48 if (start < 0) {
49 from = $max(length + start, 0);
50 } else {
51 from = $min(start, length);
52 }
53
54 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
55 var final;
56 if (end < 0) {
57 final = $max(length + end, 0);
58 } else {
59 final = $min(end, length);
60 }
61
62 var count = $min(final - from, length - to);
63 var direction = 1;
64 if (from < to && to < (from + count)) {
65 direction = -1;
66 from = from + count - 1;
67 to = to + count - 1;
68 }
69
70 while (count > 0) {
71 this[to] = this[from];
caitp (gmail) 2015/05/07 20:13:20 This is a pretty good first implementation. Might
adamk 2015/05/07 20:28:07 To expand on this a bit, is there any reason not t
72 from = from + direction;
73 to = to + direction;
74 count--;
75 }
76
77 return this;
78 }
79
80 // -------------------------------------------------------------------
81
82 // ES6 draft 05-05-15, section 22.2.3.7
83 function TypedArrayEvery(f /* thisArg */) { // length == 1
caitp (gmail) 2015/05/07 20:13:20 Moving the rest of these outside the macro is good
84 if (!%IsTypedArray(this)) {
85 throw MakeTypeError('not_typed_array', []);
86 }
39 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 87 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
40 88
41 var length = %_TypedArrayGetLength(this); 89 var length = %_TypedArrayGetLength(this);
42 var receiver; 90 var receiver;
43 91
44 if (%_ArgumentsLength() > 1) { 92 if (%_ArgumentsLength() > 1) {
45 receiver = %_Arguments(1); 93 receiver = %_Arguments(1);
46 } 94 }
47 95
48 var needs_wrapper = false; 96 var needs_wrapper = false;
(...skipping 10 matching lines...) Expand all
59 if (stepping) %DebugPrepareStepInIfStepping(f); 107 if (stepping) %DebugPrepareStepInIfStepping(f);
60 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; 108 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver;
61 if (!%_CallFunction(new_receiver, TO_OBJECT_INLINE(element), i, this, f)) { 109 if (!%_CallFunction(new_receiver, TO_OBJECT_INLINE(element), i, this, f)) {
62 return false; 110 return false;
63 } 111 }
64 } 112 }
65 return true; 113 return true;
66 } 114 }
67 115
68 // ES6 draft 08-24-14, section 22.2.3.12 116 // ES6 draft 08-24-14, section 22.2.3.12
69 function NAMEForEach(f /* thisArg */) { // length == 1 117 function TypedArrayForEach(f /* thisArg */) { // length == 1
70 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); 118 if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
71 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 119 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
72 120
73 var length = %_TypedArrayGetLength(this); 121 var length = %_TypedArrayGetLength(this);
74 var receiver; 122 var receiver;
75 123
76 if (%_ArgumentsLength() > 1) { 124 if (%_ArgumentsLength() > 1) {
77 receiver = %_Arguments(1); 125 receiver = %_Arguments(1);
78 } 126 }
79 127
80 var needs_wrapper = false; 128 var needs_wrapper = false;
81 if (IS_NULL(receiver)) { 129 if (IS_NULL(receiver)) {
82 if (%IsSloppyModeFunction(mapfn)) receiver = UNDEFINED; 130 if (%IsSloppyModeFunction(mapfn)) receiver = UNDEFINED;
83 } else if (!IS_UNDEFINED(receiver)) { 131 } else if (!IS_UNDEFINED(receiver)) {
84 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 132 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
85 } 133 }
86 134
87 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 135 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
88 for (var i = 0; i < length; i++) { 136 for (var i = 0; i < length; i++) {
89 var element = this[i]; 137 var element = this[i];
90 // Prepare break slots for debugger step in. 138 // Prepare break slots for debugger step in.
91 if (stepping) %DebugPrepareStepInIfStepping(f); 139 if (stepping) %DebugPrepareStepInIfStepping(f);
92 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; 140 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver;
93 %_CallFunction(new_receiver, TO_OBJECT_INLINE(element), i, this, f); 141 %_CallFunction(new_receiver, TO_OBJECT_INLINE(element), i, this, f);
94 } 142 }
95 } 143 }
96 144
97 // ES6 draft 08-24-14, section 22.2.2.2 145 // ES6 draft 08-24-14, section 22.2.2.2
98 function NAMEOf() { // length == 0 146 function TypedArrayOf() { // length == 0
99 var length = %_ArgumentsLength(); 147 var length = %_ArgumentsLength();
100 var array = new this(length); 148 var array = new this(length);
101 for (var i = 0; i < length; i++) { 149 for (var i = 0; i < length; i++) {
102 array[i] = %_Arguments(i); 150 array[i] = %_Arguments(i);
103 } 151 }
104 return array; 152 return array;
105 } 153 }
106 154
107 endmacro 155 %FunctionSetLength(TypedArrayCopyWithin, 2);
108 156
109 TYPED_ARRAYS(TYPED_ARRAY_HARMONY_ADDITIONS) 157 macro EXTEND_TYPED_ARRAY(NAME)
110
111
112 macro EXTEND_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE)
113 // Set up non-enumerable functions on the object. 158 // Set up non-enumerable functions on the object.
114 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ 159 $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [
115 "of", NAMEOf 160 "of", TypedArrayOf
116 ]); 161 ]);
117 162
118 // Set up non-enumerable functions on the prototype object. 163 // Set up non-enumerable functions on the prototype object.
119 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ 164 $installFunctions(GlobalNAME.prototype, DONT_ENUM, [
120 "every", NAMEEvery, 165 "copyWithin", TypedArrayCopyWithin,
121 "forEach", NAMEForEach 166 "every", TypedArrayEvery,
167 "forEach", TypedArrayForEach
122 ]); 168 ]);
123 endmacro 169 endmacro
124 170
125 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) 171 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
126 172
127 })(); 173 })();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarray-copywithin.js » ('j') | test/mjsunit/harmony/typedarray-copywithin.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698