Skip to content

Commit

Permalink
progress on fibers exprima transform
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Mar 5, 2015
1 parent fe764c2 commit a50716d
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/fibers/transform-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ function transform(source, options) {
buffer.add('fstreamline__.then.call(this,');
walk(expr.object());
catchup(expr.object.range[1]);
buffer.add(', "' + expr.property.name + '", _');
skipTo(args[1].range[1]);
buffer.add(', "' + expr.property.name + '", _)');
skipTo(this.range[1]);
++didRewrite;
return;
}
Expand Down Expand Up @@ -439,8 +439,10 @@ function transform(source, options) {
catchup(expr.range[1]);
}
}
buffer.add('(');
for (var ii = 0; ii < args.length; ++ii) {
catchup(args[ii].range[0]);
skipTo(args[ii].range[0]);
if (ii > 0) buffer.add(', ');
if (ii !== idx) {
walk(args[ii]);
catchup(args[ii].range[1]);
Expand All @@ -449,35 +451,41 @@ function transform(source, options) {
skipTo(args[ii].range[1]);
}
}
skipTo(this.range[1]);
buffer.add(')');
} else if (idx !== -1) {
// Rewrite streamlined calls
// issue #108: process between expr.range[0] and last arg end rather than this.range[0]/end
catchup(expr.range[0]);
catchup(this.range[0]);
buffer.add('fstreamline__.invoke(');
skipTo(expr.range[0])
if (expr.type === 'MemberExpression') {
skipTo(expr.object.range[0]);
walk(expr.object);
catchup(expr.object.range[1]);
if (!expr.computed) {
// Method call: foo.bar(_)
buffer.add(', '+ JSON.stringify(expr.property.name));
buffer.add(', '+ JSON.stringify(expr.property.name));
} else {
// Dynamic method call: foo[bar](_)
buffer.add(', ');
skipTo(expr.property.range[0]);
walk(expr.property);
catchup(expr.property.range[1]);
}
skipTo(expr.range[1]);
} else {
// Function call
buffer.add('null, ');
walk(expr);
catchup(expr.range[1]);
}
catchup(expr.range[1]);
skipTo(args[0].range[0]);
// Render arguments
buffer.add(', [');
skipTo(args[0].range[0]);
for (var ii = 0; ii < args.length; ++ii) {
catchup(args[ii].range[0]);
skipTo(args[ii].range[0]);
if (ii > 0) buffer.add(', ');
if (ii !== idx) {
walk(args[ii]);
catchup(args[ii].range[1]);
Expand All @@ -486,10 +494,10 @@ function transform(source, options) {
skipTo(args[ii].range[1]);
}
}
catchup(args[args.length - 1].range[1]);
var options = idx;
if (args[idx].type === 'ArrayExpression') options = '{ callbackIndex: ' + idx + ', returnArray: true }';
buffer.add('], ' + options);
buffer.add('], ' + options + ')');
skipTo(this.range[1]);
++didRewrite;
} else {
var paren = 0;
Expand Down Expand Up @@ -549,8 +557,9 @@ function transform(source, options) {
buffer.add(" fstreamline__.construct(");
walk(expr);
catchup(expr.range[1]);
buffer.add("," + idx + ")");
buffer.add("," + idx + ")(");
// process arguments to avoid 'invalid usage of callback' error
skipTo(args[0].range[0]);
for (var ii = 0; ii < args.length; ++ii) {
catchup(args[ii].range[0]);
if (ii !== idx) {
Expand All @@ -561,6 +570,8 @@ function transform(source, options) {
skipTo(args[ii].range[1]);
}
}
buffer.add(")");
skipTo(this.range[1]);
} else {
walk(expr);
args.map(walk);
Expand Down

0 comments on commit a50716d

Please sign in to comment.