Skip to content

Commit

Permalink
✨ feat: Add EQ, EQEQ, NotEQ, NotEQEQ for long
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 1, 2024
1 parent 2610ddc commit 8c61bb2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
20 changes: 20 additions & 0 deletions scripts/ts/test/test.logical.operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class Test {
return a === b;
}

public logicalEQ_IL_Z(a: int, b: long): boolean {
return a == b;
}

public logicalEQEQ_IL_Z(a: int, b: long): boolean {
return a === b;
}

public logicalGE_II_Z(a: int, b: int): boolean {
return a >= b;
}
Expand Down Expand Up @@ -49,10 +57,20 @@ class Test {
public logicalNotEQ_II_Z(a: int, b: int): boolean {
return a != b;
}

public logicalNotEQEQ_IL_Z(a: int, b: long): boolean {
return a !== b;
}

public logicalNotEQ_IL_Z(a: int, b: long): boolean {
return a != b;
}
}

console.log(new Test().logicalEQ_II_Z(1, 2));
console.log(new Test().logicalEQEQ_II_Z(1, 2));
console.log(new Test().logicalEQ_IL_Z(1, 2));
console.log(new Test().logicalEQEQ_IL_Z(1, 2));
console.log(new Test().logicalGE_II_Z(1, 2));
console.log(new Test().logicalGE_IL_Z(1, 2));
console.log(new Test().logicalGT_II_Z(1, 2));
Expand All @@ -63,3 +81,5 @@ console.log(new Test().logicalLT_II_Z(1, 2));
console.log(new Test().logicalLT_IL_Z(1, 2));
console.log(new Test().logicalNotEQEQ_II_Z(1, 2));
console.log(new Test().logicalNotEQ_II_Z(1, 2));
console.log(new Test().logicalNotEQEQ_IL_Z(1, 2));
console.log(new Test().logicalNotEQ_IL_Z(1, 2));
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ private TypeDescription manipulateExpression(JavaFunctionContext functionContext
switch (expression.getType()) {
case BinExpr:
return new Ts2JavaAstBinExpr().manipulate(functionContext, expression.as(Swc4jAstBinExpr.class));
case Ident: {
case Ident:
return new Ts2JavaAstIdent().manipulate(functionContext, expression.as(Swc4jAstIdent.class));
}
default:
throw new Ts2JavaAstException(
expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
break;
case EqEq:
case EqEqEq:
opcodeCompare = Opcodes.IFNE;
break;
case NotEq:
case NotEqEq:
opcodeCompare = Opcodes.IFEQ;
break;
default:
throw new Ts2JavaException(
SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ public void testLogicalEQEQ_II_Z() throws Exception {
assertTrue((boolean) method.invoke(object, 1, 1));
}

@Test
public void testLogicalEQEQ_IL_Z() throws Exception {
Method method = clazz.getMethod("logicalEQEQ_IL_Z", int.class, long.class);
assertNotNull(method);
assertEquals(boolean.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertFalse((boolean) method.invoke(object, 1, 2L));
assertFalse((boolean) method.invoke(object, 2, 1L));
assertTrue((boolean) method.invoke(object, 1, 1L));
}

@Test
public void testLogicalEQ_II_Z() throws Exception {
assertFalse(logicalEQ_II_Z(1, 2));
Expand All @@ -171,6 +185,20 @@ public void testLogicalEQ_II_Z() throws Exception {
assertTrue((boolean) method.invoke(object, 1, 1));
}

@Test
public void testLogicalEQ_IL_Z() throws Exception {
Method method = clazz.getMethod("logicalEQ_IL_Z", int.class, long.class);
assertNotNull(method);
assertEquals(boolean.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertFalse((boolean) method.invoke(object, 1, 2L));
assertFalse((boolean) method.invoke(object, 2, 1L));
assertTrue((boolean) method.invoke(object, 1, 1L));
}

@Test
public void testLogicalGE_II_Z() throws Exception {
Method method = clazz.getMethod("logicalGE_II_Z", int.class, int.class);
Expand Down Expand Up @@ -310,4 +338,32 @@ public void testLogicalNotEQ_II_Z() throws Exception {
assertTrue((boolean) method.invoke(object, 2, 1));
assertFalse((boolean) method.invoke(object, 1, 1));
}

@Test
public void testLogicalNotEQEQ_IL_Z() throws Exception {
Method method = clazz.getMethod("logicalNotEQEQ_IL_Z", int.class, long.class);
assertNotNull(method);
assertEquals(boolean.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertTrue((boolean) method.invoke(object, 1, 2L));
assertTrue((boolean) method.invoke(object, 2, 1L));
assertFalse((boolean) method.invoke(object, 1, 1L));
}

@Test
public void testLogicalNotEQ_IL_Z() throws Exception {
Method method = clazz.getMethod("logicalNotEQ_IL_Z", int.class, long.class);
assertNotNull(method);
assertEquals(boolean.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertTrue((boolean) method.invoke(object, 1, 2L));
assertTrue((boolean) method.invoke(object, 2, 1L));
assertFalse((boolean) method.invoke(object, 1, 1L));
}
}

0 comments on commit 8c61bb2

Please sign in to comment.