tinycodinglm / mc /mc-0029.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.78 kB
opencode trace
System You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.

You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.

User

How does @Inject find its target method and injection point? Explain the InjectionPoint resolution mechanism.

Assistant

The @Inject annotation targets a method and an injection point within that method. The resolution process works as follows:

Step 1: Method Resolution
The target method is resolved from the @Mixin target class using the method name and descriptor:

java
// @Inject(method = "tick", at = @At("HEAD"))
// Resolution: find MethodNode with name "tick" and matching descriptor in target class

ClassNode targetNode = ...;
MethodNode targetMethod = null;
for (MethodNode mn : targetNode.methods) {
    if (mn.name.equals("tick") && mn.desc.equals("()V")) {
        targetMethod = mn;
        break;
    }
}

Step 2: InjectionPoint Resolution
The @At annotation specifies where to inject. The InjectionPoint class hierarchy resolves this:

java
public abstract class InjectionPoint {
    public abstract boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes);
    
    // HEAD - first instruction
    public static class AtHead extends InjectionPoint {
        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {
            nodes.add(insns.getFirst());
            return true;
        }
    }
    
    // RETURN - all return instructions
    public static class AtReturn extends InjectionPoint {
        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {
            for (AbstractInsnNode insn : insns) {
                if (insn instanceof InsnNode && isReturnOpcode(insn.getOpcode())) {
                    nodes.add(insn);
                }
            }
            return !nodes.isEmpty();
        }
    }
    
    // INVOKE - specific method invocation
    public static class AtInvoke extends InjectionPoint {
        private final String targetMethod;
        
        public boolean find(String desc, InsnList insns, List<AbstractInsnNode> nodes) {
            for (AbstractInsnNode insn : insns) {
                if (insn instanceof MethodInsnNode) {
                    MethodInsnNode min = (MethodInsnNode) insn;
                    if (min.name.equals(targetMethod)) {
                        nodes.add(min);
                    }
                }
            }
            return !nodes.isEmpty();
        }
    }
}

Step 3: Injectivity and Slice
The @At can be combined with @Slice to narrow injection points:

java
// Inject before the FIRST call to "playSound" in the method
@Inject(
    method = "tick",
    at = @At(
        value = "INVOKE",
        target = "Lnet/minecraft/world/World;playSound(...)V",
        ordinal = 0
    )
)

// With slice - only inject in the first half of the method
@Inject(
    method = "tick",
    at = @At(
        value = "INVOKE",
        target = "Lnet/minecraft/world/World;playSound(...)V"
    ),
    slice = @Slice(
        from = @At(value = "HEAD"),
        to = @At(value = "INVOKE", target = "...", ordinal = 2)
    )
)

The ordinal parameter selects which occurrence (0-indexed) of the matched instruction to inject at. Without it, all occurrences are injection targets.