Return value for EmterpreterAsync

104 views
Skip to first unread message

Lu Wang

unread,
Apr 10, 2015, 6:47:58 AM4/10/15
to emscripten-discuss
Hi all,

   Is there a way to return something from an async function with emterpreter? For example:

///main.c
void f() {
  printf("%d\n", g());
}

///main.js
function g() {
  EmterpreterAsync.handle(function(resume) {
    setTimeout(function() {
      resume(return_value); // ???
    }, 1000);
  });
}


   I saw that `resume` already takes a few parameters, so maybe we cannot simply do so. Currently the closet way is to write the return value into some memory address, but it would be better if the return value can be passed directly to the C code.


  regards,
  - Lu

Alon Zakai

unread,
Apr 10, 2015, 5:07:37 PM4/10/15
to emscripte...@googlegroups.com
I think if you add a return in g - outside of the handle() call - it will just be returned. It will however be returned both the first time when called (and starting to unwind the stack) and the second time when restarted (after reconstructing the stack). You could tell which of those you are in using EmterpreterAsync.state. Might be nicer to add an API for that.

- Alon


--
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 emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

王璐

unread,
Apr 10, 2015, 11:37:43 PM4/10/15
to emscripte...@googlegroups.com
Did you mean something like this?

EmterpreterAsync.handle(...);
return something;


But the return value might not be available until the callback, e.g. to get a key input, so I need to set the return value in `resume`.



regards,
- Lu
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Alon Zakai

unread,
Apr 12, 2015, 7:06:00 PM4/12/15
to emscripte...@googlegroups.com
Yes, and resume occurs in fact before you reach the end of that method, on the *second* time it is called, when it is restarted. So if you save the return value somewhere, you can just return it there. But, this is confusing.

So we should have a proper API for this. I implemented one in

https://github.com/kripken/emscripten/commit/d80417c665dd45e1893cb85aa8523efe57c7d58c

, see the testcase there. The resume() function has a post argument, which runs right after the stack was recreated and we are about to finish the async operation. That means it is right before the async-causing function exits, so it is a proper time to return a value. I made it so return values from that post will be returned. Note that you need to return EmterpreterAsync.handle() for that to work, as in the testcase in that commit.

- Alon


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Lu Wang

unread,
Apr 12, 2015, 11:58:38 PM4/12/15
to emscripten-discuss
Thanks! Now it looks more intuitive now, let me try it, hopefully it should make it easier and less buggy to go back-and-forth between C and JS.


regards,
- Lu

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/z2_ScsGrfrA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.

王璐

unread,
Apr 13, 2015, 7:19:14 AM4/13/15
to emscripte...@googlegroups.com
So now what I understood is, `Emterpreter.handle` should occupy the whole function, or other parts might be execute twice?
For example:

function f() {
  do_something();
  EmterpreterAsync.handle(..);
  do_something_else();
}

Then both do_something/do_something_else might be called twice (unexpected).

Further, if C code calls a 'nested' EmterpreterAsync.handle, like this

func_in_C -> func1_in_js -> ... -> funcN_in_js -> EmterpreterAsync.handle,

so everything in the chain will be called twice, as EmterpreterAsync only interprets the C code. Am I right?
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Alon Zakai

unread,
Apr 13, 2015, 2:21:19 PM4/13/15
to emscripte...@googlegroups.com
Yes.

The model is that you call f normally the first time. Then we pause execution and leave it, returning up the stack. Then later we recreate the JS stack; to do so, we have to re-do all the function calls. So we call into f a second time. handle() knows which time it is, and the code inside it will execute only once. Basically you should just call handle and return it's return value if you want that.

- Alon


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

王璐

unread,
Apr 14, 2015, 3:59:45 AM4/14/15
to emscripte...@googlegroups.com
I've incorporated the new way into my game. All the `while(blocking)sleep();  get_result()` patterns have been replace by `EmterpreterAsync.handle(..)`
I do feel that the program is a bit more responsive.

More issues I've found:

postRun no loner works, is there a standard way to inject functions upon exit?

Seems that sometimes `exit` termintates emterpreter, but sometimes it does not. Shall I use emscripten_force_exit?


Thanks a lot!


regards,
- Lu

王璐

unread,
Apr 14, 2015, 4:50:47 AM4/14/15
to emscripte...@googlegroups.com
I see, so about the questions, right now I'm using emscripten_force_exit in the place where exit is called,
and I hijacked that function to do some finalizing works.

However emscripten_force_exit is not available in JS (after optimized), so I just set noExitRuntime and call exit() myself.


- Lu

Alon Zakai

unread,
Apr 14, 2015, 4:32:50 PM4/14/15
to emscripte...@googlegroups.com
I suspect postRun is not much tested actually. Perhaps we should deprecate it? Or look at a testcase to fix.

Regarding exit not always exiting the emterpreter, a testcase would be good.

- Alon


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Lu Wang

unread,
Apr 14, 2015, 10:11:06 PM4/14/15
to emscripten-discuss

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/z2_ScsGrfrA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages