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

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

Issue 769993002: Implement .reduce() and .reduceRight() on typed arrays Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years 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 | « no previous file | test/mjsunit/harmony/typedarrays-reduce.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 'use strict'; 5 'use strict';
6 6
7 // This file relies on the fact that the following declaration has been made 7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 // ES6 draft 08-24-14, section 22.2.2.2 62 // ES6 draft 08-24-14, section 22.2.2.2
63 function NAMEOf() { // length == 0 63 function NAMEOf() { // length == 0
64 var length = %_ArgumentsLength(); 64 var length = %_ArgumentsLength();
65 var array = new this(length); 65 var array = new this(length);
66 for (var i = 0; i < length; i++) { 66 for (var i = 0; i < length; i++) {
67 array[i] = %_Arguments(i); 67 array[i] = %_Arguments(i);
68 } 68 }
69 return array; 69 return array;
70 } 70 }
71 71
72 // ES6 draft 10-14-14, section 22.2.3.19
73 function NAMEReduce(f /* initialValue */) { // length == 1
74 if (!%IsTypedArray(this)) {
75 throw MakeTypeError('not_typed_array');
76 }
77 if (!IS_SPEC_FUNCTION(f)) {
78 throw MakeTypeError('called_non_callable', [ f ]);
79 }
80
81 var length = %_TypedArrayGetLength(this);
82 var accumulator;
83 var i;
84
85 if (%_ArgumentsLength() > 1) {
86 accumulator = %_Arguments(1);
87 i = 0;
88 } else if (length == 0) {
89 throw MakeTypeError('reduce_no_initial', []);
90 } else {
91 accumulator = this[0];
92 i = 1;
93 }
94
95 var receiver = %GetDefaultReceiver(f);
96 var needs_wrapper = false;
97 if (IS_NULL_OR_UNDEFINED(receiver)) {
98 receiver = %GetDefaultReceiver(f) || receiver;
99 } else {
100 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
101 }
102
103 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
104 for (; i < length; i++) {
105 var element = this[i];
106 // Prepare break slots for debugger step in.
107 if (stepping) %DebugPrepareStepInIfStepping(f);
108 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
109 accumulator = %_CallFunction(receiver, accumulator, element, i, this, f);
110 }
111 return accumulator;
112 }
113
114 // ES6 draft 10-14-14, section 22.2.3.20
115 function NAMEReduceRight(f /* initialValue */) { // length == 1
116 if (!%IsTypedArray(this)) {
117 throw MakeTypeError('not_typed_array', []);
118 }
119 if (!IS_SPEC_FUNCTION(f)) {
120 throw MakeTypeError('called_non_callable', [ f ]);
121 }
122
123 var length = %_TypedArrayGetLength(this);
124 var accumulator;
125 var i;
126
127 if (%_ArgumentsLength() > 1) {
128 accumulator = %_Arguments(1);
129 i = length - 1;
130 } else if (length == 0) {
131 throw MakeTypeError('reduce_no_initial', []);
132 } else {
133 i = length - 1;
134 accumulator = this[i--];
135 }
136
137 var receiver = %GetDefaultReceiver(f);
138 var needs_wrapper = false;
139 if (IS_NULL_OR_UNDEFINED(receiver)) {
140 receiver = %GetDefaultReceiver(f) || receiver;
141 } else {
142 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
143 }
144
145 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
146 for (; i >= 0; i--) {
147 var element = this[i];
148 // Prepare break slots for debugger step in.
149 if (stepping) %DebugPrepareStepInIfStepping(f);
150 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
151 accumulator = %_CallFunction(receiver, accumulator, element, i, this, f);
152 }
153 return accumulator;
154 }
155
72 endmacro 156 endmacro
73 157
74 TYPED_ARRAYS(TYPED_ARRAY_HARMONY_ADDITIONS) 158 TYPED_ARRAYS(TYPED_ARRAY_HARMONY_ADDITIONS)
75 159
76 160
77 function HarmonyTypedArrayExtendPrototypes() { 161 function HarmonyTypedArrayExtendPrototypes() {
78 macro EXTEND_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) 162 macro EXTEND_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE)
79 %CheckIsBootstrapping(); 163 %CheckIsBootstrapping();
80 164
81 // Set up non-enumerable functions on the object. 165 // Set up non-enumerable functions on the object.
82 InstallFunctions(global.NAME, DONT_ENUM | DONT_DELETE | READ_ONLY, $Array( 166 InstallFunctions(global.NAME, DONT_ENUM | DONT_DELETE | READ_ONLY, $Array(
83 "of", NAMEOf 167 "of", NAMEOf
84 )); 168 ));
85 169
86 // Set up non-enumerable functions on the prototype object. 170 // Set up non-enumerable functions on the prototype object.
87 InstallFunctions(global.NAME.prototype, DONT_ENUM, $Array( 171 InstallFunctions(global.NAME.prototype, DONT_ENUM | DONT_DELETE | READ_ONLY, $ Array(
88 "forEach", NAMEForEach 172 "forEach", NAMEForEach,
173 "reduce", NAMEReduce,
174 "reduceRight", NAMEReduceRight
89 )); 175 ));
90 endmacro 176 endmacro
91 177
92 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) 178 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
93 } 179 }
94 180
95 HarmonyTypedArrayExtendPrototypes(); 181 HarmonyTypedArrayExtendPrototypes();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays-reduce.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698