Skip to content

Commit

Permalink
Enable MINIMAL_RUNTIME + pthreads + node
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Apr 5, 2024
1 parent 5e9ed03 commit 3be56a0
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 74 deletions.
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: 0 additions & 7 deletions src/shell_minimal_runtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
<body>
<canvas style='display:block; margin:auto;'></canvas>
<script>
#if !MODULARIZE
var {{{ EXPORT_NAME }}} = {
#if PTHREADS
worker: '{{{ PTHREAD_WORKER_FILE }}}'
#endif
};
#endif

#if WASM == 2
var supportsWasm = window.WebAssembly && location.search.indexOf('_rwasm=0') < 0;
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.html": 659,
"a.html.gz": 424,
"a.js": 7080,
"a.js.gz": 2999,
"a.wasm": 11611,
"a.wasm.gz": 5816,
"total": 19364,
"total_gz": 9246
"total": 19350,
"total_gz": 9239
}
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 737,
"a.html.gz": 433,
"a.html": 723,
"a.html.gz": 427,
"a.js": 665,
"a.js.gz": 455,
"a.wasm": 1852,
"a.wasm.gz": 1049,
"total": 3254,
"total_gz": 1937
"total": 3240,
"total_gz": 1931
}
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.html": 659,
"a.html.gz": 424,
"a.js": 291,
"a.js.gz": 249,
"a.wasm": 103,
"a.wasm.gz": 113,
"total": 1067,
"total_gz": 793
"total": 1053,
"total_gz": 786
}
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 323,
"a.html.gz": 253,
"a.html": 309,
"a.html.gz": 243,
"a.js": 1060,
"a.js.gz": 636,
"total": 1383,
"total_gz": 889
"total": 1369,
"total_gz": 879
}
8 changes: 4 additions & 4 deletions test/code_size/math_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.html": 659,
"a.html.gz": 424,
"a.js": 110,
"a.js.gz": 125,
"a.wasm": 2723,
"a.wasm.gz": 1667,
"total": 3506,
"total_gz": 2223
"total": 3492,
"total_gz": 2216
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 12766,
"a.html.gz": 7017,
"total": 12766,
"total_gz": 7017
"a.html": 12756,
"a.html.gz": 7011,
"total": 12756,
"total_gz": 7011
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 17345,
"a.html.gz": 7549,
"total": 17345,
"total_gz": 7549
"a.html": 17335,
"a.html.gz": 7545,
"total": 17335,
"total_gz": 7545
}
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6100
6107
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13505
13525
4 changes: 3 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,6 +2539,7 @@ 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):
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,6 +2619,7 @@ 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):
self.do_run_in_out_file_test('pthread/test_pthread_run_on_main_thread.c')

Expand Down

0 comments on commit 3be56a0

Please sign in to comment.