Skip to content

Commit

Permalink
Made the worklet changes work with ES6_IMPORT
Browse files Browse the repository at this point in the history
  • Loading branch information
tklajnscek committed Oct 14, 2020
1 parent 5bf9bb3 commit 358b422
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
36 changes: 18 additions & 18 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2856,28 +2856,28 @@ def modularize():
with open(final_js, 'w') as f:
f.write(src)

# Export using a UMD style export, or ES6 exports if selected
# Add ; if src doesn't contain it so we generate valid JS (otherwise acorn fails)
if src.rstrip()[-1] != ';':
f.write(';')

# Store the export on the global scope for audio worklets
f.write('''if (typeof AudioWorkletGlobalScope === 'function')
globalThis["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME
})

# Export using a UMD style export, or ES6 exports if selected
if shared.Settings.EXPORT_ES6:
f.write('''export default %s;''' % shared.Settings.EXPORT_NAME)
f.write('''export default %s;\n''' % shared.Settings.EXPORT_NAME)
elif not shared.Settings.MINIMAL_RUNTIME:
f.write('''if (typeof exports === 'object' && typeof module === 'object')
module.exports = %(EXPORT_NAME)s;
else if (typeof define === 'function' && define['amd'])
define([], function() { return %(EXPORT_NAME)s; });
else if (typeof exports === 'object')
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
else if (typeof AudioWorkletGlobalScope === 'function')
globalThis["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME
})
else:
if(not src.endswith(';')):
f.write(';') # This is needed to make it a valid JS file after appending the below
f.write('''if (typeof AudioWorkletGlobalScope === 'function')
globalThis["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
module.exports = %(EXPORT_NAME)s;
else if (typeof define === 'function' && define['amd'])
define([], function() { return %(EXPORT_NAME)s; });
else if (typeof exports === 'object')
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME
})

Expand Down
38 changes: 20 additions & 18 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ globalObj.onmessage = function(e) {
{{{ makeAsmImportsAccessInPthread('ENVIRONMENT_IS_PTHREAD') }}} = true;
#endif

#if MODULARIZE && EXPORT_ES6
import(e.data.urlOrBlob).then(function({{{ EXPORT_NAME }}}) {
return {{{ EXPORT_NAME }}}.default(Module);
}).then(function(instance) {
Module = instance;
postMessage({ 'cmd': 'loaded' });
});
#if MODULARIZE
// When running as an AudioWorklet all the scripts are imported from the main thread (via .addModule)
// Technically, this script should just 'import' the main script, but this doesn't work (yet) in all
// browsers and requires the script be packaged as an ES6 module so we do multiple .addModule calls.
#if EXPORT_ES6
(!isWorklet ?
import(e.data.urlOrBlob).then(function(importedModule) {
return importedModule.default(Module);
}) :
#if MINIMAL_RUNTIME
{{{ EXPORT_NAME }}}(imports))
#else
// For worklets the script must have been added via .addModule from the main thread so we skip the import
{{{ EXPORT_NAME }}}(Module))
#endif
#else // EXPORT_ES6
if(!isWorklet) {
if (typeof e.data.urlOrBlob === 'string') {
importScripts(e.data.urlOrBlob);
Expand All @@ -130,27 +136,23 @@ globalObj.onmessage = function(e) {
URL.revokeObjectURL(objectUrl);
}
}
#if MODULARIZE
#if MINIMAL_RUNTIME
{{{ EXPORT_NAME }}}(imports).then(function (instance) {
Module = instance;
postMessage({ 'cmd': 'loaded' });
});
{{{ EXPORT_NAME }}}(imports)
#else
{{{ EXPORT_NAME }}}(Module).then(function (instance) {
{{{ EXPORT_NAME }}}(Module)
#endif
#endif // EXPORT_ES6
.then(function(instance) {
Module = instance;
postMessage({ 'cmd': 'loaded' });
});
#endif
#endif
#endif // MODULARIZE

#if !MODULARIZE && !MINIMAL_RUNTIME
// MINIMAL_RUNTIME always compiled Wasm (&Wasm2JS) asynchronously, even in pthreads. But
// regular runtime and asm.js are loaded synchronously, so in those cases
// we are now loaded, and can post back to main thread.
postMessage({ 'cmd': 'loaded' });
#endif

#endif
} else if (e.data.cmd === 'objectTransfer') {
Module['PThread'].receiveObjectTransfer(e.data);
Expand Down

0 comments on commit 358b422

Please sign in to comment.