diff --git a/emcc.py b/emcc.py index e091e4dd34402..b2577127df01b 100755 --- a/emcc.py +++ b/emcc.py @@ -2438,6 +2438,8 @@ def generate_html(target, options, js_target, target_basename, } else if (Module['memoryInitializerPrefixURL']) { memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer; } + // If URL to memory initializer is relative, treat it relative with respect to Module['scriptDirectory'], if that is present. + if (Module['scriptDirectory'] && !/^(?:[a-z]+:)?\/\//i.test(memoryInitializer)) memoryInitializer = Module['scriptDirectory'] + memoryInitializer; var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest(); meminitXHR.open('GET', memoryInitializer, true); meminitXHR.responseType = 'arraybuffer'; diff --git a/src/Fetch.js b/src/Fetch.js index e7b8477c704cf..f092f909e8049 100644 --- a/src/Fetch.js +++ b/src/Fetch.js @@ -115,6 +115,7 @@ var Fetch = { // of these are passed, then the default URL 'pthread-main.js' relative to the main html file is loaded. if (typeof Module['locateFile'] === 'function') fetchJs = Module['locateFile'](fetchJs); else if (Module['pthreadMainPrefixURL']) fetchJs = Module['pthreadMainPrefixURL'] + fetchJs; + fetchJs = joinUrl(Module['scriptDirectory'], fetchJs); Fetch.worker = new Worker(fetchJs); Fetch.worker.onmessage = function(e) { Module['print']('fetch-worker sent a message: ' + e.filename + ':' + e.lineno + ': ' + e.message); diff --git a/src/library_debugger_toolkit.js b/src/library_debugger_toolkit.js index 29cb532aecaaf..f70d3caf73aea 100644 --- a/src/library_debugger_toolkit.js +++ b/src/library_debugger_toolkit.js @@ -451,6 +451,7 @@ var CyberDWARFHeapPrinter = function(cdFileLocation) { } else if (Module['cdInitializerPrefixURL']) { cdFileLocation = Module['cdInitializerPrefixURL'] + cdFileLocation; } + cdFileLocation = joinUrl(Module['scriptDirectory'], cdFileLocation); if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) { var data = Module['read'](cdFileLocation); install_cyberdwarf(data); diff --git a/src/library_pthread.js b/src/library_pthread.js index f438799b7a367..c0c81210b4c4d 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -259,6 +259,7 @@ var LibraryPThread = { // of these are passed, then the default URL 'pthread-main.js' relative to the main html file is loaded. if (typeof Module['locateFile'] === 'function') pthreadMainJs = Module['locateFile'](pthreadMainJs); else if (Module['pthreadMainPrefixURL']) pthreadMainJs = Module['pthreadMainPrefixURL'] + pthreadMainJs; + pthreadMainJs = joinUrl(Module['scriptDirectory'], pthreadMainJs); var worker = new Worker(pthreadMainJs); worker.onmessage = function(e) { diff --git a/src/postamble.js b/src/postamble.js index 929d294c97bb2..8fd4c760ba6a3 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -44,6 +44,8 @@ if (memoryInitializer) { } else if (Module['memoryInitializerPrefixURL']) { memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer; } + memoryInitializer = joinUrl(Module['scriptDirectory'], memoryInitializer); + if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) { var data = Module['readBinary'](memoryInitializer); HEAPU8.set(data, Runtime.GLOBAL_BASE); diff --git a/src/preamble.js b/src/preamble.js index 70145dc0f9b4a..77a82c80cfde7 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -932,6 +932,16 @@ function stackTrace() { } {{{ maybeExport('stackTrace') }}} +function isAbsoluteUrl(url) { + return /^(?:[a-z]+:)?\/\//i.test(url); +} + +function joinUrl(a, b) { +#if ASSERTIONS + if (a && !a.endsWith('/')) throw 'First parameter to joinUrl() should end in /'!; +#endif + return a && !isAbsoluteUrl(b) ? a + b : b; +} // Memory management var PAGE_SIZE = 16384; @@ -2114,6 +2124,9 @@ function integrateWasmJS(Module) { wasmBinaryFile = Module['locateFile'](wasmBinaryFile); asmjsCodeFile = Module['locateFile'](asmjsCodeFile); } + wasmTextFile = joinUrl(Module['scriptDirectory'], wasmTextFile); + wasmBinaryFile = joinUrl(Module['scriptDirectory'], wasmBinaryFile); + asmjsCodeFile = joinUrl(Module['scriptDirectory'], asmjsCodeFile); // utilities diff --git a/src/shell.js b/src/shell.js index 789de77778b9f..fd3867ed2e61c 100644 --- a/src/shell.js +++ b/src/shell.js @@ -76,6 +76,7 @@ if (ENVIRONMENT_IS_NODE) { // Note that we pollute the global namespace here, otherwise we break in node if (!Module['print']) Module['print'] = console.log; if (!Module['printErr']) Module['printErr'] = console.warn; + if (!Module['scriptDirectory']) Module['scriptDirectory'] = __dirname + '/'; var nodeFS; var nodePath; diff --git a/tools/file_packager.py b/tools/file_packager.py index 26e2ae92faab6..521f3c64b9aa5 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -833,6 +833,7 @@ def was_seen(name): var REMOTE_METADATA_NAME = typeof Module['locateFile'] === 'function' ? Module['locateFile']('%(metadata_file)s') : ((Module['filePackagePrefixURL'] || '') + '%(metadata_file)s'); + REMOTE_METADATA_NAME = joinUrl(Module['scriptDirectory'], REMOTE_METADATA_NAME); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) {