Wow.  Learn something every day:

  http://blog.stchur.com/2006/07/14/the-javascript-ternary-operator/

Personally, I like to use more parens than less, especially if there is any 
doubt, but perhaps that is just my lisp showing.

On 2011-09-27, at 10:12, [email protected] wrote:

> Author: dda
> Date: 2011-09-27 07:11:59 -0700 (Tue, 27 Sep 2011)
> New Revision: 19459
> 
> Modified:
>   
> openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
> Log:
> Change dda-20110927-8Ei by [email protected] on 2011-09-27 09:48:08 EDT
>    in /Users/dda/laszlo/src/svn/openlaszlo/trunk-f
>    for http://svn.openlaszlo.org/openlaszlo/trunk
> 
> Summary: Fix parsing for ternery expression mixed with assignments
> 
> New Features:
> 
> Bugs Fixed: LPP-10087 (Compiler error rewriting ternary operator expressions 
> of type (condition) ? ifTrueDoThis : ifFalseDoThis)
> 
> Technical Reviewer: (pending)
> QA Reviewer: (pending)
> Doc Reviewer: (pending)
> 
> Documentation:
> 
> Release Notes:
> 
> Overview:
>    Cause variable->register rewriting to happen in more cases
> 
> Details:
>    This expression: (result == 999) ? visible = true : visible = false;
>    apparently is parsed as:
>        ((result == 999) ? visible = true : visible) = false;
>    My reading of the precedence rules is that this is probably correct
>    interpretation since "?"/":" binds more tightly than assignment.
>    Having a ternary operator with multiple assignments makes
>    the interpretation more muddy.
> 
>    In any event, '((result == 999) ? visible = true : visible)' was
>    dealt with as a 'left hand side reference', and it was an unexpected
>    case, so register rewriting was not performed.
> 
>    There are a couple reasonable fixes: The one I applied was to do
>    the rewriting in this case, let the code be emitted and voila,
>    it is interpreted 'correctly' on firefox (at least the same as
>    typing the original JS expression to Rhino and Chrome).
> 
>    The road not taken would be to issue an error for a non-sensical
>    lvalue (left hand side expression).  FWIW, this is what java and C
>    tend to do:
>        $ cc -c t.c
>        t.c: In function 'main':
>        t.c:6: error: lvalue required as left operand of assignment
>        $ javac t.java
>        t.java:7: unexpected type
>        required: variable
>        found   : value
>                (result == 999) ? visible = 1 : visible = 0;
>                                ^
> 
> Tests:
>    test case (swf10, dhtml)
>    smokecheck (swf10, dhtml)
>    runlzunit
> 
> 
> 
> Modified: 
> openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
> ===================================================================
> --- 
> openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
>         2011-09-27 08:08:32 UTC (rev 19458)
> +++ 
> openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
>         2011-09-27 14:11:59 UTC (rev 19459)
> @@ -2057,9 +2057,12 @@
>       args[0] = visitExpression(args[0]);
>       args[1] = visitExpression(args[1]);
>       return new IndexReference(this, node, args[0], args[1]);
> +    } else if (node instanceof ASTThisReference) {
> +      return new JavascriptReference(this, node);
>     }
> -
> -    return new JavascriptReference(this, node);
> +    else {
> +      return new JavascriptReference(this, visitExpression(node));
> +    }
>   }
> 
> 
> 
> 
> _______________________________________________
> Laszlo-checkins mailing list
> [email protected]
> http://www.openlaszlo.org/mailman/listinfo/laszlo-checkins


Reply via email to