From 96189e4f02f5e78481925ab2add6044a2a55fa5e Mon Sep 17 00:00:00 2001 From: max99x Date: Thu, 25 Aug 2011 08:26:39 +0300 Subject: [PATCH] Fixed usage of parent global vars inside shared libs. --- src/jsifier.js | 7 ++++++- tests/runner.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/jsifier.js b/src/jsifier.js index 1e02555f6..76e4ec9ea 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -186,7 +186,12 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { '\n}\n'; return ret; } else { - item.JS = 'var ' + item.ident + ';'; + if (item.external && BUILD_AS_SHARED_LIB) { + // External variables in shared libraries should not be declared. + item.JS = ''; + } else { + item.JS = 'var ' + item.ident + ';'; + } var constant = null; if (item.external) { return ret; diff --git a/tests/runner.py b/tests/runner.py index 39cab477a..06e2e99ae 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -2043,6 +2043,53 @@ if 'benchmark' not in sys.argv: output_nicerizer=lambda x: x.replace('\n', '*'), post_build=add_pre_run_and_checks) + def test_dlfcn_alias(self): + global BUILD_AS_SHARED_LIB, EXPORTED_FUNCTIONS, INCLUDE_FULL_LIBRARY + lib_src = r''' + #include + extern int parent_global; + void func() { + printf("Parent global: %d.\n", parent_global); + } + ''' + dirname = self.get_dir() + filename = os.path.join(dirname, 'liblib.cpp') + BUILD_AS_SHARED_LIB = 1 + EXPORTED_FUNCTIONS = ['__Z4funcv'] + self.build(lib_src, dirname, filename) + shutil.move(filename + '.o.js', os.path.join(dirname, 'liblib.so')) + + src = r''' + #include + + int parent_global = 123; + + int main() { + void* lib_handle; + void (*fptr)(); + + lib_handle = dlopen("liblib.so", RTLD_NOW); + fptr = (void (*)())dlsym(lib_handle, "_Z4funcv"); + fptr(); + parent_global = 456; + fptr(); + + return 0; + } + ''' + BUILD_AS_SHARED_LIB = 0 + INCLUDE_FULL_LIBRARY = 1 + EXPORTED_FUNCTIONS = ['_main'] + def add_pre_run_and_checks(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + '''FS.createLazyFile('/', 'liblib.so', 'liblib.so', true, false);''' + ) + open(filename, 'w').write(src) + self.do_test(src, 'Parent global: 123.*Parent global: 456.*', + output_nicerizer=lambda x: x.replace('\n', '*'), + post_build=add_pre_run_and_checks) + def test_rand(self): src = r''' #include