Skip to content

Commit 3ae78a1

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

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
@@ -2558,6 +2558,8 @@ def generate_html(target, options, js_target, target_basename,
25582558
} else if (Module['memoryInitializerPrefixURL']) {
25592559
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
25602560
}
2561+
// If URL to memory initializer is relative, treat it relative with respect to Module['scriptDirectory'], if that is present.
2562+
if (Module['scriptDirectory'] && !/^(?:[a-z]+:)?\/\//i.test(memoryInitializer)) memoryInitializer = Module['scriptDirectory'] + memoryInitializer;
25612563
Module['memoryInitializerRequestURL'] = memoryInitializer;
25622564
var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest();
25632565
meminitXHR.open('GET', memoryInitializer, true);

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
@@ -46,6 +46,8 @@ if (memoryInitializer) {
4646
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
4747
}
4848
}
49+
memoryInitializer = joinUrl(Module['scriptDirectory'], memoryInitializer);
50+
4951
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
5052
var data = Module['readBinary'](memoryInitializer);
5153
HEAPU8.set(data, GLOBAL_BASE);

src/preamble.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ function stackTrace() {
825825
return demangleAll(js);
826826
}
827827

828+
function isAbsoluteUrl(url) {
829+
return /^(?:[a-z]+:)?\/\//i.test(url);
830+
}
831+
832+
function joinUrl(a, b) {
833+
#if ASSERTIONS
834+
if (a && !a.endsWith('/')) throw 'First parameter to joinUrl() should end in /!';
835+
#endif
836+
return a && !isAbsoluteUrl(b) ? a + b : b;
837+
}
828838
// Memory management
829839

830840
var PAGE_SIZE = 16384;
@@ -1986,6 +1996,9 @@ function integrateWasmJS() {
19861996
asmjsCodeFile = Module['locateFile'](asmjsCodeFile);
19871997
}
19881998
}
1999+
wasmTextFile = joinUrl(Module['scriptDirectory'], wasmTextFile);
2000+
wasmBinaryFile = joinUrl(Module['scriptDirectory'], wasmBinaryFile);
2001+
asmjsCodeFile = joinUrl(Module['scriptDirectory'], asmjsCodeFile);
19892002

19902003
// utilities
19912004

src/shell.js

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

8687
var nodeFS;
8788
var nodePath;

tools/file_packager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ def was_seen(name):
848848
var REMOTE_METADATA_NAME = typeof Module['locateFile'] === 'function' ?
849849
Module['locateFile']('%(metadata_file)s') :
850850
((Module['filePackagePrefixURL'] || '') + '%(metadata_file)s');
851+
REMOTE_METADATA_NAME = joinUrl(Module['scriptDirectory'], REMOTE_METADATA_NAME);
851852
var xhr = new XMLHttpRequest();
852853
xhr.onreadystatechange = function() {
853854
if (xhr.readyState === 4 && xhr.status === 200) {

0 commit comments

Comments
 (0)