Skip to content

Commit

Permalink
✨ feat: Add exp
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 2, 2024
1 parent 687ee63 commit 7bbc4ca
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
10 changes: 10 additions & 0 deletions scripts/ts/test/test.basic.operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Test {
return a * b;
}

public pow_DD_D(a: double, b: double): double {
return a ** b;
}

public pow_II_D(a: int, b: int): double {
return a ** b;
}

public shiftLeft_II_I(a: int, b: int): int {
return a << b;
}
Expand All @@ -64,6 +72,8 @@ console.log(new Test().divide_II_I(3, 2));
console.log(new Test().minus_II_I(3, 2));
console.log(new Test().mod_II_I(3, 2));
console.log(new Test().multiply_II_I(3, 2));
console.log(new Test().pow_DD_D(3, 2));
console.log(new Test().pow_II_D(3, 2));
console.log(new Test().shiftLeft_II_I(3, 2));
console.log(new Test().shiftRight_II_I(3, 1));
console.log(new Test().subtract_II_I(3, 2));
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstB
final TypeDescription leftType = manipulateExpression(functionContext, leftExpression);
final int stackManipulationSize = stackManipulations.size();
final TypeDescription rightType = manipulateExpression(functionContext, rightExpression);
TypeDescription upCaseType = JavaClassCast.getUpCastTypeForMathOp(leftType, rightType);
TypeDescription upCaseType;
switch (ast.getOp()) {
case Exp:
upCaseType = TypeDescription.ForLoadedType.of(double.class);
break;
default:
upCaseType = JavaClassCast.getUpCastTypeForMathOp(leftType, rightType);
break;
}
// Insert the type cast for left expression if possible.
JavaClassCast.getUpCastStackManipulation(leftType, upCaseType)
.ifPresent(stackManipulation -> stackManipulations.add(stackManipulationSize, stackManipulation));
Expand Down Expand Up @@ -78,6 +86,9 @@ public TypeDescription manipulate(JavaFunctionContext functionContext, Swc4jAstB
case Sub:
stackManipulation = Ts2JavaAstBinaryOp.getSubtraction(upCaseType);
break;
case Exp:
stackManipulation = Ts2JavaAstBinaryOp.getExp();
break;
case Gt:
case GtEq:
case Lt:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;

public final class Ts2JavaAstBinaryOp {
public static final String JAVA_LANG_MATH = "java/lang/Math";

private Ts2JavaAstBinaryOp() {
}

Expand Down Expand Up @@ -83,6 +85,20 @@ public static Division getDivision(TypeDescription type) {
SimpleMap.of("type", type.getName())));
}

public static StackManipulation getExp() {
return new StackManipulation.Simple((
MethodVisitor methodVisitor,
Implementation.Context implementationContext) -> {
methodVisitor.visitMethodInsn(
Opcodes.INVOKESTATIC,
JAVA_LANG_MATH,
"pow",
"(DD)D",
false);
return new StackManipulation.Size(-2, 0);
});
}

public static StackManipulation getLogical(
JavaFunctionContext functionContext,
Swc4jAstBinaryOp binaryOp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ public int minus(int a, int b) {
return -(a + b);
}

/*
public pow(DD)D
L0
LINENUMBER 109 L0
DLOAD 1
DLOAD 3
INVOKESTATIC java/lang/Math.pow (DD)D
DRETURN
L1
LOCALVARIABLE this Lcom/caoccao/javet/buddy/ts2java/ast/TestBasicOperations; L0 L1 0
LOCALVARIABLE a D L0 L1 1
LOCALVARIABLE b D L0 L1 3
MAXSTACK = 4
MAXLOCALS = 5
*/
public double pow(double a, double b) {
return Math.pow(a, b);
}

@Test
public void testAdd_DD_I() throws Exception {
Method method = clazz.getMethod("add_DD_I", double.class, double.class);
Expand Down Expand Up @@ -232,6 +251,31 @@ public void testMultiply_II_I() throws Exception {
assertEquals(3 * 2, method.invoke(object, 3, 2));
}

@Test
public void testPow_DD_D() throws Exception {
assertEquals(8D, pow(2D, 3D), 0.001D);
Method method = clazz.getMethod("pow_DD_D", double.class, double.class);
assertNotNull(method);
assertEquals(double.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();
assertEquals(8D, (double) method.invoke(object, 2D, 3D), 0.001D);
}

@Test
public void testPow_II_D() throws Exception {
Method method = clazz.getMethod("pow_II_D", int.class, int.class);
assertNotNull(method);
assertEquals(double.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(int.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertEquals(8D, (double) method.invoke(object, 2, 3), 0.001D);
}

@Test
public void testShiftLeft_II_I() throws Exception {
Method method = clazz.getMethod("shiftLeft_II_I", int.class, int.class);
Expand Down

0 comments on commit 7bbc4ca

Please sign in to comment.