Skip to content

Commit cdec294

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

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 6 additions & 4 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
}
@@ -667,7 +669,7 @@ protected static JsExpression shortCircuitAnd(JsBinaryOperation expr,
667669
return arg1;
668670
}
669671
}
670-
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg1)) {
672+
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(expr)) {
671673
CanBooleanEval eval2 = (CanBooleanEval) arg2;
672674
if (eval2.isBooleanTrue() && !arg2.hasSideEffects()) {
673675
return arg1;
@@ -709,7 +711,7 @@ protected static JsExpression shortCircuitOr(JsBinaryOperation expr,
709711
return arg1;
710712
}
711713
}
712-
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(arg1)) {
714+
if (arg2 instanceof CanBooleanEval && evalContext.containsKey(expr)) {
713715
CanBooleanEval eval2 = (CanBooleanEval) arg2;
714716
if (eval2.isBooleanFalse() && !arg2.hasSideEffects()) {
715717
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)