Fixes #2567 -- optimize generated code with an existential is directly negated.

This commit is contained in:
Jeremy Ashkenas 2012-10-23 18:17:53 -04:00
parent f029695db8
commit 35787ef79b
3 changed files with 15 additions and 2 deletions

View File

@ -2258,10 +2258,14 @@
Op.prototype.compileUnary = function(o) {
var op, parts, plusMinus;
parts = [op = this.operator];
if (op === '!' && this.first instanceof Existence) {
this.first.negated = !this.first.negated;
return this.first.compile(o);
}
if (o.level >= LEVEL_ACCESS) {
return (new Parens(this)).compile(o);
}
parts = [op = this.operator];
plusMinus = op === '+' || op === '-';
if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) {
parts.push(' ');

View File

@ -1530,9 +1530,12 @@ exports.Op = class Op extends Base
# Compile a unary **Op**.
compileUnary: (o) ->
parts = [op = @operator]
if op is '!' and @first instanceof Existence
@first.negated = not @first.negated
return @first.compile o
if o.level >= LEVEL_ACCESS
return (new Parens this).compile o
parts = [op = @operator]
plusMinus = op in ['+', '-']
parts.push ' ' if op in ['new', 'typeof', 'delete'] or
plusMinus and @first instanceof Op and @first.operator is op

View File

@ -281,4 +281,10 @@ test "#2197: Existential existential double trouble", ->
func = -> counter++
func()? ? 100
eq counter, 1
test "#2567: Optimization of negated existential produces correct result", ->
a = 1
ok !(!a?)
ok !b?