Array interation

33 views
Skip to first unread message

Bryan White

unread,
Nov 18, 2009, 3:36:34 PM11/18/09
to v8-u...@googlegroups.com
My C++ program supplies to V8 javascript code objects that represent
some internal data structures. Some of these data structures are in
fact arrays of other objects. I have implemented the 'length'
accessor and the indexed property interceptor. This has been working
for a while now like this:

for(var i = 0; i < list.length; ++i)
... using list[i]

I would like to get this working as well:

for(elem in list)
... do something with elem

If I just do the above, elem will take on the values of the names of
the accessors installed in the ObjectTemplate. (length and a couple
of other unrelated accessors). This makes sense as it is enumerating
it as an object, not an array.

To fix this I created an call back and installed it with
objtmpl->SetIndexedPropertyHandler(V8Get,0,0,0,V8Enum)
(V8Enum is the new function, V8Get has been there all along.

In the V8Enum function I create an array, add all the values to the
array and return it.

For reference this is the function (with app specific stuff elided to
keep it simple):

Handle<Array> V8Enum(const AccessorInfo& ai)
{
int sz = ...;
Handle<Array> arry = Array::New(sz);
for(int i = 0; i < sz; ++i)
arry->Set( V8Int(i), ...);
return arry;
}


The V8Enum function is being called. However the javascript
enumeration is still seeing the names of the template accessors and
not the array elements being returned.

Am I misinterpreting how this is supposed to work?

--
Bryan White

Christian Plesner Hansen

unread,
Nov 19, 2009, 3:13:34 AM11/19/09
to v8-u...@googlegroups.com
What you're seeing is maybe not the most meaningful behavior but it is
expected and correct. If you try the same with a standard JS array it
will also iterate through the indexes, not the element values.
Reply all
Reply to author
Forward
0 new messages