Hello Charles,

This is demo with global callback:
https://github.com/AndriiHeonia/async-emcc/tree/7207aebdcc65b5d183f1a26516ccdd77eb06ce8e
and
it also doesn't work. My callback dies before async call even if it is
global (and I don't get how to keep it alive)

Best regards,
Andrii

On Mon, Mar 7, 2016 at 4:57 PM, Charles Vaughn <[email protected]> wrote:

> Hello ANdrii,
>
> Your code flows like this:
>
> 1. Emscripten's main is called
> 2. myObj is created within the scope of main
> 3. sendRequest is called with myObj as its parameter
> 4. sendRequest sets a timeout to call "sayHi" on myObj
> 5. main returns, destroying myObj
> 100ms later
> 6. The timeout fires, trying to access myObj
>
> You can create myObj with a static lifetime or heap allocate it and return
> a smart pointer.
>
>
> On Monday, March 7, 2016 at 3:49:10 AM UTC-8, Andrii Heonia wrote:
>>
>> Hello Brion,
>>
>> Thank you for your answer. I've changed a bit my example (now
>> noExitRuntime=true and callback is global) but it still doesn't work. My
>> callback dies before async call (and I don't get how to keep it alive). You
>> can find my code here
>> <https://github.com/AndriiHeonia/async-emcc/tree/7207aebdcc65b5d183f1a26516ccdd77eb06ce8e>.
>> What I'm doing wrong?
>>
>> Thanks in advance!
>>
>> On Friday, March 4, 2016 at 7:45:15 PM UTC+1, Brion Vibber wrote:
>>>
>>> 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 a topic in the
> Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/emscripten-discuss/8bzdVaR_lEE/unsubscribe
> .
> To unsubscribe from this group and all its topics, 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