Two things:

First, you need to make sure the C runtime doesn't shut down when main()
exits -- see
https://kripken.github.io/emscripten-site/docs/api_reference/module.html#Module.noExitRuntime

Second, you need to make sure your object doesn't go out of scope and get
deleted at the end of the main() function. You'll have to store it in a
global variable or something.

-- brion

On Fri, Mar 4, 2016 at 9:57 AM, Andrii Heonia <[email protected]> wrote:

> How can I call C++ callback asynchronously from JavaScript?
>
>
> This is my JS code:
>
>
> <script type="text/javascript">
>   function sendRequest(callback) {
>     setTimeout(function(){
>       callback["sayHi"]();
>     }, 100);
>   }
> </script>
>
>
> This is my C++ code:
>
>
> #include <emscripten/emscripten.h>
> #include <emscripten/bind.h>
>
> using namespace emscripten;
> class MyClass {
>   public:
>     void sayHi () {
>       printf("Hello! \n");
>     };
> };
> EMSCRIPTEN_BINDINGS(MyClass)
> {
>   class_<MyClass>("MyClass")
>     .function("sayHi", &MyClass::sayHi);
> }
>
> int main() {
>   val window = val::global("window");
>   auto myObj = MyClass();
>   window.call<void>("sendRequest", myObj);
>   return 0;
> }
>
>
> When I execute this code in browser it fails with error:
>
> Uncaught BindingError: Cannot pass deleted object as a pointer of type
> MyClass*
>
>
> I use emcc 1.35.22 and compile it with this command:
>
> ~/app/emsdk_portable/emscripten/tag-1.35.22/emcc main.cpp --bind -o out.js
>
>
> Thank you in advance!
>
> --
> 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.
>

-- 
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