Skip to content

Commit 62de2bd

Browse files
committed
don't override Module.print and printErr if the user specified them
1 parent 41675a7 commit 62de2bd

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/shell.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIR
3838
if (ENVIRONMENT_IS_NODE) {
3939
// Expose functionality in the same simple way that the shells work
4040
// Note that we pollute the global namespace here, otherwise we break in node
41-
Module['print'] = function print(x) {
41+
if (!Module['print']) Module['print'] = function print(x) {
4242
process['stdout'].write(x + '\n');
4343
};
44-
Module['printErr'] = function printErr(x) {
44+
if (!Module['printErr']) Module['printErr'] = function printErr(x) {
4545
process['stderr'].write(x + '\n');
4646
};
4747

@@ -71,7 +71,7 @@ if (ENVIRONMENT_IS_NODE) {
7171
module['exports'] = Module;
7272
}
7373
else if (ENVIRONMENT_IS_SHELL) {
74-
Module['print'] = print;
74+
if (!Module['print']) Module['print'] = print;
7575
if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm
7676

7777
if (typeof read != 'undefined') {
@@ -107,16 +107,16 @@ else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
107107
}
108108

109109
if (typeof console !== 'undefined') {
110-
Module['print'] = function print(x) {
110+
if (!Module['print']) Module['print'] = function print(x) {
111111
console.log(x);
112112
};
113-
Module['printErr'] = function printErr(x) {
113+
if (!Module['printErr']) Module['printErr'] = function printErr(x) {
114114
console.log(x);
115115
};
116116
} else {
117117
// Probably a worker, and without console.log. We can do very little here...
118118
var TRY_USE_DUMP = false;
119-
Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) {
119+
if (!Module['print']) Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) {
120120
dump(x);
121121
}) : (function(x) {
122122
// self.postMessage(x); // enable this if you want stdout to be sent as messages

tests/test_other.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,6 @@ def test_default_obj_ext(self):
22212221
process.communicate()
22222222
assert(os.path.isfile(outdir + 'hello_world.obj'))
22232223

2224-
22252224
def test_doublestart_bug(self):
22262225
open('code.cpp', 'w').write(r'''
22272226
#include <stdio.h>
@@ -2253,3 +2252,20 @@ def test_doublestart_bug(self):
22532252

22542253
assert output.count('This should only appear once.') == 1, '\n'+output
22552254

2255+
def test_module_print(self):
2256+
open('code.cpp', 'w').write(r'''
2257+
#include <stdio.h>
2258+
int main(void) {
2259+
printf("123456789\n");
2260+
return 0;
2261+
}
2262+
''')
2263+
2264+
open('pre.js', 'w').write(r'''
2265+
var Module = { print: function(x) { throw '<{(' + x + ')}>' } };
2266+
''')
2267+
2268+
Popen([PYTHON, EMCC, 'code.cpp', '--pre-js', 'pre.js']).communicate()
2269+
output = run_js(os.path.join(self.get_dir(), 'a.out.js'), stderr=PIPE, full_output=True, engine=NODE_JS)
2270+
assert r'<{(123456789)}>' in output, output
2271+

0 commit comments

Comments
 (0)