Hi all. Bit of a generic question, but figured enough of us are on the bleeding edge that some might be able to answer or give a good example.
I’ve compiled our internal lib to WASM, and can load and run it, call various functions etc just fine. No issues there. But… I need to be able to call these function outside of the actual WASM module load - if I do so as is then the functions and many other things end up undefined since the WASM hadn’t finished loading.
I figure it needs async/await or similar on the JS side of it, but JS isn’t my strong suite.
Have to add more feature flags to enable/disable this in existing codebase,
Or, have to write yet another crate that duplicates functionality
Type support is limited (though this is good for JS)
Basically it’s a whole lot more work required when I should be able to use the existing C style exports. I just need to work out async loading. Otherwise, wasm-bindgen is looking pretty exciting!
export var Module = {
wasmBinaryFile: "test.wasm",
onRuntimeInitialized: obj => {
var wrapped = {
test_func: Module.cwrap(test_func',
'number', []),
And export the wrapped var or Module. Except I can’t access wrapped, or wasm doesn’t load quickly enough for cwrap to be available if done other ways. I’ve tried all sorts of variations on this based on how I do node.js ffi.
But, that last example of yours - I could use the onRuntimeInitialized to set a global, and wrap the WASM functions in async functions that check the global before either calling or doing a cwrap.
var wasmLoaded = false;
var Module = {
wasmBinaryFile: "./test.wasm",
onRuntimeInitialized: function() {
wasmLoaded = true;
}
};
async function wasmLoadWait() {
// Early exit
if (wasmLoaded === true) {
return true
}
let check = function() {
return new Promise( load => {
setTimeout(() => {
load(wasmLoaded);
}, 10);
});
};
// Loop until WASM is loaded
// Require an async check because the function captures the global
// as it is when the function is called - meaning infinite loops if not released
while (!await check()) {}
// will not exit until check() is true
}
So with this, you can wrap a WASM function and include a call to await wasmLoadWait(); at teh start of the function to ensure that the function doesn’t return unless the WASM is loaded, thereby eliminating undefined. Eg: