thanks for suggestion. I did try to copy the methods manually but then some other methods fail, for example toString() on the object does not work since it checks that the object really is an array. It prints out "TypeError: Array.prototype.toString is not generic" Copying the prototype seems to work better, both the forEach() and toString() seem to work, but I have to do more tests.
I am hitting another problem that is not related, I may post another question about it. Basically I create a JS object from an ObjectTemplate that is backed by a C++ object, and that object has a single key that is number-like (e.g. "0" or "10"). Then I have proper NamedPropertyGet and NamedPropertyEnumerator, but v8 just seems to act as if key does not exist. If I use the exact same object but add a letter in front of the number (e.g. "a0") then code works fine. It seems that v8 has some specific behavior if the keys are numbers, maybe it is confused and think it's an array. I started going through v8 code but if anyone knows.. On Apr 8, 3:42 am, Anton Muhin <[email protected]> wrote: > You were hit by some implementation details of v8. > > This kind of customization ideally should be performed in JS. Or you > can just putArray.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"arrayis 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::Arraywith > >> ObjectTemplate. > > >> However, it's easy to installArrayfunctions to any kind of > >> object---ECMAScriptarrayfunctions 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:Arraythat 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::Arraywith 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 typearray, 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
