Skip to content

Commit

Permalink
✨ feat: Support bool && bool, a && bool
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 14, 2024
1 parent 503e1f2 commit f98b0e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,32 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
final int opcodeCompareFalse = bangFlipped ? Opcodes.IFNE : Opcodes.IFEQ;
switch (resolvedOp) {
case LogicalAnd: {
if (isLeftBool) {
if (isLeftBool && isRightBool) {
Ts2JavaAstBool leftBool = left.as(Ts2JavaAstBool.class);
Ts2JavaAstBool rightBool = right.as(Ts2JavaAstBool.class);
if (!rightBool.isValue()) {
sizes.add(rightBool.apply(methodVisitor, context));
} else {
sizes.add(leftBool.apply(methodVisitor, context));
}
ignoreClose = true;
break;
} else if (isLeftBool) {
Ts2JavaAstBool leftBool = left.as(Ts2JavaAstBool.class);
if (!leftBool.isValue()) {
sizes.add(leftBool.apply(methodVisitor, context));
ignoreClose = true;
break;
}
} else {
} else if (isRightBool) {
Ts2JavaAstBool rightBool = right.as(Ts2JavaAstBool.class);
if (!rightBool.isValue()) {
sizes.add(rightBool.apply(methodVisitor, context));
ignoreClose = true;
break;
}
}
if (!isLeftBool) {
sizes.add(left.apply(methodVisitor, context));
if (!isLeftLogical) {
methodVisitor.visitJumpInsn(opcodeCompareFalse, labelFalse);
Expand All @@ -69,9 +87,11 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
if (labelSwitched && isRightLogical) {
right.as(Ts2JavaAstBinExprLogical.class).setLabelSwitched(true);
}
sizes.add(right.apply(methodVisitor, context));
if (!isRightLogical) {
methodVisitor.visitJumpInsn(opcodeCompareFalse, labelSwitched ? labelTrue : labelFalse);
if (!isRightBool) {
sizes.add(right.apply(methodVisitor, context));
if (!isRightLogical) {
methodVisitor.visitJumpInsn(opcodeCompareFalse, labelSwitched ? labelTrue : labelFalse);
}
}
if (labelSwitched && isLeftLogical) {
Ts2JavaAstBinExprLogical leftLogical = left.as(Ts2JavaAstBinExprLogical.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ public void testAndAndAnd_ZZZ_Z() throws Exception {
assertFalse((boolean) tsClass.invoke(true, false, true));
}

@Test
public void testAndBool_Z_Z() throws Exception {
tsClass = new TsClass(
"return a && false;",
boolean.class,
TsMethodArgument.of("a", boolean.class));
assertFalse((boolean) tsClass.invoke(true));
assertFalse((boolean) tsClass.invoke(false));
tsClass = new TsClass(
"return a && true;",
boolean.class,
TsMethodArgument.of("a", boolean.class));
assertTrue((boolean) tsClass.invoke(true));
assertFalse((boolean) tsClass.invoke(false));
}

@Test
public void testAndOrAnd_II_Z() throws Exception {
assertTrue(andOrAnd_II_Z(2, 2));
Expand Down Expand Up @@ -500,6 +516,18 @@ public void testAnd_ZZ_Z() throws Exception {
assertFalse((boolean) tsClass.invoke(false, false));
}

@Test
public void testBoolAndBool_Z_Z() throws Exception {
tsClass = new TsClass("return true && true;");
assertTrue((boolean) tsClass.invoke());
tsClass = new TsClass("return false && true;");
assertFalse((boolean) tsClass.invoke());
tsClass = new TsClass("return true && false;");
assertFalse((boolean) tsClass.invoke());
tsClass = new TsClass("return false && false;");
assertFalse((boolean) tsClass.invoke());
}

@Test
public void testBoolAnd_Z_Z() throws Exception {
tsClass = new TsClass(
Expand Down

0 comments on commit f98b0e2

Please sign in to comment.