Skip to content

Commit efe4a37

Browse files
hansonrhansonr
authored andcommitted
SwingJS-site.zip and transpiler for long l = (long) double
- sets upper/lower limit for long values to 53 bits (+/-1FFFFFFFFFFFFF) - fixes issues with number parsing involving limits and "0x"
1 parent be7bd70 commit efe4a37

File tree

20 files changed

+346
-103
lines changed

20 files changed

+346
-103
lines changed
Binary file not shown.

sources/net.sf.j2s.core/dist/swingjs/differences.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ever be shadowed or overridden by subclasses. For example, we see in java.lang.T
3131
----------------------------------
3232

3333

34+
updated 12/6/2020 -- note about restrictions on long, including BitSet
3435
updated 3/21/2020 -- adds note about HashMap, Hashtable, and HashSet iterator ordering
3536
updated 3/20/2020 -- adds note about interning, new String("xxx"), and "xxx"
3637
updated 2/26/2020 -- adds Graphics.setClip issue
@@ -480,6 +481,7 @@ MINOR ISSUES--requiring some rewriting/refactoring outside of SwingJS
480481

481482
See below for a full discussion.
482483

484+
Restrictions on long and BitSet
483485
HashMap, Hashtable, and HashSet iterator ordering
484486
interning, new String("xxx") vs "xxx"
485487
Names with "$" and "_"
@@ -586,6 +588,42 @@ changed to JSToolkit.isDispatchThread()
586588
MINOR ISSUES--requiring some rewriting/refactoring outside of SwingJS
587589
=====================================================================
588590

591+
restrictions on long and BitSet
592+
-------------------------------
593+
594+
Java's 64-bit long type is not supported in JavaScript. There is no Int64Array in JavaScript,
595+
and 0x20000000000000 + 1 evaluates to 0x20000000000000, not 0x20000000000001.
596+
(Likewise, -0x20000000000000 - 1 is left unchanged.)
597+
598+
The largest "integer" value in JavaScript is 9007199254740991 (9.007199254740991E13, or 0x1FFFFFFFFFFFFFF).
599+
Effectively, you get to use only 53 bits of the long, not 64. Trying to set a long larger than
600+
0x1FFFFFFFFFFFFFF or smaller than -0x1FFFFFFFFFFFFFF will result in a NumberFormatException.
601+
602+
The transpiler handles conversion to long the same as Java for all cases other than from double.
603+
604+
For small double values, there is no problem, and, in fact, this is a known trick used to round
605+
doubles and floats toward zero:
606+
607+
double d;
608+
d = (long) 3.8;
609+
assert(d == 3);
610+
d = (long) -3.8;
611+
assert(d == -3);
612+
613+
SwingJS will evaluate (long) d as 0 for d > 9007199254740991
614+
or d < -9007199254740991, same as Java returns for Double.NaN.
615+
So, in Java we have:
616+
617+
assert(((long) Double.NaN) == 0);
618+
assert(((int) Double.NaN) == 0);
619+
assert(((long) Float.NaN) == 0);
620+
assert(((int) Float.NaN) == 0);
621+
622+
and also, in JavaScript only, we also have:
623+
624+
double d = 0x2000000000000L;
625+
assert(((long) d) == 0);
626+
589627
HashMap, Hashtable, and HashSet iterator ordering
590628
-------------------------------------------------
591629

Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20201205191847
1+
20201206104534
Binary file not shown.

sources/net.sf.j2s.core/dist/swingjs/ver/3.2.9/differences.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ever be shadowed or overridden by subclasses. For example, we see in java.lang.T
3131
----------------------------------
3232

3333

34+
updated 12/6/2020 -- note about restrictions on long, including BitSet
3435
updated 3/21/2020 -- adds note about HashMap, Hashtable, and HashSet iterator ordering
3536
updated 3/20/2020 -- adds note about interning, new String("xxx"), and "xxx"
3637
updated 2/26/2020 -- adds Graphics.setClip issue
@@ -480,6 +481,7 @@ MINOR ISSUES--requiring some rewriting/refactoring outside of SwingJS
480481

481482
See below for a full discussion.
482483

