This might be a little tricky. Overall, here is how EM_ASM works:
1. EM_ASM is a C macro. It gets turned into emscripten_asm_const(param)
where param is a C string containing the code.
2. In the asm.js backend, we find those calls and emit something else,
emscripten_asm_const(id) where id is a numeric id of the code, and the
codes and their ids arrive as metadata
3. emscripten.py receives all that, and emits in JS the table of id =>
code. Then when asm.js executes emscripten_asm_const(id), in JS we call the
right thing in the table.
It might be tricky to add to that. An alternative is to add a JS API. Maybe
something like this could be written:
EM_ASM({
Module.runOnMainThread(function(arg1, arg2) {
.. some code ..
}, arg1, arg2)
});
in this case EM_ASM itself is not changed. And runOnMainThread would just
send the code to the main thread as a string, where it would be run.
The risky thing is that sending code means we don't have closure capture of
variables. So input to that code would need to be passed as explicit
parameters as in the example (arg1 etc). Not sure i like that. It's a
general problem of calling to workers and back, though, not sure what
better we can do...
On Mon, May 2, 2016 at 3:57 PM, Kerby Geffrard <[email protected]>
wrote:
> I'm looking to do something like this now, but I can't see where I should
> start my search. I found that EM_ASM is a define of an other fonction
> insite of `emscripten.h` but I don't know where I have to look to start to
> implement an `EM_ASM_MAIN_THREAD` macro.
>
> Le dimanche 24 avril 2016 04:08:38 UTC-4, jj a écrit :
>>
>> Something that I've been thinking about is if we did a
>> EM_ASM_MAIN_THREAD(...) macro that would be identical to EM_ASM, except
>> that when a pthread executes it, it would proxy the EM_ASM call to the main
>> thread to execute, and then pause to wait for the main thread to report
>> back. This might allow these kind of EM_ASM_MAIN_THREAD(alert('message'));
>> situations at least, where performance is not a concern (e.g. debugging,
>> startup, critical failures, ..). This could also work in a limited fashion
>> in non-pthreads builds when --proxy-to-worker is used.
>>
>> As a sidenote, console.log() and console.error() are already posted from
>> pthreads to the main thread, so those can be freely used in EM_ASM blocks
>> in pthreads and should work.
>>
>> 2016-04-22 20:45 GMT+03:00 Alon Zakai <[email protected]>:
>>
>>> If I understand the question, it sounds like you want to write a method
>>> in JavaScript in your HTML, and have it be executed when a worker is
>>> created? Do you want it to run in the HTML (on the main thread) or in the
>>> worker?
>>>
>>> If in HTML, then you can modify the code that creates workers (not sure
>>> where, but looking for new Worker() in src/ should find it). Or, you might
>>> just patch Worker itself in the main scope, replace it with another method
>>> that wraps around it. Overall I don't think we have a nice way to do that
>>> right now.
>>>
>>> If in the worker, then you can't directly do that - functions can't be
>>> passed from workers, only JSON-type data.
>>>
>>> On Thu, Apr 21, 2016 at 7:55 AM, Kerby Geffrard <[email protected]>
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> I was wondering if there's a way to attach any function to any worker
>>>> that Emscripten create. I want to do that because I want to add an event
>>>> listener to all of my threads.
>>>>
>>>> What I would like to do:
>>>>
>>>>
>>>> - I want to be able to add a function in my shell.html (Let's call
>>>> it *onNewWorker*)
>>>> - And when emscripten create a Worker because of a pthread in my
>>>> project, *onNewWorker *is called with my worker passed in argument
>>>> - So since inside of my *onNewWorker *function, I can add event
>>>> listener with my worker, all my pthread have the event listener that I
>>>> want
>>>>
>>>>
>>>> I want to do this because I need to add an event listener for when my
>>>> C++ thread send message with EM_ASM*. This is needed because I want to send
>>>> a message from my thread to the main thread that trigger an
>>>> `alert('message')` since we can't use alert() from workers.
>>>>
>>>> --
>>>> 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.
>>>
>>
>> --
> 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.