Skip to content

Commit bda5a4a

Browse files
committed
Do not remove ||0 in non-boolean contexts
1 parent ab591de commit bda5a4a

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

dev/core/src/com/google/gwt/dev/js/JsStaticEval.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static class MustExecVisitor extends JsVisitor {
104104

105105
private final List<JsStatement> mustExec = new ArrayList<JsStatement>();
106106

107-
public MustExecVisitor() {
107+
MustExecVisitor() {
108108
}
109109

110110
@Override
@@ -184,7 +184,9 @@ public boolean visit(JsBinaryOperation x, JsContext ctx) {
184184
evalContext.put(x.getArg2(), evalContext.get(x));
185185
} else if (x.getOperator() == JsBinaryOperator.COMMA) {
186186
evalContext.put(x.getArg1(), EvalMode.VOID);
187-
evalContext.put(x.getArg2(), evalContext.get(x));
187+
if (evalContext.containsKey(x)) {
188+
evalContext.put(x.getArg2(), evalContext.get(x));
189+
}
188190
}
189191
return true;
190192
}
@@ -226,8 +228,12 @@ public void endVisit(JsBinaryOperation x, JsContext ctx) {
226228
break;
227229
}
228230
}
229-
evalContext.remove(arg1);
230-
evalContext.remove(arg2);
231+
if (arg1 != result) {
232+
evalContext.remove(arg1);
233+
}
234+
if (arg2 != result) {
235+
evalContext.remove(arg2);
236+
}
231237
result = maybeReorderOperations(result);
232238

233239
if (result != x) {
@@ -667,7 +673,7 @@ protected static JsExpression shortCircuitAnd(JsBinaryOperation expr,
667673
return arg1;
668674
}
669675
}
670-
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg1)) {
676+
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg2)) {
671677
CanBooleanEval eval2 = (CanBooleanEval) arg2;
672678
if (eval2.isBooleanTrue() && !arg2.hasSideEffects()) {
673679
return arg1;
@@ -709,7 +715,7 @@ protected static JsExpression shortCircuitOr(JsBinaryOperation expr,
709715
return arg1;
710716
}
711717
}
712-
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg1)) {
718+
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg2)) {
713719
CanBooleanEval eval2 = (CanBooleanEval) arg2;
714720
if (eval2.isBooleanFalse() && !arg2.hasSideEffects()) {
715721
return arg1;

dev/core/test/com/google/gwt/dev/js/JsStaticEvalTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public void testShortCircuitAnd() throws Exception {
163163
assertEquals("alert(a&&true);", optimize("alert(a && true)"));
164164
assertEquals("alert(a&&false);", optimize("alert(a && false)"));
165165
assertEquals("alert(!!a&&!!b);", optimize("alert(!!a && !!b)"));
166+
assertEquals("alert(c|(bits&&1));", optimize("alert(c | (a, bits && 1))"));
166167

167168
// in boolean context we can simplify more
168169
assertEquals("alert(a&&b?c:d);", optimize("alert(!!a && !!b ? c :d)"));
@@ -180,6 +181,11 @@ public void testShortCircuitAndWithSideEffects() throws Exception {
180181
assertEquals("a()&&c()?d():e();", optimize("a() && true && c() ? d() : e();"));
181182
}
182183

184+
public void testSimplifyComma() throws Exception {
185+
assertEquals("alert(!!a());", optimize("alert((true, !!a()))"));
186+
assertEquals("alert((a(),true));", optimize("alert((!!a(), true))"));
187+
}
188+
183189
public void testShortCircuitOr() throws Exception {
184190
assertEquals("alert(true);", optimize("alert(true || a)"));
185191
assertEquals("alert(a);", optimize("alert(false || a)"));
@@ -188,6 +194,7 @@ public void testShortCircuitOr() throws Exception {
188194
assertEquals("alert(a||true);", optimize("alert(a || true)"));
189195
assertEquals("alert(a||false);", optimize("alert(a || false)"));
190196
assertEquals("alert(!!a||!!b);", optimize("alert(!!a || !!b)"));
197+
assertEquals("alert(c|(bits||0));", optimize("alert(c | (a, bits || 0))"));
191198

192199
// in boolean context we can simplify more
193200
assertEquals("alert(a||b?c:d);", optimize("alert(!!a || !!b ? c :d)"));

0 commit comments

Comments
 (0)