Last active
July 3, 2021 02:56
-
-
Save anthumchris/c1b5f5526b966011dac39fbb17dacafe to your computer and use it in GitHub Desktop.
Emscripten WebAssembly Module.ready Promise initialization similar to Module.onRuntimeInitialized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
emcc \ | |
-O0 `# leave uncompressed for example` \ | |
-s WASM=1 \ | |
-s EXPORTED_FUNCTIONS="['_hello']" \ | |
-s EXTRA_EXPORTED_RUNTIME_METHODS="['cwrap']" \ | |
-o emscripten-module.js \ | |
--post-js module-post.js \ | |
hello.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *hello() { | |
return "Hello there. Welcome to WebAssembly."; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Module.ready resolves when WASM instantiates. (ready is now a property and not function via @surma fork) | |
Module.ready = new Promise(function(resolve, reject) { | |
addOnPreMain(function() { | |
var api = { | |
sayHello: Module.cwrap('hello', 'string', []) | |
}; | |
resolve(api); | |
}); | |
// Propagate error to Module.ready.catch() | |
// WARNING: this is a hack based Emscripten's current abort() implementation | |
// and could break in the future. | |
// Rewrite existing abort(what) function to reject Promise before it executes. | |
var origAbort = this.abort; | |
this.abort = function(what) { | |
reject(Error(what)); | |
origAbort.call(this, what); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const worker = new Worker('web-worker.js'); | |
worker.onmessage = event => { | |
const error = event.data.error; | |
if (error) { | |
console.error('WASM failed to instantiate', error); | |
} | |
} | |
// post a few messages and view console | |
for (let i=0; i<10; i++) { | |
worker.postMessage(i); | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="emscripten-module.js"></script> | |
<script> | |
Module.ready | |
.then(api => console.log( api.sayHello() )) | |
.catch(e => console.error('💩', e)) | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.importScripts('emscripten-module.js'); | |
self.onmessage = event => { | |
Module.ready | |
.then(api => { | |
if (self.isClosed) return; | |
const data = event.data; | |
// Interact with Module... | |
console.log( data, api.sayHello() ); | |
}) | |
.catch(e => { | |
if (self.isClosed) return; | |
exitOnError(e); | |
console.error('Module failed to initialize', e); | |
}); | |
} | |
// Terminate Worker and prevent Promise microtasks from completing | |
function exitOnError(e) { | |
// "throw e" won't be received by controller. Post as message instead | |
self.postMessage({error: e.message}); | |
self.close(); | |
self.isClosed = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
have you phread version?