From a2c493319ab277fc44736d25f14e1de1d860e4f9 Mon Sep 17 00:00:00 2001 From: BYVoid Date: Wed, 10 Oct 2012 14:01:26 +0800 Subject: [PATCH] Add error param for transformed if statement --- lib/syntax/IfStatement.js | 32 +++++++++++++++++++++++++++----- test/results/diskusage.js | 4 +++- test/results/if.js | 8 ++++++-- test/results/ifvar.js | 4 +++- test/results/switch.js | 4 +++- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/syntax/IfStatement.js b/lib/syntax/IfStatement.js index 4207cc7..e7429df 100644 --- a/lib/syntax/IfStatement.js +++ b/lib/syntax/IfStatement.js @@ -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) { @@ -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; } diff --git a/test/results/diskusage.js b/test/results/diskusage.js index b275e75..4ad391e 100644 --- a/test/results/diskusage.js +++ b/test/results/diskusage.js @@ -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); })); diff --git a/test/results/if.js b/test/results/if.js index 4326a61..26a81c5 100644 --- a/test/results/if.js +++ b/test/results/if.js @@ -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; })); \ No newline at end of file diff --git a/test/results/ifvar.js b/test/results/ifvar.js index 10f89a5..c27f407 100644 --- a/test/results/ifvar.js +++ b/test/results/ifvar.js @@ -23,6 +23,8 @@ var b, c, i, j, k, p; } cont(); } -}(function () { +}(function (err) { + if (err !== undefined) + return cont(err); console.log(b); })); \ No newline at end of file diff --git a/test/results/switch.js b/test/results/switch.js index 8d1a399..29aa93e 100644 --- a/test/results/switch.js +++ b/test/results/switch.js @@ -30,7 +30,9 @@ var err, text, end; cont(); }); } - }(function () { + }(function (err) { + if (err !== undefined) + return cont(err); case_4(cont); })); }