diff --git a/README.MD b/README.MD index db980146..e9ff361e 100644 --- a/README.MD +++ b/README.MD @@ -509,5 +509,15 @@ plugin developed by [DemonWav](https://github.com/demonwav). July 2017 + + 0.6.14 + + + + July 2017 + diff --git a/gradle.properties b/gradle.properties index 0ea7f8b6..57019429 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.6.13 +buildVersion=0.6.14 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 8b5a2faf..aafa5569 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -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."; diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Coerce.java b/src/main/java/org/spongepowered/asm/mixin/injection/Coerce.java index 149c156f..8bdb9bd3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Coerce.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Coerce.java @@ -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. * *

During LVT generation it is not always possible to inflect the exact local * type for types represented internally as integers, for example booleans and diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 8a244da2..82c7e37b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -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 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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java index b85d03fe..b307e1df 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java @@ -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 from can be coerced to to */ 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 from type can be coerced to the to * 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 from type can be coerced to the to + * type. This is effectively a superclass check: the check suceeds if + * to is a subclass of from. + * + * @param from type to coerce from + * @param to type to coerce to + * @return true if from can be coerced to to + */ + private static boolean canCoerce(ClassInfo from, ClassInfo to) { + return from != null && to != null && (to == from || to.hasSuperClass(from)); + } } diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index 5c465243..d60f64a3 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -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++; } }