Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

asyncify: fix splitting of branch blocks

  • Loading branch information...
commit 925bd51dee7d927fcd6c85565fc36f24140f4a0e 1 parent 3928c16
@indutny authored
Showing with 52 additions and 6 deletions.
  1. +16 −4 lib/spoon/cfg.js
  2. +2 −2 package.json
  3. +34 −0 test/asyncify-test.js
View
20 lib/spoon/cfg.js
@@ -119,13 +119,25 @@ Cfg.prototype.split = function split(at, root, asyncify, marker) {
var info = this.split(block.instructions[0], root, false, marker);
block.predecessors.forEach(function(pred) {
- // Remove goto
- var last = pred.instructions.pop();
+ if (pred.successors.length < 2) {
+ pred.successors = [];
+
+ // Remove goto
+ var last = pred.instructions.pop();
+
+ pred.ended = false;
+ } else {
+ // Replace one of the branches
+ var index = pred.successors[0] === block ? 0 : 1,
+ tmp = this.createBlock();
+
+ pred.successors[index] = tmp;
+ tmp.addPredecessor(pred);
+ pred = tmp;
+ }
- pred.ended = false;
pred.add('async-goto', [ pred.add('get', [ info.fn.name ]) ]);
pred.end();
- pred.successors = [];
}, this);
block.predecessors = [];
}, this);
View
4 package.json
@@ -3,9 +3,9 @@
"version": "0.1.3",
"main": "lib/spoon",
"dependencies": {
- "esprima": "~0.9.9",
+ "esprima": "~1.0.2",
"uglify-js": "~1.3.3",
- "escodegen": "0.0.9"
+ "escodegen": "~0.0.15"
},
"devDependencies": {
"mocha": "~1.4.2"
View
34 test/asyncify-test.js
@@ -19,6 +19,7 @@ describe('Spoon', function() {
var res,
once = false;
+ console.log(cfg.toString());
vm.runInNewContext(code + ';\nfn(callback)', {
callback: function(err, r) {
assert.equal(err, null);
@@ -140,6 +141,39 @@ describe('Spoon', function() {
r = assert.equal(r, 44253432);
});
+ it('should asyncify call in while loop', function() {
+ var r = test(function fn(__$callback) {
+ "enable spoon";
+ function async(a, b, callback) {
+ callback(null, a + b);
+ }
+
+ var x = 0,
+ p = false,
+ i;
+
+ if (p) {
+ i = 11;
+ while (i) {
+ i--;
+ x = async(i, x);
+ }
+ }
+
+ if (!p) {
+ i = 11;
+ while (i) {
+ i--;
+ x = async(i, x);
+ }
+ }
+
+ return x + 1;
+ });
+
+ r = assert.equal(r, 56);
+ });
+
it('should asyncify call in do while loop', function() {
var r = test(function fn(__$callback) {
"enable spoon";
Please sign in to comment.
Something went wrong with that request. Please try again.