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

Side by Side Diff: src/array.js

Issue 1316933002: [es6] Initial steps towards a correct implementation of IsCallable. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 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
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 var $arrayConcat; 5 var $arrayConcat;
6 var $arrayPush; 6 var $arrayPush;
7 var $arrayPop; 7 var $arrayPop;
8 var $arrayShift; 8 var $arrayShift;
9 var $arraySlice; 9 var $arraySlice;
10 var $arraySplice; 10 var $arraySplice;
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 if (IS_ARRAY(this)) { 387 if (IS_ARRAY(this)) {
388 func = this.join; 388 func = this.join;
389 if (func === ArrayJoin) { 389 if (func === ArrayJoin) {
390 return Join(this, this.length, ',', ConvertToString); 390 return Join(this, this.length, ',', ConvertToString);
391 } 391 }
392 array = this; 392 array = this;
393 } else { 393 } else {
394 array = TO_OBJECT(this); 394 array = TO_OBJECT(this);
395 func = array.join; 395 func = array.join;
396 } 396 }
397 if (!IS_SPEC_FUNCTION(func)) { 397 if (!IS_CALLABLE(func)) {
398 return %_CallFunction(array, ObjectToString); 398 return %_CallFunction(array, ObjectToString);
399 } 399 }
400 return %_CallFunction(array, func); 400 return %_CallFunction(array, func);
401 } 401 }
402 402
403 403
404 function InnerArrayToLocaleString(array, length) { 404 function InnerArrayToLocaleString(array, length) {
405 var len = TO_UINT32(length); 405 var len = TO_UINT32(length);
406 if (len === 0) return ""; 406 if (len === 0) return "";
407 return Join(array, len, ',', ConvertToLocaleString); 407 return Join(array, len, ',', ConvertToLocaleString);
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 904
905 // Return the deleted elements. 905 // Return the deleted elements.
906 return deleted_elements; 906 return deleted_elements;
907 } 907 }
908 908
909 909
910 function InnerArraySort(length, comparefn) { 910 function InnerArraySort(length, comparefn) {
911 // In-place QuickSort algorithm. 911 // In-place QuickSort algorithm.
912 // For short (length <= 22) arrays, insertion sort is used for efficiency. 912 // For short (length <= 22) arrays, insertion sort is used for efficiency.
913 913
914 if (!IS_SPEC_FUNCTION(comparefn)) { 914 if (!IS_CALLABLE(comparefn)) {
915 comparefn = function (x, y) { 915 comparefn = function (x, y) {
916 if (x === y) return 0; 916 if (x === y) return 0;
917 if (%_IsSmi(x) && %_IsSmi(y)) { 917 if (%_IsSmi(x) && %_IsSmi(y)) {
918 return %SmiLexicographicCompare(x, y); 918 return %SmiLexicographicCompare(x, y);
919 } 919 }
920 x = ToString(x); 920 x = ToString(x);
921 y = ToString(y); 921 y = ToString(y);
922 if (x == y) return 0; 922 if (x == y) return 0;
923 else return x < y ? -1 : 1; 923 else return x < y ? -1 : 1;
924 }; 924 };
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 var array = TO_OBJECT(this); 1196 var array = TO_OBJECT(this);
1197 var length = TO_UINT32(array.length); 1197 var length = TO_UINT32(array.length);
1198 return %_CallFunction(array, length, comparefn, InnerArraySort); 1198 return %_CallFunction(array, length, comparefn, InnerArraySort);
1199 } 1199 }
1200 1200
1201 1201
1202 // The following functions cannot be made efficient on sparse arrays while 1202 // The following functions cannot be made efficient on sparse arrays while
1203 // preserving the semantics, since the calls to the receiver function can add 1203 // preserving the semantics, since the calls to the receiver function can add
1204 // or delete elements from the array. 1204 // or delete elements from the array.
1205 function InnerArrayFilter(f, receiver, array, length) { 1205 function InnerArrayFilter(f, receiver, array, length) {
1206 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 1206 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
1207 var needs_wrapper = false; 1207 var needs_wrapper = false;
1208 if (IS_NULL(receiver)) { 1208 if (IS_NULL(receiver)) {
1209 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; 1209 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED;
1210 } else if (!IS_UNDEFINED(receiver)) { 1210 } else if (!IS_UNDEFINED(receiver)) {
1211 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 1211 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1212 } 1212 }
1213 1213
1214 var accumulator = new InternalArray(); 1214 var accumulator = new InternalArray();
1215 var accumulator_length = 0; 1215 var accumulator_length = 0;
1216 var is_array = IS_ARRAY(array); 1216 var is_array = IS_ARRAY(array);
(...skipping 19 matching lines...) Expand all
1236 // loop will not affect the looping and side effects are visible. 1236 // loop will not affect the looping and side effects are visible.
1237 var array = TO_OBJECT(this); 1237 var array = TO_OBJECT(this);
1238 var length = TO_UINT32(array.length); 1238 var length = TO_UINT32(array.length);
1239 var accumulator = InnerArrayFilter(f, receiver, array, length); 1239 var accumulator = InnerArrayFilter(f, receiver, array, length);
1240 var result = new GlobalArray(); 1240 var result = new GlobalArray();
1241 %MoveArrayContents(accumulator, result); 1241 %MoveArrayContents(accumulator, result);
1242 return result; 1242 return result;
1243 } 1243 }
1244 1244
1245 function InnerArrayForEach(f, receiver, array, length) { 1245 function InnerArrayForEach(f, receiver, array, length) {
1246 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 1246 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
1247 var needs_wrapper = false; 1247 var needs_wrapper = false;
1248 if (IS_NULL(receiver)) { 1248 if (IS_NULL(receiver)) {
1249 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; 1249 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED;
1250 } else if (!IS_UNDEFINED(receiver)) { 1250 } else if (!IS_UNDEFINED(receiver)) {
1251 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 1251 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1252 } 1252 }
1253 1253
1254 var is_array = IS_ARRAY(array); 1254 var is_array = IS_ARRAY(array);
1255 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1255 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1256 for (var i = 0; i < length; i++) { 1256 for (var i = 0; i < length; i++) {
(...skipping 12 matching lines...) Expand all
1269 1269
1270 // Pull out the length so that modifications to the length in the 1270 // Pull out the length so that modifications to the length in the
1271 // loop will not affect the looping and side effects are visible. 1271 // loop will not affect the looping and side effects are visible.
1272 var array = TO_OBJECT(this); 1272 var array = TO_OBJECT(this);
1273 var length = TO_UINT32(array.length); 1273 var length = TO_UINT32(array.length);
1274 InnerArrayForEach(f, receiver, array, length); 1274 InnerArrayForEach(f, receiver, array, length);
1275 } 1275 }
1276 1276
1277 1277
1278 function InnerArraySome(f, receiver, array, length) { 1278 function InnerArraySome(f, receiver, array, length) {
1279 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 1279 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
1280 var needs_wrapper = false; 1280 var needs_wrapper = false;
1281 if (IS_NULL(receiver)) { 1281 if (IS_NULL(receiver)) {
1282 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; 1282 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED;
1283 } else if (!IS_UNDEFINED(receiver)) { 1283 } else if (!IS_UNDEFINED(receiver)) {
1284 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 1284 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1285 } 1285 }
1286 1286
1287 var is_array = IS_ARRAY(array); 1287 var is_array = IS_ARRAY(array);
1288 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1288 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1289 for (var i = 0; i < length; i++) { 1289 for (var i = 0; i < length; i++) {
(...skipping 16 matching lines...) Expand all
1306 1306
1307 // Pull out the length so that modifications to the length in the 1307 // Pull out the length so that modifications to the length in the
1308 // loop will not affect the looping and side effects are visible. 1308 // loop will not affect the looping and side effects are visible.
1309 var array = TO_OBJECT(this); 1309 var array = TO_OBJECT(this);
1310 var length = TO_UINT32(array.length); 1310 var length = TO_UINT32(array.length);
1311 return InnerArraySome(f, receiver, array, length); 1311 return InnerArraySome(f, receiver, array, length);
1312 } 1312 }
1313 1313
1314 1314
1315 function InnerArrayEvery(f, receiver, array, length) { 1315 function InnerArrayEvery(f, receiver, array, length) {
1316 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 1316 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
1317 var needs_wrapper = false; 1317 var needs_wrapper = false;
1318 if (IS_NULL(receiver)) { 1318 if (IS_NULL(receiver)) {
1319 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; 1319 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED;
1320 } else if (!IS_UNDEFINED(receiver)) { 1320 } else if (!IS_UNDEFINED(receiver)) {
1321 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 1321 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1322 } 1322 }
1323 1323
1324 var is_array = IS_ARRAY(array); 1324 var is_array = IS_ARRAY(array);
1325 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1325 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
1326 for (var i = 0; i < length; i++) { 1326 for (var i = 0; i < length; i++) {
(...skipping 13 matching lines...) Expand all
1340 1340
1341 // Pull out the length so that modifications to the length in the 1341 // Pull out the length so that modifications to the length in the
1342 // loop will not affect the looping and side effects are visible. 1342 // loop will not affect the looping and side effects are visible.
1343 var array = TO_OBJECT(this); 1343 var array = TO_OBJECT(this);
1344 var length = TO_UINT32(array.length); 1344 var length = TO_UINT32(array.length);
1345 return InnerArrayEvery(f, receiver, array, length); 1345 return InnerArrayEvery(f, receiver, array, length);
1346 } 1346 }
1347 1347
1348 1348
1349 function InnerArrayMap(f, receiver, array, length) { 1349 function InnerArrayMap(f, receiver, array, length) {
1350 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); 1350 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f);
1351 var needs_wrapper = false; 1351 var needs_wrapper = false;
1352 if (IS_NULL(receiver)) { 1352 if (IS_NULL(receiver)) {
1353 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; 1353 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED;
1354 } else if (!IS_UNDEFINED(receiver)) { 1354 } else if (!IS_UNDEFINED(receiver)) {
1355 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); 1355 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver);
1356 } 1356 }
1357 1357
1358 var accumulator = new InternalArray(length); 1358 var accumulator = new InternalArray(length);
1359 var is_array = IS_ARRAY(array); 1359 var is_array = IS_ARRAY(array);
1360 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); 1360 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 function ArrayLastIndexOf(element, index) { 1504 function ArrayLastIndexOf(element, index) {
1505 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf"); 1505 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
1506 1506
1507 var length = TO_UINT32(this.length); 1507 var length = TO_UINT32(this.length);
1508 return %_CallFunction(this, element, index, length, 1508 return %_CallFunction(this, element, index, length,
1509 %_ArgumentsLength(), InnerArrayLastIndexOf); 1509 %_ArgumentsLength(), InnerArrayLastIndexOf);
1510 } 1510 }
1511 1511
1512 1512
1513 function InnerArrayReduce(callback, current, array, length, argumentsLength) { 1513 function InnerArrayReduce(callback, current, array, length, argumentsLength) {
1514 if (!IS_SPEC_FUNCTION(callback)) { 1514 if (!IS_CALLABLE(callback)) {
1515 throw MakeTypeError(kCalledNonCallable, callback); 1515 throw MakeTypeError(kCalledNonCallable, callback);
1516 } 1516 }
1517 1517
1518 var is_array = IS_ARRAY(array); 1518 var is_array = IS_ARRAY(array);
1519 var i = 0; 1519 var i = 0;
1520 find_initial: if (argumentsLength < 2) { 1520 find_initial: if (argumentsLength < 2) {
1521 for (; i < length; i++) { 1521 for (; i < length; i++) {
1522 if (HAS_INDEX(array, i, is_array)) { 1522 if (HAS_INDEX(array, i, is_array)) {
1523 current = array[i++]; 1523 current = array[i++];
1524 break find_initial; 1524 break find_initial;
(...skipping 22 matching lines...) Expand all
1547 // loop will not affect the looping and side effects are visible. 1547 // loop will not affect the looping and side effects are visible.
1548 var array = TO_OBJECT(this); 1548 var array = TO_OBJECT(this);
1549 var length = TO_UINT32(array.length); 1549 var length = TO_UINT32(array.length);
1550 return InnerArrayReduce(callback, current, array, length, 1550 return InnerArrayReduce(callback, current, array, length,
1551 %_ArgumentsLength()); 1551 %_ArgumentsLength());
1552 } 1552 }
1553 1553
1554 1554
1555 function InnerArrayReduceRight(callback, current, array, length, 1555 function InnerArrayReduceRight(callback, current, array, length,
1556 argumentsLength) { 1556 argumentsLength) {
1557 if (!IS_SPEC_FUNCTION(callback)) { 1557 if (!IS_CALLABLE(callback)) {
1558 throw MakeTypeError(kCalledNonCallable, callback); 1558 throw MakeTypeError(kCalledNonCallable, callback);
1559 } 1559 }
1560 1560
1561 var is_array = IS_ARRAY(array); 1561 var is_array = IS_ARRAY(array);
1562 var i = length - 1; 1562 var i = length - 1;
1563 find_initial: if (argumentsLength < 2) { 1563 find_initial: if (argumentsLength < 2) {
1564 for (; i >= 0; i--) { 1564 for (; i >= 0; i--) {
1565 if (HAS_INDEX(array, i, is_array)) { 1565 if (HAS_INDEX(array, i, is_array)) {
1566 current = array[i--]; 1566 current = array[i--];
1567 break find_initial; 1567 break find_initial;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1713 1713
1714 $arrayConcat = ArrayConcatJS; 1714 $arrayConcat = ArrayConcatJS;
1715 $arrayPush = ArrayPush; 1715 $arrayPush = ArrayPush;
1716 $arrayPop = ArrayPop; 1716 $arrayPop = ArrayPop;
1717 $arrayShift = ArrayShift; 1717 $arrayShift = ArrayShift;
1718 $arraySlice = ArraySlice; 1718 $arraySlice = ArraySlice;
1719 $arraySplice = ArraySplice; 1719 $arraySplice = ArraySplice;
1720 $arrayUnshift = ArrayUnshift; 1720 $arrayUnshift = ArrayUnshift;
1721 1721
1722 }); 1722 });
OLDNEW
« no previous file with comments | « src/api-natives.cc ('k') | src/bootstrapper.cc » ('j') | src/objects-debug.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698