Skip to content

Commit

Permalink
Add Module['scriptDirectory'] to fix loading files that should be rel…
Browse files Browse the repository at this point in the history
…ative to main .js file on Node.js.
  • Loading branch information
juj committed Sep 8, 2017
1 parent da9e8a9 commit 3c88610
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/library_debugger_toolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3c88610

Please sign in to comment.