aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-01-16 14:12:25 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-01-16 14:17:12 -0800
commit62de2bd37e003763733b3147ea32164a68e5962e (patch)
tree45bfaf799ab38bf3dd4b01bd3f668e31fb67ca6c
parent41675a7501a7d6b07f1c4a8045209ec96e9507bd (diff)
don't override Module.print and printErr if the user specified them
-rw-r--r--src/shell.js12
-rw-r--r--tests/test_other.py18
2 files changed, 23 insertions, 7 deletions
diff --git a/src/shell.js b/src/shell.js
index b41fbb51..84844c85 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -38,10 +38,10 @@ var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIR
if (ENVIRONMENT_IS_NODE) {
// Expose functionality in the same simple way that the shells work
// Note that we pollute the global namespace here, otherwise we break in node
- Module['print'] = function print(x) {
+ if (!Module['print']) Module['print'] = function print(x) {
process['stdout'].write(x + '\n');
};
- Module['printErr'] = function printErr(x) {
+ if (!Module['printErr']) Module['printErr'] = function printErr(x) {
process['stderr'].write(x + '\n');
};
@@ -71,7 +71,7 @@ if (ENVIRONMENT_IS_NODE) {
module['exports'] = Module;
}
else if (ENVIRONMENT_IS_SHELL) {
- Module['print'] = print;
+ if (!Module['print']) Module['print'] = print;
if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm
if (typeof read != 'undefined') {
@@ -107,16 +107,16 @@ else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
}
if (typeof console !== 'undefined') {
- Module['print'] = function print(x) {
+ if (!Module['print']) Module['print'] = function print(x) {
console.log(x);
};
- Module['printErr'] = function printErr(x) {
+ if (!Module['printErr']) Module['printErr'] = function printErr(x) {
console.log(x);
};
} else {
// Probably a worker, and without console.log. We can do very little here...
var TRY_USE_DUMP = false;
- Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) {
+ if (!Module['print']) Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) {
dump(x);
}) : (function(x) {
// self.postMessage(x); // enable this if you want stdout to be sent as messages
diff --git a/tests/test_other.py b/tests/test_other.py
index 5e5dd7a6..b2fc4cf6 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -2221,7 +2221,6 @@ mergeInto(LibraryManager.library, {
process.communicate()
assert(os.path.isfile(outdir + 'hello_world.obj'))
-
def test_doublestart_bug(self):
open('code.cpp', 'w').write(r'''
#include <stdio.h>
@@ -2253,3 +2252,20 @@ Module["preRun"].push(function () {
assert output.count('This should only appear once.') == 1, '\n'+output
+ def test_module_print(self):
+ open('code.cpp', 'w').write(r'''
+#include <stdio.h>
+int main(void) {
+ printf("123456789\n");
+ return 0;
+}
+''')
+
+ open('pre.js', 'w').write(r'''
+var Module = { print: function(x) { throw '<{(' + x + ')}>' } };
+''')
+
+ Popen([PYTHON, EMCC, 'code.cpp', '--pre-js', 'pre.js']).communicate()
+ output = run_js(os.path.join(self.get_dir(), 'a.out.js'), stderr=PIPE, full_output=True, engine=NODE_JS)
+ assert r'<{(123456789)}>' in output, output
+