You were hit by some implementation details of v8.

This kind of customization ideally should be performed in JS.  Or you
can just put Array.prototype as a prototype for your objects.

yours,
anton.

On Fri, Apr 8, 2011 at 12:14 AM, ajg <[email protected]> wrote:
> I tried using the code below, but the "names" array is empty (I guess
> because these properties are marked as "DontEnum")
>
>                Local<v8::Object> proto =
> Local<v8::Object>::Cast(v8::Array::New(1)->GetPrototype());
>                internalFieldArrays =
> Persistent<ObjectTemplate>::New(ObjectTemplate::New());
>                Local<v8::Array> names = proto->GetPropertyNames();
>                        for ( unsigned int i=0; i<names->Length(); i++ ) {
>                                v8::Local<v8::String> name = 
> names->Get(i)->ToString();
>                                internalFieldArrays->Set(name, 
> proto->Get(name));
>                        }
>
> It seems that the only way to get the forEach property is:
> Local<v8::Value> val4 = arr->Get(v8::String::New("forEach"));
> Hence I would have to manually copy each property by name, there is no
> better way?
>
> On Apr 7, 11:57 am, Anton Muhin <[email protected]> wrote:
>> Good day, ajg,
>>
>> I don't think you can (and should be able to) create v8::Array with
>> ObjectTemplate.
>>
>> However, it's easy to install Array functions to any kind of
>> object---ECMAScript array functions were intentionally designed to
>> work with generic objects---all you need is to add a length property.
>> Simple test would be to do something like [].forEach.call(yourobject,
>> args).  To use convenient syntax yourobject.forEach just add
>> Array.prototype.forEach and other functions to prototype of your
>> object (see PrototypTemplate) or an object directly (1st option is
>> preferable).
>>
>> hth and yours,
>> anton.
>>
>> On Thu, Apr 7, 2011 at 9:59 PM, ajg <[email protected]> wrote:
>> > I already have a working ObjectTemplate that enables js code to access
>> > C++ objects in a lazy fashion, using the NamedPropertyHandler.
>> > Now I also need to create v8:Array that is backed by a C++ object
>> > using the IndexedPropertyHandler.
>> > But it seems that ObjectTemplate can only create a v8::Object, not a
>> > v8::Array.
>> > Doing an indexed access on v8::Object works fine but then the built-in
>> > functions like "forEach" are not available.
>> > Basically I need to either:
>> > - create a v8::Array with IndexedPropertyHandlers
>> > - create a v8::Object with IndexedPropertyHandlers and transfer the
>> > builtin methods to it, or somehow convert it to a v8::Array.
>> > Note that on C++ side it is not a native type array, so I cannot use
>> > the v8::Object::SetIndexedPropertiesToExternalArrayData
>> > Any suggestion?
>> > thanks
>>
>> > --
>> > v8-users mailing list
>> > [email protected]
>> >http://groups.google.com/group/v8-users
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to