Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various bugs #929

Merged
merged 6 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ public static boolean equals(final byte[] a, final byte[] b) {
}
return true;
}
public static boolean equals(final short[] a, final short[] b) {
if (a == b) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int i=0;i<a.length;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

public static boolean equals(final long[] a, final long[] b) {
if (a == b) {
Expand Down Expand Up @@ -236,6 +250,20 @@ public static boolean equals(final float[] a, final float[] b) {
}
return true;
}
public static boolean equals(final double[] a, final double[] b) {
if (a == b) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int i=0;i<a.length;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

public static boolean equals(final int[] a, final int[] b) {
if (a == b) {
Expand All @@ -252,6 +280,36 @@ public static boolean equals(final int[] a, final int[] b) {
return true;
}

public static boolean equals(final boolean[] a, final boolean[] b) {
if (a == b) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int i=0;i<a.length;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

public static boolean equals(final char[] a, final char[] b) {
if (a == b) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int i=0;i<a.length;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

public static boolean equals(final Object[] a, final Object[] b) {
if (a == b) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,11 @@ private void generateInvokeDynamicLambdaMetaFactoryInvocation(final InvokeDynami
}
}
}
if (source.getSort() == Type.OBJECT && target.getSort() != Type.OBJECT) {
if (source.getSort() == Type.OBJECT && target.getSort() != Type.OBJECT && target.getSort() != Type.VOID) {
// Object to primitive
switch (source.getSort()) {
default: {
throw new IllegalStateException("No converter from " + source + " to " + target + " implemented!");
throw new IllegalStateException("No converter from " + source + " to " + target + " implemented! (" + argMethodName +")");
}
}
}
Expand Down Expand Up @@ -1988,7 +1988,7 @@ public void finishIfBlock() {
@Override
public void startBlock(final Sequencer.Block block) {
writeIndent();
pw.print(block.label);
pw.print(block.label.replace("-",""));
pw.print(": ");
if (block.type == Sequencer.Block.Type.LOOP) {
pw.print("while(true) ");
Expand Down Expand Up @@ -2066,15 +2066,15 @@ public void write(final ReturnValue node) {
public void writeBreakTo(final String label) {
writeIndent();
pw.print("break ");
pw.print(label);
pw.print(label.replace("-",""));
pw.println(";");
}

@Override
public void writeContinueTo(final String label) {
writeIndent();
pw.print("continue ");
pw.print(label);
pw.print(label.replace("-",""));
pw.println(";");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
*/
package de.mirkosertic.bytecoder.core.parser;

import de.mirkosertic.bytecoder.core.ir.AnalysisStack;
import de.mirkosertic.bytecoder.core.ir.ControlTokenConsumer;
import de.mirkosertic.bytecoder.core.ir.Graph;
import de.mirkosertic.bytecoder.core.ir.InvocationType;
import de.mirkosertic.bytecoder.core.ir.ResolvedClass;
import de.mirkosertic.bytecoder.core.ir.ResolvedMethod;
import de.mirkosertic.bytecoder.core.ir.Value;
import de.mirkosertic.bytecoder.core.ir.*;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.MethodInsnNode;
Expand All @@ -35,54 +29,63 @@ public ControlTokenConsumer intrinsifyMethodInvocation(final CompileUnit compile
if (System.class.getName().equals(targetClass.getClassName())) {
if ("arraycopy".equals(node.name)) {
final Value source = incomingData[1];
final Type methodType;
final ResolvedClass systemClass = compileUnit.resolveClass(Type.getType(System.class), analysisStack);

if (source.type.getSort() != Type.ARRAY) {
throw new IllegalArgumentException("Expected array type for arrayCopy, got " + source.type);
}

final ResolvedClass systemClass = compileUnit.resolveClass(Type.getType(System.class), analysisStack);

final Type methodType;
switch (source.type.getElementType().getSort()) {
case Type.CHAR: {
methodType = Type.getMethodType("([CI[CII)V");
break;
}
case Type.BYTE: {
methodType = Type.getMethodType("([BI[BII)V");
break;
}
case Type.DOUBLE: {
methodType = Type.getMethodType("([DI[DII)V");
break;
}
case Type.FLOAT: {
methodType = Type.getMethodType("([FI[FII)V");
break;
}
case Type.INT: {
methodType = Type.getMethodType("([II[III)V");
break;
}
case Type.LONG: {
methodType = Type.getMethodType("([JI[JII)V");
break;
}
case Type.SHORT: {
methodType = Type.getMethodType("([SI[SII)V");
break;
}
case Type.BOOLEAN: {
methodType = Type.getMethodType("([ZI[ZII)V");
break;
}
default: {
// (Ljava/lang/Object;ILjava/lang/Object;II)V
methodType = Type.getMethodType(node.desc);
break;
if (source.type.getClassName().contentEquals(Object.class.getName())) {
// (Ljava/lang/Object;ILjava/lang/Object;II)V
methodType = Type.getMethodType(node.desc);
} else if (source.type.getSort() == Type.ARRAY) {
switch (source.type.getElementType().getSort()) {
case Type.CHAR: {
methodType = Type.getMethodType("([CI[CII)V");
break;
}
case Type.BYTE: {
methodType = Type.getMethodType("([BI[BII)V");
break;
}
case Type.DOUBLE: {
methodType = Type.getMethodType("([DI[DII)V");
break;
}
case Type.FLOAT: {
methodType = Type.getMethodType("([FI[FII)V");
break;
}
case Type.INT: {
methodType = Type.getMethodType("([II[III)V");
break;
}
case Type.LONG: {
methodType = Type.getMethodType("([JI[JII)V");
break;
}
case Type.SHORT: {
methodType = Type.getMethodType("([SI[SII)V");
break;
}
case Type.BOOLEAN: {
methodType = Type.getMethodType("([ZI[ZII)V");
break;
}
case Type.OBJECT: {
// (Ljava/lang/Object;ILjava/lang/Object;II)V
methodType = Type.getMethodType(node.desc);
break;
}
default: {
throw new IllegalArgumentException(source.type.getElementType().getSort()+" is not a valid type for an array in System.arraycopy() !");
}
}
} else {
throw new IllegalArgumentException("Expected array type or "+Object.class.getName()+" for arrayCopy, got " + source.type);
}



final ResolvedMethod rm = systemClass.resolveMethod(node.name,
methodType, analysisStack);
final ControlTokenConsumer n = graph.newMethodInvocation(InvocationType.STATIC, node, rm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Iterator;

@RunWith(UnitTestRunner.class)
public class SystemTest {

Expand All @@ -42,4 +44,74 @@ public void testPrintSingleChars() {
}
System.out.println();
}

@Test
public void arrayCopyNatives(){
byte[] byteSource = new byte[]{1,2,3,4};
byte[] byteTarget = new byte[4];
System.arraycopy(byteSource,1,byteTarget,2,2);
Assert.assertArrayEquals(byteTarget, new byte[]{0,0,2,3});


short[] shortSource = new short[]{1,2,3,4};
short[] shortTarget = new short[4];
System.arraycopy(shortSource,1,shortTarget,2,2);
Assert.assertArrayEquals(shortTarget, new short[]{0,0,2,3});


int[] intArr = new int[]{1,2,3,4};
int[] target = new int[4];
System.arraycopy(intArr,1,target,2,2);
Assert.assertArrayEquals(target, new int[]{0,0,2,3});

long[] longSource = new long[]{1,2,3,4};
long[] longTarget = new long[4];
System.arraycopy(longSource,1,longTarget,2,2);
Assert.assertArrayEquals(longTarget, new long[]{0,0,2,3});

double[] doubleSource = new double[]{1,2,3,4};
double[] doubleTarget = new double[4];
System.arraycopy(doubleSource,1,doubleTarget,2,2);
Assert.assertArrayEquals(doubleTarget, new double[]{0,0,2,3},0);

float[] floatSource = new float[]{1,2,3,4};
float[] floatTarget = new float[4];
System.arraycopy(floatSource,1,floatTarget,2,2);
Assert.assertArrayEquals(floatTarget, new float[]{0,0,2,3},0);

char[] charSource = new char[]{1,2,3,4};
char[] charTarget = new char[4];
System.arraycopy(charSource,1,charTarget,2,2);
Assert.assertArrayEquals(charTarget, new char[]{0,0,2,3});


boolean[] booleanSource = new boolean[]{true,false,true,false};
boolean[] booleanTarget = new boolean[4];
System.arraycopy(booleanSource,1,booleanTarget,2,2);
Assert.assertArrayEquals(booleanTarget, new boolean[]{false,false,false,true});
}

@Test
public void arraycopyObjects(){
Object[] objArr = new Object[]{"A","b","c"};
Object[] targetArr = new Object[2];
System.arraycopy(objArr,1,targetArr,0,2);
Assert.assertArrayEquals(targetArr, new Object[]{"b","c"});

Object[] stringarr = new String[]{"A","b","c"};
Object[] targetStringArr = new String[2];
System.arraycopy(stringarr,1,targetStringArr,0,2);
Assert.assertArrayEquals(targetStringArr, new String[]{"b","c"});

Object concealedArray = new Object[]{"A","b","c"};
Object concealedTargetArray = new Object[2];
System.arraycopy(concealedArray,1,concealedTargetArray,0,2);
Assert.assertArrayEquals((Object[]) concealedTargetArray, new Object[]{"b","c"});


Object concealedStringArray = new String[]{"A","b","c"};
Object concealedTargetStringArray = new String[2];
System.arraycopy(concealedStringArray,1,concealedTargetStringArray,0,2);
Assert.assertArrayEquals((String[]) concealedTargetStringArray, new String[]{"b","c"});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.mirkosertic.bytecoder.core;

import de.mirkosertic.bytecoder.core.test.UnitTestRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.function.Consumer;

@RunWith(UnitTestRunner.class)
public class MethodReferenceTest {
public static String transform(String input){
consumed = input+input;
return input+input;
}
static String consumed;
public static void consume(String input){
consumed = input;
}

@Test
public void passMatchingReference(){
consumed = "";
useConsumer(MethodReferenceTest::consume);
Assert.assertEquals(consumed,"test");
}

@Test
public void passUnmatchingReturnType(){
consumed = "";
useConsumer(MethodReferenceTest::transform);
Assert.assertEquals(consumed,"testtest");
}

public static void useConsumer(Consumer<String> consumer){
consumer.accept("test");
}
}