From 44264c7ea831f205a487b7ad31877f4467299d1b Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 12 Mar 2014 22:35:20 +0400 Subject: [PATCH] Fixes some bugs in JCL --- .../classlib/impl/charset/CharBuffer.java | 3 +- .../classlib/java/io/TBufferedReader.java | 6 ++-- .../classlib/java/io/TInputStreamReader.java | 2 +- .../org/teavm/classlib/java/lang/TDouble.java | 2 +- .../org/teavm/classlib/java/lang/TLong.java | 30 +++++++++---------- .../org/teavm/classlib/java/lang/TMath.java | 4 +-- .../resources/org/teavm/javascript/runtime.js | 2 +- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/CharBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/CharBuffer.java index 52c700ac4..78ef94423 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/CharBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/impl/charset/CharBuffer.java @@ -67,7 +67,8 @@ public class CharBuffer { } public void put(CharBuffer buffer) { - while (buffer.pos < buffer.end) { + int sz = Math.min(buffer.end - buffer.pos, end - pos); + for (int i = 0; i < sz; ++i) { data[pos++] = buffer.data[buffer.pos++]; } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedReader.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedReader.java index 04338c6df..af7302bbf 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedReader.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedReader.java @@ -38,7 +38,7 @@ public class TBufferedReader extends TReader { throw new TIllegalArgumentException(); } this.innerReader = innerReader; - this.buffer = new char[TMath.min(64, size)]; + this.buffer = new char[TMath.max(64, size)]; } public TBufferedReader(TReader innerReader) { @@ -160,8 +160,8 @@ public class TBufferedReader extends TReader { if (eof) { return false; } - while (true) { - int charsRead = innerReader.read(buffer, offset, buffer.length - 1); + while (offset < buffer.length) { + int charsRead = innerReader.read(buffer, offset, buffer.length - offset); if (charsRead == -1) { eof = true; break; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TInputStreamReader.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TInputStreamReader.java index 45c8da9bc..88308ff6b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TInputStreamReader.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TInputStreamReader.java @@ -82,7 +82,7 @@ public class TInputStreamReader extends TReader { break; } } - return outBuffer.position() - off; + return wrapBuffer.position() - off; } private boolean fillBuffer() throws TIOException { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index feeb56d86..1395f76e2 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -274,7 +274,7 @@ public class TDouble extends TNumber implements TComparable { return NaN; } } - boolean negative = (bits & (1 << 63)) != 0; + boolean negative = (bits & (1L << 63)) != 0; int rawExp = (int)((bits >> 52) & 0x7FFL); long mantissa = bits & 0xFFFFFFFFFFFFFL; if (rawExp == 0) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index 38c01effe..b9fa35553 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -307,12 +307,12 @@ public class TLong extends TNumber implements TComparable { } public static int bitCount(long i) { - i = (i & 0xAAAAAAAAAAAAAAAAL) >> 1 + i & 0x5555555555555555L; - i = (i & 0xCCCCCCCCCCCCCCCCL) >> 2 + i & 0x3333333333333333L; - i = (i & 0x3030303030303030L) >> 4 + i & 0x0303030303030303L; - i = (i & 0x0700070007000700L) >> 8 + i & 0x0007000700070007L; - i = (i & 0x000F0000000F0000L) >> 16 + i & 0x0000000F0000000FL; - i = (i & 0x0000001F00000000L) >> 32 + i & 0x000000000000001FL; + i = ((i & 0xAAAAAAAAAAAAAAAAL) >> 1) + (i & 0x5555555555555555L); + i = ((i & 0xCCCCCCCCCCCCCCCCL) >> 2) + (i & 0x3333333333333333L); + i = ((i & 0x3030303030303030L) >> 4) + (i & 0x0303030303030303L); + i = ((i & 0x0700070007000700L) >> 8) + (i & 0x0007000700070007L); + i = ((i & 0x000F0000000F0000L) >> 16) + (i & 0x0000000F0000000FL); + i = ((i & 0x0000001F00000000L) >> 32) + (i & 0x000000000000001FL); return (int)i; } @@ -327,19 +327,19 @@ public class TLong extends TNumber implements TComparable { } public static long reverse(long i) { - i = (i & 0xAAAAAAAAAAAAAAAAL) >> 1 | (i & 0x5555555555555555L) << 1; - i = (i & 0xCCCCCCCCCCCCCCCCL) >> 2 | (i & 0x3333333333333333L) << 2; - i = (i & 0xF0F0F0F0F0F0F0F0L) >> 4 | (i & 0x0F0F0F0F0F0F0F0FL) << 4; - i = (i & 0xFF00FF00FF00FF00L) >> 8 | (i & 0x00FF00FF00FF00FFL) << 8; - i = (i & 0xFFFF0000FFFF0000L) >> 16 | (i & 0x0000FFFF0000FFFFL) << 16; - i = (i & 0xFFFF0000FFFF0000L) >> 32 | (i & 0x0000FFFF0000FFFFL) << 32; + i = ((i & 0xAAAAAAAAAAAAAAAAL) >> 1) | ((i & 0x5555555555555555L) << 1); + i = ((i & 0xCCCCCCCCCCCCCCCCL) >> 2) | ((i & 0x3333333333333333L) << 2); + i = ((i & 0xF0F0F0F0F0F0F0F0L) >> 4) | ((i & 0x0F0F0F0F0F0F0F0FL) << 4); + i = ((i & 0xFF00FF00FF00FF00L) >> 8) | ((i & 0x00FF00FF00FF00FFL) << 8); + i = ((i & 0xFFFF0000FFFF0000L) >> 16) | ((i & 0x0000FFFF0000FFFFL) << 16); + i = ((i & 0xFFFF0000FFFF0000L) >> 32) | ((i & 0x0000FFFF0000FFFFL) << 32); return i; } public static long reverseBytes(long i) { - i = (i & 0xFF00FF00FF00FF00L) >> 8 | (i & 0x00FF00FF00FF00FFL) << 8; - i = (i & 0xFFFF0000FFFF0000L) >> 16 | (i & 0x0000FFFF0000FFFFL) << 16; - i = i >> 32 | i << 32; + i = ((i & 0xFF00FF00FF00FF00L) >> 8) | ((i & 0x00FF00FF00FF00FFL) << 8); + i = ((i & 0xFFFF0000FFFF0000L) >> 16) | ((i & 0x0000FFFF0000FFFFL) << 16); + i = (i >> 32) | (i << 32); return i; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index c765ace22..12abdb30c 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -169,12 +169,12 @@ public final class TMath extends TObject { public static double sinh(double x) { double e = exp(x); - return e - 1 / e; + return (e - 1 / e) / 2; } public static double cosh(double x) { double e = exp(x); - return e + 1 / e; + return (e + 1 / e) / 2; } public static double tanh(double x) { 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 1c7c7fd58..e51cb45cb 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -413,7 +413,7 @@ Long_fromNumber = function(val) { return new Long(val | 0, (val / 0x100000000) | 0); } Long_toNumber = function(val) { - return val.lo + 0x100000000 * val.hi; + return val.hi >= 0 ? val.lo + 0x100000000 * val.hi : -0x100000000 * (val.hi ^ 0xFFFFFFFF) + val.lo; } Long_add = function(a, b) { var a_lolo = a.lo & 0xFFFF;