diff options
author | alon@honor <none@none> | 2010-10-31 15:50:56 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-10-31 15:50:56 -0700 |
commit | db2dfd4026c5aaba0a4b1fc2778b1ca36bec8a62 (patch) | |
tree | eeffd846e36469148e66c7e1b58cfc8a19ca1248 /src | |
parent | 9343f81a117efd605ff274fd290d38d674c55d25 (diff) |
remove getters from global vars
Diffstat (limited to 'src')
-rw-r--r-- | src/jsifier.js | 35 | ||||
-rw-r--r-- | src/postamble.js | 17 | ||||
-rw-r--r-- | src/preamble.js | 1 |
3 files changed, 43 insertions, 10 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index f140cecc..2f79b18c 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -173,10 +173,11 @@ function JSify(data) { } } - // globalVariablw + // globalVariable substrate.addZyme('GlobalVariable', { processItem: function(item) { - dprint('gconst', '// zz global Cons: ' + dump(item) + ' :: ' + dump(item.value)); + item.intertype = 'GlobalVariableStub'; + item.__result__ = true; if (item.ident == '_llvm_global_ctors') { item.JS = '\n__globalConstructor__ = function() {\n' + item.ctors.map(function(ctor) { return ' ' + toNiceIdent(ctor) + '();' }).join('\n') + @@ -184,10 +185,13 @@ function JSify(data) { } else if (item.type == 'external') { item.JS = 'var ' + item.ident + ' = ' + '0; /* external value? */'; } else { - // GETTER - lazy loading, fixes issues with ordering. - item.JS = 'this.__defineGetter__("' + item.ident + '", function() { delete ' + item.ident + '; ' + item.ident + ' = ' + parseConst(item.value, item.type) + '; return ' + item.ident + ' });'; + item.JS = 'var ' + item.ident + ';'; + return [item, { + intertype: 'GlobalVariable', + JS: 'globalFuncs.push(function() { ' + item.ident + ' = ' + parseConst(item.value, item.type) + ' });', + __result__: true, + }]; } - item.__result__ = true; return [item]; }, }); @@ -864,12 +868,25 @@ function JSify(data) { function finalCombiner(items) { var ret = items.filter(function(item) { return item.intertype == 'type' }); - ret = ret.concat(items.filter(function(item) { return item.intertype == 'globalVariable' })); + ret = ret.concat(items.filter(function(item) { return item.intertype == 'GlobalVariableStub' })); ret.push('\n'); ret = ret.concat(items.filter(function(item) { return item.intertype == 'functionStub' })); ret.push('\n'); ret = ret.concat(items.filter(function(item) { return item.intertype == 'function' })); - return ret.map(function(item) { return item.JS }).join('\n'); + ret = ret.map(function(item) { return item.JS }).join('\n'); + + var params = { 'QUANTUM_SIZE': QUANTUM_SIZE }; + var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + ret + read('postamble.js'), params); + function reverse_(x) { + if (LLVM_STYLE === 'old') { + return x.reverse(); + } else { + return x; + } + } + var globalVars = reverse_(items.filter(function(item) { return item.intertype == 'GlobalVariable' }).map(function(item) { return item.JS })).join('\n'); + return read('shell.js').replace('{{BODY}}', indentify(body, 2)) + .replace('{{GLOBAL_VARS}}', indentify(globalVars, 4)); } // Data @@ -879,8 +896,6 @@ function JSify(data) { substrate.addItems(data.functions, 'FunctionSplitter'); substrate.addItems(data.functionStubs, 'FunctionStub'); - var params = { 'QUANTUM_SIZE': QUANTUM_SIZE }; - var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + finalCombiner(substrate.solve()) + read('postamble.js'), params); - return read('shell.js').replace('{{BODY}}', indentify(body, 2)); + return finalCombiner(substrate.solve()); } diff --git a/src/postamble.js b/src/postamble.js index 142ee013..448ee0fb 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -4,6 +4,23 @@ function run(args) { __initializeRuntime__(); + var globalFuncs = []; + +{{GLOBAL_VARS}} + + var counter = Math.pow(globalFuncs.length,2)+1; + while (globalFuncs.length > 0 && counter >= 0) { + counter--; + var func = globalFuncs.pop(); + try { + func(); + } 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... + } + } + var argc = args.length+1; function pad() { for (var i = 0; i < QUANTUM_SIZE-1; i++) { diff --git a/src/preamble.js b/src/preamble.js index 22473ce9..66cb37bb 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -77,6 +77,7 @@ function Pointer_make(slab, pos, allocator) { var ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc][allocator ? allocator : ALLOC_STATIC](Math.max(slab.length - pos, 1)); for (var i = 0; i < slab.length - pos; i++) { var curr = slab[pos + i]; + if (curr === undefined) throw 'Invalid element in slab'; // This can be caught, and you can try again to allocate later, see globalFuncs in run() if (typeof curr === 'function') { curr = Runtime.getFunctionIndex(curr); } |