Skip to content

Commit

Permalink
Fix bug when using multiple parallel statements in a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
BYVoid committed Jun 9, 2013
1 parent 5f181de commit 6cda1bf
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 28 deletions.
20 changes: 18 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ exports.parallelName = 'parallel';
exports.callbackName = '_$cont';
exports.loopPrefix = '_$loop_';
exports.errName = '_$err';
exports.errArray = '_$errors';
exports.errArray = '_$errors_';
exports.forInIter = '_$itmp';
exports.forInArray = '_$itmp_list';
exports.parallelCounter = '_$parallel_done';
exports.parallelCounter = '_$parallel_done_';

exports.extractVariableDeclarations = function (block) {
var decMap = {};
Expand Down Expand Up @@ -68,6 +68,22 @@ exports.getLoopFunctionName = function () {
return name;
};

var parallelCount = 0;
exports.getParallelCountName = function () {
var name = exports.parallelCounter + parallelCount;
parallelCount ++;
return name;
};

var errArrayCount = 0;
exports.getErrorArrayName = function () {
var name = exports.errArray + errArrayCount;
errArrayCount ++;
return name;
};

exports.reset = function () {
loopCount = 0;
parallelCount = 0;
errArrayCount = 0;
};
4 changes: 2 additions & 2 deletions lib/syntax/CallExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ CallExpression.prototype.transformParallel = function (place) {

var innerPlace = [];
var nextPlace = [];
var parallelCounter = new Identifier(helpers.parallelCounter);
var parallelCounter = new Identifier(helpers.getParallelCountName());
var callbackId = new Identifier(helpers.callbackName);
var errId = new Identifier(helpers.errName);
var errArray = new Identifier(helpers.errArray);
var errArray = new Identifier(helpers.getErrorArrayName());

//Define var _$parallel_done = 0
place.push(new VariableDeclaration(
Expand Down
19 changes: 19 additions & 0 deletions test/cases/multiple_parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var test = function(value, callback) {
console.log('called');
callback(value);
};

parallel(
test(1, cont(a)),
test(2, cont(b)),
test(3, cont(c))
);
console.log(a, b, c);

parallel(
test(4, cont(d)),
test(5, cont(e)),
test(6, cont(f))
);
console.log(d, e, f);

66 changes: 66 additions & 0 deletions test/results/multiple_parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var test, a, b, c, d, e, f;
test = function (value, callback) {
console.log('called');
callback(value);
};
var _$parallel_done_0 = 0;
var _$errors_0 = [];
(function (_$cont) {
test(1, function () {
++_$parallel_done_0;
a = arguments[0];
_$cont();
});
test(2, function () {
++_$parallel_done_0;
b = arguments[0];
_$cont();
});
test(3, function () {
++_$parallel_done_0;
c = arguments[0];
_$cont();
});
}(function (_$err) {
if (_$err)
_$errors_0.push(_$err);
if (_$parallel_done_0 !== 3)
return;
if (_$errors_0.length > 0)
throw _$errors_0;
_$parallel_done_0 = undefined;
_$err = undefined;
_$errors_0 = undefined;
console.log(a, b, c);
var _$parallel_done_1 = 0;
var _$errors_1 = [];
(function (_$cont) {
test(4, function () {
++_$parallel_done_1;
d = arguments[0];
_$cont();
});
test(5, function () {
++_$parallel_done_1;
e = arguments[0];
_$cont();
});
test(6, function () {
++_$parallel_done_1;
f = arguments[0];
_$cont();
});
}(function (_$err) {
if (_$err)
_$errors_1.push(_$err);
if (_$parallel_done_1 !== 3)
return;
if (_$errors_1.length > 0)
throw _$errors_1;
_$parallel_done_1 = undefined;
_$err = undefined;
_$errors_1 = undefined;
console.log(d, e, f);
}));
}));
/* Generated by Continuation.js v0.1.2 */
24 changes: 12 additions & 12 deletions test/results/parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ f = function (i, next) {
}, i);
};
r = [];
var _$parallel_done = 0;
var _$errors = [];
var _$parallel_done_0 = 0;
var _$errors_0 = [];
(function (_$cont) {
f(50, function () {
++_$parallel_done;
++_$parallel_done_0;
r[0] = arguments[0];
_$cont();
});
f(100, function () {
++_$parallel_done;
++_$parallel_done_0;
r[1] = arguments[0];
_$cont();
});
f(20, function () {
++_$parallel_done;
++_$parallel_done_0;
r[2] = arguments[0];
_$cont();
});
f(10, function () {
++_$parallel_done;
++_$parallel_done_0;
r[3] = arguments[0];
_$cont();
});
}(function (_$err) {
if (_$err)
_$errors.push(_$err);
if (_$parallel_done !== 4)
_$errors_0.push(_$err);
if (_$parallel_done_0 !== 4)
return;
if (_$errors.length > 0)
throw _$errors;
_$parallel_done = undefined;
if (_$errors_0.length > 0)
throw _$errors_0;
_$parallel_done_0 = undefined;
_$err = undefined;
_$errors = undefined;
_$errors_0 = undefined;
console.log('Done', r);
}));
/* Generated by Continuation.js v0.1.2 */
24 changes: 12 additions & 12 deletions test/results/parallel_exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ f = function (i, next) {
r = [];
(function (_$cont) {
try {
var _$parallel_done = 0;
var _$errors = [];
var _$parallel_done_0 = 0;
var _$errors_0 = [];
(function (_$cont) {
try {
f(50, function () {
try {
++_$parallel_done;
++_$parallel_done_0;
_$err = arguments[0];
r[0] = arguments[1];
if (_$err)
Expand All @@ -29,7 +29,7 @@ r = [];
});
f(100, function () {
try {
++_$parallel_done;
++_$parallel_done_0;
_$err = arguments[0];
r[1] = arguments[1];
if (_$err)
Expand All @@ -41,7 +41,7 @@ r = [];
});
f(20, function () {
try {
++_$parallel_done;
++_$parallel_done_0;
_$err = arguments[0];
r[2] = arguments[1];
if (_$err)
Expand All @@ -53,7 +53,7 @@ r = [];
});
f(10, function () {
try {
++_$parallel_done;
++_$parallel_done_0;
_$err = arguments[0];
r[3] = arguments[1];
if (_$err)
Expand All @@ -69,14 +69,14 @@ r = [];
}(function (_$err) {
try {
if (_$err)
_$errors.push(_$err);
if (_$parallel_done !== 4)
_$errors_0.push(_$err);
if (_$parallel_done_0 !== 4)
return;
if (_$errors.length > 0)
throw _$errors;
_$parallel_done = undefined;
if (_$errors_0.length > 0)
throw _$errors_0;
_$parallel_done_0 = undefined;
_$err = undefined;
_$errors = undefined;
_$errors_0 = undefined;
_$cont();
} catch (_$err) {
_$cont(_$err);
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var files = [
'parallel.js',
'parallel_exception.js',
'nested_block.js',
'multiple_parallel.js',
];

var compileByApi = function(filename, done) {
Expand Down

0 comments on commit 6cda1bf

Please sign in to comment.