Yes, you'd usually either store a Persistent in the C++ class itself or
maintain a map.

The Persistent would usually be weak, so you get notified if the object is
no longer needed by V8, and you can create new Local handles to the js
object when needed.

A common pattern to get from the js object to the C++ object is to store
the pointer to the C++ object in one of the internal fields
(SetAlignedPointerInInternalField/GetAlignedPointerInInternalField). In a
callback, you can use args.Holder() to get to the js object, and then read
the pointer from the internal field.

It's also good practice to store some kind of type id in another internal
field on the same object, so you can verify that the pointer actually
points to a C++ class of the type you expect.

best
-jochen


On Wed, Jan 29, 2014 at 1:24 AM, Pierre Saunier <
pierre.saun...@ppmodeler.com> wrote:

> It is very simple:
> I want to set javascript callback functions where the first parameter is a
> javascript object.
> so I have a C++ object, let say a button. When the button is pressed, a
> callback function is called with the button in parameter.
> In my v8 code, I create an persistent object template for the button,
> store the button address in the 1st internal field and somewhere else I add
> a method to create a button.
> the button object template has a method to set a callback.
> in this setCallback method, I store a persistent with the function in
> parameter (it will be called outside the current c++ method, so after the
> release of the handlescope), a persistent with the global object, a
> persistent with the v8 button object. the problem is with the last one. my
> method to set the callback is called with an local handle on the v8 object
> in parameter (retrieved with: v8::Local<v8::Object> self = args.Holder();).
> I have no way to go from this Local to the persistent I created first, so I
> need to create another persistent from the local, or I managed to find a
> way to retrieve the persistent from the c++ object (for which i have the
> address in the 1st internal field).
> Is that more clear?
>
>  --
> --
> v8-users mailing list
> v8-users@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to