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

Side by Side Diff: src/array.js

Issue 553413002: Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper f… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Apply same changes to MapForEach and SetForEach Created 6 years, 3 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/collection.js » ('j') | src/collection.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 declarations have been made 7 // This file relies on the fact that the following declarations have been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
(...skipping 1108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 // Pull out the length so that modifications to the length in the 1119 // Pull out the length so that modifications to the length in the
1120 // loop will not affect the looping and side effects are visible. 1120 // loop will not affect the looping and side effects are visible.
1121 var array = ToObject(this); 1121 var array = ToObject(this);
1122 var length = ToUint32(array.length); 1122 var length = ToUint32(array.length);
1123 1123
1124 if (!IS_SPEC_FUNCTION(f)) { 1124 if (!IS_SPEC_FUNCTION(f)) {
1125 throw MakeTypeError('called_non_callable', [ f ]); 1125 throw MakeTypeError('called_non_callable', [ f ]);
1126 } 1126 }
1127 if (IS_NULL_OR_UNDEFINED(receiver)) { 1127 if (IS_NULL_OR_UNDEFINED(receiver)) {
1128 receiver = %GetDefaultReceiver(f) || receiver; 1128 receiver = %GetDefaultReceiver(f) || receiver;
1129 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1130 receiver = ToObject(receiver);
1131 } 1129 }
1132 1130
1133 var result = new $Array(); 1131 var result = new $Array();
1134 var accumulator = new InternalArray(); 1132 var accumulator = new InternalArray();
1135 var accumulator_length = 0; 1133 var accumulator_length = 0;
1136 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1134 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1137 for (var i = 0; i < length; i++) { 1135 for (var i = 0; i < length; i++) {
1138 if (i in array) { 1136 if (i in array) {
1139 var element = array[i]; 1137 var element = array[i];
1138 var new_receiver = receiver;
1139 if (!IS_NULL_OR_UNDEFINED(receiver) &&
arv (Not doing code reviews) 2014/09/12 16:29:17 Can this test be moved out of the loop?
Diego Pino 2014/09/16 10:45:46 In strict mode, GetDefaultReceiver can return 'und
wingo 2014/09/16 12:04:04 I think Erik was referring to the test, not the bo
1140 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1141 new_receiver = ToObject(receiver);
1142 }
1140 // Prepare break slots for debugger step in. 1143 // Prepare break slots for debugger step in.
1141 if (stepping) %DebugPrepareStepInIfStepping(f); 1144 if (stepping) %DebugPrepareStepInIfStepping(f);
1142 if (%_CallFunction(receiver, element, i, array, f)) { 1145 if (%_CallFunction(new_receiver, element, i, array, f)) {
1143 accumulator[accumulator_length++] = element; 1146 accumulator[accumulator_length++] = element;
1144 } 1147 }
1145 } 1148 }
1146 } 1149 }
1147 %MoveArrayContents(accumulator, result); 1150 %MoveArrayContents(accumulator, result);
1148 return result; 1151 return result;
1149 } 1152 }
1150 1153
1151 1154
1152 function ArrayForEach(f, receiver) { 1155 function ArrayForEach(f, receiver) {
1153 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); 1156 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach");
1154 1157
1155 // Pull out the length so that modifications to the length in the 1158 // Pull out the length so that modifications to the length in the
1156 // loop will not affect the looping and side effects are visible. 1159 // loop will not affect the looping and side effects are visible.
1157 var array = ToObject(this); 1160 var array = ToObject(this);
1158 var length = TO_UINT32(array.length); 1161 var length = TO_UINT32(array.length);
1159 1162
1160 if (!IS_SPEC_FUNCTION(f)) { 1163 if (!IS_SPEC_FUNCTION(f)) {
1161 throw MakeTypeError('called_non_callable', [ f ]); 1164 throw MakeTypeError('called_non_callable', [ f ]);
1162 } 1165 }
1163 if (IS_NULL_OR_UNDEFINED(receiver)) { 1166 if (IS_NULL_OR_UNDEFINED(receiver)) {
1164 receiver = %GetDefaultReceiver(f) || receiver; 1167 receiver = %GetDefaultReceiver(f) || receiver;
1165 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1166 receiver = ToObject(receiver);
1167 } 1168 }
1168 1169
1169 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1170 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1170 for (var i = 0; i < length; i++) { 1171 for (var i = 0; i < length; i++) {
1171 if (i in array) { 1172 if (i in array) {
1172 var element = array[i]; 1173 var element = array[i];
1174 var new_receiver = receiver;
1175 if (!IS_NULL_OR_UNDEFINED(receiver) &&
arv (Not doing code reviews) 2014/09/12 16:29:17 Maybe we can introduce a helper function/macro for
Diego Pino 2014/09/16 10:45:46 Acknowledged.
1176 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1177 new_receiver = ToObject(receiver);
1178 }
1173 // Prepare break slots for debugger step in. 1179 // Prepare break slots for debugger step in.
1174 if (stepping) %DebugPrepareStepInIfStepping(f); 1180 if (stepping) %DebugPrepareStepInIfStepping(f);
1175 %_CallFunction(receiver, element, i, array, f); 1181 %_CallFunction(new_receiver, element, i, array, f);
1176 } 1182 }
1177 } 1183 }
1178 } 1184 }
1179 1185
1180 1186
1181 // Executes the function once for each element present in the 1187 // Executes the function once for each element present in the
1182 // array until it finds one where callback returns true. 1188 // array until it finds one where callback returns true.
1183 function ArraySome(f, receiver) { 1189 function ArraySome(f, receiver) {
1184 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); 1190 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some");
1185 1191
1186 // Pull out the length so that modifications to the length in the 1192 // Pull out the length so that modifications to the length in the
1187 // loop will not affect the looping and side effects are visible. 1193 // loop will not affect the looping and side effects are visible.
1188 var array = ToObject(this); 1194 var array = ToObject(this);
1189 var length = TO_UINT32(array.length); 1195 var length = TO_UINT32(array.length);
1190 1196
1191 if (!IS_SPEC_FUNCTION(f)) { 1197 if (!IS_SPEC_FUNCTION(f)) {
1192 throw MakeTypeError('called_non_callable', [ f ]); 1198 throw MakeTypeError('called_non_callable', [ f ]);
1193 } 1199 }
1194 if (IS_NULL_OR_UNDEFINED(receiver)) { 1200 if (IS_NULL_OR_UNDEFINED(receiver)) {
1195 receiver = %GetDefaultReceiver(f) || receiver; 1201 receiver = %GetDefaultReceiver(f) || receiver;
1196 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1197 receiver = ToObject(receiver);
1198 } 1202 }
1199 1203
1200 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1204 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1201 for (var i = 0; i < length; i++) { 1205 for (var i = 0; i < length; i++) {
1202 if (i in array) { 1206 if (i in array) {
1203 var element = array[i]; 1207 var element = array[i];
1208 var new_receiver = receiver;
1209 if (!IS_NULL_OR_UNDEFINED(receiver) &&
1210 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1211 new_receiver = ToObject(receiver);
1212 }
1204 // Prepare break slots for debugger step in. 1213 // Prepare break slots for debugger step in.
1205 if (stepping) %DebugPrepareStepInIfStepping(f); 1214 if (stepping) %DebugPrepareStepInIfStepping(f);
1206 if (%_CallFunction(receiver, element, i, array, f)) return true; 1215 if (%_CallFunction(new_receiver, element, i, array, f)) return true;
1207 } 1216 }
1208 } 1217 }
1209 return false; 1218 return false;
1210 } 1219 }
1211 1220
1212 1221
1213 function ArrayEvery(f, receiver) { 1222 function ArrayEvery(f, receiver) {
1214 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); 1223 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every");
1215 1224
1216 // Pull out the length so that modifications to the length in the 1225 // Pull out the length so that modifications to the length in the
1217 // loop will not affect the looping and side effects are visible. 1226 // loop will not affect the looping and side effects are visible.
1218 var array = ToObject(this); 1227 var array = ToObject(this);
1219 var length = TO_UINT32(array.length); 1228 var length = TO_UINT32(array.length);
1220 1229
1221 if (!IS_SPEC_FUNCTION(f)) { 1230 if (!IS_SPEC_FUNCTION(f)) {
1222 throw MakeTypeError('called_non_callable', [ f ]); 1231 throw MakeTypeError('called_non_callable', [ f ]);
1223 } 1232 }
1224 if (IS_NULL_OR_UNDEFINED(receiver)) { 1233 if (IS_NULL_OR_UNDEFINED(receiver)) {
1225 receiver = %GetDefaultReceiver(f) || receiver; 1234 receiver = %GetDefaultReceiver(f) || receiver;
1226 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1227 receiver = ToObject(receiver);
1228 } 1235 }
1229 1236
1230 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1237 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1231 for (var i = 0; i < length; i++) { 1238 for (var i = 0; i < length; i++) {
1232 if (i in array) { 1239 if (i in array) {
1233 var element = array[i]; 1240 var element = array[i];
1241 var new_receiver = receiver;
1242 if (!IS_NULL_OR_UNDEFINED(receiver) &&
1243 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1244 new_receiver = ToObject(receiver);
1245 }
1234 // Prepare break slots for debugger step in. 1246 // Prepare break slots for debugger step in.
1235 if (stepping) %DebugPrepareStepInIfStepping(f); 1247 if (stepping) %DebugPrepareStepInIfStepping(f);
1236 if (!%_CallFunction(receiver, element, i, array, f)) return false; 1248 if (!%_CallFunction(new_receiver, element, i, array, f)) return false;
1237 } 1249 }
1238 } 1250 }
1239 return true; 1251 return true;
1240 } 1252 }
1241 1253
1242 function ArrayMap(f, receiver) { 1254 function ArrayMap(f, receiver) {
1243 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); 1255 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
1244 1256
1245 // Pull out the length so that modifications to the length in the 1257 // Pull out the length so that modifications to the length in the
1246 // loop will not affect the looping and side effects are visible. 1258 // loop will not affect the looping and side effects are visible.
1247 var array = ToObject(this); 1259 var array = ToObject(this);
1248 var length = TO_UINT32(array.length); 1260 var length = TO_UINT32(array.length);
1249 1261
1250 if (!IS_SPEC_FUNCTION(f)) { 1262 if (!IS_SPEC_FUNCTION(f)) {
1251 throw MakeTypeError('called_non_callable', [ f ]); 1263 throw MakeTypeError('called_non_callable', [ f ]);
1252 } 1264 }
1253 if (IS_NULL_OR_UNDEFINED(receiver)) { 1265 if (IS_NULL_OR_UNDEFINED(receiver)) {
1254 receiver = %GetDefaultReceiver(f) || receiver; 1266 receiver = %GetDefaultReceiver(f) || receiver;
1255 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1256 receiver = ToObject(receiver);
1257 } 1267 }
1258 1268
1259 var result = new $Array(); 1269 var result = new $Array();
1260 var accumulator = new InternalArray(length); 1270 var accumulator = new InternalArray(length);
1261 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1271 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1262 for (var i = 0; i < length; i++) { 1272 for (var i = 0; i < length; i++) {
1263 if (i in array) { 1273 if (i in array) {
1264 var element = array[i]; 1274 var element = array[i];
1275 var new_receiver = receiver;
1276 if (!IS_NULL_OR_UNDEFINED(receiver) &&
1277 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1278 new_receiver = ToObject(receiver);
1279 }
1265 // Prepare break slots for debugger step in. 1280 // Prepare break slots for debugger step in.
1266 if (stepping) %DebugPrepareStepInIfStepping(f); 1281 if (stepping) %DebugPrepareStepInIfStepping(f);
1267 accumulator[i] = %_CallFunction(receiver, element, i, array, f); 1282 accumulator[i] = %_CallFunction(new_receiver, element, i, array, f);
1268 } 1283 }
1269 } 1284 }
1270 %MoveArrayContents(accumulator, result); 1285 %MoveArrayContents(accumulator, result);
1271 return result; 1286 return result;
1272 } 1287 }
1273 1288
1274 1289
1275 function ArrayIndexOf(element, index) { 1290 function ArrayIndexOf(element, index) {
1276 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf"); 1291 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
1277 1292
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1404 } 1419 }
1405 } 1420 }
1406 throw MakeTypeError('reduce_no_initial', []); 1421 throw MakeTypeError('reduce_no_initial', []);
1407 } 1422 }
1408 1423
1409 var receiver = %GetDefaultReceiver(callback); 1424 var receiver = %GetDefaultReceiver(callback);
1410 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1425 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1411 for (; i < length; i++) { 1426 for (; i < length; i++) {
1412 if (i in array) { 1427 if (i in array) {
1413 var element = array[i]; 1428 var element = array[i];
1429 var new_receiver = receiver;
1430 if (!IS_NULL_OR_UNDEFINED(receiver) &&
1431 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1432 new_receiver = ToObject(receiver);
1433 }
1414 // Prepare break slots for debugger step in. 1434 // Prepare break slots for debugger step in.
1415 if (stepping) %DebugPrepareStepInIfStepping(callback); 1435 if (stepping) %DebugPrepareStepInIfStepping(callback);
1416 current = %_CallFunction(receiver, current, element, i, array, callback); 1436 current = %_CallFunction(new_receiver, current, element, i, array, callbac k);
1417 } 1437 }
1418 } 1438 }
1419 return current; 1439 return current;
1420 } 1440 }
1421 1441
1422 function ArrayReduceRight(callback, current) { 1442 function ArrayReduceRight(callback, current) {
1423 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); 1443 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
1424 1444
1425 // Pull out the length so that side effects are visible before the 1445 // Pull out the length so that side effects are visible before the
1426 // callback function is checked. 1446 // callback function is checked.
(...skipping 14 matching lines...) Expand all
1441 } 1461 }
1442 } 1462 }
1443 throw MakeTypeError('reduce_no_initial', []); 1463 throw MakeTypeError('reduce_no_initial', []);
1444 } 1464 }
1445 1465
1446 var receiver = %GetDefaultReceiver(callback); 1466 var receiver = %GetDefaultReceiver(callback);
1447 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1467 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1448 for (; i >= 0; i--) { 1468 for (; i >= 0; i--) {
1449 if (i in array) { 1469 if (i in array) {
1450 var element = array[i]; 1470 var element = array[i];
1471 var new_receiver = receiver;
1472 if (!IS_NULL_OR_UNDEFINED(receiver) &&
1473 !IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1474 new_receiver = ToObject(receiver);
1475 }
1451 // Prepare break slots for debugger step in. 1476 // Prepare break slots for debugger step in.
1452 if (stepping) %DebugPrepareStepInIfStepping(callback); 1477 if (stepping) %DebugPrepareStepInIfStepping(callback);
1453 current = %_CallFunction(receiver, current, element, i, array, callback); 1478 current = %_CallFunction(new_receiver, current, element, i, array, callbac k);
1454 } 1479 }
1455 } 1480 }
1456 return current; 1481 return current;
1457 } 1482 }
1458 1483
1459 // ES5, 15.4.3.2 1484 // ES5, 15.4.3.2
1460 function ArrayIsArray(obj) { 1485 function ArrayIsArray(obj) {
1461 return IS_ARRAY(obj); 1486 return IS_ARRAY(obj);
1462 } 1487 }
1463 1488
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 )); 1571 ));
1547 1572
1548 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1573 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1549 "join", getFunction("join", ArrayJoin), 1574 "join", getFunction("join", ArrayJoin),
1550 "pop", getFunction("pop", ArrayPop), 1575 "pop", getFunction("pop", ArrayPop),
1551 "push", getFunction("push", ArrayPush) 1576 "push", getFunction("push", ArrayPush)
1552 )); 1577 ));
1553 } 1578 }
1554 1579
1555 SetUpArray(); 1580 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | src/collection.js » ('j') | src/collection.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698