diff options
-rw-r--r-- | src/library.js | 2 | ||||
-rw-r--r-- | src/parseTools.js | 11 | ||||
-rw-r--r-- | tests/runner.py | 15 |
3 files changed, 25 insertions, 3 deletions
diff --git a/src/library.js b/src/library.js index c73b94ad..53bfaa02 100644 --- a/src/library.js +++ b/src/library.js @@ -599,7 +599,7 @@ var Library = { return ret; }, - abs: 'Math.abs', + abs: 'Math.abs', // XXX should be integer? atoi: function(s) { return Math.floor(Number(Pointer_stringify(s))); diff --git a/src/parseTools.js b/src/parseTools.js index 98ebd32c..e520f919 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -692,14 +692,21 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned) { } } -function indexizeFunctions(value) { // TODO: Also check for externals +function indexizeFunctions(value) { if (value in Functions.currFunctions) { return Functions.getIndex(value); } if (value && value[0] && value[0] == '_') { var rootIdent = LibraryManager.getRootIdent(value.slice(1)); - if (rootIdent && typeof Library[rootIdent] === 'function') { + if (!rootIdent) return value; + if (typeof Library[rootIdent] === 'function') { return Functions.getIndex('_' + rootIdent); + } else if (rootIdent.substr(0, 5) === 'Math.') { + // Library[..] can be a string, in which case we apply that string. There is one + // case where this can be a function: Math.*, since we try to optimize those as much + // as possible. In other words, we don't want to have a wrapper function(x) return Math.sqrt(x). + // If other functions are deemed important as well, we will need to add them here. + return Functions.getIndex(rootIdent); } } return value; diff --git a/tests/runner.py b/tests/runner.py index 59062302..4215bfea 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -1097,6 +1097,21 @@ if 'benchmark' not in sys.argv: ''' self.do_test(src, '*26,26,90,90,26,90*\n*1,0,0,1*\n*goodbye!*') + def test_mathfuncptr(self): + src = ''' + #include <math.h> + #include <stdio.h> + + int + main(void) { + float (*fn)(float) = &sqrtf; + float (*fn2)(float) = &fabsf; + printf("fn2(-5) = %d, fn(10) = %f\\n", (int)fn2(-5), fn(10)); + return 0; + } + ''' + self.do_test(src, 'fn2(-5) = 5, fn(10) = 3.16') + def test_emptyclass(self): src = ''' #include <stdio.h> |