One possible use case of multiple internal fields is to use the first  
as a specific "type identifier" object, and ALWAYS do so, and the  
second as the actual external.

This way, you can check type by checking the type identifier in field  
0 (which you could even have be a v8::Integer for performance), and if  
the type is correct, go on and use the external in internal field 1.

I use a variation of this technique, but with only one external field:  
I have a "native" class that imitates the class hierarchy of the  
actual C++ classes. No matter what is stored in the external field, it  
is guaranteed to be one of these "native" objects, so I can  
static_cast it to one. Then, I can dynamic_cast to validate the type.

Alex

On Mar 28, 2009, at 5:49 AM, Stephan Beal wrote:

>
> On Sat, Mar 28, 2009 at 11:47 AM,  <[email protected]> wrote:
>>> var foo = new Foo();
>>> var bar = new Bar();
>>> bar.somefunc = foo.somefunc;
>>> bar.somefunc()
>>>
>>> segfault! (or at least an error)
>>
>> Hmm.. but shouldn't a simple check on IsExternal() work out?
>> Anyway, my objects can not be constructed so that should be safe,
>> at least I hope so :-)
>
> What if Foo and Bar both hold an External at slot 0? Then IsExternal()
> will work, External::Cast() will work, but static_cast<Foo*>() won't
> work. In my experience, only one internal slot is ever used, so all
> bound classes are using slot 0 (i have yet to see a use case which
> would be simplified by using more than one slot).
>
> -- 
> ----- stephan beal
> http://wanderinghorse.net/home/stephan/
>
> >

Alex Iskander, TPSi






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

Reply via email to