On Mon, Apr 18, 2011 at 5:25 PM, quotient7 <[email protected]> wrote:

> 1) I want to create a JavaScript "class" (prototype), that
> instantiates a C++ class. So for example, if script makes: "new
> MyClass(blah)", in C++ there should be also a class instantiated. The
> pointer should be saved and connected with the JS class, and should be
> deleted when the JS class gets deleted (I think the GC deletes JS
> classes). How can I detect in C++ WHEN the JS class gets deleted?
>

By using the so-called "weak pointer callback."

http://code.google.com/apis/v8/embed.html

however, that doc only briefly discusses the binding process. 99% of what
you need to know, you'll have to figure out by trial and error or by looking
closely at other people's code which use the WeakPointer mechanism. For
example:
http://code.google.com/p/v8-juice/source/browse/convert/include/v8/convert/ClassCreator.hpp
Don't bother searching for extensive documentation from the v8 project - you
won't find any.


> 2) Another thing is to always work with the same class. Script makes
> "new MyTest(blub)" and should work with the same C++ instance
> internally. If the script calls "new MyTest(blub)" again, it should
> use the same class again. How can I do that?
>

Why would v8 use a different _class_ for two calls to (new MyTest())??? Do
you mean the same INSTANCE? If so, you can arrange for that to happen via
the weak-binding mechanism, by simply binding the same instance of to each
JS object (and making sure you don't delete that instance in every
destructor).

Also be VERY AWARE that v8 DOES NOT guaranty that garbage collection will
EVER happen. That means that there is (sadly) no 100% guaranty that the C++
destructors for JS/C++-bound native types will ever be called. This can be
problematic for types which have very specific destruction requirements.

-- 
----- stephan beal
http://wanderinghorse.net/home/stephan/

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

Reply via email to