Re: Web IDL sequence<T> and item() method

On 11/12/11 11:55 PM, Marcos Caceres wrote:
> I'm also unsure as to the purpose of sequence in practice. Perhaps some examples of expected usage would help a bit?

Sequences are for "pass by value" lists of values.  As with the array 
type, they allow you to pass in a JS array to an operation:

   interface A {
     void f(sequence<float> xs);
   };

   getA().f([10, 20, 30]);

or indeed anything that feels enough like an array:

   var o = { length: 3 };
   o[0] = 10;
   o[1] = 20;
   o[2] = 30;
   getA().f(o);

When used as an operation return type, a new JS array is created:

   interface B {
     sequence<float> f();
   };

   var a = getB().f();
   assert(a instanceof Array);
   var b = getB().f();
   assert(a != b);

The array type T[] is used for array-like objects for which references 
can be kept by platform objects or by user script, and possibly modified 
by either of them.

If we were ever going to extend one of the built in types with item(), 
it would be the array type T[], which corresponds to a platform object 
that looks a bit like a JS array but is different in various ways (extra 
prototype object in the chain, non-sparse, can be read only / fixed length).

To get an object that is like a T[] but with other custom members, you 
currently have to use [ArrayClass] on an interface:

   [ArrayClass]
   interface FixedLengthWritableNodeList {
     readonly attribute unsigned long length;
     getter Node item(unsigned long index);
     setter void (unsigned long index, Node value);
   };

All [ArrayClass] means is that 
Object.getPrototypeOf(FixedLengthWritableNodeList.prototype) will be 
Array.prototype instead of Object.prototype.  So you can see you need to 
hook up the length and getters/setters yourself there.

Received on Sunday, 11 December 2011 23:34:28 UTC