Skip to content

Commit

Permalink
Fix pthread + modularize + running in node worker
Browse files Browse the repository at this point in the history
Fixes: #21827
  • Loading branch information
sbc100 committed Apr 25, 2024
1 parent d9c87f2 commit 54f83f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ if (ENVIRONMENT_IS_WORKER) {
// `/` should be present at the end if `scriptDirectory` is not empty
var scriptDirectory = '';
function locateFile(path) {
#if RUNTIME_DEBUG
dbg('locateFile:', path, 'scriptDirectory:', scriptDirectory);
#endif
#if expectToReceiveOnModule('locateFile')
if (Module['locateFile']) {
return Module['locateFile'](path, scriptDirectory);
Expand Down Expand Up @@ -232,18 +235,14 @@ if (ENVIRONMENT_IS_NODE) {
var fs = require('fs');
var nodePath = require('path');

if (ENVIRONMENT_IS_WORKER) {
scriptDirectory = nodePath.dirname(scriptDirectory) + '/';
} else {
#if EXPORT_ES6
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
// since there's no way getting the current absolute path of the module when
// support for that is not available.
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
// since there's no way getting the current absolute path of the module when
// support for that is not available.
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
#else
scriptDirectory = __dirname + '/';
scriptDirectory = __dirname + '/';
#endif
}

#include "node_shell_read.js"

Expand Down
41 changes: 32 additions & 9 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10166,21 +10166,44 @@ def test_node_js_run_from_different_directory(self):

# Tests that a pthreads + modularize build can be run in node js
@node_pthreads
def test_node_js_pthread_module(self):
@parameterized({
'': (False,),
'es6': (True,),
})
def test_node_js_pthread_module(self, es6):
# create module loader script
if es6:
ext = '.mjs'
create_file('moduleLoader.mjs', '''
import test_module from "./subdir/module.mjs";
test_module().then((test_module_instance) => {
test_module_instance._main();
});
''')
else:
ext = '.js'
create_file('moduleLoader.js', '''
const test_module = require("./subdir/module.js");
test_module().then((test_module_instance) => {
test_module_instance._main();
});
''')
ensure_dir('subdir')
create_file('subdir/moduleLoader.js', '''
const test_module = require("./module");
test_module().then((test_module_instance) => {
test_module_instance._main();
});
''')

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

# run the module
ret = self.run_js('subdir/moduleLoader.js')
ret = self.run_js('moduleLoader' + ext)
self.assertContained('hello, world!', ret)

create_file('workerLoader.js', f'''
const {{ Worker, isMainThread }} = require('worker_threads');
new Worker('./moduleLoader{ext}');
''')

# run the same module, but inside of a worker
ret = self.run_js('workerLoader.js')
self.assertContained('hello, world!', ret)

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

0 comments on commit 54f83f9

Please sign in to comment.