Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous WasmThemis initialization #507

Merged
merged 1 commit into from
Jul 23, 2019

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Jul 23, 2019

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.

It turns out that WebAssembly is compiled and loaded asynchronously [1].
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.

[1]: https://emscripten.org/docs/compiling/WebAssembly.html#wasm-files-and-compilation
@ilammy ilammy added the W-WasmThemis 🌐 Wrapper: WasmThemis, JavaScript API, npm packages label Jul 23, 2019
@ilammy ilammy requested a review from vixentael July 23, 2019 13:38
@ilammy ilammy requested a review from Lagovas as a code owner July 23, 2019 13:38
@Lagovas
Copy link
Collaborator

Lagovas commented Jul 23, 2019

we should not forget to warn users in docs that they should use themis wasm only after initialization and onRuntimeInitialized call or they will take error of usage uninitialized wasm function

@ilammy
Copy link
Collaborator Author

ilammy commented Jul 23, 2019

Sure thing, it will be in the docs.

The error message usually looks like this:

Assertion failed: you need to wait for the runtime to be ready (e.g. wait for main() to be called)

wasm-themis/src/libthemis.js:117
      throw ex;
      ^
abort("Assertion failed: you need to wait for the runtime to be ready (e.g. wait for main() to be called)") at Error
    at jsStackTrace (wasm-themis/src/libthemis.js:1067:13)
    at stackTrace (wasm-themis/src/libthemis.js:1084:12)
    at abort (wasm-themis/src/libthemis.js:5641:44)
    at assert (wasm-themis/src/libthemis.js:498:5)
    at Array.Module.stackAlloc (wasm-themis/src/libthemis.js:5328:3)
    at Object.allocate (wasm-themis/src/libthemis.js:616:29)
    at generateECKeyPair (wasm-themis/src/secure_keygen.js:125:37)
    at new KeyPair (wasm-themis/src/secure_keygen.js:110:27)

@ilammy ilammy merged commit 8c8acea into cossacklabs:master Jul 23, 2019
@ilammy ilammy deleted the wasm-async-init branch July 23, 2019 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
W-WasmThemis 🌐 Wrapper: WasmThemis, JavaScript API, npm packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants