V8 has two types of handles local and persistent. The local handles are used
for local operations whereas the persistent handles can be stored and used
for different operations. Therefore in you struct you should use explicitly
use persistent handles.
struct Foo
{
 Persistent<...> foo;
 Persistent<...> bar;
 ...
};

Internally a persistent handle is a pointer to a memory location inside V8
where the object pointer is stored and properly processed during garbage
collection. A persistent handle needs to be explicitly deallocated using the
Dispose method. Note that it is possible to have several persistent handles
referring to the same memory location (e.g. when passing persistent handles
by value). When a persistent handle is disposed all references to it becomes
invalid.

Regards,
Søren

On Sun, Mar 15, 2009 at 2:42 PM, Stephan Beal <[email protected]> wrote:

>
> Hi, all!
>
> As i understand it, we should not create Handles and such on the heap.
> In fact, we cannot because their API won't allow it. But is it legal
> to do this:
>
> struct Foo
> {
>  Handle<...> foo;
>  Handle<...> bar;
>  ...
> };
>
> ....
>
> static Foo * shared = new Foo;
> if( shared->foo.IsEmpty() )
> {
>   ... initialize shared object ...
> }
>
> :-?
>
> My instinct tells me it's a bad idea, in particular if the Foo class
> ever wants to use the v8 API internally (it could theoretically do so
> after v8::IsDead(), post-main()).
>
> Without the ability to share instances of JS-related data around
> within an app, it becomes next to impossible for disparate C++ code to
> instantiate new JS instances of a given class. e.g. if i bind MyClass
> to JS, that binding necessarily creates a ctor function JS-side. So
> far i have found no safe way to pass that Function handle amongst
> native code in my application. i tried using a static instance of my
> binder class, but that segfaults in v8 when static initialization
> calls the default ctor. The only workaround i've come up with so far
> (but untested) is to abuse the global object and store my private
> ctors there using silly names which client code hopefully won't use.
>
> If i can safely creating the object using 'new', even though it
> contains an arbitrary amount of Handles and such, i might be able to
> use a slightly less ugly hack to share my ctors (and to do so type-
> safely, so the callers don't have to do so much error checking).
>
> :-?
>
> >
>

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

Reply via email to