Skip to content

Commit 68647cf

Browse files
hansonrhansonr
authored andcommitted
Boolean boxing fix
1 parent ad1552b commit 68647cf

File tree

8 files changed

+96
-21
lines changed

8 files changed

+96
-21
lines changed
337 Bytes
Binary file not shown.
337 Bytes
Binary file not shown.

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

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,7 @@ public boolean visit(Assignment node) {
31893189
IVariableBinding toBinding = getLeftVariableBinding(left, leftTypeBinding);
31903190
String op = node.getOperator().toString();
31913191
String opType = (op.length() == 1 ? null : op.substring(0, op.length() - 1));
3192+
31923193
boolean needNewStaticParenthesis = false;
31933194
boolean isParenthesized = (right instanceof ParenthesizedExpression);
31943195
// boolean haveDocRight = (getJ2sJavadoc(right, DOC_CHECK_ONLY) != null);
@@ -3206,12 +3207,14 @@ public boolean visit(Assignment node) {
32063207
if ("boolean".equals(leftName) && "boolean".equals(rightName)) {
32073208
if (("^=".equals(op))) {
32083209
opType = "!=";
3209-
} else {
3210+
} else if (opType == null) {
32103211
// all boolean should be OK -- no automatic here
32113212
left.accept(this);
3212-
buffer.append((opType == null ? "=" : op));
3213+
buffer.append("=");
32133214
right.accept(this);
32143215
leftName = null;
3216+
} else {
3217+
// a&=b -> !!(a&(b)) because both must execute
32153218
}
32163219
} else if (opType == null) {
32173220
// = operator is no problem
@@ -3234,11 +3237,13 @@ public boolean visit(Assignment node) {
32343237
else
32353238
left.accept(this);
32363239
int ptArray2 = (temp_processingArrayIndex ? buffer.length() : -1);
3240+
boolean leftIsString = leftName.equals("String");
3241+
boolean mustBoxAll = !(leftIsString || leftTypeBinding != null && leftTypeBinding.isPrimitive());
32373242
if (!"char".equals(leftName)) {
3238-
if (isIntegerType(leftName) || "boolean".equals(leftName)) {
3243+
if (isIntegerType(leftName) || "boolean".equals(leftName) || mustBoxAll) {
32393244
// can't just use a |= b because that ends up as 1 or 0, not true or false.
32403245
// byte|short|int|long += ...
3241-
if (!addPrimitiveTypedExpression(left, toBinding, leftName, opType, right, rightName, null, true))
3246+
if (!addPrimitiveTypedExpression(left, toBinding, leftName, opType, right, rightName, null, true, mustBoxAll ? leftTypeBinding : null))
32423247
ptArray = -1;
32433248
} else {
32443249
ptArray = -1;
@@ -3249,7 +3254,6 @@ public boolean visit(Assignment node) {
32493254
buffer.append(' ');
32503255
buffer.append(op);
32513256
buffer.append(' ');
3252-
boolean leftIsString = leftName.equals("String");
32533257
if ("char".equals(rightName)) {
32543258
if (right instanceof CharacterLiteral) {
32553259
// ... = 'c'
@@ -3443,7 +3447,7 @@ public boolean visit(CastExpression node) {
34433447
String nameFROM = expBinding.getName();
34443448
String nameTO = ((PrimitiveType) typeTO).getPrimitiveTypeCode().toString();
34453449
if (!nameTO.equals(nameFROM)) {
3446-
addPrimitiveTypedExpression(null, null, nameTO, null, expression, nameFROM, null, false);
3450+
addPrimitiveTypedExpression(null, null, nameTO, null, expression, nameFROM, null, false, null);
34473451
return false;
34483452
}
34493453
}
@@ -3600,7 +3604,7 @@ public boolean visit(InfixExpression node) {
36003604
if ("/".equals(operator) && leftIsInt && rightIsInt) {
36013605
// left and right are one of byte, short, int, or long
36023606
// division must take care of this.
3603-
addPrimitiveTypedExpression(left, null, leftName, operator, right, rightName, extendedOperands, false);
3607+
addPrimitiveTypedExpression(left, null, leftName, operator, right, rightName, extendedOperands, false, null);
36043608
return false;
36053609
}
36063610

@@ -4329,7 +4333,7 @@ private void addExpressionAsTargetType(Expression exp, Object targetType, String
43294333
if ((isNumeric || paramName.equals("char")) && !isBoxTyped(exp)) {
43304334
// using operator "m" to limit int application of $i$
43314335

4332-
addPrimitiveTypedExpression(null, null, paramName, op, exp, rightName, extendedOperands, false);
4336+
addPrimitiveTypedExpression(null, null, paramName, op, exp, rightName, extendedOperands, false, null);
43334337
} else {
43344338
// char f() { return Character }
43354339
// Character f() { return char }
@@ -4575,11 +4579,11 @@ private void addOperand(Expression exp, boolean isToString) {
45754579
* @param rightName
45764580
* @param extendedOperands
45774581
* @param isAssignment (+=, &=, etc)
4578-
* @param return true if is an assignment and a = (a op b) was
4579-
* used
4582+
* @param mustBoxAll Integer != b
4583+
* @return true if is an assignment and a = (a op b) was used
45804584
*/
45814585
private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding assignmentBinding, String leftName,
4582-
String op, Expression right, String rightName, List<?> extendedOperands, boolean isAssignment) {
4586+
String op, Expression right, String rightName, List<?> extendedOperands, boolean isAssignment, ITypeBinding allBinding) {
45834587
// byte|short|int|long /= ...
45844588
// convert to proper number of bits
45854589

@@ -4589,6 +4593,9 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
45894593

45904594
// a = ($b$[0] = a | right, $b$[0])
45914595

4596+
4597+
4598+
// also boolean |= boolean op will be !!|
45924599
String classIntArray = null;
45934600
String more = null, less = null;
45944601

@@ -4598,7 +4605,16 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
45984605
boolean addParens = (op != "r" || fromChar || right instanceof ParenthesizedExpression);
45994606
boolean isDiv = "/".equals(op);
46004607
boolean toChar = false;
4608+
boolean isBoolean = false;
46014609
switch (leftName) {
4610+
case "Boolean":
4611+
isBoolean = true;
4612+
break;
4613+
case "boolean":
4614+
less = "!!(";
4615+
more = ")";
4616+
addParens = true;
4617+
break;
46024618
case "char":
46034619
if (!fromChar) {
46044620
prefix += "String.fromCharCode(";
@@ -4608,7 +4624,7 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46084624
toChar = true;
46094625
break;
46104626
default:
4611-
// double, float
4627+
// double, float, boxed
46124628
break;
46134629
case "long":
46144630
if (isDiv || !fromIntType) {
@@ -4655,6 +4671,8 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46554671
}
46564672
boolean wasArray = temp_processingArrayIndex;
46574673

4674+
4675+
46584676
if (isAssignment && left == null) {
46594677
buffer.append(op);
46604678
}
@@ -4670,11 +4688,23 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46704688
buffer.append("(");
46714689
}
46724690
if (left != null) {
4691+
46734692
// a += b
4693+
4694+
4695+
if (allBinding != null) {
4696+
// Boolean.from(!!(a | (b))
4697+
buffer.append(leftName + ".valueOf$" + getJSTypeCode(leftName.toLowerCase()))
4698+
.append(isBoolean ? "(!!(" : "(");
4699+
}
4700+
46744701
addFieldName(left, assignmentBinding);
46754702
buffer.append(op);
4676-
if (isAssignment)
4703+
4704+
if (isAssignment) {
46774705
buffer.append("(");
4706+
}
4707+
46784708
}
46794709
if (!appendBoxingNode(right, fromChar) && fromChar && !toChar) {
46804710
buffer.append(CHARCODEAT0);
@@ -4685,6 +4715,10 @@ private boolean addPrimitiveTypedExpression(Expression left, IVariableBinding as
46854715
if (left != null && isAssignment) {
46864716
buffer.append(")");
46874717
}
4718+
4719+
if (allBinding != null)
4720+
buffer.append(isBoolean ? "))" : ")");
4721+
46884722
if (classIntArray != null) {
46894723
// this is necessary because in JavaScript,
46904724
// a = new Int8Array(1)
@@ -5719,13 +5753,15 @@ private String getJSTypeCode(String className) {
57195753
return "Z";
57205754
case "byte":
57215755
return "B";
5756+
case "character":
57225757
case "char":
57235758
return "C";
57245759
case "double":
57255760
return "D";
57265761
case "float":
57275762
return "F";
5728-
case "int":
5763+
case "integer":
5764+
case "int":
57295765
return "I";
57305766
case "long":
57315767
return "J";

sources/net.sf.j2s.java.core/resources/WASM/README.TXT

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
The ES6 directory can contain JavaScript ES6 module code that is not to be compressed using GCC.
1+
The /_ES6 directory can contain JavaScript ES6 module code that is not to be compressed using GCC.
22

33
For example, molfile_to_inchi.wasm and wasi.esm.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The /_WASM directory can contain Web Assembly code.

sources/net.sf.j2s.java.core/resources/WASM/wasi.esm.js renamed to sources/net.sf.j2s.java.core/resources/_WASM/wasi.esm.js

File renamed without changes.

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,54 @@ class Test_Boolean extends Test_ {
77
public static int i_;
88

99
public static void main(String[] args) {
10+
11+
12+
// test that b1 &= b2 is b1 = !!(b1 & b2), not just b1 && b2:
13+
boolean a = false;
14+
int c = 2;
15+
a &= (++c > 1);
16+
assert(a == false && c == 3);
1017

18+
// test unboxing right
19+
a &= Boolean.TRUE; // a=!!(a&((Boolean.TRUE).valueOf()));
20+
assert(a == false);
21+
a |= Boolean.TRUE;
22+
assert(a == true);
23+
a = false;
24+
a |= Boolean.FALSE;
25+
assert(a == false);
26+
27+
// test unboxing left Boolean then boxing Boolean
1128
Boolean b = Boolean.FALSE;
29+
b |= true; // b=Boolean.valueOf$Z(!!((b).valueOf()|(true)));
30+
assert(b == Boolean.TRUE);
31+
32+
// test unboxing left Integer then boxing Integer
33+
Integer ai = Integer.valueOf(33);
34+
ai |= 23; // ai=Integer.valueOf$I((ai).valueOf()|(23));
35+
assert (ai == 55);
36+
37+
// test unboxing left Integer and right Short, then boxing Integer
38+
Short si = Short.valueOf((short)22);
39+
ai = Integer.valueOf(33);
40+
ai |= si; // ai=Integer.valueOf$I((ai).valueOf()|((si).valueOf()));
41+
assert (ai == 55);
42+
43+
// test Boolean.FALSE is not new Boolean(false);
44+
b = Boolean.FALSE;
1245
Boolean b1 = new Boolean(false);
13-
System.out.println(Integer.valueOf(3));
1446
assert(new Boolean(false).equals(Boolean.FALSE));
1547
assert(new Boolean(false) != Boolean.FALSE);
1648
boolean boo = false;
1749
assert(new Boolean(boo) != Boolean.FALSE);
1850
assert(testBool(false) == Boolean.FALSE);
19-
assert(b ? false : true);
20-
assert(b1 ? false : true);
51+
52+
// test unboxing right Boolean
53+
assert(b == false);
54+
assert(b1 == false);
2155
assert(Boolean.FALSE ? false : true);
56+
57+
// test ! Boolean
2258
System.out.println("" + (new Boolean("true")) + (!new Boolean("false")));
2359
assert(new Boolean("true"));
2460
assert(!new Boolean("false"));
@@ -40,21 +76,24 @@ public static void main(String[] args) {
4076
assert(Boolean.TRUE.compareTo(false) == 1);
4177
assert(Boolean.FALSE.compareTo(true) == -1);
4278

79+
// test system properties getBoolean
4380
assert(!Boolean.getBoolean("user.home"));
4481
System.setProperty("mytest", "true");
4582
assert(Boolean.getBoolean("mytest"));
4683

84+
// test hashcode same for Boolean.TRUE and just true
4785
assert(Boolean.TRUE.hashCode() == Boolean.hashCode(true) );
4886
assert(Boolean.FALSE.hashCode() == Boolean.hashCode(false));
4987

88+
// test string values
5089
assert(Boolean.TRUE.toString() == "true");
5190
assert(Boolean.FALSE.toString() == "false");
52-
5391
assert(Boolean.valueOf("true"));
5492
assert(Boolean.valueOf("TRUE"));
5593
assert(!Boolean.valueOf("false"));
5694
assert(!Boolean.valueOf("FALSE"));
57-
95+
96+
// Boolean.valueOf
5897
assert(Boolean.valueOf(true));
5998
assert(!Boolean.valueOf(false));
6099

0 commit comments

Comments
 (0)