Permalink
Browse files

Another bug found in if's + expressions + Ops. Fixed, with a new test

case.
  • Loading branch information...
1 parent e2c6c58 commit 47db980ae988cd2b4228f7ff09a3cc3c4bf4d8b9 @maxtaco committed Jan 5, 2012
Showing with 31 additions and 22 deletions.
  1. +9 −11 lib/coffee-script/nodes.js
  2. +13 −11 src/nodes.coffee
  3. +9 −0 test/tame.coffee
View
20 lib/coffee-script/nodes.js
@@ -48,6 +48,7 @@
this.tameCpsPivotFlag = false;
this.tameHasAutocbFlag = false;
this.tameParentAwait = null;
+ this.tameCallContinuationFlag = false;
}
Base.prototype.compile = function(o, lvl) {
@@ -74,16 +75,10 @@
};
Base.prototype.compileCps = function(o) {
- var code, k, l, me, pb;
+ var code, k, l, pb;
this.tameGotCpsSplitFlag = true;
if ((l = this.tamePrequels.length)) {
- me = this.tameWrapContinuation() ? new TameTailCall(null, this) : this;
- if (this.tameContinuationBlock) {
- k = this.tameContinuationBlock;
- k.unshift(me);
- } else {
- k = me;
- }
+ k = this.tameContinuationBlock ? [this, this.tameContinuationBlock] : this.tameWrapContinuation() ? new TameTailCall(null, this) : this;
while (l--) {
pb = this.tamePrequels[l];
k = CpsCascade.wrap(pb.block, k, pb.retval, o);
@@ -360,7 +355,9 @@
return !!this.tameContinuationBlock || ((_ref2 = this.tamePrequels) != null ? _ref2.length : void 0);
};
- Base.prototype.tameCallContinuation = function() {};
+ Base.prototype.tameCallContinuation = function() {
+ return this.tameCallContinuationFlag = true;
+ };
Base.prototype.tameWrapContinuation = NO;
@@ -2406,11 +2403,12 @@
this.first = first;
this.second = second;
this.flip = !!flip;
- this.tameCallContinuationFlag = false;
return this;
}
- Op.prototype.tameWrapContinuation = YES;
+ Op.prototype.tameWrapContinuation = function() {
+ return this.tameCallContinuationFlag;
+ };
CONVERSIONS = {
'==': '===',
View
24 src/nodes.coffee
@@ -45,7 +45,9 @@ exports.Base = class Base
@tameGotCpsSplitFlag = false
@tameCpsPivotFlag = false
@tameHasAutocbFlag = false
+
@tameParentAwait = null
+ @tameCallContinuationFlag = false
# 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
@@ -85,12 +87,15 @@ exports.Base = class Base
@tameGotCpsSplitFlag = true
if (l = @tamePrequels.length)
- me = if @tameWrapContinuation() then (new TameTailCall null, this) else this
- if @tameContinuationBlock
- k = @tameContinuationBlock
- k.unshift me
+
+ # This is an optimization. We smush the "this" expression and the continuation
+ # into a flat block.
+ k = if @tameContinuationBlock
+ [ this, @tameContinuationBlock ]
+ else if @tameWrapContinuation()
+ new TameTailCall null, this
else
- k = me
+ this
while l--
pb = @tamePrequels[l]
@@ -99,7 +104,7 @@ exports.Base = class Base
else
code = CpsCascade.wrap this, @tameContinuationBlock, null, o
-
+
code.compile o
# If the code generation wishes to use the result of a complex expression
@@ -203,10 +208,8 @@ exports.Base = class Base
new Op '!', this
unwrapAll: ->
- #console.log "pre #{@toString()}"
node = this
continue until node is node = node.unwrap()
- #console.log "post #{node.toString()}"
node
# Don't try this at home with actual human kids. Added for tame
@@ -317,7 +320,7 @@ exports.Base = class Base
tameIsCpsPivot : -> @tameCpsPivotFlag
tameNestContinuationBlock : (b) -> @tameContinuationBlock = b
tameHasContinuation : -> (!!@tameContinuationBlock or @tamePrequels?.length)
- tameCallContinuation : ->
+ tameCallContinuation : -> @tameCallContinuationFlag = true
tameWrapContinuation : NO
tameIsJump : NO
tameIsTamedExpr : -> (this not instanceof Code) and @tameNodeFlag
@@ -1949,10 +1952,9 @@ exports.Op = class Op extends Base
@first = first
@second = second
@flip = !!flip
- @tameCallContinuationFlag = false
return this
- tameWrapContinuation : YES
+ tameWrapContinuation : -> @tameCallContinuationFlag
# The map of conversions from CoffeeScript to JavaScript symbols.
CONVERSIONS =
View
9 test/tame.coffee
@@ -482,3 +482,12 @@ atest 'expressions + ops + if (2)', (cb) ->
y = if true
(await parrot 10, defer _) + 1 + (await parrot 12, defer _)
cb(y == 23, {})
+
+atest 'expressions + ops + if (3)', (cb) ->
+ parrot = (n, cb) ->
+ await delay defer()
+ cb n
+ y = if true
+ (await parrot 10, defer _) + 1
+ 3
+ cb(y == 3, {})

0 comments on commit 47db980

Please sign in to comment.