188.8.131.52 Function.prototype.apply (thisArg, argArray)
Allen.Wirfs-Brock at microsoft.com
Thu Oct 28 10:27:43 PDT 2010
If we assume “Array like object” is an object that can serve as the this value of the Array.prototype methods then pretty much any object is “Array like”. The Array.prototype methods generally iterate based upon the value of the this object’s length property. They use ToUint32 to interpret length as a positive value in the 0..2^32-1 range. If the this object does not have a length property, [[Get]] will return undefined which ToUint32 converts to 0. All other non-numeric values also convert to values in the 0..2^32-1 range. Things like non-numeric string values first convert to NaN and then to 0.
So objects without a length or with a length value for which ToNumber produces NaN are treated as 0 length “arrays”.
If the converted length is non-zero, then the algorithms iterate over the “array indexed” property names between 0 and ToUint32(length)-1. Each property is accessed individually and may or may not actually exist. Specific behavior for each “array indexed” property (whether present or not) is defined by each algorithm.
Hence, all objects are “array like”. Some are just more obviously (or usefully)) so than others.
Regarding, passing an object whose length is -1 to apply as the “array” of arguments. As has been pointed out, -1 would be interpreted as +2^32-1 – a very large array. It is reasonable to assume that all implementations have some limit (perhaps dynamically determined) on the size of an argument list that can be passed to a function so such an object would presumably trip that limit. The actual limit value and the action that occurs is not specified by ES5.
Some will make an argument that all such limits should be made explicit in the specification. However, that is probably folly. There are dozens of places in the specification where plausible implementation limits might be encountered. In many cases, the specific would have to pick an arbitrary limit value as there is no obvious natural limit value. Even if a such values were specified, there is little reason to believe that implementation would actually respect them. Often actual limits are determined dynamically based upon various available resources. What’s an implementation going to do if the spec. says it must support arguments list of up to 1000 elements but that exhausts available stack space for some specific call. It’s still going to fail, regardless of what the spec. says.
While trying to spec. all possible implementation limits is probably not a very good idea. It may make sense to specify what should happen if an implementation limit is exceeded. Probably throwing some specific exception.
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Jose Antonio Perez
Sent: Thursday, October 28, 2010 8:41 AM
To: Asen Bozhilov
Subject: Re: 184.108.40.206 Function.prototype.apply (thisArg, argArray)
In ES5-15.4 is clearly defined what is an index and what conditions must verify length
"A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32−1"
"Every Array object has a length property whose value is always a nonnegative integer less than 2^32"
Also in the construction of an array with explicit length (220.127.116.11) that condition is checked with a possible RangeError exception
"If the argument len is a Number and ToUint32(len) is equal to len, then the length property of the newly constructed object is set to ToUint32(len). If the argument len is a Number and ToUint32(len) is not equal to len, a RangeError exception is thrown."
As Asen suggests would be interesting to define explicity the concept of "Array like object", demanding the fulfillment of such conditions.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss