Skip to content

Commit fd5a831

Browse files
committed
Add Module['scriptDirectory'] to fix loading files that should be relative to main .js file on Node.js.
1 parent 2ca4bd8 commit fd5a831

File tree

8 files changed

+22
-0
lines changed

8 files changed

+22
-0
lines changed

emcc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,8 @@ def generate_html(target, options, js_target, target_basename,
24132413
} else if (Module['memoryInitializerPrefixURL']) {
24142414
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
24152415
}
2416+
// If URL to memory initializer is relative, treat it relative with respect to Module['scriptDirectory'], if that is present.
2417+
if (Module['scriptDirectory'] && !/^(?:[a-z]+:)?\/\//i.test(memoryInitializer)) memoryInitializer = Module['scriptDirectory'] + memoryInitializer;
24162418
var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest();
24172419
meminitXHR.open('GET', memoryInitializer, true);
24182420
meminitXHR.responseType = 'arraybuffer';

src/Fetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var Fetch = {
115115
// of these are passed, then the default URL 'pthread-main.js' relative to the main html file is loaded.
116116
if (typeof Module['locateFile'] === 'function') fetchJs = Module['locateFile'](fetchJs);
117117
else if (Module['pthreadMainPrefixURL']) fetchJs = Module['pthreadMainPrefixURL'] + fetchJs;
118+
fetchJs = joinUrl(Module['scriptDirectory'], fetchJs);
118119
Fetch.worker = new Worker(fetchJs);
119120
Fetch.worker.onmessage = function(e) {
120121
Module['print']('fetch-worker sent a message: ' + e.filename + ':' + e.lineno + ': ' + e.message);

src/library_debugger_toolkit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ var CyberDWARFHeapPrinter = function(cdFileLocation) {
451451
} else if (Module['cdInitializerPrefixURL']) {
452452
cdFileLocation = Module['cdInitializerPrefixURL'] + cdFileLocation;
453453
}
454+
cdFileLocation = joinUrl(Module['scriptDirectory'], cdFileLocation);
454455
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
455456
var data = Module['read'](cdFileLocation);
456457
install_cyberdwarf(data);

src/library_pthread.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ var LibraryPThread = {
259259
// of these are passed, then the default URL 'pthread-main.js' relative to the main html file is loaded.
260260
if (typeof Module['locateFile'] === 'function') pthreadMainJs = Module['locateFile'](pthreadMainJs);
261261
else if (Module['pthreadMainPrefixURL']) pthreadMainJs = Module['pthreadMainPrefixURL'] + pthreadMainJs;
262+
pthreadMainJs = joinUrl(Module['scriptDirectory'], pthreadMainJs);
262263
var worker = new Worker(pthreadMainJs);
263264

264265
worker.onmessage = function(e) {

src/postamble.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ if (memoryInitializer) {
4444
} else if (Module['memoryInitializerPrefixURL']) {
4545
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
4646
}
47+
memoryInitializer = joinUrl(Module['scriptDirectory'], memoryInitializer);
48+
4749
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
4850
var data = Module['readBinary'](memoryInitializer);
4951
HEAPU8.set(data, Runtime.GLOBAL_BASE);

src/preamble.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,16 @@ function stackTrace() {
932932
}
933933
{{{ maybeExport('stackTrace') }}}
934934

935+
function isAbsoluteUrl(url) {
936+
return /^(?:[a-z]+:)?\/\//i.test(url);
937+
}
938+
939+
function joinUrl(a, b) {
940+
#if ASSERTIONS
941+
if (a && !a.endsWith('/')) throw 'First parameter to joinUrl() should end in /'!;
942+
#endif
943+
return a && !isAbsoluteUrl(b) ? a + b : b;
944+
}
935945
// Memory management
936946

937947
var PAGE_SIZE = 16384;
@@ -2114,6 +2124,9 @@ function integrateWasmJS(Module) {
21142124
wasmBinaryFile = Module['locateFile'](wasmBinaryFile);
21152125
asmjsCodeFile = Module['locateFile'](asmjsCodeFile);
21162126
}
2127+
wasmTextFile = joinUrl(Module['scriptDirectory'], wasmTextFile);
2128+
wasmBinaryFile = joinUrl(Module['scriptDirectory'], wasmBinaryFile);
2129+
asmjsCodeFile = joinUrl(Module['scriptDirectory'], asmjsCodeFile);
21172130

21182131
// utilities
21192132

src/shell.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if (ENVIRONMENT_IS_NODE) {
7676
// Note that we pollute the global namespace here, otherwise we break in node
7777
if (!Module['print']) Module['print'] = console.log;
7878
if (!Module['printErr']) Module['printErr'] = console.warn;
79+
if (!Module['scriptDirectory']) Module['scriptDirectory'] = __dirname + '/';
7980

8081
var nodeFS;
8182
var nodePath;

tools/file_packager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ def was_seen(name):
833833
var REMOTE_METADATA_NAME = typeof Module['locateFile'] === 'function' ?
834834
Module['locateFile']('%(metadata_file)s') :
835835
((Module['filePackagePrefixURL'] || '') + '%(metadata_file)s');
836+
REMOTE_METADATA_NAME = joinUrl(Module['scriptDirectory'], REMOTE_METADATA_NAME);
836837
var xhr = new XMLHttpRequest();
837838
xhr.onreadystatechange = function() {
838839
if (xhr.readyState === 4 && xhr.status === 200) {

0 commit comments

Comments
 (0)