aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/postamble.js17
-rw-r--r--src/shell.js4
-rwxr-xr-xtests/runner.py19
3 files changed, 34 insertions, 6 deletions
diff --git a/src/postamble.js b/src/postamble.js
index 56586487..10ac1888 100644
--- a/src/postamble.js
+++ b/src/postamble.js
@@ -33,11 +33,13 @@ function run(args) {
args = args || Module['arguments'];
if (Module['preRun']) {
- Module['preRun']();
- if (runDependencies > 0) {
- // preRun added a dependency, run will be called later
- Module['preRun'] = null;
- return 0;
+ if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
+ while (Module['preRun'].length > 0) {
+ Module['preRun'].pop()();
+ if (runDependencies > 0) {
+ // preRun added a dependency, run will be called later
+ return 0;
+ }
}
}
@@ -51,7 +53,10 @@ function run(args) {
}
}
if (Module['postRun']) {
- Module['postRun']();
+ if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
+ while (Module['postRun'].length > 0) {
+ Module['postRun'].pop()();
+ }
}
return ret;
}
diff --git a/src/shell.js b/src/shell.js
index e48be3b0..754cdb8e 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -119,6 +119,10 @@ if (!Module['arguments']) {
Module.print = Module['print'];
Module.printErr = Module['printErr'];
+// Callbacks
+if (!Module['preRun']) Module['preRun'] = [];
+if (!Module['postRun']) Module['postRun'] = [];
+
{{BODY}}
// {{MODULE_ADDITIONS}}
diff --git a/tests/runner.py b/tests/runner.py
index f8dfc97e..852dce42 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7047,6 +7047,25 @@ f.close()
Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--pre-js', 'pre.js', '--pre-js', 'pre2.js']).communicate()
self.assertContained('pre-run\nhello from main\npost-run\n', run_js(os.path.join(self.get_dir(), 'a.out.js')))
+ def test_prepre(self):
+ open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write('''
+ #include <stdio.h>
+ int main() {
+ printf("hello from main\\n");
+ return 0;
+ }
+ ''')
+ open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''
+ var Module = {
+ preRun: [function() { Module.print('pre-run') }],
+ };
+ ''')
+ open(os.path.join(self.get_dir(), 'pre2.js'), 'w').write('''
+ Module.preRun.push(function() { Module.print('prepre') });
+ ''')
+ Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--pre-js', 'pre.js', '--pre-js', 'pre2.js']).communicate()
+ self.assertContained('prepre\npre-run\nhello from main\n', run_js(os.path.join(self.get_dir(), 'a.out.js')))
+
def test_eliminator(self):
input = open(path_from_root('tools', 'eliminator', 'eliminator-test.js')).read()
expected = open(path_from_root('tools', 'eliminator', 'eliminator-test-output.js')).read()