diff options
-rw-r--r-- | src/intertyper.js | 4 | ||||
-rw-r--r-- | src/jsifier.js | 6 | ||||
-rw-r--r-- | src/postamble.js | 5 | ||||
-rw-r--r-- | tests/runner.py | 17 |
4 files changed, 28 insertions, 4 deletions
diff --git a/src/intertyper.js b/src/intertyper.js index 99a7d76f..6b52d5c7 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -352,7 +352,11 @@ function intertyper(data, parseFunctions) { if (item.tokens[3].text == 'c') item.tokens.splice(3, 1); + ret.value = item.tokens[3]; + if (ret.value.text in PARSABLE_LLVM_FUNCTIONS) { + ret.value = parseLLVMFunctionCall(item.tokens.slice(2)); + } } return [ret]; } diff --git a/src/jsifier.js b/src/jsifier.js index bac2038d..4c02cf26 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -147,7 +147,9 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { // Gets an entire constant expression function parseConst(value, type) { //dprint('gconst', '//yyyyy ' + JSON.stringify(value) + ',' + type + '\n'); - if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { + if (value.intertype) { + return makePointer(finalizeLLVMFunctionCall(value), null, 'ALLOC_STATIC', type); + } else if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { return makePointer(indexizeFunctions(parseNumerical(toNiceIdent(value.text))), null, 'ALLOC_STATIC', type); } else if (value.text == 'zeroinitializer') { return makePointer(JSON.stringify(makeEmptyStruct(type)), null, 'ALLOC_STATIC', type); @@ -222,7 +224,7 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { item.JS = 'var ' + item.ident + ';'; return [item, { intertype: 'GlobalVariable', - JS: 'globalFuncs.push(function() { ' + item.ident + ' = ' + parseConst(item.value, item.type) + ' });', + JS: 'globalFuncs.push(function() { return ' + item.ident + ' = ' + parseConst(item.value, item.type) + ' });', __result__: true, }]; } diff --git a/src/postamble.js b/src/postamble.js index 066b83e3..010e6e66 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -13,13 +13,14 @@ function run(args) { counter--; var func = globalFuncs.pop(); try { - func(); + var x = func(); + if (x == undefined) throw 'undefined'; } catch (e) { globalFuncs.unshift(func); // We will try again later. The global vars we depend on should be resolved by then - // XXX: We leak here, as we malloc, then fail and catch... } } + assert(counter > 0); var argc = args.length+1; function pad() { diff --git a/tests/runner.py b/tests/runner.py index ecabe9eb..2a988f8c 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -369,6 +369,23 @@ if 'benchmark' not in sys.argv: ''' self.do_test(src, '*7*') + def test_globals(self): + src = ''' + #include <stdio.h> + + + char cache[256], *next = cache; + + int main() + { + cache[10] = 25; + next[20] = 51; + printf("*%d,%d*\\n", next[10], cache[20]); + return 0; + } + ''' + self.do_test(src, '*25,51*') + def test_linked_list(self): src = ''' #include <stdio.h> |