Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

feat: Add support for custom justifications #39

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -2,21 +2,18 @@

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ai.timefold.jpyinterpreter.implementors.DelegatingInterfaceImplementor;
import ai.timefold.jpyinterpreter.implementors.JavaPythonTypeConversionImplementor;
import ai.timefold.jpyinterpreter.types.BuiltinTypes;
import ai.timefold.jpyinterpreter.types.PythonLikeType;
import ai.timefold.jpyinterpreter.util.MethodVisitorAdapters;
import ai.timefold.jpyinterpreter.util.arguments.ArgumentSpec;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

Expand Down Expand Up @@ -251,64 +248,26 @@ private static void createMethodDelegate(ClassWriter classWriter,
interfaceMethodVisitor.visitFieldInsn(Opcodes.GETSTATIC, wrapperInternalName,
"argumentSpec$" + interfaceMethod.getName(),
Type.getDescriptor(ArgumentSpec.class));
interfaceMethodVisitor.visitLdcInsn(interfaceMethod.getParameterCount());
interfaceMethodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(PythonLikeObject.class));
interfaceMethodVisitor.visitVarInsn(Opcodes.ASTORE, interfaceMethod.getParameterCount() + 2);
for (int i = 0; i < interfaceMethod.getParameterCount(); i++) {
var parameterType = interfaceMethod.getParameterTypes()[i];
interfaceMethodVisitor.visitVarInsn(Opcodes.ALOAD, interfaceMethod.getParameterCount() + 2);
interfaceMethodVisitor.visitLdcInsn(i);
interfaceMethodVisitor.visitVarInsn(Type.getType(parameterType).getOpcode(Opcodes.ILOAD),
i + 1);
if (parameterType.isPrimitive()) {
convertPrimitiveToObjectType(parameterType, interfaceMethodVisitor);
}
interfaceMethodVisitor.visitVarInsn(Opcodes.ALOAD, interfaceMethod.getParameterCount() + 1);
interfaceMethodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
Type.getInternalName(JavaPythonTypeConversionImplementor.class),
"wrapJavaObject",
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class), Type.getType(Object.class), Type.getType(
Map.class)),
false);
interfaceMethodVisitor.visitInsn(Opcodes.AASTORE);
}

var functionSignature = delegateType.getMethodType(interfaceMethod.getName())
.orElseThrow(() -> new IllegalArgumentException(
"Type %s cannot implement interface %s because it missing method %s."
.formatted(delegateType, interfaceMethod.getDeclaringClass(), interfaceMethod)))
.getDefaultFunctionSignature()
.orElseThrow();

interfaceMethodVisitor.visitVarInsn(Opcodes.ALOAD, interfaceMethod.getParameterCount() + 2);
interfaceMethodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(List.class),
"of", Type.getMethodDescriptor(Type.getType(List.class), Type.getType(Object[].class)),
true);
interfaceMethodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Collections.class),
"emptyMap", Type.getMethodDescriptor(Type.getType(Map.class)), false);
interfaceMethodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ArgumentSpec.class),
"extractArgumentList", Type.getMethodDescriptor(
Type.getType(List.class), Type.getType(List.class), Type.getType(Map.class)),
DelegatingInterfaceImplementor.prepareParametersForMethodCallFromArgumentSpec(
interfaceMethod, interfaceMethodVisitor, functionSignature.getParameterTypes().length,
Type.getType(functionSignature.getMethodDescriptor().getMethodDescriptor()),
false);

for (int i = 0; i < functionSignature.getParameterTypes().length; i++) {
interfaceMethodVisitor.visitInsn(Opcodes.DUP);
interfaceMethodVisitor.visitLdcInsn(i);
interfaceMethodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(List.class),
"get", Type.getMethodDescriptor(Type.getType(Object.class), Type.INT_TYPE), true);
interfaceMethodVisitor.visitTypeInsn(Opcodes.CHECKCAST,
functionSignature.getParameterTypes()[i].getJavaTypeInternalName());
interfaceMethodVisitor.visitInsn(Opcodes.SWAP);
}
interfaceMethodVisitor.visitInsn(Opcodes.POP);
functionSignature.getMethodDescriptor().callMethod(interfaceMethodVisitor);

var returnType = interfaceMethod.getReturnType();
if (returnType.equals(void.class)) {
interfaceMethodVisitor.visitInsn(Opcodes.RETURN);
} else {
if (returnType.isPrimitive()) {
loadBoxedPrimitiveTypeClass(returnType, interfaceMethodVisitor);
DelegatingInterfaceImplementor.loadBoxedPrimitiveTypeClass(returnType, interfaceMethodVisitor);
} else {
interfaceMethodVisitor.visitLdcInsn(Type.getType(returnType));
}
Expand All @@ -320,7 +279,7 @@ private static void createMethodDelegate(ClassWriter classWriter,
PythonLikeObject.class)),
false);
if (returnType.isPrimitive()) {
unboxBoxedPrimitiveType(returnType, interfaceMethodVisitor);
DelegatingInterfaceImplementor.unboxBoxedPrimitiveType(returnType, interfaceMethodVisitor);
interfaceMethodVisitor.visitInsn(Type.getType(returnType).getOpcode(Opcodes.IRETURN));
} else {
interfaceMethodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(returnType));
Expand All @@ -330,94 +289,4 @@ private static void createMethodDelegate(ClassWriter classWriter,
interfaceMethodVisitor.visitMaxs(interfaceMethod.getParameterCount() + 2, 1);
interfaceMethodVisitor.visitEnd();
}

private static void convertPrimitiveToObjectType(Class<?> primitiveType, MethodVisitor methodVisitor) {
if (primitiveType.equals(boolean.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Boolean.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Boolean.class), Type.BOOLEAN_TYPE), false);
} else if (primitiveType.equals(byte.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Byte.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Byte.class), Type.BYTE_TYPE), false);
} else if (primitiveType.equals(char.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Character.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Character.class), Type.CHAR_TYPE), false);
} else if (primitiveType.equals(short.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Short.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Short.class), Type.SHORT_TYPE), false);
} else if (primitiveType.equals(int.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Integer.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Integer.class), Type.INT_TYPE), false);
} else if (primitiveType.equals(long.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Long.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Long.class), Type.LONG_TYPE), false);
} else if (primitiveType.equals(float.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Float.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Float.class), Type.FLOAT_TYPE), false);
} else if (primitiveType.equals(double.class)) {
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Double.class),
"valueOf", Type.getMethodDescriptor(Type.getType(Double.class), Type.DOUBLE_TYPE), false);
} else {
throw new IllegalStateException("Unknown primitive type %s.".formatted(primitiveType));
}
}

private static void loadBoxedPrimitiveTypeClass(Class<?> primitiveType, MethodVisitor methodVisitor) {
if (primitiveType.equals(boolean.class)) {
methodVisitor.visitLdcInsn(Type.getType(Boolean.class));
} else if (primitiveType.equals(byte.class)) {
methodVisitor.visitLdcInsn(Type.getType(Byte.class));
} else if (primitiveType.equals(char.class)) {
methodVisitor.visitLdcInsn(Type.getType(Character.class));
} else if (primitiveType.equals(short.class)) {
methodVisitor.visitLdcInsn(Type.getType(Short.class));
} else if (primitiveType.equals(int.class)) {
methodVisitor.visitLdcInsn(Type.getType(Integer.class));
} else if (primitiveType.equals(long.class)) {
methodVisitor.visitLdcInsn(Type.getType(Long.class));
} else if (primitiveType.equals(float.class)) {
methodVisitor.visitLdcInsn(Type.getType(Float.class));
} else if (primitiveType.equals(double.class)) {
methodVisitor.visitLdcInsn(Type.getType(Double.class));
} else {
throw new IllegalStateException("Unknown primitive type %s.".formatted(primitiveType));
}
}

private static void unboxBoxedPrimitiveType(Class<?> primitiveType, MethodVisitor methodVisitor) {
if (primitiveType.equals(boolean.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Boolean.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Boolean.class),
"booleanValue", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
} else if (primitiveType.equals(byte.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Byte.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Byte.class),
"byteValue", Type.getMethodDescriptor(Type.BYTE_TYPE), false);
} else if (primitiveType.equals(char.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Character.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Character.class),
"charValue", Type.getMethodDescriptor(Type.CHAR_TYPE), false);
} else if (primitiveType.equals(short.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Short.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Short.class),
"shortValue", Type.getMethodDescriptor(Type.SHORT_TYPE), false);
} else if (primitiveType.equals(int.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Integer.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Integer.class),
"intValue", Type.getMethodDescriptor(Type.INT_TYPE), false);
} else if (primitiveType.equals(long.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Long.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Long.class),
"longValue", Type.getMethodDescriptor(Type.LONG_TYPE), false);
} else if (primitiveType.equals(float.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Float.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Float.class),
"floatValue", Type.getMethodDescriptor(Type.FLOAT_TYPE), false);
} else if (primitiveType.equals(double.class)) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(Double.class));
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Double.class),
"doubleValue", Type.getMethodDescriptor(Type.DOUBLE_TYPE), false);
} else {
throw new IllegalStateException("Unknown primitive type %s.".formatted(primitiveType));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.stream.Collectors;

import ai.timefold.jpyinterpreter.dag.FlowGraph;
import ai.timefold.jpyinterpreter.implementors.DelegatingInterfaceImplementor;
import ai.timefold.jpyinterpreter.implementors.JavaComparableImplementor;
import ai.timefold.jpyinterpreter.implementors.JavaEqualsImplementor;
import ai.timefold.jpyinterpreter.implementors.JavaHashCodeImplementor;
Expand Down Expand Up @@ -94,6 +95,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
var className = preparedClassInfo.className;
var internalClassName = preparedClassInfo.classInternalName;

Map<String, InterfaceDeclaration> instanceMethodNameToMethodDescriptor = new HashMap<>();
Set<PythonLikeType> superTypeSet;
Set<JavaInterfaceImplementor> javaInterfaceImplementorSet = new HashSet<>();

Expand All @@ -118,6 +120,11 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
}
}

