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

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: Created wrapper. Added tests for strict mode. Fixed nits. 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/harmony-array.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 }
1130 var needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1132 1131
1133 var result = new $Array(); 1132 var result = new $Array();
1134 var accumulator = new InternalArray(); 1133 var accumulator = new InternalArray();
1135 var accumulator_length = 0; 1134 var accumulator_length = 0;
1136 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1135 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1137 for (var i = 0; i < length; i++) { 1136 for (var i = 0; i < length; i++) {
1138 if (i in array) { 1137 if (i in array) {
1139 var element = array[i]; 1138 var element = array[i];
1140 // Prepare break slots for debugger step in. 1139 // Prepare break slots for debugger step in.
1141 if (stepping) %DebugPrepareStepInIfStepping(f); 1140 if (stepping) %DebugPrepareStepInIfStepping(f);
1142 if (%_CallFunction(receiver, element, i, array, f)) { 1141 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
1142 if (%_CallFunction(new_receiver, element, i, array, f)) {
1143 accumulator[accumulator_length++] = element; 1143 accumulator[accumulator_length++] = element;
1144 } 1144 }
1145 } 1145 }
1146 } 1146 }
1147 %MoveArrayContents(accumulator, result); 1147 %MoveArrayContents(accumulator, result);
1148 return result; 1148 return result;
1149 } 1149 }
1150 1150
1151 1151
1152 function ArrayForEach(f, receiver) { 1152 function ArrayForEach(f, receiver) {
1153 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); 1153 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach");
1154 1154
1155 // Pull out the length so that modifications to the length in the 1155 // Pull out the length so that modifications to the length in the
1156 // loop will not affect the looping and side effects are visible. 1156 // loop will not affect the looping and side effects are visible.
1157 var array = ToObject(this); 1157 var array = ToObject(this);
1158 var length = TO_UINT32(array.length); 1158 var length = TO_UINT32(array.length);
1159 1159
1160 if (!IS_SPEC_FUNCTION(f)) { 1160 if (!IS_SPEC_FUNCTION(f)) {
1161 throw MakeTypeError('called_non_callable', [ f ]); 1161 throw MakeTypeError('called_non_callable', [ f ]);
1162 } 1162 }
1163 if (IS_NULL_OR_UNDEFINED(receiver)) { 1163 if (IS_NULL_OR_UNDEFINED(receiver)) {
1164 receiver = %GetDefaultReceiver(f) || receiver; 1164 receiver = %GetDefaultReceiver(f) || receiver;
1165 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1166 receiver = ToObject(receiver);
1167 } 1165 }
1166 var needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1168 1167
1169 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1168 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1170 for (var i = 0; i < length; i++) { 1169 for (var i = 0; i < length; i++) {
1171 if (i in array) { 1170 if (i in array) {
1172 var element = array[i]; 1171 var element = array[i];
1173 // Prepare break slots for debugger step in. 1172 // Prepare break slots for debugger step in.
1174 if (stepping) %DebugPrepareStepInIfStepping(f); 1173 if (stepping) %DebugPrepareStepInIfStepping(f);
1175 %_CallFunction(receiver, element, i, array, f); 1174 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
1175 %_CallFunction(new_receiver, element, i, array, f);
1176 } 1176 }
1177 } 1177 }
1178 } 1178 }
1179 1179
1180 1180
1181 // Executes the function once for each element present in the 1181 // Executes the function once for each element present in the
1182 // array until it finds one where callback returns true. 1182 // array until it finds one where callback returns true.
1183 function ArraySome(f, receiver) { 1183 function ArraySome(f, receiver) {
1184 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); 1184 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some");
1185 1185
1186 // Pull out the length so that modifications to the length in the 1186 // Pull out the length so that modifications to the length in the
1187 // loop will not affect the looping and side effects are visible. 1187 // loop will not affect the looping and side effects are visible.
1188 var array = ToObject(this); 1188 var array = ToObject(this);
1189 var length = TO_UINT32(array.length); 1189 var length = TO_UINT32(array.length);
1190 1190
1191 if (!IS_SPEC_FUNCTION(f)) { 1191 if (!IS_SPEC_FUNCTION(f)) {
1192 throw MakeTypeError('called_non_callable', [ f ]); 1192 throw MakeTypeError('called_non_callable', [ f ]);
1193 } 1193 }
1194 if (IS_NULL_OR_UNDEFINED(receiver)) { 1194 if (IS_NULL_OR_UNDEFINED(receiver)) {
1195 receiver = %GetDefaultReceiver(f) || receiver; 1195 receiver = %GetDefaultReceiver(f) || receiver;
1196 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1197 receiver = ToObject(receiver);
1198 } 1196 }
1197 var needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1199 1198
1200 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1199 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1201 for (var i = 0; i < length; i++) { 1200 for (var i = 0; i < length; i++) {
1202 if (i in array) { 1201 if (i in array) {
1203 var element = array[i]; 1202 var element = array[i];
1204 // Prepare break slots for debugger step in. 1203 // Prepare break slots for debugger step in.
1205 if (stepping) %DebugPrepareStepInIfStepping(f); 1204 if (stepping) %DebugPrepareStepInIfStepping(f);
1206 if (%_CallFunction(receiver, element, i, array, f)) return true; 1205 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
1206 if (%_CallFunction(new_receiver, element, i, array, f)) return true;
1207 } 1207 }
1208 } 1208 }
1209 return false; 1209 return false;
1210 } 1210 }
1211 1211
1212 1212
1213 function ArrayEvery(f, receiver) { 1213 function ArrayEvery(f, receiver) {
1214 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); 1214 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every");
1215 1215
1216 // Pull out the length so that modifications to the length in the 1216 // Pull out the length so that modifications to the length in the
1217 // loop will not affect the looping and side effects are visible. 1217 // loop will not affect the looping and side effects are visible.
1218 var array = ToObject(this); 1218 var array = ToObject(this);
1219 var length = TO_UINT32(array.length); 1219 var length = TO_UINT32(array.length);
1220 1220
1221 if (!IS_SPEC_FUNCTION(f)) { 1221 if (!IS_SPEC_FUNCTION(f)) {
1222 throw MakeTypeError('called_non_callable', [ f ]); 1222 throw MakeTypeError('called_non_callable', [ f ]);
1223 } 1223 }
1224 if (IS_NULL_OR_UNDEFINED(receiver)) { 1224 if (IS_NULL_OR_UNDEFINED(receiver)) {
1225 receiver = %GetDefaultReceiver(f) || receiver; 1225 receiver = %GetDefaultReceiver(f) || receiver;
1226 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1227 receiver = ToObject(receiver);
1228 } 1226 }
1227 var needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1229 1228
1230 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1229 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1231 for (var i = 0; i < length; i++) { 1230 for (var i = 0; i < length; i++) {
1232 if (i in array) { 1231 if (i in array) {
1233 var element = array[i]; 1232 var element = array[i];
1234 // Prepare break slots for debugger step in. 1233 // Prepare break slots for debugger step in.
1235 if (stepping) %DebugPrepareStepInIfStepping(f); 1234 if (stepping) %DebugPrepareStepInIfStepping(f);
1236 if (!%_CallFunction(receiver, element, i, array, f)) return false; 1235 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
1236 if (!%_CallFunction(new_receiver, element, i, array, f)) return false;
1237 } 1237 }
1238 } 1238 }
1239 return true; 1239 return true;
1240 } 1240 }
1241 1241
1242 function ArrayMap(f, receiver) { 1242 function ArrayMap(f, receiver) {
1243 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); 1243 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
1244 1244
1245 // Pull out the length so that modifications to the length in the 1245 // Pull out the length so that modifications to the length in the
1246 // loop will not affect the looping and side effects are visible. 1246 // loop will not affect the looping and side effects are visible.
1247 var array = ToObject(this); 1247 var array = ToObject(this);
1248 var length = TO_UINT32(array.length); 1248 var length = TO_UINT32(array.length);
1249 1249
1250 if (!IS_SPEC_FUNCTION(f)) { 1250 if (!IS_SPEC_FUNCTION(f)) {
1251 throw MakeTypeError('called_non_callable', [ f ]); 1251 throw MakeTypeError('called_non_callable', [ f ]);
1252 } 1252 }
1253 if (IS_NULL_OR_UNDEFINED(receiver)) { 1253 if (IS_NULL_OR_UNDEFINED(receiver)) {
1254 receiver = %GetDefaultReceiver(f) || receiver; 1254 receiver = %GetDefaultReceiver(f) || receiver;
1255 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(f)) {
1256 receiver = ToObject(receiver);
1257 } 1255 }
1256 var needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1258 1257
1259 var result = new $Array(); 1258 var result = new $Array();
1260 var accumulator = new InternalArray(length); 1259 var accumulator = new InternalArray(length);
1261 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1260 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1262 for (var i = 0; i < length; i++) { 1261 for (var i = 0; i < length; i++) {
1263 if (i in array) { 1262 if (i in array) {
1264 var element = array[i]; 1263 var element = array[i];
1265 // Prepare break slots for debugger step in. 1264 // Prepare break slots for debugger step in.
1266 if (stepping) %DebugPrepareStepInIfStepping(f); 1265 if (stepping) %DebugPrepareStepInIfStepping(f);
1267 accumulator[i] = %_CallFunction(receiver, element, i, array, f); 1266 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
1267 accumulator[i] = %_CallFunction(new_receiver, element, i, array, f);
1268 } 1268 }
1269 } 1269 }
1270 %MoveArrayContents(accumulator, result); 1270 %MoveArrayContents(accumulator, result);
1271 return result; 1271 return result;
1272 } 1272 }
1273 1273
1274 1274
1275 function ArrayIndexOf(element, index) { 1275 function ArrayIndexOf(element, index) {
1276 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf"); 1276 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
1277 1277
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1403 break find_initial; 1403 break find_initial;
1404 } 1404 }
1405 } 1405 }
1406 throw MakeTypeError('reduce_no_initial', []); 1406 throw MakeTypeError('reduce_no_initial', []);
1407 } 1407 }
1408 1408
1409 var receiver = %GetDefaultReceiver(callback); 1409 var receiver = %GetDefaultReceiver(callback);
1410 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1410 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1411 for (; i < length; i++) { 1411 for (; i < length; i++) {
1412 if (i in array) { 1412 if (i in array) {
1413 var element = array[i]; 1413 var element = array[i];
wingo 2014/09/23 18:39:01 what happened to the ToObject that was here in the
Diego Pino 2014/09/24 10:13:01 I think it doesn't apply in this case or at least
1414 // Prepare break slots for debugger step in. 1414 // Prepare break slots for debugger step in.
1415 if (stepping) %DebugPrepareStepInIfStepping(callback); 1415 if (stepping) %DebugPrepareStepInIfStepping(callback);
1416 current = %_CallFunction(receiver, current, element, i, array, callback); 1416 current = %_CallFunction(receiver, current, element, i, array, callback);
1417 } 1417 }
1418 } 1418 }
1419 return current; 1419 return current;
1420 } 1420 }
1421 1421
1422 function ArrayReduceRight(callback, current) { 1422 function ArrayReduceRight(callback, current) {
1423 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); 1423 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
(...skipping 16 matching lines...) Expand all
1440 break find_initial; 1440 break find_initial;
1441 } 1441 }
1442 } 1442 }
1443 throw MakeTypeError('reduce_no_initial', []); 1443 throw MakeTypeError('reduce_no_initial', []);
1444 } 1444 }
1445 1445
1446 var receiver = %GetDefaultReceiver(callback); 1446 var receiver = %GetDefaultReceiver(callback);
1447 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1447 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1448 for (; i >= 0; i--) { 1448 for (; i >= 0; i--) {
1449 if (i in array) { 1449 if (i in array) {
1450 var element = array[i]; 1450 var element = array[i];
wingo 2014/09/23 18:39:01 same
1451 // Prepare break slots for debugger step in. 1451 // Prepare break slots for debugger step in.
1452 if (stepping) %DebugPrepareStepInIfStepping(callback); 1452 if (stepping) %DebugPrepareStepInIfStepping(callback);
1453 current = %_CallFunction(receiver, current, element, i, array, callback); 1453 current = %_CallFunction(receiver, current, element, i, array, callback);
1454 } 1454 }
1455 } 1455 }
1456 return current; 1456 return current;
1457 } 1457 }
1458 1458
1459 // ES5, 15.4.3.2 1459 // ES5, 15.4.3.2
1460 function ArrayIsArray(obj) { 1460 function ArrayIsArray(obj) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 )); 1546 ));
1547 1547
1548 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1548 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1549 "join", getFunction("join", ArrayJoin), 1549 "join", getFunction("join", ArrayJoin),
1550 "pop", getFunction("pop", ArrayPop), 1550 "pop", getFunction("pop", ArrayPop),
1551 "push", getFunction("push", ArrayPush) 1551 "push", getFunction("push", ArrayPush)
1552 )); 1552 ));
1553 } 1553 }
1554 1554
1555 SetUpArray(); 1555 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | src/collection.js » ('j') | src/harmony-array.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698