emscripten-core / emscripten-core/emscripten
Asyncify returns immediately even when called indirectly
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Hello, I have some problems with Asyncify, let's go straight to the code:
In **Bindings.h**:
```C++
class Factory
{
public:
Factory ();
IDecompress* NewDecompressor ();
int GetLastError ();
private:
int last_error;
};
```
In **Bindings.cpp**:
```C++
IDecompress* Factory::NewDecompressor ()
{
// StdIO* my_io = new StdIO ("...");
//
// Where StdIO uses standard IO library and virtual file system (via "--preload-file")
//
// ** WORKS FINE **
//
CppIO* my_io = new CppIO ("source-file", 0); // CppIO uses Asyncify.handleSleep()
IDecompress* ret = CreateDecompressor (my_io, &last_error);
printf ("NewDecompressor = %d\r\n", ret);
return ret;
}
```
In **Bindings.idl**:
```
interface IDecompress
{
};
interface Factory
{
void Factory ();
IDecompress NewDecompressor ();
long GetLastError ();
};
```
In **index.html/Bindings.js**:
```Js
var factory = new Module.Factory ();
console.log ("factory:");
console.log (factory);
//
// Code above is fine!
//
var decompressEx = factory.NewDecompressor ();
console.log ("decompressEx:");
console.log (decompressEx);
//
// Code above return decompressEx with ptr: 0
//
```

CreateDecompressor function strongly uses CppIO class. Inside CppIO class, all the reads are made via Asyncify.handleSleep(). CreateDecompressor works fine, calls are made synchronously the problems is JavaScript that doesn't "wait until" CreateDecompressor() is returned.
Compiled with:
```
python webidl_binder.py Bindings.idl WebIDL
emcc -O0 -std=c++98 --post-js WebIDL.js -s WASM -s WASM_BIGINT -s ENVIRONMENT=web -s ASYNCIFY -s "ASYNCIFY_IMPORTS=['read_file_chunk', 'get_file_size']" -s "EXPORTED_FUNCTIONS=['_CreateDecompressor']" -o Bindings.js --preload-file injectedFiles\ Bindings.cpp WebIDL-Wrapper.cpp
```
What I'm doing wrong? Thank you
(I'm an enscripten newbyte, please have mercy on me 😅)
PS - "read_file_chunk" is the Asyncify function, the project doesn't work (_js code invoked from index.html returns immediately_) even with the simplest form:
```C++
EM_JS (void, read_file_chunk, (char *id, int index, long long position, unsigned int length, void* destination),
{
Asyncify.handleSleep (function (wakeUp)
{
console.log ("BEFORE read_file_chunk");
setTimeout (wakeUp, 1);
console.log ("AFTER read_file_chunk");
});
});
```
Contributor guide
Assessment
This issue has not been assessed yet.