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

Side by Side Diff: src/array.js

Issue 7044054: Fix Array.prototype.{reduce,reduceRight} to pass undefined as receiver (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | src/v8natives.js » ('j') | test/mjsunit/regress/regress-1436.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 986 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 throw MakeTypeError('called_non_callable', [ f ]); 997 throw MakeTypeError('called_non_callable', [ f ]);
998 } 998 }
999 // Pull out the length so that modifications to the length in the 999 // Pull out the length so that modifications to the length in the
1000 // loop will not affect the looping. 1000 // loop will not affect the looping.
1001 var length = this.length; 1001 var length = this.length;
1002 var result = []; 1002 var result = [];
1003 var result_length = 0; 1003 var result_length = 0;
1004 for (var i = 0; i < length; i++) { 1004 for (var i = 0; i < length; i++) {
1005 var current = this[i]; 1005 var current = this[i];
1006 if (!IS_UNDEFINED(current) || i in this) { 1006 if (!IS_UNDEFINED(current) || i in this) {
1007 if (f.call(receiver, current, i, this)) { 1007 if (%_CallFunction(receiver, current, i, this, f)) {
Lasse Reichstein 2011/06/08 18:41:42 Does this correctly pass the global receiver to th
1008 result[result_length++] = current; 1008 result[result_length++] = current;
1009 } 1009 }
1010 } 1010 }
1011 } 1011 }
1012 return result; 1012 return result;
1013 } 1013 }
1014 1014
1015 1015
1016 function ArrayForEach(f, receiver) { 1016 function ArrayForEach(f, receiver) {
1017 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1017 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1018 throw MakeTypeError("called_on_null_or_undefined", 1018 throw MakeTypeError("called_on_null_or_undefined",
1019 ["Array.prototype.forEach"]); 1019 ["Array.prototype.forEach"]);
1020 } 1020 }
1021 1021
1022 if (!IS_FUNCTION(f)) { 1022 if (!IS_FUNCTION(f)) {
1023 throw MakeTypeError('called_non_callable', [ f ]); 1023 throw MakeTypeError('called_non_callable', [ f ]);
1024 } 1024 }
1025 // Pull out the length so that modifications to the length in the 1025 // Pull out the length so that modifications to the length in the
1026 // loop will not affect the looping. 1026 // loop will not affect the looping.
1027 var length = TO_UINT32(this.length); 1027 var length = TO_UINT32(this.length);
1028 for (var i = 0; i < length; i++) { 1028 for (var i = 0; i < length; i++) {
1029 var current = this[i]; 1029 var current = this[i];
1030 if (!IS_UNDEFINED(current) || i in this) { 1030 if (!IS_UNDEFINED(current) || i in this) {
1031 f.call(receiver, current, i, this); 1031 %_CallFunction(receiver, current, i, this, f);
1032 } 1032 }
1033 } 1033 }
1034 } 1034 }
1035 1035
1036 1036
1037 // Executes the function once for each element present in the 1037 // Executes the function once for each element present in the
1038 // array until it finds one where callback returns true. 1038 // array until it finds one where callback returns true.
1039 function ArraySome(f, receiver) { 1039 function ArraySome(f, receiver) {
1040 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1040 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1041 throw MakeTypeError("called_on_null_or_undefined", 1041 throw MakeTypeError("called_on_null_or_undefined",
1042 ["Array.prototype.some"]); 1042 ["Array.prototype.some"]);
1043 } 1043 }
1044 1044
1045 if (!IS_FUNCTION(f)) { 1045 if (!IS_FUNCTION(f)) {
1046 throw MakeTypeError('called_non_callable', [ f ]); 1046 throw MakeTypeError('called_non_callable', [ f ]);
1047 } 1047 }
1048 // Pull out the length so that modifications to the length in the 1048 // Pull out the length so that modifications to the length in the
1049 // loop will not affect the looping. 1049 // loop will not affect the looping.
1050 var length = TO_UINT32(this.length); 1050 var length = TO_UINT32(this.length);
1051 for (var i = 0; i < length; i++) { 1051 for (var i = 0; i < length; i++) {
1052 var current = this[i]; 1052 var current = this[i];
1053 if (!IS_UNDEFINED(current) || i in this) { 1053 if (!IS_UNDEFINED(current) || i in this) {
1054 if (f.call(receiver, current, i, this)) return true; 1054 if (%_CallFunction(receiver, current, i, this, f)) return true;
1055 } 1055 }
1056 } 1056 }
1057 return false; 1057 return false;
1058 } 1058 }
1059 1059
1060 1060
1061 function ArrayEvery(f, receiver) { 1061 function ArrayEvery(f, receiver) {
1062 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1062 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1063 throw MakeTypeError("called_on_null_or_undefined", 1063 throw MakeTypeError("called_on_null_or_undefined",
1064 ["Array.prototype.every"]); 1064 ["Array.prototype.every"]);
1065 } 1065 }
1066 1066
1067 if (!IS_FUNCTION(f)) { 1067 if (!IS_FUNCTION(f)) {
1068 throw MakeTypeError('called_non_callable', [ f ]); 1068 throw MakeTypeError('called_non_callable', [ f ]);
1069 } 1069 }
1070 // Pull out the length so that modifications to the length in the 1070 // Pull out the length so that modifications to the length in the
1071 // loop will not affect the looping. 1071 // loop will not affect the looping.
1072 var length = TO_UINT32(this.length); 1072 var length = TO_UINT32(this.length);
1073 for (var i = 0; i < length; i++) { 1073 for (var i = 0; i < length; i++) {
1074 var current = this[i]; 1074 var current = this[i];
1075 if (!IS_UNDEFINED(current) || i in this) { 1075 if (!IS_UNDEFINED(current) || i in this) {
1076 if (!f.call(receiver, current, i, this)) return false; 1076 if (!%_CallFunction(receiver, current, i, this, f)) return false;
1077 } 1077 }
1078 } 1078 }
1079 return true; 1079 return true;
1080 } 1080 }
1081 1081
1082 function ArrayMap(f, receiver) { 1082 function ArrayMap(f, receiver) {
1083 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1083 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1084 throw MakeTypeError("called_on_null_or_undefined", 1084 throw MakeTypeError("called_on_null_or_undefined",
1085 ["Array.prototype.map"]); 1085 ["Array.prototype.map"]);
1086 } 1086 }
1087 1087
1088 if (!IS_FUNCTION(f)) { 1088 if (!IS_FUNCTION(f)) {
1089 throw MakeTypeError('called_non_callable', [ f ]); 1089 throw MakeTypeError('called_non_callable', [ f ]);
1090 } 1090 }
1091 // Pull out the length so that modifications to the length in the 1091 // Pull out the length so that modifications to the length in the
1092 // loop will not affect the looping. 1092 // loop will not affect the looping.
1093 var length = TO_UINT32(this.length); 1093 var length = TO_UINT32(this.length);
1094 var result = new $Array(); 1094 var result = new $Array();
1095 var accumulator = new InternalArray(length); 1095 var accumulator = new InternalArray(length);
1096 for (var i = 0; i < length; i++) { 1096 for (var i = 0; i < length; i++) {
1097 var current = this[i]; 1097 var current = this[i];
1098 if (!IS_UNDEFINED(current) || i in this) { 1098 if (!IS_UNDEFINED(current) || i in this) {
1099 accumulator[i] = f.call(receiver, current, i, this); 1099 accumulator[i] = %_CallFunction(receiver, current, i, this, f);
1100 } 1100 }
1101 } 1101 }
1102 %MoveArrayContents(accumulator, result); 1102 %MoveArrayContents(accumulator, result);
1103 return result; 1103 return result;
1104 } 1104 }
1105 1105
1106 1106
1107 function ArrayIndexOf(element, index) { 1107 function ArrayIndexOf(element, index) {
1108 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1108 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1109 throw MakeTypeError("called_on_null_or_undefined", 1109 throw MakeTypeError("called_on_null_or_undefined",
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 i++; 1245 i++;
1246 break find_initial; 1246 break find_initial;
1247 } 1247 }
1248 } 1248 }
1249 throw MakeTypeError('reduce_no_initial', []); 1249 throw MakeTypeError('reduce_no_initial', []);
1250 } 1250 }
1251 1251
1252 for (; i < length; i++) { 1252 for (; i < length; i++) {
1253 var element = this[i]; 1253 var element = this[i];
1254 if (!IS_UNDEFINED(element) || i in this) { 1254 if (!IS_UNDEFINED(element) || i in this) {
1255 current = callback.call(null, current, element, i, this); 1255 current = %_CallFunction(void 0, current, element, i, this, callback);
1256 } 1256 }
1257 } 1257 }
1258 return current; 1258 return current;
1259 } 1259 }
1260 1260
1261 function ArrayReduceRight(callback, current) { 1261 function ArrayReduceRight(callback, current) {
1262 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1262 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1263 throw MakeTypeError("called_on_null_or_undefined", 1263 throw MakeTypeError("called_on_null_or_undefined",
1264 ["Array.prototype.reduceRight"]); 1264 ["Array.prototype.reduceRight"]);
1265 } 1265 }
(...skipping 10 matching lines...) Expand all
1276 i--; 1276 i--;
1277 break find_initial; 1277 break find_initial;
1278 } 1278 }
1279 } 1279 }
1280 throw MakeTypeError('reduce_no_initial', []); 1280 throw MakeTypeError('reduce_no_initial', []);
1281 } 1281 }
1282 1282
1283 for (; i >= 0; i--) { 1283 for (; i >= 0; i--) {
1284 var element = this[i]; 1284 var element = this[i];
1285 if (!IS_UNDEFINED(element) || i in this) { 1285 if (!IS_UNDEFINED(element) || i in this) {
1286 current = callback.call(null, current, element, i, this); 1286 current = %_CallFunction(void 0, current, element, i, this, callback);
1287 } 1287 }
1288 } 1288 }
1289 return current; 1289 return current;
1290 } 1290 }
1291 1291
1292 // ES5, 15.4.3.2 1292 // ES5, 15.4.3.2
1293 function ArrayIsArray(obj) { 1293 function ArrayIsArray(obj) {
1294 return IS_ARRAY(obj); 1294 return IS_ARRAY(obj);
1295 } 1295 }
1296 1296
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1359 InternalArray.prototype.join = getFunction("join", ArrayJoin); 1359 InternalArray.prototype.join = getFunction("join", ArrayJoin);
1360 InternalArray.prototype.pop = getFunction("pop", ArrayPop); 1360 InternalArray.prototype.pop = getFunction("pop", ArrayPop);
1361 InternalArray.prototype.push = getFunction("push", ArrayPush); 1361 InternalArray.prototype.push = getFunction("push", ArrayPush);
1362 InternalArray.prototype.toString = function() { 1362 InternalArray.prototype.toString = function() {
1363 return "Internal Array, length " + this.length; 1363 return "Internal Array, length " + this.length;
1364 }; 1364 };
1365 } 1365 }
1366 1366
1367 1367
1368 SetupArray(); 1368 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | src/v8natives.js » ('j') | test/mjsunit/regress/regress-1436.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698