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

Side by Side Diff: src/array.js

Issue 9419044: Fix sequence of element access in array builtins. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 10 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 | test/mjsunit/regress/regress-1790.js » ('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 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 1006 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 if (IS_NULL_OR_UNDEFINED(receiver)) { 1017 if (IS_NULL_OR_UNDEFINED(receiver)) {
1018 receiver = %GetDefaultReceiver(f) || receiver; 1018 receiver = %GetDefaultReceiver(f) || receiver;
1019 } else if (!IS_SPEC_OBJECT(receiver)) { 1019 } else if (!IS_SPEC_OBJECT(receiver)) {
1020 receiver = ToObject(receiver); 1020 receiver = ToObject(receiver);
1021 } 1021 }
1022 1022
1023 var result = new $Array(); 1023 var result = new $Array();
1024 var accumulator = new InternalArray(); 1024 var accumulator = new InternalArray();
1025 var accumulator_length = 0; 1025 var accumulator_length = 0;
1026 for (var i = 0; i < length; i++) { 1026 for (var i = 0; i < length; i++) {
1027 var current = array[i]; 1027 if (i in array) {
1028 if (!IS_UNDEFINED(current) || i in array) { 1028 var element = array[i];
1029 if (%_CallFunction(receiver, current, i, array, f)) { 1029 if (%_CallFunction(receiver, element, i, array, f)) {
1030 accumulator[accumulator_length++] = current; 1030 accumulator[accumulator_length++] = element;
1031 } 1031 }
1032 } 1032 }
1033 } 1033 }
1034 %MoveArrayContents(accumulator, result); 1034 %MoveArrayContents(accumulator, result);
1035 return result; 1035 return result;
1036 } 1036 }
1037 1037
1038 1038
1039 function ArrayForEach(f, receiver) { 1039 function ArrayForEach(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.forEach"]); 1042 ["Array.prototype.forEach"]);
1043 } 1043 }
1044 1044
1045 // Pull out the length so that modifications to the length in the 1045 // Pull out the length so that modifications to the length in the
1046 // loop will not affect the looping and side effects are visible. 1046 // loop will not affect the looping and side effects are visible.
1047 var array = ToObject(this); 1047 var array = ToObject(this);
1048 var length = TO_UINT32(array.length); 1048 var length = TO_UINT32(array.length);
1049 1049
1050 if (!IS_SPEC_FUNCTION(f)) { 1050 if (!IS_SPEC_FUNCTION(f)) {
1051 throw MakeTypeError('called_non_callable', [ f ]); 1051 throw MakeTypeError('called_non_callable', [ f ]);
1052 } 1052 }
1053 if (IS_NULL_OR_UNDEFINED(receiver)) { 1053 if (IS_NULL_OR_UNDEFINED(receiver)) {
1054 receiver = %GetDefaultReceiver(f) || receiver; 1054 receiver = %GetDefaultReceiver(f) || receiver;
1055 } else if (!IS_SPEC_OBJECT(receiver)) { 1055 } else if (!IS_SPEC_OBJECT(receiver)) {
1056 receiver = ToObject(receiver); 1056 receiver = ToObject(receiver);
1057 } 1057 }
1058 1058
1059 for (var i = 0; i < length; i++) { 1059 for (var i = 0; i < length; i++) {
1060 var current = array[i]; 1060 if (i in array) {
1061 if (!IS_UNDEFINED(current) || i in array) { 1061 var element = array[i];
1062 %_CallFunction(receiver, current, i, array, f); 1062 %_CallFunction(receiver, element, i, array, f);
1063 } 1063 }
1064 } 1064 }
1065 } 1065 }
1066 1066
1067 1067
1068 // Executes the function once for each element present in the 1068 // Executes the function once for each element present in the
1069 // array until it finds one where callback returns true. 1069 // array until it finds one where callback returns true.
1070 function ArraySome(f, receiver) { 1070 function ArraySome(f, receiver) {
1071 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1071 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1072 throw MakeTypeError("called_on_null_or_undefined", 1072 throw MakeTypeError("called_on_null_or_undefined",
1073 ["Array.prototype.some"]); 1073 ["Array.prototype.some"]);
1074 } 1074 }
1075 1075
1076 // Pull out the length so that modifications to the length in the 1076 // Pull out the length so that modifications to the length in the
1077 // loop will not affect the looping and side effects are visible. 1077 // loop will not affect the looping and side effects are visible.
1078 var array = ToObject(this); 1078 var array = ToObject(this);
1079 var length = TO_UINT32(array.length); 1079 var length = TO_UINT32(array.length);
1080 1080
1081 if (!IS_SPEC_FUNCTION(f)) { 1081 if (!IS_SPEC_FUNCTION(f)) {
1082 throw MakeTypeError('called_non_callable', [ f ]); 1082 throw MakeTypeError('called_non_callable', [ f ]);
1083 } 1083 }
1084 if (IS_NULL_OR_UNDEFINED(receiver)) { 1084 if (IS_NULL_OR_UNDEFINED(receiver)) {
1085 receiver = %GetDefaultReceiver(f) || receiver; 1085 receiver = %GetDefaultReceiver(f) || receiver;
1086 } else if (!IS_SPEC_OBJECT(receiver)) { 1086 } else if (!IS_SPEC_OBJECT(receiver)) {
1087 receiver = ToObject(receiver); 1087 receiver = ToObject(receiver);
1088 } 1088 }
1089 1089
1090 for (var i = 0; i < length; i++) { 1090 for (var i = 0; i < length; i++) {
1091 var current = array[i]; 1091 if (i in array) {
1092 if (!IS_UNDEFINED(current) || i in array) { 1092 var element = array[i];
1093 if (%_CallFunction(receiver, current, i, array, f)) return true; 1093 if (%_CallFunction(receiver, element, i, array, f)) return true;
1094 } 1094 }
1095 } 1095 }
1096 return false; 1096 return false;
1097 } 1097 }
1098 1098
1099 1099
1100 function ArrayEvery(f, receiver) { 1100 function ArrayEvery(f, receiver) {
1101 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1101 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1102 throw MakeTypeError("called_on_null_or_undefined", 1102 throw MakeTypeError("called_on_null_or_undefined",
1103 ["Array.prototype.every"]); 1103 ["Array.prototype.every"]);
1104 } 1104 }
1105 1105
1106 // Pull out the length so that modifications to the length in the 1106 // Pull out the length so that modifications to the length in the
1107 // loop will not affect the looping and side effects are visible. 1107 // loop will not affect the looping and side effects are visible.
1108 var array = ToObject(this); 1108 var array = ToObject(this);
1109 var length = TO_UINT32(array.length); 1109 var length = TO_UINT32(array.length);
1110 1110
1111 if (!IS_SPEC_FUNCTION(f)) { 1111 if (!IS_SPEC_FUNCTION(f)) {
1112 throw MakeTypeError('called_non_callable', [ f ]); 1112 throw MakeTypeError('called_non_callable', [ f ]);
1113 } 1113 }
1114 if (IS_NULL_OR_UNDEFINED(receiver)) { 1114 if (IS_NULL_OR_UNDEFINED(receiver)) {
1115 receiver = %GetDefaultReceiver(f) || receiver; 1115 receiver = %GetDefaultReceiver(f) || receiver;
1116 } else if (!IS_SPEC_OBJECT(receiver)) { 1116 } else if (!IS_SPEC_OBJECT(receiver)) {
1117 receiver = ToObject(receiver); 1117 receiver = ToObject(receiver);
1118 } 1118 }
1119 1119
1120 for (var i = 0; i < length; i++) { 1120 for (var i = 0; i < length; i++) {
1121 var current = array[i]; 1121 if (i in array) {
1122 if (!IS_UNDEFINED(current) || i in array) { 1122 var element = array[i];
1123 if (!%_CallFunction(receiver, current, i, array, f)) return false; 1123 if (!%_CallFunction(receiver, element, i, array, f)) return false;
1124 } 1124 }
1125 } 1125 }
1126 return true; 1126 return true;
1127 } 1127 }
1128 1128
1129 function ArrayMap(f, receiver) { 1129 function ArrayMap(f, receiver) {
1130 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1130 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1131 throw MakeTypeError("called_on_null_or_undefined", 1131 throw MakeTypeError("called_on_null_or_undefined",
1132 ["Array.prototype.map"]); 1132 ["Array.prototype.map"]);
1133 } 1133 }
1134 1134
1135 // Pull out the length so that modifications to the length in the 1135 // Pull out the length so that modifications to the length in the
1136 // loop will not affect the looping and side effects are visible. 1136 // loop will not affect the looping and side effects are visible.
1137 var array = ToObject(this); 1137 var array = ToObject(this);
1138 var length = TO_UINT32(array.length); 1138 var length = TO_UINT32(array.length);
1139 1139
1140 if (!IS_SPEC_FUNCTION(f)) { 1140 if (!IS_SPEC_FUNCTION(f)) {
1141 throw MakeTypeError('called_non_callable', [ f ]); 1141 throw MakeTypeError('called_non_callable', [ f ]);
1142 } 1142 }
1143 if (IS_NULL_OR_UNDEFINED(receiver)) { 1143 if (IS_NULL_OR_UNDEFINED(receiver)) {
1144 receiver = %GetDefaultReceiver(f) || receiver; 1144 receiver = %GetDefaultReceiver(f) || receiver;
1145 } else if (!IS_SPEC_OBJECT(receiver)) { 1145 } else if (!IS_SPEC_OBJECT(receiver)) {
1146 receiver = ToObject(receiver); 1146 receiver = ToObject(receiver);
1147 } 1147 }
1148 1148
1149 var result = new $Array(); 1149 var result = new $Array();
1150 var accumulator = new InternalArray(length); 1150 var accumulator = new InternalArray(length);
1151 for (var i = 0; i < length; i++) { 1151 for (var i = 0; i < length; i++) {
1152 var current = array[i]; 1152 if (i in array) {
1153 if (!IS_UNDEFINED(current) || i in array) { 1153 var element = array[i];
1154 accumulator[i] = %_CallFunction(receiver, current, i, array, f); 1154 accumulator[i] = %_CallFunction(receiver, element, i, array, f);
1155 } 1155 }
1156 } 1156 }
1157 %MoveArrayContents(accumulator, result); 1157 %MoveArrayContents(accumulator, result);
1158 return result; 1158 return result;
1159 } 1159 }
1160 1160
1161 1161
1162 function ArrayIndexOf(element, index) { 1162 function ArrayIndexOf(element, index) {
1163 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1163 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1164 throw MakeTypeError("called_on_null_or_undefined", 1164 throw MakeTypeError("called_on_null_or_undefined",
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 if (!IS_UNDEFINED(current) || i in array) { 1301 if (!IS_UNDEFINED(current) || i in array) {
1302 i++; 1302 i++;
1303 break find_initial; 1303 break find_initial;
1304 } 1304 }
1305 } 1305 }
1306 throw MakeTypeError('reduce_no_initial', []); 1306 throw MakeTypeError('reduce_no_initial', []);
1307 } 1307 }
1308 1308
1309 var receiver = %GetDefaultReceiver(callback); 1309 var receiver = %GetDefaultReceiver(callback);
1310 for (; i < length; i++) { 1310 for (; i < length; i++) {
1311 var element = array[i]; 1311 if (i in array) {
1312 if (!IS_UNDEFINED(element) || i in array) { 1312 var element = array[i];
1313 current = %_CallFunction(receiver, current, element, i, array, callback); 1313 current = %_CallFunction(receiver, current, element, i, array, callback);
1314 } 1314 }
1315 } 1315 }
1316 return current; 1316 return current;
1317 } 1317 }
1318 1318
1319 function ArrayReduceRight(callback, current) { 1319 function ArrayReduceRight(callback, current) {
1320 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1320 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1321 throw MakeTypeError("called_on_null_or_undefined", 1321 throw MakeTypeError("called_on_null_or_undefined",
1322 ["Array.prototype.reduceRight"]); 1322 ["Array.prototype.reduceRight"]);
(...skipping 15 matching lines...) Expand all
1338 if (!IS_UNDEFINED(current) || i in array) { 1338 if (!IS_UNDEFINED(current) || i in array) {
1339 i--; 1339 i--;
1340 break find_initial; 1340 break find_initial;
1341 } 1341 }
1342 } 1342 }
1343 throw MakeTypeError('reduce_no_initial', []); 1343 throw MakeTypeError('reduce_no_initial', []);
1344 } 1344 }
1345 1345
1346 var receiver = %GetDefaultReceiver(callback); 1346 var receiver = %GetDefaultReceiver(callback);
1347 for (; i >= 0; i--) { 1347 for (; i >= 0; i--) {
1348 var element = array[i]; 1348 if (i in array) {
1349 if (!IS_UNDEFINED(element) || i in array) { 1349 var element = array[i];
1350 current = %_CallFunction(receiver, current, element, i, array, callback); 1350 current = %_CallFunction(receiver, current, element, i, array, callback);
1351 } 1351 }
1352 } 1352 }
1353 return current; 1353 return current;
1354 } 1354 }
1355 1355
1356 // ES5, 15.4.3.2 1356 // ES5, 15.4.3.2
1357 function ArrayIsArray(obj) { 1357 function ArrayIsArray(obj) {
1358 return IS_ARRAY(obj); 1358 return IS_ARRAY(obj);
1359 } 1359 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 // exposed to user code. 1418 // exposed to user code.
1419 // Adding only the functions that are actually used. 1419 // Adding only the functions that are actually used.
1420 SetUpLockedPrototype(InternalArray, $Array(), $Array( 1420 SetUpLockedPrototype(InternalArray, $Array(), $Array(
1421 "join", getFunction("join", ArrayJoin), 1421 "join", getFunction("join", ArrayJoin),
1422 "pop", getFunction("pop", ArrayPop), 1422 "pop", getFunction("pop", ArrayPop),
1423 "push", getFunction("push", ArrayPush) 1423 "push", getFunction("push", ArrayPush)
1424 )); 1424 ));
1425 } 1425 }
1426 1426
1427 SetUpArray(); 1427 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-1790.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698