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

Side by Side Diff: src/array.js

Issue 22545007: Hack forEach to use HasFastPackedElements (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Branch on proxies Created 7 years, 4 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/objects.h » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 1209 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 if (i in array) { 1220 if (i in array) {
1221 var element = array[i]; 1221 var element = array[i];
1222 %_CallFunction(receiver, element, i, array, f); 1222 %_CallFunction(receiver, element, i, array, f);
1223 } 1223 }
1224 } 1224 }
1225 // End of duplicate. 1225 // End of duplicate.
1226 } 1226 }
1227 } 1227 }
1228 1228
1229 1229
1230 function ArrayForEach2(f, receiver) {
1231 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1232 throw MakeTypeError("called_on_null_or_undefined",
1233 ["Array.prototype.forEach"]);
1234 }
1235
1236 // Pull out the length so that modifications to the length in the
1237 // loop will not affect the looping and side effects are visible.
1238 var array = ToObject(this);
1239 var length = TO_UINT32(array.length);
1240
1241 if (!IS_SPEC_FUNCTION(f)) {
1242 throw MakeTypeError('called_non_callable', [ f ]);
1243 }
1244 if (IS_NULL_OR_UNDEFINED(receiver)) {
1245 receiver = %GetDefaultReceiver(f) || receiver;
1246 } else if (!IS_SPEC_OBJECT(receiver) && %IsClassicModeFunction(f)) {
1247 receiver = ToObject(receiver);
1248 }
1249
1250 if (%DebugCallbackSupportsStepping(f)) {
1251 for (var i = 0; i < length; i++) {
1252 if (i in array) {
1253 var element = array[i];
1254 // Prepare break slots for debugger step in.
1255 %DebugPrepareStepInIfStepping(f);
1256 %_CallFunction(receiver, element, i, array, f);
1257 }
1258 }
1259 } else {
1260 // This is mostly a duplicate of the previous loop sans debug stepping.
1261 var isProxy = %IsJSProxy(array);
1262 for (var i = 0; i < length; i++) {
1263 if (!isProxy && %HasFastPackedElements(array) || i in array) {
Michael Starzinger 2013/08/13 14:10:22 It is _really_ surprising that the runtime call in
Michael Starzinger 2013/08/13 15:47:16 Now that I think about it, I don't actually think
Toon Verwaest 2013/08/13 20:47:42 I don't think you can have indexed accessors while
Michael Starzinger 2013/08/14 07:48:47 OK, admittedly, my example does not trigger the pr
1264 var element = array[i];
1265 %_CallFunction(receiver, element, i, array, f);
1266 }
1267 }
1268 // End of duplicate.
1269 }
1270 }
1271
1272
1273 function ArrayForEach3(f, receiver) {
1274 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1275 throw MakeTypeError("called_on_null_or_undefined",
1276 ["Array.prototype.forEach"]);
1277 }
1278
1279 // Pull out the length so that modifications to the length in the
1280 // loop will not affect the looping and side effects are visible.
1281 var array = ToObject(this);
1282 var length = TO_UINT32(array.length);
1283
1284 if (!IS_SPEC_FUNCTION(f)) {
1285 throw MakeTypeError('called_non_callable', [ f ]);
1286 }
1287 if (IS_NULL_OR_UNDEFINED(receiver)) {
1288 receiver = %GetDefaultReceiver(f) || receiver;
1289 } else if (!IS_SPEC_OBJECT(receiver) && %IsClassicModeFunction(f)) {
1290 receiver = ToObject(receiver);
1291 }
1292
1293 if (%DebugCallbackSupportsStepping(f)) {
1294 for (var i = 0; i < length; i++) {
1295 if (i in array) {
1296 var element = array[i];
1297 // Prepare break slots for debugger step in.
1298 %DebugPrepareStepInIfStepping(f);
1299 %_CallFunction(receiver, element, i, array, f);
1300 }
1301 }
1302 } else if (%IsJSProxy(array)) {
1303 // This is a duplicate of the previous loop sans debug stepping.
1304 for (var i = 0; i < length; i++) {
1305 if (i in array) {
1306 var element = array[i];
1307 %_CallFunction(receiver, element, i, array, f);
1308 }
1309 }
1310 } else {
1311 // This is mostly a duplicate of the previous loop but since the order is
1312 // not observable we can optimize further.
1313 for (var i = 0; i < length; i++) {
1314 var element = array[i];
1315 if (!IS_UNDEFINED(element) || i in array) {
Michael Starzinger 2013/08/13 14:10:22 This is actually wrong as the i-th element might h
Michael Starzinger 2013/08/13 14:18:58 This is the issue tracking exactly this: https://
1316 %_CallFunction(receiver, element, i, array, f);
1317 }
1318 }
1319 // End of duplicate.
1320 }
1321 }
1322
1323
1230 // Executes the function once for each element present in the 1324 // Executes the function once for each element present in the
1231 // array until it finds one where callback returns true. 1325 // array until it finds one where callback returns true.
1232 function ArraySome(f, receiver) { 1326 function ArraySome(f, receiver) {
1233 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1327 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1234 throw MakeTypeError("called_on_null_or_undefined", 1328 throw MakeTypeError("called_on_null_or_undefined",
1235 ["Array.prototype.some"]); 1329 ["Array.prototype.some"]);
1236 } 1330 }
1237 1331
1238 // Pull out the length so that modifications to the length in the 1332 // Pull out the length so that modifications to the length in the
1239 // loop will not affect the looping and side effects are visible. 1333 // loop will not affect the looping and side effects are visible.
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1548 current = array[i]; 1642 current = array[i];
1549 if (!IS_UNDEFINED(current) || i in array) { 1643 if (!IS_UNDEFINED(current) || i in array) {
1550 i--; 1644 i--;
1551 break find_initial; 1645 break find_initial;
1552 } 1646 }
1553 } 1647 }
1554 throw MakeTypeError('reduce_no_initial', []); 1648 throw MakeTypeError('reduce_no_initial', []);
1555 } 1649 }
1556 1650
1557 var receiver = %GetDefaultReceiver(callback); 1651 var receiver = %GetDefaultReceiver(callback);
1558
1559 if (%DebugCallbackSupportsStepping(callback)) { 1652 if (%DebugCallbackSupportsStepping(callback)) {
1560 for (; i >= 0; i--) { 1653 for (; i >= 0; i--) {
1561 if (i in array) { 1654 if (i in array) {
1562 var element = array[i]; 1655 var element = array[i];
1563 // Prepare break slots for debugger step in. 1656 // Prepare break slots for debugger step in.
1564 %DebugPrepareStepInIfStepping(callback); 1657 %DebugPrepareStepInIfStepping(callback);
1565 current = 1658 current =
1566 %_CallFunction(receiver, current, element, i, array, callback); 1659 %_CallFunction(receiver, current, element, i, array, callback);
1567 } 1660 }
1568 } 1661 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1625 "push", getFunction("push", ArrayPush, 1), 1718 "push", getFunction("push", ArrayPush, 1),
1626 "concat", getFunction("concat", ArrayConcat, 1), 1719 "concat", getFunction("concat", ArrayConcat, 1),
1627 "reverse", getFunction("reverse", ArrayReverse), 1720 "reverse", getFunction("reverse", ArrayReverse),
1628 "shift", getFunction("shift", ArrayShift), 1721 "shift", getFunction("shift", ArrayShift),
1629 "unshift", getFunction("unshift", ArrayUnshift, 1), 1722 "unshift", getFunction("unshift", ArrayUnshift, 1),
1630 "slice", getFunction("slice", ArraySlice, 2), 1723 "slice", getFunction("slice", ArraySlice, 2),
1631 "splice", getFunction("splice", ArraySplice, 2), 1724 "splice", getFunction("splice", ArraySplice, 2),
1632 "sort", getFunction("sort", ArraySort), 1725 "sort", getFunction("sort", ArraySort),
1633 "filter", getFunction("filter", ArrayFilter, 1), 1726 "filter", getFunction("filter", ArrayFilter, 1),
1634 "forEach", getFunction("forEach", ArrayForEach, 1), 1727 "forEach", getFunction("forEach", ArrayForEach, 1),
1728 "forEach2", getFunction("forEach2", ArrayForEach2, 1),
1729 "forEach3", getFunction("forEach3", ArrayForEach3, 1),
1635 "some", getFunction("some", ArraySome, 1), 1730 "some", getFunction("some", ArraySome, 1),
1636 "every", getFunction("every", ArrayEvery, 1), 1731 "every", getFunction("every", ArrayEvery, 1),
1637 "map", getFunction("map", ArrayMap, 1), 1732 "map", getFunction("map", ArrayMap, 1),
1638 "indexOf", getFunction("indexOf", ArrayIndexOf, 1), 1733 "indexOf", getFunction("indexOf", ArrayIndexOf, 1),
1639 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1734 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1640 "reduce", getFunction("reduce", ArrayReduce, 1), 1735 "reduce", getFunction("reduce", ArrayReduce, 1),
1641 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) 1736 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1)
1642 )); 1737 ));
1643 1738
1644 %FinishArrayPrototypeSetup($Array.prototype); 1739 %FinishArrayPrototypeSetup($Array.prototype);
(...skipping 11 matching lines...) Expand all
1656 )); 1751 ));
1657 1752
1658 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1753 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1659 "join", getFunction("join", ArrayJoin), 1754 "join", getFunction("join", ArrayJoin),
1660 "pop", getFunction("pop", ArrayPop), 1755 "pop", getFunction("pop", ArrayPop),
1661 "push", getFunction("push", ArrayPush) 1756 "push", getFunction("push", ArrayPush)
1662 )); 1757 ));
1663 } 1758 }
1664 1759
1665 SetUpArray(); 1760 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698