Re: DOM collections index out of bounds and JavaScript.

On 10/29/10, Cameron McCormack <cam@mcc.id.au> wrote:
> Cameron McCormack:
>> > I don’t know that sequence is appropriate for this.  They are meant
>> > to be for pass-by-value lists.
>>
> Anne van Kesteren:
>> I'm not sure I follow this. Could you elaborate a bit?
>
> As currently defined, sequence types are used solely to represent lists
> of values, and not objects that have other methods on them.  Also, those
> lists are passed by value.  If you had an operation that took a
> sequence<long> argument, then in ECMAScript you could pass an Array
> object where each of the elements were treated as longs (and converted
> appropriately).  The object on which you called the method wouldn’t be
> able to keep a reference to the Array.
>

Objects references are passed as value in ES but that doesn't mean the
same thing you're intending here. Here, by "pass by value", you mean
to convey that a copy of value of the object is passed.

Object references are passed by value in ES. Dmitry Soshnikov explains
this pretty well:
<http://dmitrysoshnikov.com/ecmascript/chapter-8-evaluation-strategy/#ecmascript-implementation>

// Modifying an Array that was passed to drawPolygon() is guaranteed not to
// have an effect on the Canvas, since the Array is effectively passed by value.
a[0] = 20;

Where is Array host object is implemented?

A method that accepts an array can use that array without storing a
reference to it. And if it needs those items, then it can make a
"defensive copy" of the Array.

Below is an example of how that would look in user-defined code.
Anything that is possible in user-defined code can be implemented in
native code.


var o = new function() {
  var items = [],
      slice = Array.prototype.slice;
  function setItems(a) {
    items = slice.call(a);
  }
  function getItems(a) {
    return slice.call(items);
  }
  this.setItems = setItems;
  this.getItems = getItems;
};

var a = [1, 2, 3];
o.setItems(a);

The difference here is that the object that was passed in uses normal
ES syntax; it's pass by value (value of reference) not referenced by
the object. The effect is what you wanted, I think, and that is that
1) the method that accepts the array makes a defensive copy of it and
2) that the method (or getter) that returns the array always returns a
new copy of it.

But where is the array host object used?

[...]

>> >  interface Collection {
>> >    attribute unsigned long length;
>> >    getter any item(unsigned long index);
>> >  }
>> >
>> >where you need to use “any” because you don’t know what the type of the
>> >derived interface elements will be?
>>
>> Well, and you would need to define that base interface somewhere and
>> all other specifications would need to use it.
>
> A minor issue, I guess.
>

That seems like the main issue to me. What are a Collection object's
indexed properties? Are they real object properties or is there a
proxy for [[Get]] and [[Has]]? Both behaviors can be seen in
implementations but it varies, depending on the implementation and on
the object.

But where are proxies really needed? How important is it, for example,
for document.styleSheets[-1] to throw an "index out of bounds"
exception, or for document.childNodes(99999) to return null instead of
undefined?

It seems that some implementations have gone out of the way to use
proxies to adhere to the spec to fulfill the odd cases above (albeit
inconsistently) while others have chosen to just use property access
to return undefined.  How important are those cases? Do we have any
bug reports for implementations returning `undefined` instead of
throwing/returning null?

I get that the spec requires ob[n] to delegate to item, and so for
that a proxy is needed. But what type of situation is it really
necessary for obj[n] to delegate to item? Which Collections really
need a proxy to function as required by code?

[...]

Garrett

Received on Saturday, 30 October 2010 05:33:17 UTC