Re: Arrays in WebIDL aren't making sense to me so far

On 4/2/12 7:34 PM, Cameron McCormack wrote:
> If a platform array object is not [[Extensible]], is not a fixed length
> array, and the array gets longer, what happens? Does the length property
> reflect the new longer value but not expose the additional array index
> properties with its [[GetOwnProperty]]? It seemed simpler to disallow this.

OK, fair.

>> 2) http://dev.w3.org/2006/webapi/WebIDL/#idl-array says that arrays are
>> passed by reference and that "Passing an array to a platform object
>> could result in that array being modified by the object." However
>> http://dev.w3.org/2006/webapi/WebIDL/#es-array says that converting an
>> ES value to an IDL array always allocates a new array. I'm not sure how
>> to reconcile these two statements; at the moment I'm assuming that the
>> informative text at the first link is in error and the normative
>> requirements are correct: arrays are passed by value but returned by
>> reference. Is that right?
>
> I think the confusions is because I'm using "array" in two different
> senses.

No, I don't think that's the confusion.

> The set of values for an IDL array type T[] are just references
> to platform array objects of that element type.

Yes, agreed.

> When you pass in a JS Array object

I'm not talking about passing a JS array.

> the JS value is converted to the IDL type by creating a
> new platform array object from the values in the JS Array.

Yes, this part makes sense.

> interface A {
> attribute long[] x;
> };
>
> where x is defined to just store and return the same value, nothing
> special

Then consider the following script:

   var A1 = new A();
   var A2 = new A();
   A1.x = [1, 2, 3];
   A2.x = A1.x;
   alert(A1.x == A2.x);

Per the spec as currently written this should alert false, as far as I 
can tell, because the assignment to A2.x created a new platform array 
object unconditionally.  Is that intended?

> The statement that says "passing an array to a platform object could
> result in that array being modified by the object" is referring to the
> IDL array type, not a JS array.

OK, but passing something to an argument taking an IDL array always 
creates a new object right now.

> interface B {
> long[] range(unsigned long min, unsigned long max);
> void incrementAll(long[] a);
> };
>
> Let's say range returns a reference to a newly created IDL array (so in
> the ECMAScript binding this means a new platform array object) with
> values from min to max.
>
> var a1 = myB.range(1, 5);
> assert(a1[0] == 1);
> myB.incrementAll(a);

This would create a copy of "a" and pass that to myB, as currently specced.

-Boris

Received on Tuesday, 3 April 2012 00:33:39 UTC