diff -r b42911b78a16 -r 035fcbd7a33c 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 Sun Jan 27 10:19:02 2013 +0100 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Mon Jan 28 00:15:38 2013 +0100 @@ -8,64 +8,63 @@ Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; -var Long = function(low, hi) { - this.low = low; - this.hi = hi; +Number.prototype.next32 = function(low) { + if (this === 0) { + return low; + } + var l = new Number(low); + l.hi = this; + return l; }; -function LongFromNumber(x) { - return new Long(x % 0xFFFFFFFF, Math.floor(x / 0xFFFFFFFF)); +Number.prototype.high32 = function() { + return this.hi ? this.hi : (Math.floor(this / 0xFFFFFFFF)) | 0; +}; +Number.prototype.toInt32 = function() { return this | 0; }; +Number.prototype.toFP = function() { + return this.hi ? this.hi * 0xFFFFFFFF + this : this; +}; +Number.prototype.toLong = function() { + var hi = (this > 0xFFFFFFFF) ? (Math.floor(this / 0xFFFFFFFF)) | 0 : 0; + return hi.next32(this % 0xFFFFFFFF); +} + +Number.prototype.add64 = function(x) { + var low = this + x; + carry = 0; + if (low > 0xFFFFFFFF) { + carry = 1; + low -= 0xFFFFFFFF; // should not here be also -1? + } + var hi = (this.high32() + x.high32() + carry) & 0xFFFFFFFF; + return hi.next32(low); }; -function MakeLong(x) { - if ((x.hi == undefined) && (x.low == undefined)) { - return LongFromNumber(x); +Number.prototype.div64 = function(x) { + var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough + if (low > 0xFFFFFFFF) { + var hi = Math.floor(low / 0xFFFFFFFF) | 0; + return hi.next32(low % 0xFFFFFFFF); } - return x; + return low; }; -Long.prototype.toInt32 = function() { return this.low | 0; }; -Long.prototype.toFP = function() { return this.hi * 0xFFFFFFFF + this.low; }; - -Long.prototype.toString = function() { - return String(this.toFP()); +Number.prototype.shl64 = function(x) { + if (x > 32) { + var hi = (this << (x - 32)) & 0xFFFFFFFF; + return hi.next32(0); + } else { + var hi = (this.high32() << x) & 0xFFFFFFFF; + var low_reminder = this >> x; + hi |= low_reminder; + var low = this << x; + return hi.next32(low); + } }; -Long.prototype.valueOf = function() { - return this.toFP(); -}; - -Long.prototype.compare64 = function(x) { +Number.prototype.compare64 = function(x) { if (this.hi == x.hi) { - return (this.low == x.low) ? 0 : ((this.low < x.low) ? -1 : 1); + return (this == x) ? 0 : ((this < x) ? -1 : 1); } return (this.hi < x.hi) ? -1 : 1; }; - -Long.prototype.add64 = function(x) { - low = this.low + x.low; - carry = 0; - if (low > 0xFFFFFFFF) { - carry = 1; - low -= 0xFFFFFFFF; - } - hi = (this.hi + x.hi + carry) & 0xFFFFFFFF; - return new Long(low, hi); -}; - -Long.prototype.div64 = function(x) { - return LongFromNumber(Math.floor(this.toFP() / x.toFP())); -}; - -Long.prototype.shl64 = function(x) { - if (x > 32) { - hi = (this.low << (x - 32)) & 0xFFFFFFFF; - low = 0; - } else { - hi = (this.hi << x) & 0xFFFFFFFF; - low_reminder = this.low >> x; - hi |= low_reminder; - low = this.low << x; - } - return new Long(low, hi); -};