# HG changeset patch # User Martin Soch # Date 1359636134 -3600 # Node ID 9cbf1f2ad7eeea4a605e834b3e9bdc9c682b0013 # Parent e3f671b50e93048b444feaa04232c064b62fba7d Implementation of "<<" for Long works, added tests for "+". diff -r e3f671b50e93 -r 9cbf1f2ad7ee emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Wed Jan 30 22:23:21 2013 +0100 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Thu Jan 31 13:42:14 2013 +0100 @@ -15,7 +15,7 @@ return low; } var l = new Number(low); - l.hi = this; + l.hi = this | 0; return l; }; @@ -28,7 +28,7 @@ }; Number.prototype.toLong = function() { var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0; - return hi.next32(this % (__m32+1)); + return hi.next32(Math.floor(this % (__m32+1))); }; Number.prototype.toExactString = function() { @@ -88,14 +88,15 @@ }; Number.prototype.shl64 = function(x) { - if (x > 32) { - var hi = (this << (x - 32)) & 0xFFFFFFFF; + if (x >= 32) { + var hi = (this << (x - 32)) | 0; return hi.next32(0); } else { - var hi = (this.high32() << x) & 0xFFFFFFFF; + var hi = (this.high32() << x) | 0; var low_reminder = this >> (32 - x); hi |= low_reminder; var low = this << x; + low += (low < 0) ? (__m32+1) : 0; return hi.next32(low); } }; diff -r e3f671b50e93 -r 9cbf1f2ad7ee vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Jan 30 22:23:21 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Thu Jan 31 13:42:14 2013 +0100 @@ -151,6 +151,26 @@ ); } + @Test public void longAddOverflow() throws Exception { + final long res = Long.MAX_VALUE + 1l; + assertExec("Addition 1+MAX", + Numbers.class, "addL__J_3B_3B", + Double.valueOf(res), + new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, + new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 } + ); + } + + @Test public void longAddMaxAndMax() throws Exception { + final long res = Long.MAX_VALUE + Long.MAX_VALUE; + assertExec("Addition MAX+MAX", + Numbers.class, "addL__J_3B_3B", + Double.valueOf(res), + new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, + new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff } + ); + } + private static CharSequence codeSeq; private static Invocable code; @@ -173,10 +193,11 @@ if (expRes instanceof Double && ret instanceof Double) { double expD = ((Double)expRes).doubleValue(); double retD = ((Double)ret).doubleValue(); - assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq)); + assertEquals(retD, expD, 0.000004, msg + " " + + StaticMethodTest.dumpJS(codeSeq)); return; } - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq)); + assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq)); } } diff -r e3f671b50e93 -r 9cbf1f2ad7ee vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Jan 30 22:23:21 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Thu Jan 31 13:42:14 2013 +0100 @@ -72,8 +72,12 @@ return Long.MAX_VALUE; } - public static long addL(long x, long y) { - return (x + y); + public static long addL(byte[] arrX, byte[] arrY) throws IOException { + ByteArrayInputStream isX = new ByteArrayInputStream(arrX); + DataInputStream disX = new DataInputStream(isX); + ByteArrayInputStream isY = new ByteArrayInputStream(arrY); + DataInputStream disY = new DataInputStream(isY); + return (disX.readLong() + disY.readLong()); } public static long subL(long x, long y) {