Chromium Code Reviews| Index: src/array.js |
| diff --git a/src/array.js b/src/array.js |
| index 6ed1476080d49c5d1e552d63011c34964e798ea8..51a9b1768ddb834aace9cfe124014b7dff07aa83 100644 |
| --- a/src/array.js |
| +++ b/src/array.js |
| @@ -172,6 +172,7 @@ function Join(array, length, separator, convert) { |
| function ConvertToString(x) { |
| // Assumes x is a non-string. |
| + // Differs from NonStringToString in handling of null and undefined. |
| if (IS_NUMBER(x)) return %_NumberToString(x); |
| if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); |
| @@ -182,14 +183,15 @@ function ConvertToLocaleString(e) { |
| if (e == null) { |
| return ''; |
| } else { |
| - // e_obj's toLocaleString might be overwritten, check if it is a function. |
| - // Call ToString if toLocaleString is not a function. |
| - // See issue 877615. |
| - var e_obj = ToObject(e); |
| - if (IS_FUNCTION(e_obj.toLocaleString)) |
| - return ToString(e_obj.toLocaleString()); |
| - else |
| - return ToString(e); |
| + var o = ToObject(e); |
| + var func = o.toLocaleString; |
| + if (IS_FUNCTION(func)) { |
| + return ToString(%_CallFunction(o, func)); |
| + } |
| + // For compatibility with JSC, we default to ToString on the original |
| + // value if toLocaleString isn't usable, instead of throwing a |
| + // TypeError. |
| + return ToString(e); |
| } |
| } |
| @@ -359,22 +361,24 @@ function SimpleMove(array, start_i, del_count, len, num_additional_args) { |
| function ArrayToString() { |
| - if (!IS_ARRAY(this)) { |
| - throw new $TypeError('Array.prototype.toString is not generic'); |
| + var array = ToObject(this); |
|
sandholm
2011/03/11 13:16:53
You probably want IS_ARRAY(this) to be the fast ca
|
| + var func = array.join; |
|
sandholm
2011/03/11 13:16:53
Not calling "Join" directly may have performance i
|
| + if (!IS_FUNCTION(func)) { |
| + func = ObjectToString; |
| } |
| - return Join(this, this.length, ',', ConvertToString); |
| + return %_CallFunction(array, func); |
| } |
| function ArrayToLocaleString() { |
| - if (!IS_ARRAY(this)) { |
| - throw new $TypeError('Array.prototype.toString is not generic'); |
| - } |
| - return Join(this, this.length, ',', ConvertToLocaleString); |
| + var array = ToObject(this); |
|
sandholm
2011/03/11 13:16:53
Same comment re IS_ARRAY and ToObject.
|
| + var length = TO_UINT32(array.length); |
| + return Join(array, length, ',', ConvertToLocaleString); |
| } |
| function ArrayJoin(separator) { |
| + var length = TO_UINT32(this.length); |
| if (IS_UNDEFINED(separator)) { |
| separator = ','; |
| } else if (!IS_STRING(separator)) { |
| @@ -384,7 +388,7 @@ function ArrayJoin(separator) { |
| var result = %_FastAsciiArrayJoin(this, separator); |
| if (!IS_UNDEFINED(result)) return result; |
| - return Join(this, TO_UINT32(this.length), separator, ConvertToString); |
| + return Join(this, length, separator, ConvertToString); |
| } |
| @@ -914,12 +918,10 @@ function ArraySort(comparefn) { |
| // preserving the semantics, since the calls to the receiver function can add |
| // or delete elements from the array. |
| function ArrayFilter(f, receiver) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(f)) { |
| throw MakeTypeError('called_non_callable', [ f ]); |
| } |
| - // Pull out the length so that modifications to the length in the |
| - // loop will not affect the looping. |
| - var length = this.length; |
| var result = []; |
| var result_length = 0; |
| for (var i = 0; i < length; i++) { |
| @@ -935,12 +937,10 @@ function ArrayFilter(f, receiver) { |
| function ArrayForEach(f, receiver) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(f)) { |
| throw MakeTypeError('called_non_callable', [ f ]); |
| } |
| - // Pull out the length so that modifications to the length in the |
| - // loop will not affect the looping. |
| - var length = TO_UINT32(this.length); |
| for (var i = 0; i < length; i++) { |
| var current = this[i]; |
| if (!IS_UNDEFINED(current) || i in this) { |
| @@ -953,12 +953,10 @@ function ArrayForEach(f, receiver) { |
| // Executes the function once for each element present in the |
| // array until it finds one where callback returns true. |
| function ArraySome(f, receiver) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(f)) { |
| throw MakeTypeError('called_non_callable', [ f ]); |
| } |
| - // Pull out the length so that modifications to the length in the |
| - // loop will not affect the looping. |
| - var length = TO_UINT32(this.length); |
| for (var i = 0; i < length; i++) { |
| var current = this[i]; |
| if (!IS_UNDEFINED(current) || i in this) { |
| @@ -970,12 +968,10 @@ function ArraySome(f, receiver) { |
| function ArrayEvery(f, receiver) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(f)) { |
| throw MakeTypeError('called_non_callable', [ f ]); |
| } |
| - // Pull out the length so that modifications to the length in the |
| - // loop will not affect the looping. |
| - var length = TO_UINT32(this.length); |
| for (var i = 0; i < length; i++) { |
| var current = this[i]; |
| if (!IS_UNDEFINED(current) || i in this) { |
| @@ -986,12 +982,10 @@ function ArrayEvery(f, receiver) { |
| } |
| function ArrayMap(f, receiver) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(f)) { |
| throw MakeTypeError('called_non_callable', [ f ]); |
| } |
| - // Pull out the length so that modifications to the length in the |
| - // loop will not affect the looping. |
| - var length = TO_UINT32(this.length); |
| var result = new $Array(); |
| var accumulator = new InternalArray(length); |
| for (var i = 0; i < length; i++) { |
| @@ -1116,12 +1110,12 @@ function ArrayLastIndexOf(element, index) { |
| function ArrayReduce(callback, current) { |
| + var length = TO_UINT32(this.length); |
| if (!IS_FUNCTION(callback)) { |
| throw MakeTypeError('called_non_callable', [callback]); |
| } |
| // Pull out the length so that modifications to the length in the |
| // loop will not affect the looping. |
| - var length = this.length; |
| var i = 0; |
| find_initial: if (%_ArgumentsLength() < 2) { |
| @@ -1145,10 +1139,10 @@ function ArrayReduce(callback, current) { |
| } |
| function ArrayReduceRight(callback, current) { |
| + var i = TO_UINT32(this.length) - 1; |
| if (!IS_FUNCTION(callback)) { |
| throw MakeTypeError('called_non_callable', [callback]); |
| } |
| - var i = this.length - 1; |
| find_initial: if (%_ArgumentsLength() < 2) { |
| for (; i >= 0; i--) { |