0.6.14 Support covariant types in callback injector handlers with Coerce

updates #192
This commit is contained in:
Mumfrey committed 2017-07-03 12:57:23 +01:00
1 parent 05469e146c
commit 2a64bc35b4
7 files changed
+95 -39

No files matched your search

@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.6.13";
public static final String VERSION = "0.6.14";
// Consts
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
@@ -28,8 +28,14 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
* Indicates that a local capture injector should coerce top-level primitive
* types to covariant types.
* This annotation has two usages applicable to Callback Injectors (defined
* using {@link Inject}. For local capture injectors, it indicates that the
* injector should coerce top-level primitive types to covariant types defined
* on the handler. For other injectors, it can be used on a reference type to
* indicate that the intended type is covariant over the argument type (or that
* the argument type is contravariant on the target class type). This can be
* used for multi-target injectors with a bounded type argument on the class or
* target method.
*
* <p>During LVT generation it is not always possible to inflect the exact local
* type for types represented internally as integers, for example booleans and
@@ -122,6 +122,11 @@ public class CallbackInjector extends Injector {
* matches the corresponding argument index in the callback
*/
final String[] argNames;
/**
* Arguments which require a type cast before being LOADed
*/
// final Type[] typeCasts;
/**
* These two variables keep track of the (additional) stack size
@@ -182,6 +187,7 @@ public class CallbackInjector extends Injector {
this.isAtReturn = this.node instanceof InsnNode && this.isValueReturnOpcode(this.node.getOpcode());
this.desc = target.getCallbackDescriptor(this.localTypes, target.arguments);
this.descl = target.getCallbackDescriptor(true, this.localTypes, target.arguments, this.frameSize, this.extraArgs);
// this.typeCasts = new Type[this.frameSize + this.extraArgs];
this.invoke = target.arguments.length + (this.canCaptureLocals ? this.localTypes.length - this.frameSize : 0);
this.marshallVar = target.allocateLocal();
@@ -239,39 +245,37 @@ public class CallbackInjector extends Injector {
return true;
}
if (this.extraArgs > 0) {
Type[] inTypes = Type.getArgumentTypes(desc);
Type[] myTypes = Type.getArgumentTypes(this.descl);
if (inTypes.length != myTypes.length) {
return false;
}
for (int arg = this.frameSize + 1; arg < myTypes.length; arg++) {
Type type = inTypes[arg];
if (type.equals(myTypes[arg])) {
continue; // Type matches
}
if (type.getSort() >= Type.ARRAY) {
return false; // Reference types must match exactly
}
AnnotationNode coerce = Annotations.getInvisibleParameter(this.handler, Coerce.class, arg);
if (coerce == null) {
return false; // No @Coerce specified, types must match
}
boolean canCoerce = Injector.canCoerce(inTypes[arg], myTypes[arg]);
if (!canCoerce) {
return false; // Can't coerce source type to local type, give up
}
}
return true;
Type[] inTypes = Type.getArgumentTypes(desc);
Type[] myTypes = Type.getArgumentTypes(this.descl);
if (inTypes.length != myTypes.length) {
return false;
}
return false;
for (int arg = 0; arg < myTypes.length; arg++) {
Type type = inTypes[arg];
if (type.equals(myTypes[arg])) {
continue; // Type matches
}
if (type.getSort() == Type.ARRAY) {
return false; // Array types must match exactly
}
if (Annotations.getInvisibleParameter(this.handler, Coerce.class, arg) == null) {
return false; // No @Coerce specified, types must match
}
if (!Injector.canCoerce(inTypes[arg], myTypes[arg])) {
// if (Injector.canCoerce(myTypes[arg], inTypes[arg])) {
// this.typeCasts[arg] = inTypes[arg];
// } else {
return false; // Can't coerce or cast source type to local type, give up
// }
}
}
return true;
}
boolean captureArgs() {
@@ -318,7 +322,7 @@ public class CallbackInjector extends Injector {
protected void sanityCheck(Target target, List<InjectionPoint> injectionPoints) {
super.sanityCheck(target, injectionPoints);
if (Bytecode.methodIsStatic(target.method) != this.isStatic) {
if (target.isStatic != this.isStatic) {
throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this);
}
@@ -577,7 +581,7 @@ public class CallbackInjector extends Injector {
// Push the target method's parameters onto the stack
if (callback.captureArgs()) {
Bytecode.loadArgs(callback.target.arguments, callback, this.isStatic ? 0 : 1);
Bytecode.loadArgs(callback.target.arguments, callback, this.isStatic ? 0 : 1, -1); //, callback.typeCasts);
}
// Push the callback info onto the stack
@@ -50,6 +50,7 @@ import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.util.Bytecode;
/**
@@ -279,9 +280,13 @@ public abstract class Injector {
* @return true if <tt>from</tt> can be coerced to <tt>to</tt>
*/
public static boolean canCoerce(Type from, Type to) {
if (from.getSort() == Type.OBJECT && to.getSort() == Type.OBJECT) {
return Injector.canCoerce(ClassInfo.forType(from), ClassInfo.forType(to));
}
return Injector.canCoerce(from.getDescriptor(), to.getDescriptor());
}
/**
* Returns whether the <tt>from</tt> type can be coerced to the <tt>to</tt>
* type.
@@ -309,5 +314,18 @@ public abstract class Injector {
public static boolean canCoerce(char from, char to) {
return to == 'I' && "IBSCZ".indexOf(from) > -1;
}
/**
* Returns whether the <tt>from</tt> type can be coerced to the <tt>to</tt>
* type. This is effectively a superclass check: the check suceeds if <tt>
* to</tt> is a subclass of <tt>from</tt>.
*
* @param from type to coerce from
* @param to type to coerce to
* @return true if <tt>from</tt> can be coerced to <tt>to</tt>
*/
private static boolean canCoerce(ClassInfo from, ClassInfo to) {
return from != null && to != null && (to == from || to.hasSuperClass(from));
}
}
@@ -519,14 +519,32 @@ public final class Bytecode {
* @param end End position
*/
public static void loadArgs(Type[] args, InsnList insns, int start, int end) {
int pos = start;
Bytecode.loadArgs(args, insns, start, end, null);
}
/**
* Injects appropriate LOAD opcodes into the supplied InsnList appropriate
* for each entry in the args array starting at start and ending at end
*
* @param args Argument types
* @param insns Instruction List to inject into
* @param start Start position
* @param end End position
* @param casts Type casts array
*/
public static void loadArgs(Type[] args, InsnList insns, int start, int end, Type[] casts) {
int pos = start, index = 0;
for (Type type : args) {
insns.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), pos));
if (casts != null && index < casts.length && casts[index] != null) {
insns.add(new TypeInsnNode(Opcodes.CHECKCAST, casts[index].getInternalName()));
}
pos += type.getSize();
if (end >= start && pos >= end) {
return;
}
index++;
}
}