That construct isn't a generically safe JS optimization, unless you  
have additional type info at the JS level to tell you what Java type  
the ifExpr was.  I don't know for sure whether that information is  
available after the transition to JS (does SOYC make this available?).

I believe that GenerateJavaScriptAST could be a bit smarter and not  
generate "== null" and "!= null" for binary equality ops where the  
operand is known to be a reference type that isn't a string.

One example failure case where null equality is not equivalent to  
binary coersion is the following:

var a = false;
if (a != null) { alert('blah'); }

and

var a = false;
a && alert('blah')


BTW, this quick test shows that the problematic values are 0 (numeric  
zero), "" (the empty string), NaN and false:

<table>
<script>
tests = [ [], {}, 0, null, undefined, "", "0", "null", "undefined",  
NaN, "NaN", true, false ]
for (i = 0; i < tests.length; i++) {
   document.write("<tr><td>" + ("" + tests[i]) + "</td><td>" + (! 
tests[i]) + "</td><td>" + (tests[i] == null) + "</td></tr>");
}
</script>


On 10-Jun-09, at 5:20 PM, Scott Blum wrote:

> Ultimately that'd be great, whether that's a one step operation or a  
> two-step.
>
> On Wed, Jun 10, 2009 at 7:16 PM, Ray Cromwell  
> <cromwell...@gmail.com> wrote:
>
> Scott, do you mean replacing
>
> if(a != null) { expr; }
>
> with
>
> a && expr?
>
> -Ray
>
>
> On Thu, Jun 11, 2009 at 4:56 AM, <sco...@google.com> wrote:
> >
> > LGTM.  Want to point out that we can optimize some of these even  
> better
> > in cases where the nested code is an expression statement.
> >
> >
> > http://gwt-code-reviews.appspot.com/33845/diff/1/2
> > File dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java  
> (right):
> >
> > http://gwt-code-reviews.appspot.com/33845/diff/1/2#newcode37
> > Line 37: assertEquals("if(!a()){b()}", optimize("if (a()) { } else {
> > b(); }"));
> > a()||b()
> >
> > http://gwt-code-reviews.appspot.com/33845/diff/1/2#newcode45
> > Line 45: assertEquals("if(a()){b()}", optimize("if (a()) { b() }  
> else {
> > }"));
> > a()&&b()
> >
> > http://gwt-code-reviews.appspot.com/33845
> >
> > >
> >
>
> >
>


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to