484+
Restrictions on long and BitSet
483485
HashMap, Hashtable, and HashSet iterator ordering
484486
interning, new String("xxx") vs "xxx"
485487
Names with "$" and "_"
@@ -586,6 +588,42 @@ changed to JSToolkit.isDispatchThread()
586588
MINOR ISSUES--requiring some rewriting/refactoring outside of SwingJS
587589
=====================================================================
588590

591+
restrictions on long and BitSet
592+
-------------------------------
593+
594+
Java's 64-bit long type is not supported in JavaScript. There is no Int64Array in JavaScript,
595+
and 0x20000000000000 + 1 evaluates to 0x20000000000000, not 0x20000000000001.
596+
(Likewise, -0x20000000000000 - 1 is left unchanged.)
597+
598+
The largest "integer" value in JavaScript is 9007199254740991 (9.007199254740991E13, or 0x1FFFFFFFFFFFFFF).
599+
Effectively, you get to use only 53 bits of the long, not 64. Trying to set a long larger than
600+
0x1FFFFFFFFFFFFFF or smaller than -0x1FFFFFFFFFFFFFF will result in a NumberFormatException.
601+
602+
The transpiler handles conversion to long the same as Java for all cases other than from double.
603+
604+
For small double values, there is no problem, and, in fact, this is a known trick used to round
605+
doubles and floats toward zero:
606+
607+
double d;
608+
d = (long) 3.8;
609+
assert(d == 3);
610+
d = (long) -3.8;
611+
assert(d == -3);
612+
613+
SwingJS will evaluate (long) d as 0 for d > 9007199254740991
614+
or d < -9007199254740991, same as Java returns for Double.NaN.
615+
So, in Java we have:
616+
617+
assert(((long) Double.NaN) == 0);
618+
assert(((int) Double.NaN) == 0);
619+
assert(((long) Float.NaN) == 0);
620+
assert(((int) Float.NaN) == 0);
621+
622+
and also, in JavaScript only, we also have:
623+
624+
double d = 0x2000000000000L;
625+
assert(((long) d) == 0);
626+
589627
HashMap, Hashtable, and HashSet iterator ordering
590628
-------------------------------------------------
591629

Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20201205191847
1+
20201206104534

sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class CorePlugin extends Plugin {
3030
// if you change the x.x.x number, be sure to also indicate that in
3131
// j2sApplet.js and also (Bob only) update.bat, update-clean.bat
3232

33-
33+
// BH 2020.12.06 -- 3.2.9-v1r fix for (long) double using |0
3434
// BH 2020.11.20 -- 3.2.9-v1q fix for new ImmutableCollections.ListN<>(E...) should use Object[]
3535
// BH 2020.08.03 -- 3.2.9-v1p fix for boxing boolean should be Boolean.valueOf$, not new Boolean
3636
// BH 2020.08.01 -- 3.2.9-v1o fix for lambda expressions too static

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
// TODO: superclass inheritance for JAXB XmlAccessorType
137137

138+
//BH 2020.12.06 -- 3.2.9-v1r fix for (long) double using |0
138139
//BH 2020.11.20 -- 3.2.9-v1q fix for new ImmutableCollections.ListN<>(E...) should use Object[]
139140
//BH 2020.08.03 -- 3.2.9-v1p fix for boxing boolean should be Boolean.valueOf$, not new Boolean
140141
//BH 2020.08.01 -- 3.2.9-v1o fix for lambda expressions too static
@@ -4589,7 +4590,7 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
45894590
// a = ($b$[0] = a | right, $b$[0])
45904591

45914592
String classIntArray = null;
4592-
String more = null;
4593+
String more = null, less = null;
45934594

45944595
String prefix = (isAssignment ? "=" : "");
45954596
boolean fromChar = ("char".equals(rightName));
@@ -4611,7 +4612,8 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46114612
break;
46124613
case "long":
46134614
if (isDiv || !fromIntType) {
4614-
more = "|0)";
4615+
less = "Clazz.toLong(";
4616+
more = ")";
46154617
addParens = true;
46164618
} else {
46174619
left = null;
@@ -4662,6 +4664,8 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46624664
buffer.append("(");
46634665
buffer.append(classIntArray).append(" = ");
46644666
temp_processingArrayIndex = true;
4667+
} else if (less != null) {
4668+
buffer.append(less);
46654669
} else if (more == "|0)") {
46664670
buffer.append("(");
46674671
}

0 commit comments

Comments
 (0)