for (Class<?> javaInterface : pythonCompiledClass.javaInterfaces) {
javaInterfaceImplementorSet.add(
new DelegatingInterfaceImplementor(internalClassName, javaInterface, instanceMethodNameToMethodDescriptor));
}

if (pythonCompiledClass.superclassList.isEmpty()) {
superTypeSet = Set.of(CPythonBackedPythonLikeObject.CPYTHON_BACKED_OBJECT_TYPE);
} else {
Expand Down Expand Up @@ -159,7 +166,8 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp

List<JavaInterfaceImplementor> nonObjectInterfaceImplementors = javaInterfaceImplementorSet.stream()
.filter(implementor -> !Object.class.equals(implementor.getInterfaceClass()))
.collect(Collectors.toList());
.toList();

String[] interfaces = new String[nonObjectInterfaceImplementors.size()];
for (int i = 0; i < nonObjectInterfaceImplementors.size(); i++) {
interfaces[i] = Type.getInternalName(nonObjectInterfaceImplementors.get(i).getInterfaceClass());
Expand Down Expand Up @@ -294,7 +302,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
.entrySet()) {
instanceMethodEntry.getValue().methodKind = PythonMethodKind.VIRTUAL_METHOD;
createInstanceMethod(pythonLikeType, classWriter, internalClassName, instanceMethodEntry.getKey(),
instanceMethodEntry.getValue());
instanceMethodEntry.getValue(), instanceMethodNameToMethodDescriptor);
}

for (Map.Entry<String, PythonCompiledFunction> staticMethodEntry : pythonCompiledClass.staticFunctionNameToPythonBytecode
Expand Down Expand Up @@ -854,13 +862,15 @@ private static void addAnnotationsToMethod(PythonCompiledFunction function, Meth
}

private static void createInstanceMethod(PythonLikeType pythonLikeType, ClassWriter classWriter, String internalClassName,
String methodName, PythonCompiledFunction function) {
String methodName, PythonCompiledFunction function,
Map<String, InterfaceDeclaration> instanceMethodNameToMethodDescriptor) {
InterfaceDeclaration interfaceDeclaration = getInterfaceForInstancePythonFunction(internalClassName, function);
String interfaceDescriptor = 'L' + interfaceDeclaration.interfaceName + ';';
String interfaceDescriptor = interfaceDeclaration.descriptor();
String javaMethodName = getJavaMethodName(methodName);

classWriter.visitField(Modifier.PUBLIC | Modifier.STATIC, javaMethodName, interfaceDescriptor,
null, null);
instanceMethodNameToMethodDescriptor.put(methodName, interfaceDeclaration);
Type returnType = getVirtualFunctionReturnType(function);

List<PythonLikeType> parameterPythonTypeList = function.getParameterTypes();
Expand Down Expand Up @@ -1555,30 +1565,13 @@ public static PythonLikeType getPythonReturnTypeOfFunction(PythonCompiledFunctio
}
}

public static class InterfaceDeclaration {
final String interfaceName;
final String methodDescriptor;

public InterfaceDeclaration(String interfaceName, String methodDescriptor) {
this.interfaceName = interfaceName;
this.methodDescriptor = methodDescriptor;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
InterfaceDeclaration that = (InterfaceDeclaration) o;
return interfaceName.equals(that.interfaceName) && methodDescriptor.equals(that.methodDescriptor);
public record InterfaceDeclaration(String interfaceName, String methodDescriptor) {
public String descriptor() {
return "L" + interfaceName + ";";
}

@Override
public int hashCode() {
return Objects.hash(interfaceName, methodDescriptor);
public Type methodType() {
return Type.getMethodType(methodDescriptor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class PythonCompiledClass {
*/
public Map<String, TypeHint> typeAnnotations;

/**
* Java interfaces the class implement
*/
public List<Class<?>> javaInterfaces;

/**
* The binary type of this PythonCompiledClass;
* typically {@link CPythonType}. Used when methods
Expand Down
Loading
Loading