Asynchronous WasmThemis initialization #507
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It turns out that WebAssembly is compiled and loaded asynchronously. There is an option for synchronous compilation, but Chrome and Electron support only asynchronous loading. This means that one cannot simply call C functions. We need to wait for WebAssembly to be compiled and Emscripten runtime to boot. This is performed automatically if the whole application is compiled by Emscripten, but Themis is a library.
Emscripten provides an
onRuntimeInitialized
callback which is called when Emscripten startup is complete and C functions can be called. Expose an interface to this callback as JavaScript promise which is resolved once initialization is complete. The users should call Themis functions only after this promise is resolved. (Alternatively, they can wait for a second or two.)This behavior is not reproduced by unit tests because Mocha launches asynchronously too and it manages to start testing after WasmThemis loading is complete. But we can test that the promise is resolved.