Skip to content

Commit

Permalink
Fix _scriptDir for node.js + USE_PTHREADS + MODULARIZE (emscripten-co…
Browse files Browse the repository at this point in the history
…re#9875)

node.js doesn't have document.currentScript.src, so use __filename instead.
  • Loading branch information
VirtualTim authored and belraquib committed Dec 23, 2020
1 parent 6ea3a4a commit 7bed88a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,7 @@ def modularize():
# document.currentScript, so a simple export declaration is enough.
src = 'var %s=%s' % (shared.Settings.EXPORT_NAME, src)
else:
script_url_node = ""
# When MODULARIZE this JS may be executed later,
# after document.currentScript is gone, so we save it.
# (when MODULARIZE_INSTANCE, an instance is created
Expand All @@ -3176,15 +3177,19 @@ def modularize():
script_url = "import.meta.url"
else:
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
if shared.Settings.target_environment_may_be('node'):
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename;"
src = '''
var %(EXPORT_NAME)s = (function() {
var _scriptDir = %(script_url)s;
%(script_url_node)s
return (%(src)s);
})();
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
'src': src,
'script_url': script_url
'script_url': script_url,
'script_url_node': script_url_node,
'src': src
}
else:
# Create the MODULARIZE_INSTANCE instance
Expand Down
22 changes: 22 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8992,6 +8992,28 @@ def test_node_js_run_from_different_directory(self):
ret = run_process(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).stdout
self.assertContained('hello, world!', ret)

# Tests that a pthreads + modularize build can be run in node js
@no_fastcomp('node pthreads only supported on wasm backend')
def test_node_js_pthread_module(self):
# create module loader script
moduleLoader = 'moduleLoader.js'
moduleLoaderContents = '''
const test_module = require("./module");
test_module().then((test_module_instance) => {
test_module_instance._main();
process.exit(0);
});
'''
os.makedirs('subdir', exist_ok=True)
create_test_file(os.path.join('subdir', moduleLoader), moduleLoaderContents)

# build hello_world.c
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'])

# run the module
ret = run_process(NODE_JS + ['--experimental-wasm-threads'] + [os.path.join('subdir', moduleLoader)], stdout=PIPE).stdout
self.assertContained('hello, world!', ret)

def test_is_bitcode(self):
fname = 'tmp.o'

Expand Down

0 comments on commit 7bed88a

Please sign in to comment.