Skip to content
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

Enable MINIMAL_RUNTIME + pthreads + node #21704

Merged
merged 1 commit into from
Apr 6, 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
4 changes: 1 addition & 3 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3121,9 +3121,7 @@ addToLibrary({

#else // MINIMAL_RUNTIME
// MINIMAL_RUNTIME doesn't support the runtimeKeepalive stuff
$callUserCallback: (func) => {
func();
},
$callUserCallback: (func) => func(),
#endif // MINIMAL_RUNTIME

$asmjsMangle: (x) => {
Expand Down
2 changes: 1 addition & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ var LibraryPThread = {
allocateUnusedWorker() {
var worker;
#if MINIMAL_RUNTIME
var pthreadMainJs = Module['worker'];
var pthreadMainJs = Module['worker'] || './{{{ PTHREAD_WORKER_FILE }}}';
#else
#if EXPORT_ES6 && USE_ES6_IMPORT_META
// If we're using module output and there's no explicit override, use bundler-friendly pattern.
Expand Down
106 changes: 74 additions & 32 deletions src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,31 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
}
#endif

#if !SINGLE_FILE
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
// Wasm or Wasm2JS loading:

if (ENVIRONMENT_IS_NODE) {
var fs = require('fs');
#if WASM == 2
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');
#else
#if !WASM2JS
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
#endif
#endif
}
#endif

#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
if (ENVIRONMENT_IS_SHELL) {
#if WASM == 2
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
else eval(read('{{{ TARGET_BASENAME }}}.wasm.js')+'');
#else
#if !WASM2JS
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
#endif
#endif
}
#endif

#endif // !SINGLE_FILE

// Redefine these in a --pre-js to override behavior. If you would like to
// remove out() or err() altogether, you can no-op it out to function() {},
// and build with --closure 1 to get Closure optimize out all the uses
// altogether.

#if ENVIRONMENT_MAY_BE_NODE && PTHREADS
// Set up the out() and err() hooks, which are how we can print to stdout or
// stderr, respectively.
// Normally just binding console.log/console.error here works fine, but
// under node (with workers) we see missing/out-of-order messages so route
// directly to stdout and stderr.
// See https://github.com/emscripten-core/emscripten/issues/14804
var defaultPrint = console.log.bind(console);
var defaultPrintErr = console.error.bind(console);
if (ENVIRONMENT_IS_NODE) {
var fs = require('fs');
defaultPrint = (...args) => fs.writeSync(1, args.join(' ') + '\n');
defaultPrintErr = (...args) => fs.writeSync(2, args.join(' ') + '\n');
}
var out = defaultPrint;
var err = defaultPrintErr;
#else
var out = (text) => console.log(text);
var err = (text) => console.error(text);
#endif

// Override this function in a --pre-js file to get a signal for when
// compilation is ready. In that callback, call the function run() to start
Expand Down Expand Up @@ -170,5 +156,61 @@ var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function',
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER;
#endif

if (ENVIRONMENT_IS_WORKER) {
_scriptDir = self.location.href;
}
#if ENVIRONMENT_MAY_BE_NODE
else if (ENVIRONMENT_IS_NODE) {
_scriptDir = __filename;
}
#endif

#if ENVIRONMENT_MAY_BE_NODE
if (ENVIRONMENT_IS_NODE) {
global.Worker = require('worker_threads').Worker;
}
#endif

var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ((typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined);
#endif // PTHREADS

#if !SINGLE_FILE

#if PTHREADS
if (!ENVIRONMENT_IS_PTHREAD) {
#endif

#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
// Wasm or Wasm2JS loading:

if (ENVIRONMENT_IS_NODE) {
var fs = require('fs');
#if WASM == 2
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');
#else
#if !WASM2JS
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
#endif
#endif
}
#endif

#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
if (ENVIRONMENT_IS_SHELL) {
#if WASM == 2
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
else eval(read('{{{ TARGET_BASENAME }}}.wasm.js')+'');
#else
#if !WASM2JS
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
#endif
#endif
}
#endif

#if PTHREADS
}
#endif

#endif // !SINGLE_FILE
7 changes: 2 additions & 5 deletions src/shell_minimal_runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
<body>
<canvas style='display:block; margin:auto;'></canvas>
<script>

#if !MODULARIZE
var {{{ EXPORT_NAME }}} = {
#if PTHREADS
worker: '{{{ PTHREAD_WORKER_FILE }}}'
#endif
};
var {{{ EXPORT_NAME }}} = {}
#endif

#if WASM == 2
Expand Down
8 changes: 7 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from common import skip_if, needs_dylink, no_windows, no_mac, is_slow_test, parameterized
from common import env_modify, with_env_modify, disabled, flaky, node_pthreads, also_with_wasm_bigint
from common import read_file, read_binary, requires_v8, requires_node, requires_wasm2js, requires_node_canary
from common import compiler_for, crossplatform, no_4gb, no_2gb
from common import compiler_for, crossplatform, no_4gb, no_2gb, also_with_minimal_runtime
from common import with_both_eh_sjlj, with_both_sjlj, also_with_standalone_wasm, can_do_standalone, no_wasm64
from common import NON_ZERO, WEBIDL_BINDER, EMBUILDER, PYTHON
import clang_native
Expand Down Expand Up @@ -2539,7 +2539,10 @@ def test_pthread_setspecific_mainthread(self):
self.do_run_in_out_file_test('pthread/test_pthread_setspecific_mainthread.c')

@node_pthreads
@also_with_minimal_runtime
def test_pthread_attr_getstack(self):
if self.get_setting('MINIMAL_RUNTIME') and is_sanitizing(self.emcc_args):
self.skipTest('MINIMAL_RUNTIME + threads + asan does not work')
self.set_setting('PTHREAD_POOL_SIZE', 1)
self.do_run_in_out_file_test('pthread/test_pthread_attr_getstack.c')

Expand Down Expand Up @@ -2618,7 +2621,10 @@ def test_pthread_wait_async(self):
self.do_run_in_out_file_test('atomic/test_wait_async.c')

@node_pthreads
@also_with_minimal_runtime
def test_pthread_run_on_main_thread(self):
if self.get_setting('MINIMAL_RUNTIME') and is_sanitizing(self.emcc_args):
self.skipTest('MINIMAL_RUNTIME + threads + asan does not work')
self.do_run_in_out_file_test('pthread/test_pthread_run_on_main_thread.c')

def test_tcgetattr(self):
Expand Down
Loading