Hello Alon & Joel,

   I think that c++/js promise is more like syntax sugar, I mean you are 
still writing async programs, but the syntax allows you to write in a sync 
way, which could be more comfortable. I doubt if 
`emscripten_sync_wget2_data` is a simple solution as long as you want a 
real sync function in C++, and I'm really interested if you make it works 
without an overhaul of other parts of your program.
  
   I also learned a little bit about generators before, I'm not sure if it 
will work deep inside the calling chain: For example, if A calls B and B 
calls C, where C is a generator, I doubt if it is possible for A to control 
(pause/resume) B & C, without letting B be aware of it. If this is not 
possible, it might be necessary to rewrite all the functions to handle 
generator pause/resume events, so essentially we are still writing async 
C++.

   No matter what library/syntax you are using, async C++ can always be 
translated with emscripten out-of-the-box, but it might be better to use 
the JS engine instead of a separate event loop implemented in C++ -- maybe 
this is what you meant in `emscripten_sync_wget2_data`?


   On the other hand, if you want a real sync C++ which is then magically 
transformed into async JS, AFAIK one possible solution is to mark async 
function in JS, then transform it using streamline.js or similar tools, 
this is what I have done with vim.js. But it's too hacky, and might not 
work well with the optimization phases in emscripten (reloop, ASM.js etc), 
so it might not be worth it for a large project.

   Hopefully we will have an elegant and effective solution after the 
summer.


   regards,
   - Lu 

  

On Sunday, March 16, 2014 4:43:40 PM UTC+8, Joel Croteau wrote:
>
> I would like to have a function that makes an async call, and returns 
> control to the browser during the call, then resumes once the async call is 
> complete. However, I would like this to appear to the calling code to be a 
> synchronous call. So for instance, I could do something like this:
>
> struct FileInfo
> {
>     void* buffer;
>     unsigned size;
> };
>
> FileInfo loadUrl(const char* url)
> {
>     FileInfo info;
>     emscripten_async_wget2_data(url, "GET", "", &info, false, onload, 
> onerror, onprogress);
>     emscripten_pause(); // Blocks until emscripten_resume() is called
>     return info;
> }
>
> void onerror(void* ptr, int code, const char* desc)
> {
>     // Handle not found, etc.
> }
>
> void onprogress(void* ptr, int loaded, int total)
> {
>     // Update progress bar
> }
>
> void onload(void* ptr, void* buffer, unsigned size)
> {
>     static_cast<FileInfo*>(ptr)->buffer = buffer;
>     static_cast<FileInfo*>(ptr)->size = size;
>
>     emscripten_resume(); // Now loadUrl resumes and returns FileInfo.
> }
>
> Is there any way to do this?
>

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