Skip to content

Commit a2a8329

Browse files
committed
Add second test case for a scenario where Module.locateFile() specifies an already absolute path for loading content in node.js from a different directory, and fix it.
1 parent d753f50 commit a2a8329

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ def generate_html(target, options, js_target, target_basename,
25592559
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
25602560
}
25612561
// 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;
2562+
if (Module['scriptDirectory'] && !/^(?:[a-z]+:)?\/\/?/i.test(memoryInitializer)) memoryInitializer = Module['scriptDirectory'] + memoryInitializer;
25632563
Module['memoryInitializerRequestURL'] = memoryInitializer;
25642564
var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest();
25652565
meminitXHR.open('GET', memoryInitializer, true);

src/preamble.js

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

828-
function isAbsoluteUrl(url) {
829-
return /^(?:[a-z]+:)?\/\//i.test(url);
828+
function isAbsolutePath(path) {
829+
return /^(?:[a-z]+:)?\/\/?/i.test(path);
830830
}
831831

832832
// Joins relative URL 'b' to URL 'a'. 'a' may be empty, in which case 'b' is returned.
@@ -835,7 +835,7 @@ function joinUrl(a, b) {
835835
#if ASSERTIONS
836836
if (a && !a.endsWith('/')) throw 'First parameter to joinUrl() should end in /!';
837837
#endif
838-
return a && !isAbsoluteUrl(b) ? a + b : b;
838+
return a && !isAbsolutePath(b) ? a + b : b;
839839
}
840840
// Memory management
841841

tests/test_other.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8142,8 +8142,22 @@ def test_toolchain_profiler(self):
81428142

81438143
# Tests that Emscripten-compiled applications can be run from a relative path with node command line that is different than the current working directory.
81448144
def test_node_js_run_from_different_directory(self):
8145+
# Test that .mem.js is loaded up properly even if running the build output from a separate directory.
81458146
os.mkdir('subdir')
81468147
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '-O3']).communicate()
81478148
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8149+
try_delete('subdir')
8150+
assert 'hello, world!' in ret
8151+
8152+
# Test that the build is loaded properly when Module.locateFile is being used, where Module.locateFile() specifies an absolute path already.
8153+
os.mkdir('subdir')
8154+
open(os.path.join('subdir', 'pre.js'), 'w').write('''
8155+
var Module = {};
8156+
Module.locateFile = function(f) { return __dirname + '/' + f; }
8157+
''')
8158+
8159+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '--pre-js', os.path.join('subdir', 'pre.js'), '-O3']).communicate()
8160+
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8161+
try_delete('subdir')
81488162
assert 'hello, world!' in ret
81498163

0 commit comments

Comments
 (0)