This would be pretty easy to do with nbind 
<https://github.com/charto/nbind#readme> if you first call some C++ 
function with pointers to all JavaScript the functions you need to call. 
Note that the following code currently only works when compiled with 
Emscripten.

Your C++ code could for example have a main init function that gets called 
from JavaScript and after that, you can have most of the action happen in 
C++ if you want. In this example, init receives one JavaScript function it 
can then call at any time. init also instantiates a C++ object and passes 
that to the JavaScript function.

#include <string>
#include <iostream>

#include "nbind/nbind.h"

struct Foobar {
  void frob(std::string data) {
    std::cout << data << "\n";
  }
};

void init(nbind::cbFunction &jsFunction) {
  Foobar *foo = new Foobar();

  jsFunction(foo);
}

#include "nbind/nbind.h"

NBIND_CLASS(Foobar) {
  method(frob);
}

NBIND_GLOBAL() {
  function(init);
}


Now let's see the JavaScript side, which calls the C++ init function and 
can give it pointers to JavaScript functions. The example function here 
takes a C++ object, and calls the frob method after some delay, passing it 
a string.

var nbind = require('nbind');
var lib = nbind.init().lib;

function something(foo) {
  setTimeout(function() { foo.frob('Hello, World!'); }, 1000);
}

lib.init(something);


Hopefully this would fit your needs. The same code should also work in a 
future 0.3 version of nbind when compiled using GCC, Clang or Visual Studio 
2015 into a native Node.js addon. Currently however passing pointers to 
objects constructed in C++ to JavaScript is only supported when compiling 
with Emscripten.

tiistai 21. kesäkuuta 2016 20.00.14 UTC-5 PSN kirjoitti:
>
> I would like to instantiate an object in C++ and have a Javascript onload 
> function callback to one of the instantiated objects methods. The plan 
> would be to call the Javascript code from C++ and then have the Javascript 
> callback to the object function. What is the best way to do this? I've been 
> looking at EMBind, but I don't want to instantiate in Javascript. Thanks!
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to