From 81404885333898b87da39c074b33ed70f327ea63 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sun, 17 Jun 2012 13:24:22 -0700 Subject: [PATCH] allow multiple pre-js and post-js --- emcc | 10 +++++----- tests/runner.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/emcc b/emcc index 7b1f9f981..3b84d3253 100755 --- a/emcc +++ b/emcc @@ -409,8 +409,8 @@ try: llvm_lto = None closure = None js_transform = None - pre_js = None - post_js = None + pre_js = '' + post_js = '' minify_whitespace = None preload_files = [] embed_files = [] @@ -452,12 +452,12 @@ try: newargs[i+1] = '' elif newargs[i].startswith('--pre-js'): check_bad_eq(newargs[i]) - pre_js = open(newargs[i+1]).read() + pre_js += open(newargs[i+1]).read() + '\n' newargs[i] = '' newargs[i+1] = '' elif newargs[i].startswith('--post-js'): check_bad_eq(newargs[i]) - post_js = open(newargs[i+1]).read() + post_js += open(newargs[i+1]).read() + '\n' newargs[i] = '' newargs[i+1] = '' elif newargs[i].startswith('--minify'): @@ -860,7 +860,7 @@ try: if DEBUG: print >> sys.stderr, 'emcc: applying pre/postjses' src = open(final).read() final += '.pp.js' - open(final, 'w').write((pre_js or '') + src + (post_js or '')) + open(final, 'w').write(pre_js + src + post_js) if DEBUG: save_intermediate('pre-post') # Apply a source code transformation, if requested diff --git a/tests/runner.py b/tests/runner.py index 5714511e2..f8dfc97ed 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -7028,6 +7028,25 @@ f.close() open(os.path.join(self.get_dir(), 'a.out.js'), 'w').write(src) assert 'hello from main' in run_js(os.path.join(self.get_dir(), 'a.out.js')), 'main should print when called manually' + def test_prepost2(self): + open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(''' + #include + 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.postRun = function() { Module.print('post-run') }; + ''') + 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_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()