Skip to content

Commit 597ffc1

Browse files
authored
Enable MINIMAL_RUNTIME + pthreads + node (#21704)
1 parent 53d7b92 commit 597ffc1

File tree

5 files changed

+85
-42
lines changed

5 files changed

+85
-42
lines changed

src/library.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,9 +3121,7 @@ addToLibrary({
31213121

31223122
#else // MINIMAL_RUNTIME
31233123
// MINIMAL_RUNTIME doesn't support the runtimeKeepalive stuff
3124-
$callUserCallback: (func) => {
3125-
func();
3126-
},
3124+
$callUserCallback: (func) => func(),
31273125
#endif // MINIMAL_RUNTIME
31283126

31293127
$asmjsMangle: (x) => {

src/library_pthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ var LibraryPThread = {
441441
allocateUnusedWorker() {
442442
var worker;
443443
#if MINIMAL_RUNTIME
444-
var pthreadMainJs = Module['worker'];
444+
var pthreadMainJs = Module['worker'] || './{{{ PTHREAD_WORKER_FILE }}}';
445445
#else
446446
#if EXPORT_ES6 && USE_ES6_IMPORT_META
447447
// If we're using module output and there's no explicit override, use bundler-friendly pattern.

src/shell_minimal.js

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,45 +81,31 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
8181
}
8282
#endif
8383

84-
#if !SINGLE_FILE
85-
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
86-
// Wasm or Wasm2JS loading:
87-
88-
if (ENVIRONMENT_IS_NODE) {
89-
var fs = require('fs');
90-
#if WASM == 2
91-
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
92-
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');
93-
#else
94-
#if !WASM2JS
95-
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
96-
#endif
97-
#endif
98-
}
99-
#endif
100-
101-
#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
102-
if (ENVIRONMENT_IS_SHELL) {
103-
#if WASM == 2
104-
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
105-
else eval(read('{{{ TARGET_BASENAME }}}.wasm.js')+'');
106-
#else
107-
#if !WASM2JS
108-
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
109-
#endif
110-
#endif
111-
}
112-
#endif
113-
114-
#endif // !SINGLE_FILE
115-
11684
// Redefine these in a --pre-js to override behavior. If you would like to
11785
// remove out() or err() altogether, you can no-op it out to function() {},
11886
// and build with --closure 1 to get Closure optimize out all the uses
11987
// altogether.
12088

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

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

159+
if (ENVIRONMENT_IS_WORKER) {
160+
_scriptDir = self.location.href;
161+
}
162+
#if ENVIRONMENT_MAY_BE_NODE
163+
else if (ENVIRONMENT_IS_NODE) {
164+
_scriptDir = __filename;
165+
}
166+
#endif
167+
168+
#if ENVIRONMENT_MAY_BE_NODE
169+
if (ENVIRONMENT_IS_NODE) {
170+
global.Worker = require('worker_threads').Worker;
171+
}
172+
#endif
173+
173174
var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ((typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined);
174175
#endif // PTHREADS
176+
177+
#if !SINGLE_FILE
178+
179+
#if PTHREADS
180+
if (!ENVIRONMENT_IS_PTHREAD) {
181+
#endif
182+
183+
#if ENVIRONMENT_MAY_BE_NODE && ((WASM == 1 && !WASM2JS) || WASM == 2)
184+
// Wasm or Wasm2JS loading:
185+
186+
if (ENVIRONMENT_IS_NODE) {
187+
var fs = require('fs');
188+
#if WASM == 2
189+
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
190+
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');
191+
#else
192+
#if !WASM2JS
193+
Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
194+
#endif
195+
#endif
196+
}
197+
#endif
198+
199+
#if ENVIRONMENT_MAY_BE_SHELL && ((WASM == 1 && !WASM2JS) || WASM == 2)
200+
if (ENVIRONMENT_IS_SHELL) {
201+
#if WASM == 2
202+
if (typeof WebAssembly != 'undefined') Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
203+
else eval(read('{{{ TARGET_BASENAME }}}.wasm.js')+'');
204+
#else
205+
#if !WASM2JS
206+
Module['wasm'] = read('{{{ TARGET_BASENAME }}}.wasm', 'binary');
207+
#endif
208+
#endif
209+
}
210+
#endif
211+
212+
#if PTHREADS
213+
}
214+
#endif
215+
216+
#endif // !SINGLE_FILE

src/shell_minimal_runtime.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
<body>
33
<canvas style='display:block; margin:auto;'></canvas>
44
<script>
5+
56
#if !MODULARIZE
6-
var {{{ EXPORT_NAME }}} = {
7-
#if PTHREADS
8-
worker: '{{{ PTHREAD_WORKER_FILE }}}'
9-
#endif
10-
};
7+
var {{{ EXPORT_NAME }}} = {}
118
#endif
129

1310
#if WASM == 2

test/test_core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from common import skip_if, needs_dylink, no_windows, no_mac, is_slow_test, parameterized
2828
from common import env_modify, with_env_modify, disabled, flaky, node_pthreads, also_with_wasm_bigint
2929
from common import read_file, read_binary, requires_v8, requires_node, requires_wasm2js, requires_node_canary
30-
from common import compiler_for, crossplatform, no_4gb, no_2gb
30+
from common import compiler_for, crossplatform, no_4gb, no_2gb, also_with_minimal_runtime
3131
from common import with_both_eh_sjlj, with_both_sjlj, also_with_standalone_wasm, can_do_standalone, no_wasm64
3232
from common import NON_ZERO, WEBIDL_BINDER, EMBUILDER, PYTHON
3333
import clang_native
@@ -2539,7 +2539,10 @@ def test_pthread_setspecific_mainthread(self):
25392539
self.do_run_in_out_file_test('pthread/test_pthread_setspecific_mainthread.c')
25402540

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

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

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

26242630
def test_tcgetattr(self):

0 commit comments

Comments
 (0)