Skip to content

Commit 5977d2e

Browse files
committed
Unify and improve err/out implementation for node pthreads. NFC
We were overriding err in two different places. The overridden version of node was not correctly formatting its arguments. i.e. all objects were showing up as `[object Object]`
1 parent 7a2ee39 commit 5977d2e

6 files changed

+13
-24
lines changed

src/runtime_debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ function dbg(...args) {
209209
#if ENVIRONMENT_MAY_BE_NODE && PTHREADS
210210
// Avoid using the console for debugging in multi-threaded node applications
211211
// See https://github.com/emscripten-core/emscripten/issues/14804
212-
if (ENVIRONMENT_IS_NODE && fs) {
213-
fs.writeSync(2, args.join(' ') + '\n');
212+
if (ENVIRONMENT_IS_NODE) {
213+
err(...args);
214214
} else
215215
#endif
216216
// TODO(sbc): Make this configurable somehow. Its not always convenient for

src/runtime_pthread.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
3737
// Thread-local guard variable for one-time init of the JS state
3838
var initializedJS = false;
3939

40-
function threadPrintErr(...args) {
41-
#if ENVIRONMENT_MAY_BE_NODE
42-
// See https://github.com/emscripten-core/emscripten/issues/14804
43-
if (ENVIRONMENT_IS_NODE) {
44-
fs.writeSync(2, args.join(' ') + '\n');
45-
return;
46-
}
47-
#endif
48-
console.error(...args);
49-
}
50-
5140
#if LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER
5241
// When using postMessage to send an object, it is processed by the structured
5342
// clone algorithm. The prototype, and hence methods, on that object is then
@@ -60,11 +49,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
6049
}
6150
#endif
6251

63-
#if expectToReceiveOnModule('printErr')
64-
if (!Module['printErr'])
65-
#endif
66-
err = threadPrintErr;
67-
6852
// Turn unhandled rejected promises into errors so that the main thread will be
6953
// notified about them.
7054
self.onunhandledrejection = (e) => { throw e.reason || e; };

src/shell.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ if (!ENVIRONMENT_IS_AUDIO_WORKLET)
390390
var defaultPrint = console.log.bind(console);
391391
var defaultPrintErr = console.error.bind(console);
392392
if (ENVIRONMENT_IS_NODE) {
393-
defaultPrint = (...args) => fs.writeSync(1, args.join(' ') + '\n');
394-
defaultPrintErr = (...args) => fs.writeSync(2, args.join(' ') + '\n');
393+
var utils = require('util');
394+
var stringify = (a) => typeof a == 'object' ? utils.inspect(a) : a;
395+
defaultPrint = (...args) => fs.writeSync(1, args.map(stringify).join(' ') + '\n');
396+
defaultPrintErr = (...args) => fs.writeSync(2, args.map(stringify).join(' ') + '\n');
395397
}
396398
{{{ makeModuleReceiveWithVar('out', 'print', 'defaultPrint', true) }}}
397399
{{{ makeModuleReceiveWithVar('err', 'printErr', 'defaultPrintErr', true) }}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4037
1+
4045
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8374
1+
8382

test/test_other.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15280,7 +15280,7 @@ def test_preload_module(self, args):
1528015280
assert(found);
1528115281
} else {
1528215282
int found = EM_ASM_INT(
15283-
err(sharedModules);
15283+
err('sharedModules:', sharedModules);
1528415284
return sharedModules['/library.so'] !== undefined;
1528515285
);
1528615286
assert(found);
@@ -15295,7 +15295,10 @@ def test_preload_module(self, args):
1529515295
return 0;
1529615296
}
1529715297
''')
15298-
self.do_runf('main.c', 'done\n', emcc_args=['-sMAIN_MODULE=2', '--preload-file', 'tmp.so@library.so', '--use-preload-plugins'] + args)
15298+
expected = 'done\n'
15299+
if args:
15300+
expected = "sharedModules: { '/library.so': Module [WebAssembly.Module] {} }\ndone\n"
15301+
self.do_runf('main.c', expected, emcc_args=['-sMAIN_MODULE=2', '--preload-file', 'tmp.so@library.so', '--use-preload-plugins'] + args)
1529915302

1530015303
@node_pthreads
1530115304
def test_standalone_whole_archive(self):

0 commit comments

Comments
 (0)