From 754ba991083952041418df4da54d01d85f9d73f1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 24 Jun 2013 16:59:39 -0700 Subject: [PATCH] refactor and calculate new imports --- emlink.py | 25 +++++++++++++++++++------ tests/runner.py | 10 +++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/emlink.py b/emlink.py index 686a2bed5..da393036f 100644 --- a/emlink.py +++ b/emlink.py @@ -29,19 +29,32 @@ print 'Output:', out class AsmModule(): def __init__(self, filename): self.js = open(filename).read() + + start_asm = self.js.find(js_optimizer.start_asm_marker) + start_funcs = self.js.find(js_optimizer.start_funcs_marker) + end_funcs = self.js.rfind(js_optimizer.end_funcs_marker) + end_asm = self.js.rfind(js_optimizer.end_asm_marker) + # imports - imports_js = self.js[self.js.find(js_optimizer.start_asm_marker):self.js.rfind(js_optimizer.start_funcs_marker)] - self.imports = [m.group(0) for m in js_optimizer.import_sig.finditer(imports_js)] + self.imports_js = self.js[start_asm:start_funcs] + self.imports = [m.group(0) for m in js_optimizer.import_sig.finditer(self.imports_js)] #print 'imports', self.imports # funcs - funcs_js = self.js[self.js.find(js_optimizer.start_funcs_marker):self.js.rfind(js_optimizer.end_funcs_marker)] - self.funcs = [m.group(2) for m in js_optimizer.func_sig.finditer(funcs_js)] + self.funcs_js = self.js[start_funcs:end_funcs] + self.funcs = [m.group(2) for m in js_optimizer.func_sig.finditer(self.funcs_js)] #print 'funcs', self.funcs - # exports + # tables and exports + self.post_js = self.js[end_funcs:end_asm] def relocate(self, main): + # imports + main_imports = set(main.imports) + new_imports = [imp for imp in self.imports if imp not in main_imports] + print 'new imports', new_imports + main.imports_js += '\n'.join(new_imports) + # Find function name replacements TODO: do not rename duplicate names with duplicate contents, just merge them main_funcs = set(main.funcs) replacements = {} @@ -50,7 +63,7 @@ class AsmModule(): while rep in main_funcs: rep += '_' replacements[func] = rep - print replacements + #print replacements def write(self, out): open(out, 'w').write(self.js) diff --git a/tests/runner.py b/tests/runner.py index e2545deec..d9f14ed96 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -10614,17 +10614,17 @@ f.close() return 0; } ''') - open(os.path.join(self.get_dir(), 'lib.cpp'), 'w').write(''' + open(os.path.join(self.get_dir(), 'side.cpp'), 'w').write(''' #include void printey() { - printf("hello from lib\\n"); + printf("hello from side\\n"); } ''') - Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'lib.cpp'), '-o', 'lib.js', '-s', 'SIDE_MODULE=1', '-O2']).communicate() + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'side.cpp'), '-o', 'side.js', '-s', 'SIDE_MODULE=1', '-O2']).communicate() # TODO: test with and without DISABLE_GL_EMULATION, check that file sizes change Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'main.cpp'), '-o', 'main.js', '-s', 'MAIN_MODULE=1', '-O2', '-s', 'DISABLE_GL_EMULATION=1']).communicate() - Popen([PYTHON, EMLINK, 'main.js', 'lib.js', 'together.js']) - self.assertContained('hello from lib', run_js('together.js')) + Popen([PYTHON, EMLINK, 'main.js', 'side.js', 'together.js']) + self.assertContained('hello from side', run_js('together.js')) def test_symlink(self): if os.name == 'nt':