diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-04-29 17:14:08 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-04-29 17:14:08 -0700 |
commit | 60960a6a1f5137239af6b11d91d1ca668f54f5f1 (patch) | |
tree | 76bac93f97480cc84b780070985e7bce0092131b | |
parent | d6ea75e7f22a5efdfe689ed65e5b1cb0806762f2 (diff) |
do not get confused by local vars that alias names of library functions, and are used as function pointers
-rw-r--r-- | src/jsifier.js | 21 | ||||
-rw-r--r-- | src/parseTools.js | 4 | ||||
-rwxr-xr-x | tests/runner.py | 30 |
3 files changed, 47 insertions, 8 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 3f47e967..350d2c8b 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -1326,16 +1326,21 @@ function JSify(data, functionsOnly, givenFunctions) { ident = Variables.resolveAliasToIdent(ident); var shortident = ident.slice(1); var simpleIdent = shortident; - var callIdent = LibraryManager.getRootIdent(simpleIdent); - if (callIdent) { - simpleIdent = callIdent; // ident may not be in library, if all there is is ident__inline, but in this case it is - if (callIdent.indexOf('.') < 0) { - callIdent = '_' + callIdent; // Not Math.*, so add the normal prefix - } + if (isLocalVar(ident)) { + var callIdent = ident; } else { - callIdent = ident; + // Not a local var, check if in library + var callIdent = LibraryManager.getRootIdent(simpleIdent); + if (callIdent) { + simpleIdent = callIdent; // ident may not be in library, if all there is is ident__inline, but in this case it is + if (callIdent.indexOf('.') < 0) { + callIdent = '_' + callIdent; // Not Math.*, so add the normal prefix + } + } else { + callIdent = ident; + } + if (callIdent == '0') return 'abort(-2)'; } - if (callIdent == '0') return 'abort(-2)'; var args = []; var argsTypes = []; diff --git a/src/parseTools.js b/src/parseTools.js index 6b6196f0..39de4b7c 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -109,6 +109,10 @@ function isJSVar(ident) { } +function isLocalVar(ident) { + return ident[0] == '$'; +} + function isStructPointerType(type) { // This test is necessary for clang - in llvm-gcc, we // could check for %struct. The downside is that %1 can diff --git a/tests/runner.py b/tests/runner.py index f9f46f10..de3c72de 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -3336,6 +3336,36 @@ Exiting setjmp function, level: 0, prev_jmp: -1 ''' self.do_run(src, '*0x1*') + def test_funcptr_namecollide(self): + src = r''' + #include <stdio.h> + + void do_call(void (*puts)(const char *), const char *str); + + void do_print(const char *str) { + if (!str) do_call(NULL, "delusion"); + if ((int)str == -1) do_print(str+10); + puts("===="); + puts(str); + puts("===="); + } + + void do_call(void (*puts)(const char *), const char *str) { + if (!str) do_print("confusion"); + if ((int)str == -1) do_call(NULL, str-10); + (*puts)(str); + } + + int main(int argc, char **argv) + { + for (int i = 0; i < argc; i++) { + do_call(i != 10 ? do_print : NULL, i != 15 ? "waka waka" : NULL); + } + return 0; + } + ''' + self.do_run(src, 'waka', force_c=True) + def test_emptyclass(self): if self.emcc_args is None: return self.skip('requires emcc') src = ''' |