Skip to content

Commit

Permalink
Add error param for transformed if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
BYVoid committed Oct 10, 2012
1 parent 05c2ce7 commit a2c4933
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
32 changes: 27 additions & 5 deletions lib/syntax/IfStatement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var assert = require('assert');

var BlockStatement = require('./BlockStatement');
var Identifier = require('./Identifier');
var VariableDeclaration = require('./VariableDeclaration');

var helpers = require('../helpers');

var IfStatement = module.exports = function(test, consequent, alternate) {
Expand Down Expand Up @@ -49,11 +52,30 @@ IfStatement.prototype.transform = function (place) {
}

var nextPlace = [];
place.push(helpers.makeCPS([this], nextPlace));
var ifWrapper = helpers.makeCPS([this], nextPlace);
var ifExpression = ifWrapper.expression;
var ifContFunc = ifExpression.arguments[0];

//Add err param for continuation function
var errIdentifier = new Identifier(helpers.errName);
ifContFunc.params = [errIdentifier];

//Add error judging statement
var BinaryExpression = require('./BinaryExpression');
var CallExpression = require('./CallExpression');
var ReturnStatement = require('./ReturnStatement');
var judgeErrorStatement = new IfStatement(
new BinaryExpression('!==', errIdentifier, new Identifier('undefined')),
new ReturnStatement(new CallExpression(new Identifier(helpers.continuationIdentifier), errIdentifier)),
null
);
nextPlace.push(judgeErrorStatement);

place.push(ifWrapper);
return nextPlace;
} else {
//Not transfrom if no async calls
place.push(this);
return place;
}

//Not transfrom if no async calls
place.push(this);
return place;
}
4 changes: 3 additions & 1 deletion test/results/diskusage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ function calcDirSize(path, callback) {
dirBlockSize += 512 * stats.blocks;
cont();
}
}(function () {
}(function (err) {
if (err !== undefined)
return cont(err);
i++;
loop_0(loop_0_cont);
}));
Expand Down
8 changes: 6 additions & 2 deletions test/results/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ var err, text, a;
} else {
cont();
}
}(function () {
}(function (err) {
if (err !== undefined)
return cont(err);
cont();
}));
}, 1000);
}
}(function () {
}(function (err) {
if (err !== undefined)
return cont(err);
a = err;
}));
4 changes: 3 additions & 1 deletion test/results/ifvar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var b, c, i, j, k, p;
}
cont();
}
}(function () {
}(function (err) {
if (err !== undefined)
return cont(err);
console.log(b);
}));
4 changes: 3 additions & 1 deletion test/results/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ var err, text, end;
cont();
});
}
}(function () {
}(function (err) {
if (err !== undefined)
return cont(err);
case_4(cont);
}));
}
Expand Down

0 comments on commit a2c4933

Please sign in to comment.