Skip to content
Browse files

more things working, now nested awaits are properly serialized.

  • Loading branch information...
1 parent cc3c09f commit 50ce7b612dd9d24d522ec2b94c9a054ab513acbd @maxtaco committed Jan 3, 2012
Showing with 50 additions and 25 deletions.
  1. +17 −10 lib/coffee-script/nodes.js
  2. +18 −14 src/nodes.coffee
  3. +15 −1 test/tame.coffee
View
27 lib/coffee-script/nodes.js
@@ -45,6 +45,7 @@
this.tameGotCpsSplitFlag = false;
this.tameCpsPivotFlag = false;
this.tameHasAutocbFlag = false;
+ this.tameParentAwait = null;
}
Base.prototype.compile = function(o, lvl) {
@@ -163,6 +164,7 @@
if (this.tameLoopFlag) extras += "L";
if (this.tameCpsPivotFlag) extras += "P";
if (this.tameHasAutocbFlag) extras += "C";
+ if (this.tameParentAwait) extras += "D";
if (extras.length) extras = " (" + extras + ")";
tree = '\n' + idt + name;
if (this.soak) tree += '?';
@@ -271,12 +273,13 @@
return _results;
};
- Base.prototype.tameWalkAst = function() {
+ Base.prototype.tameWalkAst = function(p) {
var child, _i, _len, _ref2;
+ this.tameParentAwait = p;
_ref2 = this.flattenChildren();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
- if (child.tameWalkAst()) this.tameNodeFlag = true;
+ if (child.tameWalkAst(p)) this.tameNodeFlag = true;
}
return this.tameNodeFlag;
};
@@ -366,9 +369,10 @@
};
Base.prototype.tameNestPrequelBlock = function(bb) {
- var rv;
+ var obj, rv;
rv = new TameReturnValue();
- this.tamePrequels.push({
+ obj = this.tameParentAwait || this;
+ obj.tamePrequels.push({
block: bb,
retval: rv
});
@@ -670,7 +674,7 @@
Block.prototype.tameTransform = function() {
if (!this.tameGo()) return this;
- this.tameWalkAst();
+ this.tameWalkAst(null);
if (this.tameNeedsRuntime() && !this.tameFindRequire()) {
this.tameAddRuntime();
}
@@ -1980,8 +1984,9 @@
}
};
- Code.prototype.tameWalkAst = function() {
- if (Code.__super__.tameWalkAst.call(this)) this.tameNodeFlag = true;
+ Code.prototype.tameWalkAst = function(p) {
+ this.tameParentAwait = p;
+ if (Code.__super__.tameWalkAst.call(this, null)) this.tameNodeFlag = true;
return false;
};
@@ -2655,9 +2660,11 @@
return this.body.compile(o);
};
- Await.prototype.tameWalkAst = function() {
- Await.__super__.tameWalkAst.call(this);
- return this.tameNodeFlag = !this.parent;
+ Await.prototype.tameWalkAst = function(p) {
+ p = p || this;
+ this.tameParentAwait = p;
+ Await.__super__.tameWalkAst.call(this, p);
+ return this.tameNodeFlag = true;
};
return Await;
View
32 src/nodes.coffee
@@ -45,8 +45,7 @@ exports.Base = class Base
@tameGotCpsSplitFlag = false
@tameCpsPivotFlag = false
@tameHasAutocbFlag = false
-
-
+ @tameParentAwait = null
# Common logic for determining whether to wrap this node in a closure before
# compiling it, or to compile directly. We need to wrap if this node is a
@@ -170,6 +169,7 @@ exports.Base = class Base
extras += "L" if @tameLoopFlag
extras += "P" if @tameCpsPivotFlag
extras += "C" if @tameHasAutocbFlag
+ extras += "D" if @tameParentAwait
if extras.length
extras = " (" + extras + ")"
tree = '\n' + idt + name
@@ -238,21 +238,21 @@ exports.Base = class Base
#
# AST Walking Routines for CPS Pivots, etc.
#
- # There are five passes:
+ # There are three passes:
# 3. Find await's and trace upward.
# 4. Find loops found in #1, and flood downward
# 5. Find break/continue found in #2, and trace upward
#
-
# tameWalkAst
# Walk the AST looking for taming. Mark a node as with tame flags
# if any of its children are tamed, but don't cross scope boundary
# when considering the children.
#
- tameWalkAst : ->
+ tameWalkAst : (p) ->
+ @tameParentAwait = p
for child in @flattenChildren()
- @tameNodeFlag = true if child.tameWalkAst()
+ @tameNodeFlag = true if child.tameWalkAst p
@tameNodeFlag
# tameWalkAstLoops
@@ -315,7 +315,8 @@ exports.Base = class Base
tameNestPrequelBlock: (bb) ->
rv = new TameReturnValue()
- @tamePrequels.push { block : bb, retval : rv }
+ obj = @tameParentAwait || this
+ obj.tamePrequels.push { block : bb, retval : rv }
rv
isStatement : NO
@@ -599,7 +600,7 @@ exports.Block = class Block extends Base
# Perform all steps of the Tame transform
tameTransform : ->
return this unless @tameGo()
- @tameWalkAst()
+ @tameWalkAst null
@tameAddRuntime() if @tameNeedsRuntime() and not @tameFindRequire()
@tameWalkAstLoops(false)
@tameWalkCpsPivots()
@@ -1633,12 +1634,13 @@ exports.Code = class Code extends Base
# we are taming as a feature of all of our children. However, if we
# are tamed, it's not the case that our parent is tamed!
- tameWalkAst : ->
- @tameNodeFlag = true if super()
+ tameWalkAst : (p) ->
+ @tameParentAwait = p
+ @tameNodeFlag = true if super null
false
tameWalkAstLoops : (flood) ->
- @tameLoopFlag = true if super(false)
+ @tameLoopFlag = true if super false
false
tameWalkCpsPivots: ->
@@ -2221,9 +2223,11 @@ exports.Await = class Await extends Base
# We still need to walk our children to see if there are any embedded
# function which might also be tamed. But we're always going to report
# to our parent that we are tamed, since we are!
- tameWalkAst : ->
- super()
- @tameNodeFlag = not @parent
+ tameWalkAst : (p) ->
+ p = p || this
+ @tameParentAwait = p
+ super p
+ @tameNodeFlag = true
#### tameRequire
#
View
16 test/tame.coffee
@@ -388,7 +388,7 @@ atest 'expressions -- addition (2)', (cb) ->
y = (await slowAdd 10, 20, defer _) + (await slowAdd 30, 40, defer _)
cb(y == 100, {})
-atest 'chaining', (cb) ->
+atest 'expressions - chaining', (cb) ->
id = "image data"
class Img
render : -> id
@@ -397,6 +397,20 @@ atest 'chaining', (cb) ->
cb new Img
x = (await loadImage "test.png", defer _).render()
cb(x is id, {})
+
+atest 'expressions - call args', (cb) ->
+ slowAdd = (a,b,autocb) ->
+ await delay defer()
+ a+b
+ x = await slowAdd 3, (await slowAdd 3, 4, defer _), defer _
+ cb(x is 10, {})
+
+atest 'expressions - call args (2)', (cb) ->
+ slowAdd = (a,b,autocb) ->
+ await delay defer()
+ a+b
+ x = await slowAdd (await slowAdd 1, 2, defer _), (await slowAdd 3, 4, defer _), defer _
+ cb(x is 10, {})
#atest 'arrays and objects', (cb) ->
# id = "image data"

0 comments on commit 50ce7b6

Please sign in to comment.
Something went wrong with that request. Please try again.