Skip to content

Commit 7bed88a

Browse files
VirtualTimbelraquib
authored andcommitted
Fix _scriptDir for node.js + USE_PTHREADS + MODULARIZE (emscripten-core#9875)
node.js doesn't have document.currentScript.src, so use __filename instead.
1 parent 6ea3a4a commit 7bed88a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

emcc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,6 +3166,7 @@ def modularize():
31663166
# document.currentScript, so a simple export declaration is enough.
31673167
src = 'var %s=%s' % (shared.Settings.EXPORT_NAME, src)
31683168
else:
3169+
script_url_node = ""
31693170
# When MODULARIZE this JS may be executed later,
31703171
# after document.currentScript is gone, so we save it.
31713172
# (when MODULARIZE_INSTANCE, an instance is created
@@ -3176,15 +3177,19 @@ def modularize():
31763177
script_url = "import.meta.url"
31773178
else:
31783179
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
3180+
if shared.Settings.target_environment_may_be('node'):
3181+
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename;"
31793182
src = '''
31803183
var %(EXPORT_NAME)s = (function() {
31813184
var _scriptDir = %(script_url)s;
3185+
%(script_url_node)s
31823186
return (%(src)s);
31833187
})();
31843188
''' % {
31853189
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
3186-
'src': src,
3187-
'script_url': script_url
3190+
'script_url': script_url,
3191+
'script_url_node': script_url_node,
3192+
'src': src
31883193
}
31893194
else:
31903195
# Create the MODULARIZE_INSTANCE instance

tests/test_other.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8992,6 +8992,28 @@ def test_node_js_run_from_different_directory(self):
89928992
ret = run_process(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).stdout
89938993
self.assertContained('hello, world!', ret)
89948994

8995+
# Tests that a pthreads + modularize build can be run in node js
8996+
@no_fastcomp('node pthreads only supported on wasm backend')
8997+
def test_node_js_pthread_module(self):
8998+
# create module loader script
8999+
moduleLoader = 'moduleLoader.js'
9000+
moduleLoaderContents = '''
9001+
const test_module = require("./module");
9002+
test_module().then((test_module_instance) => {
9003+
test_module_instance._main();
9004+
process.exit(0);
9005+
});
9006+
'''
9007+
os.makedirs('subdir', exist_ok=True)
9008+
create_test_file(os.path.join('subdir', moduleLoader), moduleLoaderContents)
9009+
9010+
# build hello_world.c
9011+
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'module.js'), '-s', 'USE_PTHREADS=1', '-s', 'PTHREAD_POOL_SIZE=2', '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME=test_module', '-s', 'ENVIRONMENT=worker,node'])
9012+
9013+
# run the module
9014+
ret = run_process(NODE_JS + ['--experimental-wasm-threads'] + [os.path.join('subdir', moduleLoader)], stdout=PIPE).stdout
9015+
self.assertContained('hello, world!', ret)
9016+
89959017
def test_is_bitcode(self):
89969018
fname = 'tmp.o'
89979019

0 commit comments

Comments
 (0)