tinycodinglm / mc /mc-0012.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.6 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 LocalCapture work? How are local variables from the target method captured and passed to mixin callbacks?

Assistant

LocalCapture captures the state of local variables at the injection point and passes them to the callback method.

Usage:

java
@Mixin(MyEntity.class)
public class MyEntityMixin {
    @Inject(
        method = "tick",
        at = @At(value = "INVOKE", target = "...", ordinal = 0),
        locals = LocalCapture.CAPTURE_FAILHARD
    )
    private void onTick(CallbackInfo ci, int localHealth, float localSpeed) {
        // localHealth and localSpeed are captured from the target method
        System.out.println("Health: " + localHealth + ", Speed: " + localSpeed);
    }
}

How it works at bytecode level:

java
// Target method bytecode before transformation:
public void tick() {
    int health = this.getHealth();   // local variable at index 1
    float speed = this.getSpeed();   // local variable at index 2
    // ... more code
    this.playSound(...);
    // ... more code
}

The mixin processor reads the target method's LocalVariableNode table to determine variable slots:

java
// During mixin application:
MethodNode targetMethod = findTargetMethod(targetNode, "tick", "()V");

// Analyze local variables at injection point
List<LocalVariableNode> locals = targetMethod.localVariables;

// For @Inject at INVOKE ordinal=0, find the instruction
AbstractInsnNode injectPoint = findInjectionPoint(targetMethod, ...);

// Determine which locals are live at this point
Map<Integer, Type> liveLocals = analyzeLiveness(targetMethod, injectPoint);

// Generate callback with captured locals as parameters
MethodNode callbackMethod = generateCallback(mixinMethod, liveLocals);

// Insert capture code before the callback invocation
InsnList capture = new InsnList();
for (Map.Entry<Integer, Type> entry : liveLocals.entrySet()) {
    capture.add(new VarInsnNode(entry.getValue().getOpcode(ILOAD), entry.getKey()));
}
insertBefore(callbackInvocation, capture);

Generated bytecode:

java
// Before mixin transformation:
public void tick() {
    int health = this.getHealth();
    float speed = this.getSpeed();
    this.playSound(...);
}

// After transformation with LocalCapture:
public void tick() {
    int health = this.getHealth();
    float speed = this.getSpeed();
    // CAPTURE START
    int captured_health = health;   // ILOAD 1
    float captured_speed = speed;   // FLOAD 2
    MyMixin.onTick(this, ci, captured_health, captured_speed);
    // CAPTURE END
    this.playSound(...);
}

Capture modes:

  • LocalCapture.NO_CAPTURE - don't capture (default)
  • LocalCapture.CAPTURE_FAILHARD - fail if variables can't be captured
  • LocalCapture.CAPTURE_FAILSOFT - try to capture, skip if unable

The CAPTURE_FAILHARD mode is recommended because missing locals cause silent bugs. The mixin processor uses ASM's LocalVariablesSorter and frame analysis to determine which locals are in scope at the injection point.