Skip to content

Fix pthread + modularize + running in node worker #21832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/wasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if (ENVIRONMENT_IS_NODE) {
self: global,
require,
__filename,
__dirname,
Worker: nodeWorkerThreads.Worker,
importScripts: (f) => vm.runInThisContext(fs.readFileSync(f, 'utf8'), {filename: f}),
postMessage: (msg) => parentPort.postMessage(msg),
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