title: How to call a javascript callback from C++
body: <p>I'm trying to call a callback in V8 from another point of my function. So this piece of code register the callback:</p>
  
  <pre><code>        if (args.Length())
          {
              String::Utf8Value event(args[0]-&gt;ToString());
              if (event.length())
              {  
                  Isolate* isolate = V8Interface::getCurrent()-&gt;getIsolate();
  
                  Locker locker(isolate);
                  HandleScope scope(isolate);
  
                  callback cb = callback(isolate, Local&lt;Function&gt;::Cast(args[1]));
  
                   if(!events.count(*event))
                   {
                  events[*event] = callbacks({ cb });
                   } 
                   else 
                  {
                      events.find(*event)-&gt;second.push_back(cb);
                   }
              }
          }
  </code></pre>
  
  <p>And this one calls it:</p>
  
  <pre><code> void trigger(std::string event)
      {
  
          Isolate* isolate = V8Interface::getCurrent()-&gt;getIsolate();
  
          Locker locker(isolate);
          HandleScope scope(isolate);
  
          if(events.count(event))
          {
              for(callback cb : events.find(event)-&gt;second)
              {
                  Local&lt;Function&gt; local = Local&lt;Function&gt;::New(isolate, cb);
                  local-&gt;Call(isolate-&gt;GetCurrentContext()-&gt;Global(), 0, {});
              }
          }
      }
  </code></pre>
  
  <p>The trigger function can be called anywhere at any time in my C++ code. I tried a simple example (init v8 then call trigger), and I get:</p>
  
  <pre><code>#
  # Fatal error in C:\OgreSDK\Projects\whitedrop\third_party\io.js\deps\v8\src/api.h, line 386
  # CHECK(allow_empty_handle || that != 0) failed
  #
  </code></pre>
  
  <p>This is my main:</p>
  
  <pre><code>Scribe::V8Interface v8Interface = Scribe::V8Interface();
  v8Interface.initialize();
  
  for(Whitedrop::WorldEvent* event : Whitedrop::worldEvents)
  {
      event-&gt;onStart();
  }
  </code></pre>
  
  <p>You can get the whole source here:</p>
  
  <p><a href="https://github.com/whitedrop/whitedrop/tree/feature/v8" rel="nofollow">https://github.com/whitedrop/whitedrop/tree/feature/v8</a></p>
  
  <p>Line 386 is:</p>
  
  <pre><code>/**
     * Create a local handle for the content of another handle.
     * The referee is kept alive by the local handle even when
     * the original handle is destroyed/disposed.
     */
    V8_INLINE static Local&lt;T&gt; New(Isolate* isolate, Handle&lt;T&gt; that); // &lt;&lt;&lt;&lt;&lt;&lt;
    V8_INLINE static Local&lt;T&gt; New(Isolate* isolate,
                                  const PersistentBase&lt;T&gt;&amp; that);
  </code></pre>
  
  <p>EDIT: I tried, thanks to Ben Noordhuis, like this:</p>
  
  <pre><code>Isolate* isolate = V8Interface::getCurrent()-&gt;getIsolate();
  
  Locker locker(isolate);
  HandleScope scope(isolate);
  
  callback cb;
  cb.Reset(isolate, Local&lt;Function&gt;::Cast(args[1]));
  </code></pre>
  
  <p>And to call:</p>
  
  <pre><code>Isolate* isolate = V8Interface::getCurrent()-&gt;getIsolate();
  
  Locker locker(isolate);
  HandleScope scope(isolate);
  
  if(events.count(event))
  {
      for(callback cb : events.find(event)-&gt;second)
      {
          Local&lt;Function&gt; local = Local&lt;Function&gt;::New(isolate, cb);
          local-&gt;Call(isolate-&gt;GetCurrentContext()-&gt;Global(), 0, {});
      }
  }
  </code></pre>
  
  <p>But same error :'(</p>