Skip to content

Commit 7c0d2e7

Browse files
authored
Fix pthread + modularize + running in node worker (#21832)
The issue here was the special case for the setting of `scriptDirectory` under nodejs when `ENVIRONMENT_IS_WORKER`. Previously this path was not being hit when the main thread was running in a worker because we were not setting `ENVIRONMENT_IS_WORKER` in this case. This was fixed in #21701 and `ENVIRONMENT_IS_WORKER` was correctly set, but that meant that `scriptDirectory` then started being set incorrectly. Fixes: #21827
1 parent 9fdf8af commit 7c0d2e7

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

src/shell.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ if (ENVIRONMENT_IS_WORKER) {
198198
// `/` should be present at the end if `scriptDirectory` is not empty
199199
var scriptDirectory = '';
200200
function locateFile(path) {
201+
#if RUNTIME_DEBUG
202+
dbg('locateFile:', path, 'scriptDirectory:', scriptDirectory);
203+
#endif
201204
#if expectToReceiveOnModule('locateFile')
202205
if (Module['locateFile']) {
203206
return Module['locateFile'](path, scriptDirectory);
@@ -232,18 +235,14 @@ if (ENVIRONMENT_IS_NODE) {
232235
var fs = require('fs');
233236
var nodePath = require('path');
234237

235-
if (ENVIRONMENT_IS_WORKER) {
236-
scriptDirectory = nodePath.dirname(scriptDirectory) + '/';
237-
} else {
238238
#if EXPORT_ES6
239-
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
240-
// since there's no way getting the current absolute path of the module when
241-
// support for that is not available.
242-
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
239+
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
240+
// since there's no way getting the current absolute path of the module when
241+
// support for that is not available.
242+
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
243243
#else
244-
scriptDirectory = __dirname + '/';
244+
scriptDirectory = __dirname + '/';
245245
#endif
246-
}
247246

248247
#include "node_shell_read.js"
249248

src/wasm_worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if (ENVIRONMENT_IS_NODE) {
2323
self: global,
2424
require,
2525
__filename,
26+
__dirname,
2627
Worker: nodeWorkerThreads.Worker,
2728
importScripts: (f) => vm.runInThisContext(fs.readFileSync(f, 'utf8'), {filename: f}),
2829
postMessage: (msg) => parentPort.postMessage(msg),

test/test_other.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10166,21 +10166,44 @@ def test_node_js_run_from_different_directory(self):
1016610166

1016710167
# Tests that a pthreads + modularize build can be run in node js
1016810168
@node_pthreads
10169-
def test_node_js_pthread_module(self):
10169+
@parameterized({
10170+
'': (False,),
10171+
'es6': (True,),
10172+
})
10173+
def test_node_js_pthread_module(self, es6):
1017010174
# create module loader script
10175+
if es6:
10176+
ext = '.mjs'
10177+
create_file('moduleLoader.mjs', '''
10178+
import test_module from "./subdir/module.mjs";
10179+
test_module().then((test_module_instance) => {
10180+
test_module_instance._main();
10181+
});
10182+
''')
10183+
else:
10184+
ext = '.js'
10185+
create_file('moduleLoader.js', '''
10186+
const test_module = require("./subdir/module.js");
10187+
test_module().then((test_module_instance) => {
10188+
test_module_instance._main();
10189+
});
10190+
''')
1017110191
ensure_dir('subdir')
10172-
create_file('subdir/moduleLoader.js', '''
10173-
const test_module = require("./module");
10174-
test_module().then((test_module_instance) => {
10175-
test_module_instance._main();
10176-
});
10177-
''')
1017810192

1017910193
# build hello_world.c
10180-
self.run_process([EMCC, test_file('hello_world.c'), '-o', Path('subdir/module.js'), '-pthread', '-sPTHREAD_POOL_SIZE=2', '-sMODULARIZE', '-sEXPORT_NAME=test_module', '-sENVIRONMENT=worker,node'])
10194+
self.run_process([EMCC, test_file('hello_world.c'), '-o', 'subdir/module' + ext, '-pthread', '-sPTHREAD_POOL_SIZE=2', '-sMODULARIZE', '-sEXPORT_NAME=test_module'] + self.get_emcc_args())
1018110195

1018210196
# run the module
10183-
ret = self.run_js('subdir/moduleLoader.js')
10197+
ret = self.run_js('moduleLoader' + ext)
10198+
self.assertContained('hello, world!', ret)
10199+
10200+
create_file('workerLoader.js', f'''
10201+
const {{ Worker, isMainThread }} = require('worker_threads');
10202+
new Worker('./moduleLoader{ext}');
10203+
''')
10204+
10205+
# run the same module, but inside of a worker
10206+
ret = self.run_js('workerLoader.js')
1018410207
self.assertContained('hello, world!', ret)
1018510208

1018610209
@no_windows('node system() does not seem to work, see https://github.com/emscripten-core/emscripten/pull/10547')

0 commit comments

Comments
 (0)