Permalink
Browse files

cfg: var declarations should go up

  • Loading branch information...
indutny committed Sep 26, 2012
1 parent b71bead commit 30f1654c1cc78a7c84ceed747b92cc4b3244ca2f
Showing with 11 additions and 7 deletions.
  1. +8 −3 lib/spoon/block.js
  2. +2 −2 lib/spoon/cfg.js
  3. +1 −2 test/asyncify-test.js
View
@@ -110,14 +110,12 @@ Block.prototype.split = function split(at, root, asyncify) {
this.successors = [];
// Add function declaration to the root block
- var fn = spoon.instruction.create(root, 'fn', [ next ]);
+ var fn = root.prepend('fn', [ next ]);
fn.name = '__$fn' + next.id;
fn.params = ['__$r'];
fn.isExpression = false;
- root.instructions.unshift(fn);
-
this.ended = false;
if (asyncify) {
@@ -148,6 +146,13 @@ Block.prototype.split = function split(at, root, asyncify) {
};
};
+Block.prototype.prepend = function prepend(type, args) {
+ var instr = spoon.instruction.create(this, type, args || []);
+
+ this.instructions.unshift(instr);
+ return instr;
+};
+
Block.prototype.add = function add(type, args) {
var instr = spoon.instruction.create(this, type, args || []);
if (this.ended) return instr;
View
@@ -636,8 +636,8 @@ Cfg.prototype.visitCall = function visitCall(ast) {
};
Cfg.prototype.visitVar = function visitVar(ast) {
- // Add variables
- this.add('var', ast.declarations.map(function(ast) {
+ // Add var declaration to the root block (i.e. function start)
+ this.currentRoot.prepend('var', ast.declarations.map(function(ast) {
return ast.id.name;
}, this));
View
@@ -78,9 +78,8 @@ describe('Spoon', function() {
callback(a + b);
}
- var x = 0;
for (var i = 0; i < 10; i++) {
- x = async(i, x);
+ var x = async(i, x || 0);
}
return x + 1;

0 comments on commit 30f1654

Please sign in to comment.