title: How to store persistent handles in V8?
body: <p>I want my class to hold a <code>v8::Context</code> and a <code>v8::External</code> as members. Therefore, I thought I had to use persistent handles.</p>
  
  <pre><code>class ScriptHelper {
  public:
      ScriptHelper(v8::Persistent&lt;v8::Context&gt; Context) : context(Context) {
          // ...
      }
      // ...
  private:
      v8::Persistent&lt;v8::Context&gt; context;
      v8::Persistent&lt;v8::External&gt; external;
  };
  </code></pre>
  
  <p>However, persistent handles are non copyable in V8, so the code does not compile. The error occurs in the lines where the two memberes get initialized. For the context, this is in the initializer list of the constructor, for the external this is inside the constructor body.</p>
  
  <blockquote>
    <p>1> error C2440: '=' : cannot convert from 'v8::Primitive *' to 'v8::Object *volatile '<br>
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast<br>
    1> include\v8\v8.h(603) : see reference to function template instantiation 'void v8::NonCopyablePersistentTraits::Uncompilable(void)' being compiled</p>
  </blockquote>
  
  <p>I thought about using pointers to persistent handles but that seems counter intuitive since the concept of handles already implies some kind of reference. Moreover, I think the handles would get destructed then so that V8's internal garbage collector could clean up the objects.</p>
  
  <p>How can I store V8 objects as class members persistently?</p>
  
  <p>Update: Even if I use pointer to persistent handles, I have get same compiler errors for methods that return persistent handles.</p>