Skip to content

Commit 936ccd9

Browse files
committed
indexize Math.* properly. fixes issue 29
1 parent c9b968a commit 936ccd9

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/library.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ var Library = {
599599
return ret;
600600
},
601601

602-
abs: 'Math.abs',
602+
abs: 'Math.abs', // XXX should be integer?
603603

604604
atoi: function(s) {
605605
return Math.floor(Number(Pointer_stringify(s)));

src/parseTools.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -692,14 +692,21 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned) {
692692
}
693693
}
694694

695-
function indexizeFunctions(value) { // TODO: Also check for externals
695+
function indexizeFunctions(value) {
696696
if (value in Functions.currFunctions) {
697697
return Functions.getIndex(value);
698698
}
699699
if (value && value[0] && value[0] == '_') {
700700
var rootIdent = LibraryManager.getRootIdent(value.slice(1));
701-
if (rootIdent && typeof Library[rootIdent] === 'function') {
701+
if (!rootIdent) return value;
702+
if (typeof Library[rootIdent] === 'function') {
702703
return Functions.getIndex('_' + rootIdent);
704+
} else if (rootIdent.substr(0, 5) === 'Math.') {
705+
// Library[..] can be a string, in which case we apply that string. There is one
706+
// case where this can be a function: Math.*, since we try to optimize those as much
707+
// as possible. In other words, we don't want to have a wrapper function(x) return Math.sqrt(x).
708+
// If other functions are deemed important as well, we will need to add them here.
709+
return Functions.getIndex(rootIdent);
703710
}
704711
}
705712
return value;

tests/runner.py

+15
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,21 @@ def test_funcptr(self):
10971097
'''
10981098
self.do_test(src, '*26,26,90,90,26,90*\n*1,0,0,1*\n*goodbye!*')
10991099

1100+
def test_mathfuncptr(self):
1101+
src = '''
1102+
#include <math.h>
1103+
#include <stdio.h>
1104+
1105+
int
1106+
main(void) {
1107+
float (*fn)(float) = &sqrtf;
1108+
float (*fn2)(float) = &fabsf;
1109+
printf("fn2(-5) = %d, fn(10) = %f\\n", (int)fn2(-5), fn(10));
1110+
return 0;
1111+
}
1112+
'''
1113+
self.do_test(src, 'fn2(-5) = 5, fn(10) = 3.16')
1114+
11001115
def test_emptyclass(self):
11011116
src = '''
11021117
#include <stdio.h>

0 commit comments

Comments
 (0)