From 081efd2d603f576b47d37ea73cdaef7a0cb40bee Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 8 Aug 2016 13:07:29 +0300 Subject: [PATCH] Fix bugs in JavaScript backend --- .../java/org/teavm/javascript/Renderer.java | 20 ++++++++++++---- .../resources/org/teavm/javascript/runtime.js | 23 ++++++++++++++----- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/teavm/javascript/Renderer.java b/core/src/main/java/org/teavm/javascript/Renderer.java index b63a99411..16c6efdd4 100644 --- a/core/src/main/java/org/teavm/javascript/Renderer.java +++ b/core/src/main/java/org/teavm/javascript/Renderer.java @@ -1514,10 +1514,18 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext visitBinary(expr, "%", expr.getType() == OperationType.INT); break; case EQUALS: - visitBinary(expr, "===", false); + if (expr.getType() == OperationType.INT) { + visitBinary(expr, "==", false); + } else { + visitBinary(expr, "===", false); + } break; case NOT_EQUALS: - visitBinary(expr, "!==", false); + if (expr.getType() == OperationType.INT) { + visitBinary(expr, "!=", false); + } else { + visitBinary(expr, "!==", false); + } break; case GREATER: visitBinary(expr, ">", false); @@ -1591,7 +1599,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } case NEGATE: if (expr.getType() == OperationType.LONG) { - writer.append("Long_not("); + writer.append("Long_neg("); precedence = Precedence.min(); expr.getOperand().acceptVisitor(this); writer.append(')'); @@ -2231,7 +2239,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext String clsName = ((ValueType.Object) expr.getType()).getClassName(); ClassHolder cls = classSource.get(clsName); if (cls != null && !cls.getModifiers().contains(ElementModifier.INTERFACE)) { - precedence = Precedence.COMPARISON.next(); + boolean needsParentheses = Precedence.COMPARISON.ordinal() < precedence.ordinal(); + if (needsParentheses) { + writer.append('('); + } + precedence = Precedence.CONDITIONAL.next(); expr.getExpr().acceptVisitor(this); writer.append(" instanceof ").appendClass(clsName); if (expr.getLocation() != null) { diff --git a/core/src/main/resources/org/teavm/javascript/runtime.js b/core/src/main/resources/org/teavm/javascript/runtime.js index 499aedcd0..9b448952b 100644 --- a/core/src/main/resources/org/teavm/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/javascript/runtime.js @@ -645,8 +645,10 @@ function Long_gt(a, b) { if (a.hi > b.hi) { return true; } - if ((a.lo >>> 1) > (b.lo >>> 1)) { - return true; + var x = a.lo >>> 1; + var y = b.lo >>> 1; + if (x != y) { + return x > y; } return (a.lo & 1) > (b.lo & 1); } @@ -657,8 +659,10 @@ function Long_ge(a, b) { if (a.hi > b.hi) { return true; } - if ((a.lo >>> 1) >= (b.lo >>> 1)) { - return true; + var x = a.lo >>> 1; + var y = b.lo >>> 1; + if (x != y) { + return x >= y; } return (a.lo & 1) >= (b.lo & 1); } @@ -669,8 +673,10 @@ function Long_lt(a, b) { if (a.hi < b.hi) { return true; } - if ((a.lo >>> 1) < (b.lo >>> 1)) { - return true; + var x = a.lo >>> 1; + var y = b.lo >>> 1; + if (x != y) { + return x < y; } return (a.lo & 1) < (b.lo & 1); } @@ -684,6 +690,11 @@ function Long_le(a, b) { if ((a.lo >>> 1) <= (b.lo >>> 1)) { return true; } + var x = a.lo >>> 1; + var y = b.lo >>> 1; + if (x != y) { + return x <= y; + } return (a.lo & 1) <= (b.lo & 1); }