AFAIK there is no way to install API indexed interceptor on Array.
The main reason is probably nobody needed that before.  I think unless
you'd like to mess up with v8 internals and enable this feature, your
best bet is to adjust problematic functions.   And they are pretty
easy to implement.

BTW, v8 intentionally violates ECMAScript 5 spec here to be compatible
with Safai.  If you persuade them to change the behaviour, v8 will
quickly do that as well :)

yours,
anton.

On Fri, Apr 15, 2011 at 4:48 AM, ajg <[email protected]> wrote:
> so I'm still having issues even after copying prototype from an array
> into the object.
>
> // creating object from a template with interceptors set
> o = getInternalFieldArray()->NewInstance();
> // copying prototype from array
> o->SetPrototype(v8::Array::New(1)->GetPrototype());
>
> Then all functions from the array are available, but v8 functions do
> certain checks to make sure object really is an array.
> For example o.tostring() will fail.
> The fact that object is not of type array also creates other issues
> for me when I need to serialize it later on, it would be much easier
> if IsArray() would return true.
> Is there any other way of getting a true v8 array with index
> interceptors set?
> Anyway to change the type of a value manually?
> It seems like a strange hole in the API.
> If not I guess I'll need to override methods like tostring, but it's a
> pain to maintain.
> thx
>
> On Apr 14, 8:21 am, ajg <[email protected]> wrote:
>> thx that makes sense, just a bit unexpected.
>> I have a pure object and enumerate keys with namedenumerator, but then
>> if a key can be parsed into number the property gets requested has an
>> index.
>> It is ok since my C++ object is actually always a map, but it could
>> become confusing if I was expecting anarraybacking in IndexGet.
>>
>> On Apr 14, 5:40 am, Anton Muhin <[email protected]> wrote:
>>
>> > ajg,
>>
>> > If string can be converted to an index, it's treated as index and goes
>> > through IndexedPropertyGetter and friends.
>>
>> > yours,
>> > anton.
>>
>> > On Thu, Apr 14, 2011 at 9:14 AM, ajg <[email protected]> wrote:
>> > > 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 anarray.
>> > > 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 Icreatea JS object from anObjectTemplatethat 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
>> > >v8just 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 thatv8has some specific behavior if the keys are numbers,
>> > > maybe it is confused and think it's anarray.
>> > > I started going throughv8code but if anyone knows..
>>
>> > > On Apr 8, 3:42 am, Anton Muhin <[email protected]> wrote:
>> > >> You were hit by some implementation details ofv8.
>>
>> > >> 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)createv8::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 workingObjectTemplatethat enables js code to 
>> > >> >> > access
>> > >> >> > C++ objects in a lazy fashion, using the NamedPropertyHandler.
>> > >> >> > Now I also need tocreatev8:Arraythat is backed by a C++ object
>> > >> >> > using the IndexedPropertyHandler.
>> > >> >> > But it seems thatObjectTemplatecan onlycreateav8::Object, not a
>> > >> >> >v8::Array.
>> > >> >> > Doing an indexed access onv8::Object works fine but then the 
>> > >> >> > built-in
>> > >> >> > functions like "forEach" are not available.
>> > >> >> > Basically I need to either:
>> > >> >> > -createav8::Arraywith IndexedPropertyHandlers
>> > >> >> > -createav8::Object with IndexedPropertyHandlers and transfer the
>> > >> >> > builtin methods to it, or somehow convert it to av8::Array.
>> > >> >> > Note that on C++ side it is not a native typearray, so I cannot use
>> > >> >> > thev8::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
>>
>>
>
> --
> 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