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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays-reduce.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-typedarray.js
diff --git a/src/harmony-typedarray.js b/src/harmony-typedarray.js
index 014206c4cba8400cf745b9295d5cb223695ad6cb..17f72950ddeb57e59801ca817e0ca6bd22741766 100644
--- a/src/harmony-typedarray.js
+++ b/src/harmony-typedarray.js
@@ -69,6 +69,90 @@ function NAMEOf() { // length == 0
return array;
}
+// ES6 draft 10-14-14, section 22.2.3.19
+function NAMEReduce(f /* initialValue */) { // length == 1
+ if (!%IsTypedArray(this)) {
+ throw MakeTypeError('not_typed_array');
+ }
+ if (!IS_SPEC_FUNCTION(f)) {
+ throw MakeTypeError('called_non_callable', [ f ]);
+ }
+
+ var length = %_TypedArrayGetLength(this);
+ var accumulator;
+ var i;
+
+ if (%_ArgumentsLength() > 1) {
+ accumulator = %_Arguments(1);
+ i = 0;
+ } else if (length == 0) {
+ throw MakeTypeError('reduce_no_initial', []);
+ } else {
+ accumulator = this[0];
+ i = 1;
+ }
+
+ var receiver = %GetDefaultReceiver(f);
+ var needs_wrapper = false;
+ if (IS_NULL_OR_UNDEFINED(receiver)) {
+ receiver = %GetDefaultReceiver(f) || receiver;
+ } else {
+ needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
+ }
+
+ var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
+ for (; i < length; i++) {
+ var element = this[i];
+ // Prepare break slots for debugger step in.
+ if (stepping) %DebugPrepareStepInIfStepping(f);
+ var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
+ accumulator = %_CallFunction(receiver, accumulator, element, i, this, f);
+ }
+ return accumulator;
+}
+
+// ES6 draft 10-14-14, section 22.2.3.20
+function NAMEReduceRight(f /* initialValue */) { // length == 1
+ if (!%IsTypedArray(this)) {
+ throw MakeTypeError('not_typed_array', []);
+ }
+ if (!IS_SPEC_FUNCTION(f)) {
+ throw MakeTypeError('called_non_callable', [ f ]);
+ }
+
+ var length = %_TypedArrayGetLength(this);
+ var accumulator;
+ var i;
+
+ if (%_ArgumentsLength() > 1) {
+ accumulator = %_Arguments(1);
+ i = length - 1;
+ } else if (length == 0) {
+ throw MakeTypeError('reduce_no_initial', []);
+ } else {
+ i = length - 1;
+ accumulator = this[i--];
+ }
+
+ var receiver = %GetDefaultReceiver(f);
+ var needs_wrapper = false;
+ if (IS_NULL_OR_UNDEFINED(receiver)) {
+ receiver = %GetDefaultReceiver(f) || receiver;
+ } else {
+ needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
+ }
+
+ var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
+ for (; i >= 0; i--) {
+ var element = this[i];
+ // Prepare break slots for debugger step in.
+ if (stepping) %DebugPrepareStepInIfStepping(f);
+ var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
+ accumulator = %_CallFunction(receiver, accumulator, element, i, this, f);
+ }
+ return accumulator;
+}
+
endmacro
TYPED_ARRAYS(TYPED_ARRAY_HARMONY_ADDITIONS)
@@ -84,8 +168,10 @@ macro EXTEND_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE)
));
// Set up non-enumerable functions on the prototype object.
- InstallFunctions(global.NAME.prototype, DONT_ENUM, $Array(
- "forEach", NAMEForEach
+ InstallFunctions(global.NAME.prototype, DONT_ENUM | DONT_DELETE | READ_ONLY, $Array(
+ "forEach", NAMEForEach,
+ "reduce", NAMEReduce,
+ "reduceRight", NAMEReduceRight
));
endmacro
« 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