Skip to content

Commit

Permalink
✨ feat: Add GT, GE, LT, LE, EQ, EQEQ, NotEQ, NotEQEQ for byte, char, …
Browse files Browse the repository at this point in the history
…short
  • Loading branch information
caoccao committed Nov 1, 2024
1 parent 58509fa commit 0c2480c
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 38 deletions.
25 changes: 24 additions & 1 deletion scripts/ts/test/test.logical.operations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { type float, double, int, long } from "./test.type.aliases.ts";
import {
type byte,
char,
float,
double,
int,
long,
short,
} from "./test.type.aliases.ts";

class Test {
public logicalEQEQ_DD_Z(a: double, b: double): boolean {
Expand Down Expand Up @@ -33,6 +41,14 @@ class Test {
return a == b;
}

public logicalGE_BB_Z(a: byte, b: byte): boolean {
return a >= b;
}

public logicalGE_CC_Z(a: char, b: char): boolean {
return a >= b;
}

public logicalGE_DD_Z(a: double, b: double): boolean {
return a >= b;
}
Expand All @@ -50,6 +66,10 @@ class Test {
return c;
}

public logicalGE_SS_Z(a: short, b: short): boolean {
return a >= b;
}

public logicalGT_DD_Z(a: double, b: double): boolean {
return a > b;
}
Expand Down Expand Up @@ -139,10 +159,13 @@ 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_BB_Z(1, 2));
console.log(new Test().logicalGE_CC_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().logicalGE_SS_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));
Expand Down
3 changes: 3 additions & 0 deletions scripts/ts/test/test.type.aliases.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type byte = number;
export type char = number;
export type float = number;
export type double = number;
export type int = number;
export type long = number;
export type short = number;
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private Ts2JavaAstBinaryOp() {
}

