# HG changeset patch # User Jaroslav Tulach # Date 1360310952 -3600 # Node ID 5c349e067fa83b151f7dad5e039da69e9ed68df8 # Parent 92b628f99997f2f65214086ef2c4f5900ffa70f8# Parent b9bf26ea01182e2b1608c3ef894e4b4d80eb46ce Merging emulation of Long to default branch diff -r 92b628f99997 -r 5c349e067fa8 emul/mini/src/main/java/java/lang/Character.java --- a/emul/mini/src/main/java/java/lang/Character.java Wed Feb 06 18:35:40 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Character.java Fri Feb 08 09:09:12 2013 +0100 @@ -2193,6 +2193,10 @@ * @see Character#isDigit(int) * @since 1.5 */ + @JavaScriptBody(args = { "codePoint", "radix" }, body= + "var x = parseInt(String.fromCharCode(codePoint), radix);\n" + + "return isNaN(x) ? -1 : x;" + ) public static int digit(int codePoint, int radix) { throw new UnsupportedOperationException(); } diff -r 92b628f99997 -r 5c349e067fa8 emul/mini/src/main/java/java/lang/Long.java --- a/emul/mini/src/main/java/java/lang/Long.java Wed Feb 06 18:35:40 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Long.java Fri Feb 08 09:09:12 2013 +0100 @@ -262,7 +262,7 @@ * @param i a {@code long} to be converted. * @return a string representation of the argument in base 10. */ - @JavaScriptBody(args = "i", body = "return i.toString();") + @JavaScriptBody(args = "i", body = "return i.toExactString();") public static String toString(long i) { if (i == Long.MIN_VALUE) return "-9223372036854775808"; diff -r 92b628f99997 -r 5c349e067fa8 emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Wed Feb 06 18:35:40 2013 +0100 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Fri Feb 08 09:09:12 2013 +0100 @@ -45,7 +45,7 @@ ) public static native byte[] expandArray(byte[] arr, int expectedSize); - @JavaScriptBody(args = {}, body = "new Date().getMilliseconds();") + @JavaScriptBody(args = {}, body = "return new Date().getMilliseconds();") public static native long currentTimeMillis(); public static long nanoTime() { diff -r 92b628f99997 -r 5c349e067fa8 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 Feb 06 18:35:40 2013 +0100 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Fri Feb 08 09:09:12 2013 +0100 @@ -4,6 +4,517 @@ Number.prototype.mul32 = function(x) { return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; }; +Number.prototype.neg32 = function() { return (-this) | 0; }; Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; -Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; \ No newline at end of file +Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; + +var __m32 = 0xFFFFFFFF; + +Number.prototype.next32 = function(low) { + if (this === 0) { + return low; + } + var l = new Number(low); + l.hi = this | 0; + return l; +}; + +Number.prototype.high32 = function() { + return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0; +}; +Number.prototype.toInt32 = function() { return this | 0; }; +Number.prototype.toFP = function() { + return this.hi ? this.hi * (__m32+1) + this : this; +}; +Number.prototype.toLong = function() { + var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0; + return hi.next32(Math.floor(this % (__m32+1))); +}; + +Number.prototype.toExactString = function() { + if (this.hi) { + // check for Long.MIN_VALUE + if ((this.hi == (0x80000000 | 0)) && (this == 0)) { + return '-9223372036854775808'; + } + var res = 0; + var a = [ 6,9,2,7,6,9,4,9,2,4 ]; + var s = ''; + var digit; + var neg = this.hi < 0; + if (neg) { + var x = this.neg64(); + var hi = x.hi; + var low = x; + } else { + var hi = this.hi; + var low = this; + } + for (var i = 0; i < a.length; i++) { + res += hi * a[i]; + var low_digit = low % 10; + digit = (res % 10) + low_digit; + + low = Math.floor(low / 10); + res = Math.floor(res / 10); + + if (digit >= 10) { + digit -= 10; + res++; + } + s = String(digit).concat(s); + } + s = String(res).concat(s).replace(/^0+/, ''); + return (neg ? '-' : '').concat(s); + } + return String(this); +}; + +Number.prototype.add64 = function(x) { + var low = this + x; + carry = 0; + if (low > __m32) { + carry = 1; + low -= (__m32+1); + } + var hi = (this.high32() + x.high32() + carry) | 0; + return hi.next32(low); +}; + +Number.prototype.sub64 = function(x) { + var low = this - x; + carry = 0; + if (low < 0) { + carry = 1; + low += (__m32+1); + } + var hi = (this.high32() - x.high32() - carry) | 0; + return hi.next32(low); +}; + +Number.prototype.mul64 = function(x) { + var low = this.mul32(x); + low += (low < 0) ? (__m32+1) : 0; + // first count upper 32 bits of (this.low * x.low) + var hi_hi = 0; + var hi_low = 0; + var m = 1; + for (var i = 0; i < 32; i++) { + if (x & m) { + hi_hi += this >>> 16; + hi_low += this & 0xFFFF + } + hi_low >>= 1; + hi_low += (hi_hi & 1) ? 0x8000 : 0; + hi_hi >>= 1; + m <<= 1; + } + var hi = (hi_hi << 16) + hi_low; + + var m1 = this.high32().mul32(x); + var m2 = this.mul32(x.high32()); + hi = hi.add32(m1).add32(m2); + + return hi.next32(low); +}; + +Number.prototype.and64 = function(x) { + var low = this & x; + low += (low < 0) ? (__m32+1) : 0; + if (this.hi && x.hi) { + var hi = this.hi & x.hi; + return hi.next32(low); + }; + return low; +}; + +Number.prototype.or64 = function(x) { + var low = this | x; + low += (low < 0) ? (__m32+1) : 0; + if (this.hi || x.hi) { + var hi = this.hi | x.hi; + return hi.next32(low); + }; + return low; +}; + +Number.prototype.xor64 = function(x) { + var low = this ^ x; + low += (low < 0) ? (__m32+1) : 0; + if (this.hi || x.hi) { + var hi = this.hi ^ x.hi; + return hi.next32(low); + }; + return low; +}; + +Number.prototype.shl64 = function(x) { + if (x >= 32) { + var hi = this << (x - 32); + return hi.next32(0); + } else { + var hi = this.high32() << x; + var low_reminder = this >> (32 - x); + hi |= low_reminder; + var low = this << x; + low += (low < 0) ? (__m32+1) : 0; + return hi.next32(low); + } +}; + +Number.prototype.shr64 = function(x) { + if (x >= 32) { + var low = this.high32() >> (x - 32); + low += (low < 0) ? (__m32+1) : 0; + return low; + } else { + var low = this >> x; + var hi_reminder = this.high32() << (32 - x); + low |= hi_reminder; + low += (low < 0) ? (__m32+1) : 0; + var hi = this.high32() >> x; + return hi.next32(low); + } +}; + +Number.prototype.ushr64 = function(x) { + if (x >= 32) { + var low = this.high32() >>> (x - 32); + low += (low < 0) ? (__m32+1) : 0; + return low; + } else { + var low = this >>> x; + var hi_reminder = this.high32() << (32 - x); + low |= hi_reminder; + low += (low < 0) ? (__m32+1) : 0; + var hi = this.high32() >>> x; + return hi.next32(low); + } +}; + +Number.prototype.compare64 = function(x) { + if (this.high32() === x.high32()) { + return (this < x) ? -1 : ((this > x) ? 1 : 0); + } + return (this.high32() < x.high32()) ? -1 : 1; +}; + +Number.prototype.neg64 = function() { + var hi = this.high32(); + var low = this; + if ((hi === 0) && (low < 0)) { return -low; } + hi = ~hi; + low = ~low; + low += (low < 0) ? (__m32+1) : 0; + var ret = hi.next32(low); + return ret.add64(1); +}; + +(function(numberPrototype) { + function __Int64(hi32, lo32) { + this.hi32 = hi32 | 0; + this.lo32 = lo32 | 0; + + this.get32 = function(bitIndex) { + var v0; + var v1; + bitIndex += 32; + var selector = bitIndex >>> 5; + switch (selector) { + case 0: + v0 = 0; + v1 = this.lo32; + break; + case 1: + v0 = this.lo32; + v1 = this.hi32; + break; + case 2: + v0 = this.hi32; + v1 = 0; + break + default: + return 0; + } + + var shift = bitIndex & 31; + if (shift === 0) { + return v0; + } + + return (v1 << (32 - shift)) | (v0 >>> shift); + } + + this.get16 = function(bitIndex) { + return this.get32(bitIndex) & 0xffff; + } + + this.set16 = function(bitIndex, value) { + bitIndex += 32; + var shift = bitIndex & 15; + var svalue = (value & 0xffff) << shift; + var smask = 0xffff << shift; + var selector = bitIndex >>> 4; + switch (selector) { + case 0: + break; + case 1: + this.lo32 = (this.lo32 & ~(smask >>> 16)) + | (svalue >>> 16); + break; + case 2: + this.lo32 = (this.lo32 & ~smask) | svalue; + break; + case 3: + this.lo32 = (this.lo32 & ~(smask << 16)) + | (svalue << 16); + this.hi32 = (this.hi32 & ~(smask >>> 16)) + | (svalue >>> 16); + break; + case 4: + this.hi32 = (this.hi32 & ~smask) | svalue; + break; + case 5: + this.hi32 = (this.hi32 & ~(smask << 16)) + | (svalue << 16); + break; + } + } + + this.getDigit = function(index, shift) { + return this.get16((index << 4) - shift); + } + + this.getTwoDigits = function(index, shift) { + return this.get32(((index - 1) << 4) - shift); + } + + this.setDigit = function(index, shift, value) { + this.set16((index << 4) - shift, value); + } + + this.countSignificantDigits = function() { + var sd; + var remaining; + + if (this.hi32 === 0) { + if (this.lo32 === 0) { + return 0; + } + + sd = 2; + remaining = this.lo32; + } else { + sd = 4; + remaining = this.hi32; + } + + if (remaining < 0) { + return sd; + } + + return (remaining < 65536) ? sd - 1 : sd; + } + + this.toNumber = function() { + var lo32 = this.lo32; + if (lo32 < 0) { + lo32 += 0x100000000; + } + + return this.hi32.next32(lo32); + } + } + + function __countLeadingZeroes16(number) { + var nlz = 0; + + if (number < 256) { + nlz += 8; + number <<= 8; + } + + if (number < 4096) { + nlz += 4; + number <<= 4; + } + + if (number < 16384) { + nlz += 2; + number <<= 2; + } + + return (number < 32768) ? nlz + 1 : nlz; + } + + // q = u / v; r = u - q * v; + // v != 0 + function __div64(q, r, u, v) { + var m = u.countSignificantDigits(); + var n = v.countSignificantDigits(); + + q.hi32 = q.lo32 = 0; + + if (n === 1) { + // v has single digit + var vd = v.getDigit(0, 0); + var carry = 0; + for (var i = m - 1; i >= 0; --i) { + var ui = (carry << 16) | u.getDigit(i, 0); + if (ui < 0) { + ui += 0x100000000; + } + var qi = (ui / vd) | 0; + q.setDigit(i, 0, qi); + carry = ui - qi * vd; + } + + r.hi32 = 0; + r.lo32 = carry; + return; + } + + r.hi32 = u.hi32; + r.lo32 = u.lo32; + + if (m < n) { + return; + } + + // Normalize + var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0)); + + var vd1 = v.getDigit(n - 1, nrm); + var vd0 = v.getDigit(n - 2, nrm); + for (var j = m - n; j >= 0; --j) { + // Calculate qj estimate + var ud21 = r.getTwoDigits(j + n, nrm); + var ud2 = ud21 >>> 16; + if (ud21 < 0) { + ud21 += 0x100000000; + } + + var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0); + var rest = ud21 - qest * vd1; + + // 0 <= (qest - qj) <= 2 + + // Refine qj estimate + var ud0 = r.getDigit(j + n - 2, nrm); + while ((qest * vd0) > ((rest * 0x10000) + ud0)) { + --qest; + rest += vd1; + } + + // 0 <= (qest - qj) <= 1 + + // Multiply and subtract + var carry = 0; + for (var i = 0; i < n; ++i) { + var vi = qest * v.getDigit(i, nrm); + var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff); + r.setDigit(i + j, nrm, ui); + carry = (vi >>> 16) - (ui >> 16); + } + var uj = ud2 - carry; + + if (uj < 0) { + // qest - qj = 1 + + // Add back + --qest; + var carry = 0; + for (var i = 0; i < n; ++i) { + var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm) + + carry; + r.setDigit(i + j, nrm, ui); + carry = ui >> 16; + } + uj += carry; + } + + q.setDigit(j, 0, qest); + r.setDigit(j + n, nrm, uj); + } + } + + numberPrototype.div64 = function(x) { + var negateResult = false; + var u, v; + + if ((this.high32() & 0x80000000) != 0) { + u = this.neg64(); + negateResult = !negateResult; + } else { + u = this; + } + + if ((x.high32() & 0x80000000) != 0) { + v = x.neg64(); + negateResult = !negateResult; + } else { + v = x; + } + + if ((v === 0) && (v.high32() === 0)) { + // TODO: throw + } + + if (u.high32() === 0) { + if (v.high32() === 0) { + var result = (u / v) | 0; + return negateResult ? result.neg64() : result; + } + + return 0; + } + + var u64 = new __Int64(u.high32(), u); + var v64 = new __Int64(v.high32(), v); + var q64 = new __Int64(0, 0); + var r64 = new __Int64(0, 0); + + __div64(q64, r64, u64, v64); + + var result = q64.toNumber(); + return negateResult ? result.neg64() : result; + } + + numberPrototype.mod64 = function(x) { + var negateResult = false; + var u, v; + + if ((this.high32() & 0x80000000) != 0) { + u = this.neg64(); + negateResult = !negateResult; + } else { + u = this; + } + + if ((x.high32() & 0x80000000) != 0) { + v = x.neg64(); + } else { + v = x; + } + + if ((v === 0) && (v.high32() === 0)) { + // TODO: throw + } + + if (u.high32() === 0) { + var result = (v.high32() === 0) ? (u % v) : u; + return negateResult ? result.neg64() : result; + } + + var u64 = new __Int64(u.high32(), u); + var v64 = new __Int64(v.high32(), v); + var q64 = new __Int64(0, 0); + var r64 = new __Int64(0, 0); + + __div64(q64, r64, u64, v64); + + var result = r64.toNumber(); + return negateResult ? result.neg64() : result; + } +})(Number.prototype); diff -r 92b628f99997 -r 5c349e067fa8 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Feb 06 18:35:40 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Feb 08 09:09:12 2013 +0100 @@ -520,7 +520,7 @@ emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI()); break; case opc_ladd: - emit(out, "@1 += @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL()); break; case opc_fadd: emit(out, "@1 += @2;", smapper.getF(1), smapper.popF()); @@ -532,7 +532,7 @@ emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI()); break; case opc_lsub: - emit(out, "@1 -= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL()); break; case opc_fsub: emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF()); @@ -544,7 +544,7 @@ emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI()); break; case opc_lmul: - emit(out, "@1 *= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL()); break; case opc_fmul: emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF()); @@ -557,7 +557,7 @@ smapper.getI(1), smapper.popI()); break; case opc_ldiv: - emit(out, "@1 = Math.floor(@1 / @2);", + emit(out, "@1 = @1.div64(@2);", smapper.getL(1), smapper.popL()); break; case opc_fdiv: @@ -570,7 +570,8 @@ emit(out, "@1 %= @2;", smapper.getI(1), smapper.popI()); break; case opc_lrem: - emit(out, "@1 %= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.mod64(@2);", + smapper.getL(1), smapper.popL()); break; case opc_frem: emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF()); @@ -582,25 +583,25 @@ emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI()); break; case opc_land: - emit(out, "@1 &= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL()); break; case opc_ior: emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI()); break; case opc_lor: - emit(out, "@1 |= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL()); break; case opc_ixor: emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI()); break; case opc_lxor: - emit(out, "@1 ^= @2;", smapper.getL(1), smapper.popL()); + emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL()); break; case opc_ineg: - emit(out, "@1 = -@1;", smapper.getI(0)); + emit(out, "@1 = @1.neg32();", smapper.getI(0)); break; case opc_lneg: - emit(out, "@1 = -@1;", smapper.getL(0)); + emit(out, "@1 = @1.neg64();", smapper.getL(0)); break; case opc_fneg: emit(out, "@1 = -@1;", smapper.getF(0)); @@ -612,19 +613,19 @@ emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI()); break; case opc_lshl: - emit(out, "@1 <<= @2;", smapper.getL(1), smapper.popI()); + emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI()); break; case opc_ishr: emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI()); break; case opc_lshr: - emit(out, "@1 >>= @2;", smapper.getL(1), smapper.popI()); + emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI()); break; case opc_iushr: emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI()); break; case opc_lushr: - emit(out, "@1 >>>= @2;", smapper.getL(1), smapper.popI()); + emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI()); break; case opc_iinc: { ++i; @@ -671,14 +672,14 @@ emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD()); break; case opc_l2i: - emit(out, "var @2 = @1;", smapper.popL(), smapper.pushI()); + emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI()); break; // max int check? case opc_l2f: - emit(out, "var @2 = @1;", smapper.popL(), smapper.pushF()); + emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF()); break; case opc_l2d: - emit(out, "var @2 = @1;", smapper.popL(), smapper.pushD()); + emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD()); break; case opc_f2d: emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD()); @@ -691,7 +692,7 @@ smapper.popF(), smapper.pushI()); break; case opc_f2l: - emit(out, "var @2 = Math.floor(@1);", + emit(out, "var @2 = Math.floor(@1).toLong();", smapper.popF(), smapper.pushL()); break; case opc_d2i: @@ -699,7 +700,7 @@ smapper.popD(), smapper.pushI()); break; case opc_d2l: - emit(out, "var @2 = Math.floor(@1);", + emit(out, "var @2 = Math.floor(@1).toLong();", smapper.popD(), smapper.pushL()); break; case opc_i2b: @@ -769,11 +770,19 @@ i += 2; String v = encodeConstant(indx); int type = VarType.fromConstantType(jc.getTag(indx)); - emit(out, "var @1 = @2;", smapper.pushT(type), v); + if (type == VarType.LONG) { + final Long lv = new Long(v); + final int low = (int)(lv.longValue() & 0xFFFFFFFF); + final int hi = (int)(lv.longValue() >> 32); + emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), + Integer.toHexString(low), Integer.toHexString(hi)); + } else { + emit(out, "var @1 = @2;", smapper.pushT(type), v); + } break; } case opc_lcmp: - emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);", + emit(out, "var @3 = @2.compare64(@1);", smapper.popL(), smapper.popL(), smapper.pushI()); break; case opc_fcmpl: diff -r 92b628f99997 -r 5c349e067fa8 vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 06 18:35:40 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Fri Feb 08 09:09:12 2013 +0100 @@ -143,7 +143,7 @@ s ); } - + private static CharSequence codeSeq; private static Invocable code; @@ -157,31 +157,20 @@ } private static void assertExec( - String msg, Class clazz, String method, Object expRes, Object... args) throws Exception { - - Object ret = null; - try { - ret = code.invokeFunction("bck2brwsr"); - ret = code.invokeMethod(ret, "loadClass", clazz.getName()); - ret = code.invokeMethod(ret, method, args); - } catch (ScriptException ex) { - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex); - } catch (NoSuchMethodException ex) { - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex); - } - if (ret == null && expRes == null) { - return; - } - if (expRes.equals(ret)) { + String msg, Class clazz, String method, Object expRes, Object... args) throws Exception + { + Object ret = TestUtils.execCode(code, codeSeq, msg, clazz, method, expRes, args); + if (ret == null) { return; } 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 92b628f99997 -r 5c349e067fa8 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Wed Feb 06 18:35:40 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Fri Feb 08 09:09:12 2013 +0100 @@ -353,17 +353,8 @@ String msg, Class clazz, String method, Object expRes, Object... args ) throws Exception { - Object ret = null; - try { - ret = toRun.invokeFunction("bck2brwsr"); - ret = toRun.invokeMethod(ret, "loadClass", clazz.getName()); - ret = toRun.invokeMethod(ret, method, args); - } catch (ScriptException ex) { - fail("Execution failed in\n" + dumpJS(theCode), ex); - } catch (NoSuchMethodException ex) { - fail("Cannot find method in\n" + dumpJS(theCode), ex); - } - if (ret == null && expRes == null) { + Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args); + if (ret == null) { return; } if (expRes != null && expRes.equals(ret)) { diff -r 92b628f99997 -r 5c349e067fa8 vm/src/test/java/org/apidesign/vm4brwsr/TestUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/TestUtils.java Fri Feb 08 09:09:12 2013 +0100 @@ -0,0 +1,60 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import javax.script.Invocable; +import javax.script.ScriptException; +import static org.testng.Assert.*; + +class TestUtils { + + static Object execCode(Invocable code, CharSequence codeSeq, + String msg, Class clazz, String method, Object expRes, Object... args) + throws Exception + { + Object ret = null; + try { + ret = code.invokeFunction("bck2brwsr"); + ret = code.invokeMethod(ret, "loadClass", clazz.getName()); + ret = code.invokeMethod(ret, method, args); + } catch (ScriptException ex) { + fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex); + } catch (NoSuchMethodException ex) { + fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex); + } + if (ret == null && expRes == null) { + return null; + } + if (expRes.equals(ret)) { + return null; + } + if (expRes instanceof Number) { + // in case of Long it is necessary convert it to number + // since the Long is represented by two numbers in JavaScript + try { + ret = code.invokeMethod(ret, "toFP"); + ret = code.invokeFunction("Number", ret); + } catch (ScriptException ex) { + fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex); + } catch (NoSuchMethodException ex) { + fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex); + } + } + return ret; + } +} diff -r 92b628f99997 -r 5c349e067fa8 vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Wed Feb 06 18:35:40 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Fri Feb 08 09:09:12 2013 +0100 @@ -51,7 +51,7 @@ ScriptEngine[] arr = { null }; code = StaticMethodTest.compileClass(sb, arr, - "org/apidesign/vm4brwsr/VM" + new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"} ); arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE); codeSeq = sb; diff -r 92b628f99997 -r 5c349e067fa8 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Wed Feb 06 18:35:40 2013 +0100 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Fri Feb 08 09:09:12 2013 +0100 @@ -47,6 +47,10 @@ return x % y; } + private static int neg(int x) { + return (-x); + } + @Compare public int addOverflow() { return add(Integer.MAX_VALUE, 1); } @@ -91,6 +95,18 @@ return mod(1, 2); } + @Compare public int negate() { + return neg(123456); + } + + @Compare public int negateMaxInt() { + return neg(Integer.MAX_VALUE); + } + + @Compare public int negateMinInt() { + return neg(Integer.MIN_VALUE); + } + @Compare public int sumTwoDimensions() { int[][] matrix = createMatrix(4, 3); matrix[0][0] += 10; diff -r 92b628f99997 -r 5c349e067fa8 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Fri Feb 08 09:09:12 2013 +0100 @@ -0,0 +1,334 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class LongArithmeticTest { + + private static long add(long x, long y) { + return (x + y); + } + + private static long sub(long x, long y) { + return (x - y); + } + + private static long mul(long x, long y) { + return (x * y); + } + + private static long div(long x, long y) { + return (x / y); + } + + private static long mod(long x, long y) { + return (x % y); + } + + private static long neg(long x) { + return (-x); + } + + private static long shl(long x, int b) { + return (x << b); + } + + private static long shr(long x, int b) { + return (x >> b); + } + + private static long ushr(long x, int b) { + return (x >>> b); + } + + private static long and(long x, long y) { + return (x & y); + } + + private static long or(long x, long y) { + return (x | y); + } + + private static long xor(long x, long y) { + return (x ^ y); + } + + public static int compare(long x, long y, int zero) { + final int xyResult = compareL(x, y, zero); + final int yxResult = compareL(y, x, zero); + + return ((xyResult + yxResult) == 0) ? xyResult : -2; + } + + private static int compareL(long x, long y, int zero) { + int result = -2; + int trueCount = 0; + + x += zero; + if (x == y) { + result = 0; + ++trueCount; + } + + x += zero; + if (x < y) { + result = -1; + ++trueCount; + } + + x += zero; + if (x > y) { + result = 1; + ++trueCount; + } + + return (trueCount == 1) ? result : -2; + } + + @Compare public long conversion() { + return Long.MAX_VALUE; + } + + @Compare public long negate1() { + return neg(0x00fa37d7763e0ca1l); + } + + @Compare public long negate2() { + return neg(0x80fa37d7763e0ca1l); + } + + @Compare public long negate3() { + return neg(0xfffffffffffffeddl); + } + + @Compare public long addOverflow() { + return add(Long.MAX_VALUE, 1l); + } + + @Compare public long subUnderflow() { + return sub(Long.MIN_VALUE, 1l); + } + + @Compare public long addMaxLongAndMaxLong() { + return add(Long.MAX_VALUE, Long.MAX_VALUE); + } + + @Compare public long subMinLongAndMinLong() { + return sub(Long.MIN_VALUE, Long.MIN_VALUE); + } + + @Compare public long subMinLongAndMaxLong() { + return sub(Long.MIN_VALUE, Long.MAX_VALUE); + } + + @Compare public long multiplyMaxLong() { + return mul(Long.MAX_VALUE, 2l); + } + + @Compare public long multiplyMaxLongAndMaxLong() { + return mul(Long.MAX_VALUE, Long.MAX_VALUE); + } + + @Compare public long multiplyMinLong() { + return mul(Long.MIN_VALUE, 2l); + } + + @Compare public long multiplyMinLongAndMinLong() { + return mul(Long.MIN_VALUE, Long.MIN_VALUE); + } + + @Compare public long multiplyPrecision() { + return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el); + } + + @Compare public long divideSmallPositiveNumbers() { + return div(0xabcdef, 0x123); + } + + @Compare public long divideSmallNegativeNumbers() { + return div(-0xabcdef, -0x123); + } + + @Compare public long divideSmallMixedNumbers() { + return div(0xabcdef, -0x123); + } + + @Compare public long dividePositiveNumbersOneDigitDenom() { + return div(0xabcdef0102ffffl, 0x654); + } + + @Compare public long divideNegativeNumbersOneDigitDenom() { + return div(-0xabcdef0102ffffl, -0x654); + } + + @Compare public long divideMixedNumbersOneDigitDenom() { + return div(-0xabcdef0102ffffl, 0x654); + } + + @Compare public long dividePositiveNumbersMultiDigitDenom() { + return div(0x7ffefc003322aabbl, 0x89ab1000l); + } + + @Compare public long divideNegativeNumbersMultiDigitDenom() { + return div(-0x7ffefc003322aabbl, -0x123489ab1001l); + } + + @Compare public long divideMixedNumbersMultiDigitDenom() { + return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l); + } + + @Compare public long divideWithOverflow() { + return div(0x8000fffe0000l, 0x8000ffffl); + } + + @Compare public long divideWithCorrection() { + return div(0x7fff800000000000l, 0x800000000001l); + } + + @Compare public long moduloSmallPositiveNumbers() { + return mod(0xabcdef, 0x123); + } + + @Compare public long moduloSmallNegativeNumbers() { + return mod(-0xabcdef, -0x123); + } + + @Compare public long moduloSmallMixedNumbers() { + return mod(0xabcdef, -0x123); + } + + @Compare public long moduloPositiveNumbersOneDigitDenom() { + return mod(0xabcdef0102ffffl, 0x654); + } + + @Compare public long moduloNegativeNumbersOneDigitDenom() { + return mod(-0xabcdef0102ffffl, -0x654); + } + + @Compare public long moduloMixedNumbersOneDigitDenom() { + return mod(-0xabcdef0102ffffl, 0x654); + } + + @Compare public long moduloPositiveNumbersMultiDigitDenom() { + return mod(0x7ffefc003322aabbl, 0x89ab1000l); + } + + @Compare public long moduloNegativeNumbersMultiDigitDenom() { + return mod(-0x7ffefc003322aabbl, -0x123489ab1001l); + } + + @Compare public long moduloMixedNumbersMultiDigitDenom() { + return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l); + } + + @Compare public long moduloWithOverflow() { + return mod(0x8000fffe0000l, 0x8000ffffl); + } + + @Compare public long moduloWithCorrection() { + return mod(0x7fff800000000000l, 0x800000000001l); + } + + @Compare public long shiftL1() { + return shl(0x00fa37d7763e0ca1l, 5); + } + + @Compare public long shiftL2() { + return shl(0x00fa37d7763e0ca1l, 32); + } + + @Compare public long shiftL3() { + return shl(0x00fa37d7763e0ca1l, 45); + } + + @Compare public long shiftR1() { + return shr(0x00fa37d7763e0ca1l, 5); + } + + @Compare public long shiftR2() { + return shr(0x00fa37d7763e0ca1l, 32); + } + + @Compare public long shiftR3() { + return shr(0x00fa37d7763e0ca1l, 45); + } + + @Compare public long uShiftR1() { + return ushr(0x00fa37d7763e0ca1l, 5); + } + + @Compare public long uShiftR2() { + return ushr(0x00fa37d7763e0ca1l, 45); + } + + @Compare public long uShiftR3() { + return ushr(0xf0fa37d7763e0ca1l, 5); + } + + @Compare public long uShiftR4() { + return ushr(0xf0fa37d7763e0ca1l, 45); + } + + @Compare public long and1() { + return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el); + } + + @Compare public long or1() { + return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el); + } + + @Compare public long xor1() { + return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el); + } + + @Compare public long xor2() { + return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el); + } + + @Compare public long xor3() { + return xor(0x00000000763e0ca1l, 0x00000000ff00123el); + } + + @Compare public int compareSameNumbers() { + return compare(0x0000000000000000l, 0x0000000000000000l, 0); + } + + @Compare public int comparePositiveNumbers() { + return compare(0x0000000000200000l, 0x0000000010000000l, 0); + } + + @Compare public int compareNegativeNumbers() { + return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0); + } + + @Compare public int compareMixedNumbers() { + return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0); + } + + @Factory + public static Object[] create() { + return VMTest.create(LongArithmeticTest.class); + } +}