Skip to content

Commit 109dd7a

Browse files
authored
Improve err/out implementation for node pthreads. NFC (#23999)
The console overrides for node were not correctly formatting their arguments. i.e. all objects were showing up as `[object Object]`
1 parent f9fa234 commit 109dd7a

6 files changed

+17
-24
lines changed

src/runtime_debug.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ 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+
// TODO(sbc): Unify with err/out implementation in shell.sh.
214+
var fs = require('fs');
215+
var utils = require('util');
216+
var stringify = (a) => typeof a == 'object' ? utils.inspect(a) : a;
217+
fs.writeSync(1, args.map(stringify).join(' ') + '\n');
214218
} else
215219
#endif
216220
// TODO(sbc): Make this configurable somehow. Its not always convenient for

src/runtime_pthread.js

-16
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
4343
// Thread-local guard variable for one-time init of the JS state
4444
var initializedJS = false;
4545

46-
function threadPrintErr(...args) {
47-
#if ENVIRONMENT_MAY_BE_NODE
48-
// See https://github.com/emscripten-core/emscripten/issues/14804
49-
if (ENVIRONMENT_IS_NODE) {
50-
fs.writeSync(2, args.join(' ') + '\n');
51-
return;
52-
}
53-
#endif
54-
console.error(...args);
55-
}
56-
5746
#if LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER
5847
// When using postMessage to send an object, it is processed by the structured
5948
// clone algorithm. The prototype, and hence methods, on that object is then
@@ -66,11 +55,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
6655
}
6756
#endif
6857

69-
#if expectToReceiveOnModule('printErr')
70-
if (!Module['printErr'])
71-
#endif
72-
err = threadPrintErr;
73-
7458
// Turn unhandled rejected promises into errors so that the main thread will be
7559
// notified about them.
7660
self.onunhandledrejection = (e) => { throw e.reason || e; };

src/shell.js

+4-2
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) }}}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4037
1+
4045
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8374
1+
8382

test/test_other.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15286,7 +15286,7 @@ def test_preload_module(self, args):
1528615286
assert(found);
1528715287
} else {
1528815288
int found = EM_ASM_INT(
15289-
err(sharedModules);
15289+
err('sharedModules:', sharedModules);
1529015290
return sharedModules['/library.so'] !== undefined;
1529115291
);
1529215292
assert(found);
@@ -15301,7 +15301,10 @@ def test_preload_module(self, args):
1530115301
return 0;
1530215302
}
1530315303
''')
15304-
self.do_runf('main.c', 'done\n', emcc_args=['-sMAIN_MODULE=2', '--preload-file', '[email protected]', '--use-preload-plugins'] + args)
15304+
expected = 'done\n'
15305+
if args:
15306+
expected = "sharedModules: { '/library.so': Module [WebAssembly.Module] {} }\ndone\n"
15307+
self.do_runf('main.c', expected, emcc_args=['-sMAIN_MODULE=2', '--preload-file', '[email protected]', '--use-preload-plugins'] + args)
1530515308

1530615309
@node_pthreads
1530715310
def test_standalone_whole_archive(self):

0 commit comments

Comments
 (0)