Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the best <a href="http://web.archive.org/web/20150217015842/http://create.tpsitulsa.com/blog/2009/01/29/v8-objects" rel="noreferrer">blog post I could find on exposing C++ objects to V8 Javascript</a>. It goes into deeper detail and breaks it down into smaller steps with code snippets. Be warned: the code snippets have little inconsistencies and it took me several reads to understand. Reading my brief summary beforehand may help:</p> <ol> <li><strong>Objects must be wrapped in V8 templates.</strong> Note: The Google sample uses ObjectTemplates, but the author explains why he prefers FunctionTemplates. <ol> <li>Create a FunctionTemplate. Instances of this template have an internal field to store the memory address of the C++ object. They also get the class' accessor methods.</li> <li>Make a function wrapObject() that will wrap a C++ object in one of these FunctionTemplates.</li> </ol></li> <li><strong>The constructor must also be wrapped in a (different) V8 template.</strong> A different template is used to avoid unwanted recursion. (A method of combining both templates into one is described at the end of the blog post.) <ol> <li>Create another FunctionTemplate. This template simply connects the JavaScript global scope (where <code>new</code> will be called from) to the C++ constructor.</li> <li>Make the method that the template will call. This method actually uses the C++ <code>new</code> operator and calls the C++ class constructor. It then wraps the object by calling the wrapObject() method created in step 1.2.</li> </ol></li> </ol> <hr> <p>Now, the memory allocated in step 2.2 must be <code>delete</code>'d some time. <strong>Update:</strong> The next blog entry, "<a href="http://web.archive.org/web/20140727091841/http://create.tpsitulsa.com/blog/2009/01/31/persistent-handles/" rel="noreferrer">Persistent Handles</a>," covers this in detail.</p> <p>My notes on the <a href="http://bazaar.launchpad.net/~alex.iskander/create/trunk/annotate/head%3A/Script/Source/Environments/V8/V8Context.cpp" rel="noreferrer">actual code alluded to in these blog posts</a>:</p> <ul> <li>The <code>wrapPoint()</code> method in the blog is actually analogous to the <code>unwrap()</code> method in the actual code; <em>not</em> <code>wrap()</code></li> <li>To find other common points between the code, search for: <code>SetInternalFieldCount(0</code>, <code>constructorCall</code></li> <li>The actual code seems to do memory management by using the MakeWeak() method to set a callback method that does the cleanup.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload