Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

block: fix marker replacement

  • Loading branch information...
commit 76f0c73574b76cf58d895f930292f278b64d7d0d 1 parent 50e755e
@indutny authored
Showing with 115 additions and 23 deletions.
  1. +8 −2 lib/spoon/block.js
  2. +107 −21 test/asyncify-test.js
View
10 lib/spoon/block.js
@@ -124,10 +124,16 @@ Block.prototype.split = function split(at, root, asyncify, marker) {
this.ended = false;
if (asyncify) {
+ var getfn = this.add('get', [ fn.name ]);
if (marker >= 0) {
- at.args[at.args.length - marker].args[0] = fn.name;
+ var removed = at.args.splice(at.args.length - marker, 1, getfn)[0],
+ index = removed.block.instructions.indexOf(removed);
+
+ if (index !== -1) {
+ removed.block.instructions.splice(index, 1);
+ }
} else {
- at.args.push(this.add('get', [ fn.name ]));
+ at.args.push(getfn);
}
this.instructions.push(at);
View
128 test/asyncify-test.js
@@ -5,32 +5,32 @@ var spoon = require('..'),
uglify = require('uglify-js');
describe('Spoon', function() {
- function test(code, what) {
- var ast = esprima.parse(code.toString()),
- cfg = spoon.construct(ast);
+ describe('asyncify', function() {
+ function test(code, what) {
+ var ast = esprima.parse(code.toString()),
+ cfg = spoon.construct(ast);
- cfg.asyncify([esprima.parse(what || 'async')], {
- declaration: 'enable spoon'
- });
+ cfg.asyncify([esprima.parse(what || 'async')], {
+ declaration: 'enable spoon'
+ });
- var out = spoon.render(cfg);
- var code = uglify.uglify.gen_code(out, { beautify: true });
+ var out = spoon.render(cfg);
+ var code = uglify.uglify.gen_code(out, { beautify: true });
- var res,
- once = false;
- vm.runInNewContext(code + ';\nfn(callback)', {
- callback: function(err, r) {
- assert.equal(err, null);
- if (once) throw new Error('Called twice');
- once = true;
+ var res,
+ once = false;
+ vm.runInNewContext(code + ';\nfn(callback)', {
+ callback: function(err, r) {
+ assert.equal(err, null);
+ if (once) throw new Error('Called twice');
+ once = true;
- res = r;
- }
- });
- return res;
- }
+ res = r;
+ }
+ });
+ return res;
+ }
- describe('asyncify', function() {
it('should asyncify two-fold operation', function() {
var r = test(function fn(__$callback) {
"enable spoon";
@@ -103,6 +103,43 @@ describe('Spoon', function() {
r = assert.equal(r, 46);
});
+ it('should asyncify call in for loop #2', function() {
+ var r = test(function fn(__$callback) {
+ "enable spoon";
+ function async(a, b, callback) {
+ callback(null, a + b);
+ }
+
+ for (var i = async(0, 0); i < async(5, 5); i = async(i, 1)) {
+ var x = async(x || 0, 1);
+ }
+
+ return x + 1;
+ });
+
+ r = assert.equal(r, 11);
+ });
+
+ it('should asyncify call in property', function() {
+ var r = test(function fn(__$callback) {
+ "enable spoon";
+ function async(a, callback) {
+ callback(null, a);
+ }
+
+ var obj = { a: 123, b: 456, c: 789 },
+ x = 1;
+
+ for (var i in obj) {
+ x = async(obj[async(i)]) * async(x);
+ }
+
+ return x;
+ });
+
+ r = assert.equal(r, 44253432);
+ });
+
it('should asyncify call in do while loop', function() {
var r = test(function fn(__$callback) {
"enable spoon";
@@ -142,4 +179,53 @@ describe('Spoon', function() {
r = assert.equal(r, 4);
});
});
+
+ describe('marker', function() {
+ function test(code, what) {
+ var ast = esprima.parse(code.toString()),
+ cfg = spoon.construct(ast);
+
+ cfg.asyncify([esprima.parse(what || 'async')], {
+ declaration: 'enable spoon',
+ marker: '_'
+ });
+
+ var out = spoon.render(cfg);
+ var code = uglify.uglify.gen_code(out, { beautify: true });
+
+ var res,
+ once = false;
+ vm.runInNewContext(code + ';\nfn(callback)', {
+ callback: function(err, r) {
+ assert.equal(err, null);
+ if (once) throw new Error('Called twice');
+ once = true;
+
+ res = r;
+ }
+ });
+ return res;
+ }
+
+ it('should replace marker in property', function() {
+ var r = test(function fn(_) {
+ "enable spoon";
+ function async(_, a) {
+ "enable spoon";
+ return a;
+ }
+
+ var obj = { a: 123, b: 456, c: 789 },
+ x = 1;
+
+ for (var i in obj) {
+ x = async(_, obj[async(_, i)]) * async(_, x);
+ }
+
+ return x;
+ });
+
+ r = assert.equal(r, 44253432);
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.