Skip to content

Commit d7a25e3

Browse files
hansonrhansonr
authored andcommitted
Long.parseLong should fail for -Long.MIN_VALUE
1 parent a3001e0 commit d7a25e3

File tree

2 files changed

+96
-65
lines changed

2 files changed

+96
-65
lines changed

sources/net.sf.j2s.java.core/src/test/Test_Long.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,12 @@ private static void testToString() {
539539
k = Long.parseLong("" + Long.MAX_VALUE);
540540
assert (k == Long.MAX_VALUE);
541541

542+
try {
542543
k = Long.parseLong("9223372036854775808");
543-
assert (k == Long.MAX_VALUE);
544+
assert (false);
545+
} catch (NumberFormatException e) {
546+
assert(true);
547+
}
544548

545549

546550

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 91 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,54 +3437,51 @@ Math.min$J$J = function(x,y) { return Long.min$J$J(x,y); }
34373437

34383438
Math.round$D = function(x) { return Clazz.toLong(Math.round(x)); }
34393439

3440-
34413440
var arex = function(s) {
34423441
throw Clazz.new_(Clazz.load('ArithmeticException').c$$S,[s||"integer overflow"]);
34433442
}
34443443

34453444
Math.addExact$J$J = function(x, y) {
34463445
var r = Long.$add(x,y);
3447-
if (Long.$sign(r) != 0 && Long.$sign(x) == Long.$sign(y) && Long.$sign(x) != Long.$sign(r)) {
3448-
arex();
3449-
}
3446+
(Long.$sign(r) != 0 && Long.$sign(x) == Long.$sign(y) && Long.$sign(x) != Long.$sign(r)) && arex();
34503447
return r;
34513448
}
34523449

34533450
Math.subtractExact$J$J = function(x, y) {
34543451
var r = Long.$sub(x,y);
3455-
if (Long.$sign(r) != 0 && Long.$sign(x) == Long.$sign(y) && Long.$sign(x) != Long.$sign(r)) {
3456-
arex();
3457-
}
3452+
(Long.$sign(r) != 0 && Long.$sign(x) == Long.$sign(y) && Long.$sign(x) != Long.$sign(r)) && arex();
34583453
return r;
34593454
}
34603455

3461-
Math.multiplyExact$J$J = function(x, y) {
3462-
var r = Long.$mul(x,y);
3463-
// long check?
3464-
// if (?) { ??
3465-
// throw new ArithmeticException("integer overflow");
3466-
// }
3467-
return r;
3456+
Math.floorDiv$J$J = function(x,y) {
3457+
var r = Long.$div(x,y);
3458+
return (r < 0 && Long.$ne(Long.$mul(r,y), x) ? Long.$dec(r) : r);
34683459
}
34693460

3461+
Math.floorMod$J$J = function(x,y) { return Long.$sub(x, Long.$mul(Math.floorDiv(x, y), y)); }
3462+
34703463
Math.incrementExact$J = function(a) {
3471-
if (Long.$eq(a, Long_MAX_VALUE)) {
3472-
arex();
3473-
}
3464+
(Long.$eq(a, Long_MAX_VALUE)) && arex();
34743465
return Long.$inc(a,1);
34753466
}
34763467

34773468
Math.decrementExact$J = function(a) {
3478-
if (Long.$eq(a, Long_MIN_VALUE)) {
3469+
(Long.$eq(a, Long_MIN_VALUE)) && arex();
3470+
return Long.$inc(a,-1);
3471+
}
3472+
3473+
Math.multiplyExact$J$J = function(x, y) {
3474+
var r = Long.$mul(x,y);
3475+
if (Long.$sign(r) != Long.$sign(x) * Long.$sign(y)) {
34793476
arex();
34803477
}
3481-
return Long.$inc(a,-1);
3478+
return r;
34823479
}
34833480

34843481
Math.negateExact$J = function(a) {return Long.$neg(a);}
34853482

34863483
Math.toIntExact$J = function(value) {
3487-
if (Long.$ival(value) != Long.$lval(value)) {
3484+
if (!Long.$eq(Long.$ival(value), value)) {
34883485
arex();
34893486
}
34903487
return value;
@@ -3533,8 +3530,6 @@ Math.decrementExact = function(a) {
35333530

35343531
Math.negateExact = function(a) {return -a}
35353532

3536-
Math.toIntExact = function(a) { return a}
3537-
35383533
Math.floorDiv || (Math.floorDiv = function(x,y) {
35393534
var r = (x / y) | 0;
35403535
if ((x ^ y) < 0 && (r * y != x)) {
@@ -3933,7 +3928,7 @@ m$(Integer,"numberOfTrailingZeros$I",
39333928
function(i) {
39343929
if (i == 0) return 32;
39353930
var n = 31;
3936-
var y = i <<16; if (y != 0) { n = n -16; i = y; }
3931+
var y = i <<16; if (y != 0) { n = n - 16; i = y; }
39373932
y = i << 8; if (y != 0) { n = n - 8; i = y; }
39383933
y = i << 4; if (y != 0) { n = n - 4; i = y; }
39393934
y = i << 2; if (y != 0) { n = n - 2; i = y; }
@@ -4155,7 +4150,7 @@ var toLongRMS = function(s0, noOver) {
41554150
r += Math.floor((r0 - rh)*MAXR);
41564151

41574152
lm += r/MAXR|0;
4158-
r = r&RMASK;
4153+
r &= RMASK;
41594154

41604155
// combining the integer high part h * ti with the overflow of the
41614156
// lower numbers (rh and lm):
@@ -4629,7 +4624,7 @@ Long.$xor=function(a,b){
46294624
return fromLongRLH(a);
46304625
}
46314626

4632-
Long.$mul=function(a,b){
4627+
Long.$mul = function(a,b){
46334628
if (arguments.length > 2)
46344629
return doLong(Long.$mul,arguments);
46354630
if (fixLongAB(a,b)) {
@@ -4758,18 +4753,17 @@ var toLongRLH = function(rms) {
47584753
return [0,0,0];
47594754
}
47604755
var isNeg = (rms[2] == -1);
4761-
var rl = r;
47624756
var ml = m&LMASK;
4763-
var mh = (m - ml)/MAXL;
4757+
m = (m - ml)/MAXL;
47644758
if (isNeg) {
47654759
r = (~r&RMASK) + 1;
47664760
ml = (~ml&LMASK) + (r == MAXR ? 1 : 0);
4767-
mh = (~mh&HMASK) + (ml == MAXL ? 1 : 0);
4768-
rl = r&RMASK;
4769-
ml = ml&LMASK;
4770-
mh = mh&HMASK;
4761+
m = (~m&HMASK) + (ml == MAXL ? 1 : 0);
4762+
r &= RMASK;
4763+
ml &= LMASK;
4764+
m &= HMASK;
47714765
}
4772-
return [rl,ml,mh];
4766+
return [r,ml,m];
47734767
}
47744768

47754769
var fromLongRLH = function(rlh) {
@@ -4783,16 +4777,16 @@ var fromLongRLH = function(rlh) {
47834777
if (isNeg) {
47844778
r = (~r&RMASK) + 1;
47854779
m = MAXM - m - (r == MAXR ? 0 : 1);
4786-
r = r&RMASK;
4780+
r &= RMASK;
47874781
}
47884782
return checkLong([r,m, !r&&!m ? 0 : isNeg ? -1 : 1]);
47894783
}
47904784

47914785
//Long.TYPE=Long.prototype.TYPE=Long;
47924786
//Note that the largest usable "Long" in JavaScript is 53 digits:
47934787

4794-
Long.MIN_VALUE=Long.prototype.MIN_VALUE=-0x1fffffffffffff;
4795-
Long.MAX_VALUE=Long.prototype.MAX_VALUE=0x1fffffffffffff;
4788+
Long.MIN_VALUE=Long.prototype.MIN_VALUE=LONG_MIN_VALUE;
4789+
Long.MAX_VALUE=Long.prototype.MAX_VALUE=LONG_MAX_VALUE;
47964790

47974791
var maxLong = 0x1000000000000000000000; // ignored
47984792
var minLong = -maxLong;
@@ -4820,8 +4814,6 @@ function(v){
48204814

48214815
Long.compare$J$J = function(a,b) { return Long.$cmp(a,b); }
48224816

4823-
Long.compareUnsigned$J$J = function(a,b) { return Long.$cmp(a,b,1); }
4824-
48254817
m$(Long,"compareTo$Long", function(l){return Long.$cmp(this.valueOf(), l.valueOf());});
48264818

48274819
m$(Long,"valueOf$S",
@@ -4899,9 +4891,11 @@ m$(Long, "$box$", function(v) {
48994891
;(function(C$) {
49004892

49014893
m$(C$, 'compareUnsigned$J$J', function (x, y) {
4902-
return C$.compare$J$J(Long.$add(x,[0,549755813888,-1]), Long.$add(y,[0,549755813888,-1]));
4894+
return C$.compare$J$J(Long.$add(x,LONG_MIN_VALUE), Long.$add(y,LONG_MIN_VALUE));
49034895
}, 1);
49044896

4897+
//Long.compareUnsigned$J$J = function(a,b) { return Long.$cmp(a,b,1); }
4898+
49054899
m$(C$, 'divideUnsigned$J$J', function (dividend, divisor) {
49064900
if (Long.$lt(divisor,0 )) {
49074901
return (C$.compareUnsigned$J$J(dividend, divisor)) < 0 ? 0 : 1;
@@ -5032,34 +5026,67 @@ m$(C$, 'min$J$J', function (a, b) {
50325026
return (Long.$le(a,b) ? a : b);
50335027
}, 1);
50345028

5035-
})(Long);
5036-
5037-
/*
5038-
* TODO - Long
5039-
*
5029+
Clazz.newMeth(C$, 'getLong$S', function (nm) {
5030+
return C$.getLong$S$Long(nm, null);
5031+
}, 1);
50405032

5041-
public static Long getLong(String nm) {
5042-
public static Long getLong(String nm, long val) {
5043-
public static Long getLong(String nm, Long val) {
5044-
public static long divideUnsigned(long dividend, long divisor) {
5045-
public static long remainderUnsigned(long dividend, long divisor) {
5046-
public static long highestOneBit(long i) {
5047-
public static long lowestOneBit(long i) {
5048-
public static int numberOfLeadingZeros(long i) {
5049-
public static int numberOfTrailingZeros(long i) {
5050-
public static int bitCount(long i) {
5051-
public static long rotateLeft(long i, int distance) {
5052-
public static long rotateRight(long i, int distance) {
5053-
public static long reverse(long i) {
5054-
public static long reverseBytes(long i) {
5055-
public static long max(long a, long b) {
5056-
public static long min(long a, long b) {
5057-
public static String toUnsignedString(long i, int radix) {
5058-
public static String toUnsignedString(long i) {
5059-
public static long parseUnsignedLong(String s, int radix)
5060-
public static long parseUnsignedLong(String s) throws NumberFormatException {
5033+
Clazz.newMeth(C$, 'getLong$S$J', function (nm, val) {
5034+
var result=C$.getLong$S$Long(nm, null);
5035+
return (Long.$eq(result,null )) ? C$.valueOf$J(val) : result;
5036+
}, 1);
50615037

5062-
*/
5038+
Clazz.newMeth(C$, 'getLong$S$Long', function (nm, val) {
5039+
var v=null;
5040+
try {
5041+
v=System.getProperty$S(nm);
5042+
} catch (e) {
5043+
if (Clazz.exceptionOf(e,"IllegalArgumentException") || Clazz.exceptionOf(e,"NullPointerException")){
5044+
} else {
5045+
throw e;
5046+
}
5047+
}
5048+
if (v != null ) {
5049+
try {
5050+
return C$.decode$S(v);
5051+
} catch (e) {
5052+
if (Clazz.exceptionOf(e,"NumberFormatException")){
5053+
} else {
5054+
throw e;
5055+
}
5056+
}
5057+
}return val;
5058+
}, 1);
5059+
5060+
Clazz.newMeth(C$, 'parseUnsignedLong$S', function (s) {
5061+
return C$.parseUnsignedLong$S$I(s, 10);
5062+
}, 1);
5063+
5064+
Clazz.newMeth(C$, 'parseUnsignedLong$S$I', function (s, radix) {
5065+
if (s == null ) {
5066+
throw Clazz.new_(Clazz.load('NumberFormatException').c$$S,["null"]);
5067+
}var len=s.length$();
5068+
if (len > 0) {
5069+
var firstChar=s.charAt$I(0);
5070+
if (firstChar == "-") {
5071+
throw Clazz.new_(Clazz.load('NumberFormatException').c$$S,[String.format$S$OA("Illegal leading minus sign on unsigned string %s.", Clazz.array(java.lang.Object, -1, [s]))]);
5072+
} else {
5073+
if (len <= 12 || (radix == 10 && len <= 18 ) ) {
5074+
return C$.parseLong$S$I(s, radix);
5075+
}var first=C$.parseLong$S$I(s.substring$I$I(0, len - 1), radix);
5076+
var second=Character.digit$C$I(s.charAt$I(len - 1), radix);
5077+
if (second < 0) {
5078+
throw Clazz.new_(Clazz.load('NumberFormatException').c$$S,["Bad digit at end of " + s]);
5079+
}var result=Long.$add(Long.$mul(first,radix),second);
5080+
if (C$.compareUnsigned$J$J(result, first) < 0) {
5081+
throw Clazz.new_(Clazz.load('NumberFormatException').c$$S,[String.format$S$OA("String value %s exceeds range of unsigned long.", Clazz.array(java.lang.Object, -1, [s]))]);
5082+
}return result;
5083+
}} else {
5084+
throw Clazz.load('NumberFormatException').forInputString$S(s);
5085+
}}, 1);
5086+
5087+
5088+
})(Long);
5089+
50635090
Long.toUnsignedString$J=Long.toUnsignedString$J$I = function(i,r) {
50645091
if (i >= 0)
50655092
return Long.toString$J$I(i,r || 10);

0 commit comments

Comments
 (0)