I'm using emscripten to generate a file a.wasm.js that contains functions in C++ for encryption. The webassembly functions take in and return C-strings that hold JSON data. I want to wrap these asm.js functions for more convenient use in my client application. Unfortunately doing it directly inside body.onload doesn't seem to work so I'm using the following hack:
<script>
!function(e, t){
console.log("Loading client...");
var n = "a.wasm.js";
if(!e.WebAssembly){
n = "a.js"
}
console.log("Script set to " + n);
var o = t.createElement("script");
o.async = true, o.type = "text/javascript", o.src = n, o.onerror = function(t) {
console.error("Script Error"), console.error(t), setTimeout(function() {
e.location.reload(!0)
}, 3e3)
};
var r = t.getElementsByTagName("script")[0];
r.parentNode.insertBefore(o, r)
}(window, document);
</script>
<script>
setTimeout(function(){
let _generateKeyPair = Module.cwrap('generateKeyPair', 'string', null);
window.generateKeyPair = function() {
return JSON.parse(_generateKeyPair());
}
}, 1000)
</script>
Of course, the problem here is that I cannot use "generateKeyPair" until 1 second after the app is loaded.
Is there any callback I can hook in to to know that the asm.js Module object has fully loaded and I can start to use the functions defined within it?
One approach is to define and await a
Module.readyPromise. This is injected during Emscripten compilation using the--post-jsflag. (see full examples)