diff --git a/scripts/ts/test/test.logical.operations.ts b/scripts/ts/test/test.logical.operations.ts index 5e5291b..eb1fd8b 100644 --- a/scripts/ts/test/test.logical.operations.ts +++ b/scripts/ts/test/test.logical.operations.ts @@ -1,6 +1,10 @@ import { type float, double, int, long } from "./test.type.aliases.ts"; class Test { + public logicalEQEQ_DD_Z(a: double, b: double): boolean { + return a === b; + } + public logicalEQEQ_FF_Z(a: float, b: float): boolean { return a === b; } @@ -13,6 +17,10 @@ class Test { return a === b; } + public logicalEQ_DD_Z(a: double, b: double): boolean { + return a == b; + } + public logicalEQ_FF_Z(a: float, b: float): boolean { return a == b; } @@ -25,6 +33,10 @@ class Test { return a == b; } + public logicalGE_DD_Z(a: double, b: double): boolean { + return a >= b; + } + public logicalGE_FF_Z(a: float, b: float): boolean { return a >= b; } @@ -38,6 +50,10 @@ class Test { return c; } + public logicalGT_DD_Z(a: double, b: double): boolean { + return a > b; + } + public logicalGT_FF_Z(a: float, b: float): boolean { return a > b; } @@ -50,6 +66,10 @@ class Test { return a > b; } + public logicalLE_DD_Z(a: double, b: double): boolean { + return a <= b; + } + public logicalLE_FF_Z(a: float, b: float): boolean { return a <= b; } @@ -62,6 +82,10 @@ class Test { return a <= b; } + public logicalLT_DD_Z(a: double, b: double): boolean { + return a < b; + } + public logicalLT_FF_Z(a: float, b: float): boolean { return a < b; } @@ -74,6 +98,10 @@ class Test { return a < b; } + public logicalNotEQEQ_DD_Z(a: double, b: double): boolean { + return a !== b; + } + public logicalNotEQEQ_FF_Z(a: float, b: float): boolean { return a !== b; } @@ -82,16 +110,20 @@ class Test { return a !== b; } - public logicalNotEQ_FF_Z(a: float, b: float): boolean { + public logicalNotEQEQ_IL_Z(a: int, b: long): boolean { + return a !== b; + } + + public logicalNotEQ_DD_Z(a: double, b: double): boolean { return a != b; } - public logicalNotEQ_II_Z(a: int, b: int): boolean { + public logicalNotEQ_FF_Z(a: float, b: float): boolean { return a != b; } - public logicalNotEQEQ_IL_Z(a: int, b: long): boolean { - return a !== b; + public logicalNotEQ_II_Z(a: int, b: int): boolean { + return a != b; } public logicalNotEQ_IL_Z(a: int, b: long): boolean { @@ -99,27 +131,35 @@ class Test { } } +console.log(new Test().logicalEQEQ_DD_Z(1, 2)); console.log(new Test().logicalEQEQ_FF_Z(1, 2)); console.log(new Test().logicalEQEQ_II_Z(1, 2)); console.log(new Test().logicalEQEQ_IL_Z(1, 2)); +console.log(new Test().logicalEQ_DD_Z(1, 2)); console.log(new Test().logicalEQ_FF_Z(1, 2)); console.log(new Test().logicalEQ_II_Z(1, 2)); console.log(new Test().logicalEQ_IL_Z(1, 2)); +console.log(new Test().logicalGE_DD_Z(1, 2)); console.log(new Test().logicalGE_FF_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_DD_Z(1, 2)); console.log(new Test().logicalGT_FF_Z(1, 2)); console.log(new Test().logicalGT_II_Z(1, 2)); console.log(new Test().logicalGT_IL_Z(1, 2)); +console.log(new Test().logicalLE_DD_Z(1, 2)); console.log(new Test().logicalLE_FF_Z(1, 2)); console.log(new Test().logicalLE_II_Z(1, 2)); console.log(new Test().logicalLE_IL_Z(1, 2)); +console.log(new Test().logicalLT_DD_Z(1, 2)); console.log(new Test().logicalLT_FF_Z(1, 2)); console.log(new Test().logicalLT_II_Z(1, 2)); console.log(new Test().logicalLT_IL_Z(1, 2)); +console.log(new Test().logicalNotEQEQ_DD_Z(1, 2)); console.log(new Test().logicalNotEQEQ_FF_Z(1, 2)); console.log(new Test().logicalNotEQEQ_II_Z(1, 2)); console.log(new Test().logicalNotEQEQ_IL_Z(1, 2)); +console.log(new Test().logicalNotEQ_DD_Z(1, 2)); console.log(new Test().logicalNotEQ_FF_Z(1, 2)); console.log(new Test().logicalNotEQ_II_Z(1, 2)); console.log(new Test().logicalNotEQ_IL_Z(1, 2)); diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java index cfa31f4..0f59ea9 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstBinaryOp.java @@ -1,268 +1,310 @@ -/* - * Copyright (c) 2024. caoccao.com Sam Cao - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.caoccao.javet.buddy.ts2java.ast; - -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException; -import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; -import com.caoccao.javet.utils.SimpleFreeMarkerFormat; -import com.caoccao.javet.utils.SimpleMap; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.bytecode.*; -import net.bytebuddy.jar.asm.Label; -import net.bytebuddy.jar.asm.MethodVisitor; -import net.bytebuddy.jar.asm.Opcodes; - -import java.util.ArrayList; -import java.util.List; - -public final class Ts2JavaAstBinaryOp { - private Ts2JavaAstBinaryOp() { - } - - public static Addition getAddition(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return Addition.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return Addition.LONG; - } else if (type.isAssignableTo(float.class)) { - return Addition.FLOAT; - } else if (type.isAssignableTo(double.class)) { - return Addition.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in addition.", - SimpleMap.of("type", type.getName()))); - } - - public static Division getDivision(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return Division.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return Division.LONG; - } else if (type.isAssignableTo(float.class)) { - return Division.FLOAT; - } else if (type.isAssignableTo(double.class)) { - return Division.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in division.", - SimpleMap.of("type", type.getName()))); - } - - public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp binaryOp, TypeDescription type) { - Label labelFalse = new Label(); - Label labelTrue = new Label(); - List stackManipulations = new ArrayList<>(); - if (type.isAssignableTo(int.class)) { - int opcodeCompare; - switch (binaryOp) { - case Gt: - opcodeCompare = Opcodes.IF_ICMPLE; - break; - case GtEq: - opcodeCompare = Opcodes.IF_ICMPLT; - break; - case Lt: - opcodeCompare = Opcodes.IF_ICMPGE; - break; - case LtEq: - opcodeCompare = Opcodes.IF_ICMPGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare = Opcodes.IF_ICMPNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare = Opcodes.IF_ICMPEQ; - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); - return new StackManipulation.Size(-1, 0); - })); - } else if (type.isAssignableTo(long.class)) { - int opcodeCompare; - switch (binaryOp) { - case Gt: - opcodeCompare = Opcodes.IFLE; - break; - case GtEq: - opcodeCompare = Opcodes.IFLT; - break; - case Lt: - opcodeCompare = Opcodes.IFGE; - break; - case LtEq: - opcodeCompare = Opcodes.IFGT; - 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.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.LCMP); - methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); - return new StackManipulation.Size(-2, 0); - })); - } else if (type.isAssignableTo(float.class)) { - int opcodeCompare1; - int opcodeCompare2; - switch (binaryOp) { - case Gt: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = Opcodes.IFLE; - break; - case GtEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = Opcodes.IFLT; - break; - case Lt: - opcodeCompare1 = Opcodes.FCMPG; - opcodeCompare2 = Opcodes.IFGE; - break; - case LtEq: - opcodeCompare1 = Opcodes.FCMPG; - opcodeCompare2 = Opcodes.IFGT; - break; - case EqEq: - case EqEqEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = Opcodes.IFNE; - break; - case NotEq: - case NotEqEq: - opcodeCompare1 = Opcodes.FCMPL; - opcodeCompare2 = Opcodes.IFEQ; - break; - default: - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", - SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(opcodeCompare1); - methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); - return new StackManipulation.Size(-1, 0); - })); - } else { - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.", - SimpleMap.of("type", type.getName()))); - } - stackManipulations.add(new StackManipulation.Simple(( - MethodVisitor methodVisitor, - Implementation.Context implementationContext) -> { - methodVisitor.visitInsn(Opcodes.ICONST_1); - methodVisitor.visitJumpInsn(Opcodes.GOTO, labelTrue); - methodVisitor.visitLabel(labelFalse); - methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); - methodVisitor.visitInsn(Opcodes.ICONST_0); - methodVisitor.visitLabel(labelTrue); - methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER}); - return StackManipulation.Size.ZERO; - })); - return new StackManipulation.Compound(stackManipulations); - } - - public static Multiplication getMultiplication(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return Multiplication.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return Multiplication.LONG; - } else if (type.isAssignableTo(float.class)) { - return Multiplication.FLOAT; - } else if (type.isAssignableTo(double.class)) { - return Multiplication.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in multiplication.", - SimpleMap.of("type", type.getName()))); - } - - public static Remainder getRemainder(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return Remainder.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return Remainder.LONG; - } else if (type.isAssignableTo(float.class)) { - return Remainder.FLOAT; - } else if (type.isAssignableTo(double.class)) { - return Remainder.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in mod.", - SimpleMap.of("type", type.getName()))); - } - - public static ShiftLeft getShiftLeft(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return ShiftLeft.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return ShiftLeft.LONG; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in left shift.", - SimpleMap.of("type", type.getName()))); - } - - public static ShiftRight getShiftRight(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return ShiftRight.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return ShiftRight.LONG; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in right shift.", - SimpleMap.of("type", type.getName()))); - } - - public static Subtraction getSubtraction(TypeDescription type) { - if (type.isAssignableTo(int.class)) { - return Subtraction.INTEGER; - } else if (type.isAssignableTo(long.class)) { - return Subtraction.LONG; - } else if (type.isAssignableTo(float.class)) { - return Subtraction.FLOAT; - } else if (type.isAssignableTo(double.class)) { - return Subtraction.DOUBLE; - } - throw new Ts2JavaException( - SimpleFreeMarkerFormat.format("Unsupported type ${type} in subtraction.", - SimpleMap.of("type", type.getName()))); - } -} +/* + * Copyright (c) 2024. caoccao.com Sam Cao + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.caoccao.javet.buddy.ts2java.ast; + +import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException; +import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; +import com.caoccao.javet.utils.SimpleFreeMarkerFormat; +import com.caoccao.javet.utils.SimpleMap; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.implementation.Implementation; +import net.bytebuddy.implementation.bytecode.*; +import net.bytebuddy.jar.asm.Label; +import net.bytebuddy.jar.asm.MethodVisitor; +import net.bytebuddy.jar.asm.Opcodes; + +import java.util.ArrayList; +import java.util.List; + +public final class Ts2JavaAstBinaryOp { + private Ts2JavaAstBinaryOp() { + } + + public static Addition getAddition(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return Addition.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return Addition.LONG; + } else if (type.isAssignableTo(float.class)) { + return Addition.FLOAT; + } else if (type.isAssignableTo(double.class)) { + return Addition.DOUBLE; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in addition.", + SimpleMap.of("type", type.getName()))); + } + + public static Division getDivision(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return Division.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return Division.LONG; + } else if (type.isAssignableTo(float.class)) { + return Division.FLOAT; + } else if (type.isAssignableTo(double.class)) { + return Division.DOUBLE; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in division.", + SimpleMap.of("type", type.getName()))); + } + + public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp binaryOp, TypeDescription type) { + Label labelFalse = new Label(); + Label labelTrue = new Label(); + List stackManipulations = new ArrayList<>(); + if (type.isAssignableTo(int.class)) { + int opcodeCompare; + switch (binaryOp) { + case Gt: + opcodeCompare = Opcodes.IF_ICMPLE; + break; + case GtEq: + opcodeCompare = Opcodes.IF_ICMPLT; + break; + case Lt: + opcodeCompare = Opcodes.IF_ICMPGE; + break; + case LtEq: + opcodeCompare = Opcodes.IF_ICMPGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare = Opcodes.IF_ICMPNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare = Opcodes.IF_ICMPEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); + return new StackManipulation.Size(-1, 0); + })); + } else if (type.isAssignableTo(long.class)) { + int opcodeCompare; + switch (binaryOp) { + case Gt: + opcodeCompare = Opcodes.IFLE; + break; + case GtEq: + opcodeCompare = Opcodes.IFLT; + break; + case Lt: + opcodeCompare = Opcodes.IFGE; + break; + case LtEq: + opcodeCompare = Opcodes.IFGT; + 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.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(Opcodes.LCMP); + methodVisitor.visitJumpInsn(opcodeCompare, labelFalse); + return new StackManipulation.Size(-2, 0); + })); + } else if (type.isAssignableTo(float.class)) { + int opcodeCompare1; + int opcodeCompare2; + switch (binaryOp) { + case Gt: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = Opcodes.IFLE; + break; + case GtEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = Opcodes.IFLT; + break; + case Lt: + opcodeCompare1 = Opcodes.FCMPG; + opcodeCompare2 = Opcodes.IFGE; + break; + case LtEq: + opcodeCompare1 = Opcodes.FCMPG; + opcodeCompare2 = Opcodes.IFGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = Opcodes.IFNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare1 = Opcodes.FCMPL; + opcodeCompare2 = Opcodes.IFEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(opcodeCompare1); + methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); + return new StackManipulation.Size(-1, 0); + })); + } else if (type.isAssignableTo(double.class)) { + int opcodeCompare1; + int opcodeCompare2; + switch (binaryOp) { + case Gt: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = Opcodes.IFLE; + break; + case GtEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = Opcodes.IFLT; + break; + case Lt: + opcodeCompare1 = Opcodes.DCMPG; + opcodeCompare2 = Opcodes.IFGE; + break; + case LtEq: + opcodeCompare1 = Opcodes.DCMPG; + opcodeCompare2 = Opcodes.IFGT; + break; + case EqEq: + case EqEqEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = Opcodes.IFNE; + break; + case NotEq: + case NotEqEq: + opcodeCompare1 = Opcodes.DCMPL; + opcodeCompare2 = Opcodes.IFEQ; + break; + default: + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported binary operation ${binaryOp} for type ${type} in logical operation.", + SimpleMap.of("binaryOp", binaryOp.name(), "type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(opcodeCompare1); + methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse); + return new StackManipulation.Size(-2, 0); + })); + } else { + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in logical operation.", + SimpleMap.of("type", type.getName()))); + } + stackManipulations.add(new StackManipulation.Simple(( + MethodVisitor methodVisitor, + Implementation.Context implementationContext) -> { + methodVisitor.visitInsn(Opcodes.ICONST_1); + methodVisitor.visitJumpInsn(Opcodes.GOTO, labelTrue); + methodVisitor.visitLabel(labelFalse); + methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); + methodVisitor.visitInsn(Opcodes.ICONST_0); + methodVisitor.visitLabel(labelTrue); + methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER}); + return StackManipulation.Size.ZERO; + })); + return new StackManipulation.Compound(stackManipulations); + } + + public static Multiplication getMultiplication(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return Multiplication.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return Multiplication.LONG; + } else if (type.isAssignableTo(float.class)) { + return Multiplication.FLOAT; + } else if (type.isAssignableTo(double.class)) { + return Multiplication.DOUBLE; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in multiplication.", + SimpleMap.of("type", type.getName()))); + } + + public static Remainder getRemainder(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return Remainder.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return Remainder.LONG; + } else if (type.isAssignableTo(float.class)) { + return Remainder.FLOAT; + } else if (type.isAssignableTo(double.class)) { + return Remainder.DOUBLE; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in mod.", + SimpleMap.of("type", type.getName()))); + } + + public static ShiftLeft getShiftLeft(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return ShiftLeft.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return ShiftLeft.LONG; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in left shift.", + SimpleMap.of("type", type.getName()))); + } + + public static ShiftRight getShiftRight(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return ShiftRight.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return ShiftRight.LONG; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in right shift.", + SimpleMap.of("type", type.getName()))); + } + + public static Subtraction getSubtraction(TypeDescription type) { + if (type.isAssignableTo(int.class)) { + return Subtraction.INTEGER; + } else if (type.isAssignableTo(long.class)) { + return Subtraction.LONG; + } else if (type.isAssignableTo(float.class)) { + return Subtraction.FLOAT; + } else if (type.isAssignableTo(double.class)) { + return Subtraction.DOUBLE; + } + throw new Ts2JavaException( + SimpleFreeMarkerFormat.format("Unsupported type ${type} in subtraction.", + SimpleMap.of("type", type.getName()))); + } +} diff --git a/src/test/java/com/caoccao/javet/buddy/ts2java/TestLogicalOperations.java b/src/test/java/com/caoccao/javet/buddy/ts2java/TestLogicalOperations.java index 646b508..2e67001 100644 --- a/src/test/java/com/caoccao/javet/buddy/ts2java/TestLogicalOperations.java +++ b/src/test/java/com/caoccao/javet/buddy/ts2java/TestLogicalOperations.java @@ -169,6 +169,21 @@ public boolean logicalGT_II_Z(int a, int b) { return a > b; } + @Test + public void testLogicalEQEQ_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalEQEQ_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertFalse((boolean) method.invoke(object, 1D, 2D)); + assertFalse((boolean) method.invoke(object, 2D, 1D)); + assertTrue((boolean) method.invoke(object, 1D, 1D)); + assertTrue((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalEQEQ_FF_Z() throws Exception { Method method = clazz.getMethod("logicalEQEQ_FF_Z", float.class, float.class); @@ -212,6 +227,21 @@ public void testLogicalEQEQ_IL_Z() throws Exception { assertTrue((boolean) method.invoke(object, 1, 1L)); } + @Test + public void testLogicalEQ_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalEQ_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertFalse((boolean) method.invoke(object, 1D, 2D)); + assertFalse((boolean) method.invoke(object, 2D, 1D)); + assertTrue((boolean) method.invoke(object, 1D, 1D)); + assertTrue((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalEQ_FF_Z() throws Exception { Method method = clazz.getMethod("logicalEQ_FF_Z", float.class, float.class); @@ -256,6 +286,21 @@ public void testLogicalEQ_IL_Z() throws Exception { assertTrue((boolean) method.invoke(object, 1, 1L)); } + @Test + public void testLogicalGE_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalGE_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertFalse((boolean) method.invoke(object, 1D, 2D)); + assertTrue((boolean) method.invoke(object, 2D, 1D)); + assertTrue((boolean) method.invoke(object, 1D, 1D)); + assertTrue((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalGE_FF_Z() throws Exception { Method method = clazz.getMethod("logicalGE_FF_Z", float.class, float.class); @@ -300,6 +345,20 @@ public void testLogicalGE_IL_Z() throws Exception { assertTrue((boolean) method.invoke(object, 2, 1L)); } + @Test + public void testLogicalGT_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalGT_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertFalse((boolean) method.invoke(object, 1D, 2D)); + assertFalse((boolean) method.invoke(object, 1.23D, 1.23D)); + assertTrue((boolean) method.invoke(object, 2D, 1D)); + } + @Test public void testLogicalGT_FF_Z() throws Exception { assertFalse(logicalGT_FF_Z(1F, 2F)); @@ -344,6 +403,21 @@ public void testLogicalGT_IL_Z() throws Exception { assertTrue((boolean) method.invoke(object, 2, 1L)); } + @Test + public void testLogicalLE_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalLE_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertTrue((boolean) method.invoke(object, 1D, 2D)); + assertFalse((boolean) method.invoke(object, 2D, 1D)); + assertTrue((boolean) method.invoke(object, 1D, 1D)); + assertTrue((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalLE_FF_Z() throws Exception { Method method = clazz.getMethod("logicalLE_FF_Z", float.class, float.class); @@ -387,6 +461,21 @@ public void testLogicalLE_IL_Z() throws Exception { assertFalse((boolean) method.invoke(object, 2, 1L)); } + @Test + public void testLogicalLT_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalLT_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertTrue((boolean) method.invoke(object, 1D, 2D)); + assertFalse((boolean) method.invoke(object, 2D, 1D)); + assertFalse((boolean) method.invoke(object, 1D, 1D)); + assertFalse((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalLT_FF_Z() throws Exception { Method method = clazz.getMethod("logicalLT_FF_Z", float.class, float.class); @@ -430,6 +519,21 @@ public void testLogicalLT_IL_Z() throws Exception { assertFalse((boolean) method.invoke(object, 2, 1L)); } + @Test + public void testLogicalNotEQEQ_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalNotEQEQ_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertTrue((boolean) method.invoke(object, 1D, 2D)); + assertTrue((boolean) method.invoke(object, 2D, 1D)); + assertFalse((boolean) method.invoke(object, 1D, 1D)); + assertFalse((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalNotEQEQ_FF_Z() throws Exception { Method method = clazz.getMethod("logicalNotEQEQ_FF_Z", float.class, float.class); @@ -473,6 +577,21 @@ public void testLogicalNotEQEQ_IL_Z() throws Exception { assertFalse((boolean) method.invoke(object, 1, 1L)); } + @Test + public void testLogicalNotEQ_DD_Z() throws Exception { + Method method = clazz.getMethod("logicalNotEQ_DD_Z", double.class, double.class); + assertNotNull(method); + assertEquals(boolean.class, method.getReturnType()); + assertEquals(2, method.getParameterCount()); + assertEquals(double.class, method.getParameters()[0].getType()); + assertEquals(double.class, method.getParameters()[1].getType()); + Object object = clazz.getConstructor().newInstance(); + assertTrue((boolean) method.invoke(object, 1D, 2D)); + assertTrue((boolean) method.invoke(object, 2D, 1D)); + assertFalse((boolean) method.invoke(object, 1D, 1D)); + assertFalse((boolean) method.invoke(object, 1.23D, 1.23D)); + } + @Test public void testLogicalNotEQ_FF_Z() throws Exception { Method method = clazz.getMethod("logicalNotEQ_FF_Z", float.class, float.class);