Browse files

dummy continuations in more places

  • Loading branch information...
1 parent f8533fb commit d6252768a2fb1cb5314748744cbbe6ee5a92a38d @maxtaco committed with Nov 25, 2011
Showing with 45 additions and 14 deletions.
  1. +25 −9 lib/coffee-script/nodes.js
  2. +1 −1 lib/coffee-script/rewriter.js
  3. +19 −4 src/nodes.coffee
View
34 lib/coffee-script/nodes.js
@@ -242,6 +242,15 @@
return this.cpsPivotFlag;
};
+ Base.prototype.needsDummyContinuation = function() {
+ var empty, k_id;
+ if (!this.gotCpsSplit) {
+ k_id = new Value(new Literal(tame["const"].k));
+ empty = new Code([], new Block([]));
+ return new Assign(k_id, empty);
+ }
+ };
+
Base.prototype.children = [];
Base.prototype.tameContinuationBlock = null;
@@ -1901,7 +1910,7 @@
};
While.prototype.compileTame = function(o) {
- var break_assign, break_id, cond, continue_assign, continue_body, continue_id, empty, inner_k_assign, inner_k_id, k_id, outStatements, outer_k_assign, top_assign, top_block, top_body, top_call, top_func, top_id, top_statements;
+ var break_assign, break_id, cond, continue_assign, continue_body, continue_id, inner_k_assign, inner_k_id, k, k_id, outStatements, top_assign, top_block, top_body, top_call, top_func, top_id, top_statements;
if (!this.tameNodeFlag) return null;
outStatements = [];
top_id = new Value(new Literal(tame["const"].t_while));
@@ -1920,11 +1929,7 @@
top_assign = new Assign(top_id, top_func);
top_call = new Call(top_id, [k_id]);
top_statements = [top_assign, top_call];
- if (!this.gotCpsSplit) {
- empty = new Code([], new Block([]));
- outer_k_assign = new Assign(k_id, empty);
- top_statements.unshift(outer_k_assign);
- }
+ if (k = this.needsDummyContinuation()) top_statements.unshift(k);
top_block = new Block(top_statements);
return top_block.compile(o);
};
@@ -2194,6 +2199,10 @@
Defer.prototype.children = ['args'];
+ Defer.prototype.compileNode = function(p) {
+ return "defer()";
+ };
+
return Defer;
})();
@@ -2203,19 +2212,25 @@
__extends(Await, Base);
function Await(body) {
- var assign, call, cls, lhs, meth, rhs;
+ this.body = body;
+ }
+
+ Await.prototype.transform = function() {
+ var assign, body, call, cls, k, lhs, meth, rhs;
+ body = this.body;
lhs = new Value(new Literal(tame["const"].deferrals));
cls = new Value(new Literal(tame["const"].ns));
cls.add(new Access(new Value(new Literal(tame["const"].Deferrals))));
call = new Call(cls, [new Value(new Literal(tame["const"].k))]);
rhs = new Op("new", call);
assign = new Assign(lhs, rhs);
body.unshift(assign);
+ if (k = this.needsDummyContinuation()) body.unshift(k);
meth = lhs.add(new Access(new Value(new Literal(tame["const"].fulfill))));
call = new Call(meth, []);
body.push(call);
- this.body = body;
- }
+ return this.body = body;
+ };
Await.prototype.children = ['body'];
@@ -2226,6 +2241,7 @@
Await.prototype.makeReturn = THIS;
Await.prototype.compileNode = function(o) {
+ this.transform();
o.indent += TAB;
return this.body.compile(o);
};
View
2 lib/coffee-script/rewriter.js
@@ -296,7 +296,7 @@
IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS', 'DEFER'];
- IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'UNARY', 'SUPER', '@', '->', '=>', '[', '(', '{', '--', '++'];
+ IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'UNARY', 'SUPER', 'DEFER', '@', '->', '=>', '[', '(', '{', '--', '++'];
IMPLICIT_UNSPACED_CALL = ['+', '-'];
View
23 src/nodes.coffee
@@ -198,6 +198,12 @@ exports.Base = class Base
@cpsPivotFlag = true if child.walkCpsPivots()
@cpsPivotFlag
+ needsDummyContinuation : ->
+ if not @gotCpsSplit
+ k_id = new Value new Literal tame.const.k
+ empty = new Code [], new Block []
+ new Assign k_id, empty
+
# Default implementations of the common node properties and methods. Nodes
# will override these with custom logic, if needed.
children: []
@@ -1564,10 +1570,8 @@ exports.While = class While extends Base
top_assign = new Assign top_id, top_func
top_call = new Call top_id, [ k_id ]
top_statements = [ top_assign, top_call ]
- if not @gotCpsSplit
- empty = new Code [], new Block []
- outer_k_assign = new Assign k_id, empty
- top_statements.unshift outer_k_assign
+ if k = @needsDummyContinuation()
+ top_statements.unshift k
top_block = new Block top_statements
return top_block.compile o
@@ -1784,6 +1788,10 @@ exports.Defer = class Defer extends Base
children : ['args']
+ compileNode : (p) ->
+ # fix me
+ "defer()"
+
##### Call or Defer
#
#exports.CallOrDefer = class CallOrDefer
@@ -1795,13 +1803,19 @@ exports.Defer = class Defer extends Base
exports.Await = class Await extends Base
constructor : (body) ->
+ @body = body
+
+ transform : ->
+ body = @body
lhs = new Value new Literal tame.const.deferrals
cls = new Value new Literal tame.const.ns
cls.add(new Access(new Value new Literal tame.const.Deferrals))
call = new Call cls, [ new Value new Literal tame.const.k ]
rhs = new Op "new", call
assign = new Assign lhs, rhs
body.unshift assign
+ if k = @needsDummyContinuation()
+ body.unshift (k)
meth = lhs.add new Access new Value new Literal tame.const.fulfill
call = new Call meth, []
body.push (call)
@@ -1814,6 +1828,7 @@ exports.Await = class Await extends Base
makeReturn : THIS
compileNode: (o) ->
+ @transform()
o.indent += TAB
@body.compile o

0 comments on commit d625276

Please sign in to comment.