From c39efdc6d7297f1232c2c61adbddf93c35deaabb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 16 May 2023 09:56:24 +0200 Subject: [PATCH] classlib: fix issue with integer unsigned right shift --- .../backend/javascript/rendering/StatementRenderer.java | 3 ++- .../test/java/org/teavm/classlib/java/lang/IntegerTest.java | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java index ec5c7775b..eb87f6d67 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java @@ -755,7 +755,8 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { visitBinary(expr, ">>", false); break; case UNSIGNED_RIGHT_SHIFT: - visitBinary(expr, ">>>", false); + // JavaScript not casts -2147483648 >>> 0 to 2147483648, which is not 32 bit integer. + visitBinary(expr, ">>>", true); break; } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java index 09a19b4f4..f484ede3c 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java @@ -180,4 +180,9 @@ public class IntegerTest { assertEquals("1111111111111111111111111111111", Integer.toString(Integer.MAX_VALUE, 2)); assertEquals("-10000000000000000000000000000000", Integer.toString(Integer.MIN_VALUE, 2)); } + + @Test + public void unsignedRightShift() { + assertEquals(Integer.MIN_VALUE, Integer.MIN_VALUE >>> Integer.parseInt("0")); + } }