diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index ce4d2691e..d2fc37126 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -905,6 +905,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext expr.getOperand().acceptVisitor(this); writer.append(')'); break; + case LONG_TO_INT: + expr.getOperand().acceptVisitor(this); + writer.append(".lo"); + break; case NEGATE_LONG: writer.append("Long_neg("); expr.getOperand().acceptVisitor(this); diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index d46b7187d..91ae06a61 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -257,7 +257,7 @@ class StatementGenerator implements InstructionVisitor { value = castToInteger(value); break; case LONG: - value = castFromLong(value); + value = castLongToInt(value); break; default: break; @@ -553,6 +553,10 @@ class StatementGenerator implements InstructionVisitor { return Expr.binary(BinaryOperation.BITWISE_OR, value, Expr.constant(0)); } + private Expr castLongToInt(Expr value) { + return Expr.unary(UnaryOperation.LONG_TO_INT, value); + } + private Expr castToLong(Expr value) { return Expr.unary(UnaryOperation.NUM_TO_LONG, value); } diff --git a/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java b/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java index 0fa0d41bb..272351b43 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java +++ b/teavm-core/src/main/java/org/teavm/javascript/ast/UnaryOperation.java @@ -26,6 +26,7 @@ public enum UnaryOperation { NEGATE_LONG, LENGTH, LONG_TO_NUM, + LONG_TO_INT, NUM_TO_LONG, INT_TO_LONG, BYTE_TO_INT, diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index c2b33be1e..2499bb1c3 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -447,9 +447,9 @@ Long_sub = function(a, b) { var b_hihi = b.hi >>> 16; var lolo = (a_lolo - b_lolo) | 0; - var lohi = (a_lohi - b_lohi - (lolo >> 16)) | 0; - var hilo = (a_hilo - b_hilo - (lohi >> 16)) | 0; - var hihi = (a_hihi - b_hihi - (hilo >> 16)) | 0; + var lohi = (a_lohi - b_lohi + (lolo >> 16)) | 0; + var hilo = (a_hilo - b_hilo + (lohi >> 16)) | 0; + var hihi = (a_hihi - b_hihi + (hilo >> 16)) | 0; return new Long((lolo & 0xFFFF) | ((lohi & 0xFFFF) << 16), (hilo & 0xFFFF) | ((hihi & 0xFFFF) << 16)); } Long_compare = function(a, b) { @@ -617,7 +617,7 @@ LongInt_inc = function(a) { if (a.lo == 0) { a.hi = (a.hi + 1) | 0; if (a.hi == 0) { - a.sup = (a.sup + 1) | 0xFFFF; + a.sup = (a.sup + 1) & 0xFFFF; } } } @@ -626,7 +626,7 @@ LongInt_dec = function(a) { if (a.lo == -1) { a.hi = (a.hi - 1) | 0; if (a.hi == -1) { - a.sup = (a.sup - 1) | 0xFFFF; + a.sup = (a.sup - 1) & 0xFFFF; } } } @@ -662,7 +662,7 @@ LongInt_numOfLeadingZeroBits = function(a) { return 31 - n; } LongInt_shl = function(a, b) { - if (b == 0) { + if (b === 0) { return; } else if (b < 32) { a.sup = ((a.hi >>> (32 - b)) | (a.sup << b)) & 0xFFFF; @@ -687,7 +687,7 @@ LongInt_shl = function(a, b) { } } LongInt_shr = function(a, b) { - if (b == 0) { + if (b === 0) { return; } else if (b === 32) { a.lo = a.hi; @@ -697,7 +697,7 @@ LongInt_shr = function(a, b) { a.lo = (a.lo >>> b) | (a.hi << (32 - b)); a.hi = (a.hi >>> b) | (a.sup << (32 - b)); a.sup >>>= b; - } else if (b == 64) { + } else if (b === 64) { a.lo = a.sup; a.hi = 0; a.sup = 0;