Skip to content

Commit

Permalink
Fix EXPORT_ES6 + USE_PTHREADS (#8306)
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualTim authored and kripken committed May 24, 2019
1 parent fcafd80 commit 1e5317c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
9 changes: 7 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2846,14 +2846,19 @@ def modularize():
# after document.currentScript is gone, so we save it.
# (when MODULARIZE_INSTANCE, an instance is created
# immediately anyhow, like in non-modularize mode)
if shared.Settings.EXPORT_ES6:
script_url = "import.meta.url"
else:
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
src = '''
var %(EXPORT_NAME)s = (function() {
var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
var _scriptDir = %(script_url)s;
return (%(src)s);
})();
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
'src': src
'src': src,
'script_url': script_url
}
else:
# Create the MODULARIZE_INSTANCE instance
Expand Down
5 changes: 5 additions & 0 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,11 @@ def create_receiving(function_table_data, function_tables_defs, exported_impleme

shared.Settings.MODULE_EXPORTS = module_exports = exported_implemented_functions + function_tables(function_table_data)

# Currently USE PTHREADS + EXPORT ES6 doesn't work with the previously generated assertions.
# ES6 modules disallow re-assigning read-only properties.
if shared.Settings.USE_PTHREADS and shared.Settings.EXPORT_ES6 and shared.Settings.ASSERTIONS and receiving:
receiving = "assert(!ENVIRONMENT_IS_PTHREAD, 'Error: USE_PTHREADS and EXPORT_ES6 requires ASSERTIONS=0');\n" + receiving

if not shared.Settings.SWAPPABLE_ASM_MODULE:
if shared.Settings.DECLARE_ASM_MODULE_EXPORTS:
imported_exports = [s for s in module_exports if s not in initializers]
Expand Down
7 changes: 3 additions & 4 deletions src/shell_pthreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
#if USE_PTHREADS

if (typeof ENVIRONMENT_IS_PTHREAD === 'undefined') {
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
// N.B. this line needs to appear without 'var' keyword to avoid 'var hoisting' from occurring. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
ENVIRONMENT_IS_PTHREAD = false;
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
var ENVIRONMENT_IS_PTHREAD = Module.ENVIRONMENT_IS_PTHREAD || false;
if (!ENVIRONMENT_IS_PTHREAD) {
var PthreadWorkerInit = {}; // Collects together variables that are needed at initialization time for the web workers that host pthreads.
}
#if MODULARIZE
Expand Down
19 changes: 17 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var buffer; // All pthreads share the same Emscripten HEAP as SharedArrayBuffer
var DYNAMICTOP_PTR = 0;
var DYNAMIC_BASE = 0;

var ENVIRONMENT_IS_PTHREAD = true;
var PthreadWorkerInit = {};

// performance.now() is specced to return a wallclock time in msecs since that Web Worker/main thread launched. However for pthreads this can cause
Expand All @@ -35,6 +34,10 @@ var __performance_now_clock_drift = 0;
// Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091
// Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print.
var Module = {};
#if EXPORT_ES6
var PThread;
var HEAPU32;
#endif

#if ASSERTIONS
function assert(condition, text) {
Expand Down Expand Up @@ -128,15 +131,26 @@ this.onmessage = function(e) {
#endif

{{{ makeAsmExportAndGlobalAssignTargetInPthread('PthreadWorkerInit') }}} = e.data.PthreadWorkerInit;
Module['ENVIRONMENT_IS_PTHREAD'] = true;

#if MODULARIZE && EXPORT_ES6
import(e.data.urlOrBlob).then(function({{{ EXPORT_NAME }}}) {
Module = {{{ EXPORT_NAME }}}.default(Module);
PThread = Module['PThread'];
HEAPU32 = Module['HEAPU32'];
#if !ASMFS
if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams();
#endif
postMessage({ cmd: 'loaded' });
});
#else
if (typeof e.data.urlOrBlob === 'string') {
importScripts(e.data.urlOrBlob);
} else {
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
importScripts(objectUrl);
URL.revokeObjectURL(objectUrl);
}

#if MODULARIZE
#if !MODULARIZE_INSTANCE
Module = {{{ EXPORT_NAME }}}(Module);
Expand All @@ -149,6 +163,7 @@ this.onmessage = function(e) {
if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams();
#endif
postMessage({ cmd: 'loaded' });
#endif
} else if (e.data.cmd === 'objectTransfer') {
PThread.receiveObjectTransfer(e.data);
} else if (e.data.cmd === 'run') { // This worker was idle, and now should start executing its pthread entry point.
Expand Down

0 comments on commit 1e5317c

Please sign in to comment.