public static Addition getAddition(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return Addition.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return Addition.LONG;
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
return Addition.FLOAT;
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
return Addition.DOUBLE;
}
throw new Ts2JavaException(
Expand All @@ -50,13 +50,13 @@ public static Addition getAddition(TypeDescription type) {
}

public static Division getDivision(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return Division.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return Division.LONG;
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
return Division.FLOAT;
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
return Division.DOUBLE;
}
throw new Ts2JavaException(
Expand All @@ -68,7 +68,10 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
Label labelFalse = new Label();
Label labelTrue = new Label();
List<StackManipulation> stackManipulations = new ArrayList<>();
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)
|| type.represents(short.class)
|| type.represents(byte.class)
|| type.represents(char.class)) {
int opcodeCompare;
switch (binaryOp) {
case Gt:
Expand Down Expand Up @@ -102,7 +105,7 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
methodVisitor.visitJumpInsn(opcodeCompare, labelFalse);
return new StackManipulation.Size(-1, 0);
}));
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
int opcodeCompare;
switch (binaryOp) {
case Gt:
Expand Down Expand Up @@ -137,7 +140,7 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
methodVisitor.visitJumpInsn(opcodeCompare, labelFalse);
return new StackManipulation.Size(-2, 0);
}));
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
int opcodeCompare1;
int opcodeCompare2;
switch (binaryOp) {
Expand Down Expand Up @@ -179,7 +182,7 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
methodVisitor.visitJumpInsn(opcodeCompare2, labelFalse);
return new StackManipulation.Size(-1, 0);
}));
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
int opcodeCompare1;
int opcodeCompare2;
switch (binaryOp) {
Expand Down Expand Up @@ -242,13 +245,13 @@ public static StackManipulation getLogicalStackManipulation(Swc4jAstBinaryOp bin
}

public static Multiplication getMultiplication(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return Multiplication.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return Multiplication.LONG;
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
return Multiplication.FLOAT;
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
return Multiplication.DOUBLE;
}
throw new Ts2JavaException(
Expand All @@ -257,13 +260,13 @@ public static Multiplication getMultiplication(TypeDescription type) {
}

public static Remainder getRemainder(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return Remainder.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return Remainder.LONG;
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
return Remainder.FLOAT;
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
return Remainder.DOUBLE;
}
throw new Ts2JavaException(
Expand All @@ -272,9 +275,9 @@ public static Remainder getRemainder(TypeDescription type) {
}

public static ShiftLeft getShiftLeft(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return ShiftLeft.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return ShiftLeft.LONG;
}
throw new Ts2JavaException(
Expand All @@ -283,9 +286,9 @@ public static ShiftLeft getShiftLeft(TypeDescription type) {
}

public static ShiftRight getShiftRight(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return ShiftRight.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return ShiftRight.LONG;
}
throw new Ts2JavaException(
Expand All @@ -294,13 +297,13 @@ public static ShiftRight getShiftRight(TypeDescription type) {
}

public static Subtraction getSubtraction(TypeDescription type) {
if (type.isAssignableTo(int.class)) {
if (type.represents(int.class)) {
return Subtraction.INTEGER;
} else if (type.isAssignableTo(long.class)) {
} else if (type.represents(long.class)) {
return Subtraction.LONG;
} else if (type.isAssignableTo(float.class)) {
} else if (type.represents(float.class)) {
return Subtraction.FLOAT;
} else if (type.isAssignableTo(double.class)) {
} else if (type.represents(double.class)) {
return Subtraction.DOUBLE;
}
throw new Ts2JavaException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* Copyright (c) 2024-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.
Expand All @@ -14,8 +14,10 @@
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java;
package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import com.caoccao.javet.buddy.ts2java.Ts2Java;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* Copyright (c) 2024-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.
Expand All @@ -14,8 +14,10 @@
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java;
package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import com.caoccao.javet.buddy.ts2java.Ts2Java;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java;
package com.caoccao.javet.buddy.ts2java.ast;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* Copyright (c) 2024-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.
Expand All @@ -14,8 +14,10 @@
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java;
package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import com.caoccao.javet.buddy.ts2java.Ts2Java;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -286,6 +288,34 @@ public void testLogicalEQ_IL_Z() throws Exception {
assertTrue((boolean) method.invoke(object, 1, 1L));
}

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

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

@Test
public void testLogicalGE_DD_Z() throws Exception {
Method method = clazz.getMethod("logicalGE_DD_Z", double.class, double.class);
Expand Down Expand Up @@ -345,6 +375,20 @@ public void testLogicalGE_IL_Z() throws Exception {
assertTrue((boolean) method.invoke(object, 2, 1L));
}

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

@Test
public void testLogicalGT_DD_Z() throws Exception {
Method method = clazz.getMethod("logicalGT_DD_Z", double.class, double.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* Copyright (c) 2024-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.
Expand All @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java;
package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.compiler;

import net.bytebuddy.description.type.TypeDescription;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestJavaClassCast {
@Test
public void testTypeDescription() {
assertTrue(TypeDescription.ForLoadedType.of(int.class).isAssignableTo(int.class));
assertFalse(TypeDescription.ForLoadedType.of(short.class).isAssignableTo(int.class));
assertFalse(TypeDescription.ForLoadedType.of(long.class).isAssignableTo(int.class));
assertFalse(TypeDescription.ForLoadedType.of(int.class).isAssignableTo(long.class));
assertTrue(TypeDescription.ForLoadedType.of(List.class).isAssignableTo(Object.class));

assertTrue(TypeDescription.ForLoadedType.of(int.class).represents(int.class));
assertFalse(TypeDescription.ForLoadedType.of(short.class).represents(int.class));
assertFalse(TypeDescription.ForLoadedType.of(long.class).represents(int.class));
assertFalse(TypeDescription.ForLoadedType.of(int.class).represents(long.class));
assertFalse(TypeDescription.ForLoadedType.of(List.class).represents(Object.class));
}
}

0 comments on commit 0c2480c

Please sign in to comment.