Skip to content

Commit 2438656

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

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
@@ -2437,6 +2437,8 @@ def generate_html(target, options, js_target, target_basename,
24372437
} else if (Module['memoryInitializerPrefixURL']) {
24382438
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
24392439
}
2440+
// If URL to memory initializer is relative, treat it relative with respect to Module['scriptDirectory'], if that is present.
2441+
if (Module['scriptDirectory'] && !/^(?:[a-z]+:)?\/\//i.test(memoryInitializer)) memoryInitializer = Module['scriptDirectory'] + memoryInitializer;
24402442
var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest();
24412443
meminitXHR.open('GET', memoryInitializer, true);
24422444
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
@@ -45,6 +45,8 @@ if (memoryInitializer) {
4545
} else if (Module['memoryInitializerPrefixURL']) {
4646
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
4747
}
48+
memoryInitializer = joinUrl(Module['scriptDirectory'], memoryInitializer);
49+
4850
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
4951
var data = Module['readBinary'](memoryInitializer);
5052
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() {
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)