File size: 3,596 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "mc-0012", "name": "How LocalCapture captures local variables from target methods"}
{"type": "message", "message": {"role": "system", "content": "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."}}
{"type": "message", "message": {"role": "user", "content": "How does LocalCapture work? How are local variables from the target method captured and passed to mixin callbacks?"}}
{"type": "message", "message": {"role": "assistant", "content": "`LocalCapture` captures the state of local variables at the injection point and passes them to the callback method.\n\n**Usage:**\n\n```java\n@Mixin(MyEntity.class)\npublic class MyEntityMixin {\n    @Inject(\n        method = \"tick\",\n        at = @At(value = \"INVOKE\", target = \"...\", ordinal = 0),\n        locals = LocalCapture.CAPTURE_FAILHARD\n    )\n    private void onTick(CallbackInfo ci, int localHealth, float localSpeed) {\n        // localHealth and localSpeed are captured from the target method\n        System.out.println(\"Health: \" + localHealth + \", Speed: \" + localSpeed);\n    }\n}\n```\n\n**How it works at bytecode level:**\n\n```java\n// Target method bytecode before transformation:\npublic void tick() {\n    int health = this.getHealth();   // local variable at index 1\n    float speed = this.getSpeed();   // local variable at index 2\n    // ... more code\n    this.playSound(...);\n    // ... more code\n}\n```\n\nThe mixin processor reads the target method's `LocalVariableNode` table to determine variable slots:\n\n```java\n// During mixin application:\nMethodNode targetMethod = findTargetMethod(targetNode, \"tick\", \"()V\");\n\n// Analyze local variables at injection point\nList<LocalVariableNode> locals = targetMethod.localVariables;\n\n// For @Inject at INVOKE ordinal=0, find the instruction\nAbstractInsnNode injectPoint = findInjectionPoint(targetMethod, ...);\n\n// Determine which locals are live at this point\nMap<Integer, Type> liveLocals = analyzeLiveness(targetMethod, injectPoint);\n\n// Generate callback with captured locals as parameters\nMethodNode callbackMethod = generateCallback(mixinMethod, liveLocals);\n\n// Insert capture code before the callback invocation\nInsnList capture = new InsnList();\nfor (Map.Entry<Integer, Type> entry : liveLocals.entrySet()) {\n    capture.add(new VarInsnNode(entry.getValue().getOpcode(ILOAD), entry.getKey()));\n}\ninsertBefore(callbackInvocation, capture);\n```\n\n**Generated bytecode:**\n\n```java\n// Before mixin transformation:\npublic void tick() {\n    int health = this.getHealth();\n    float speed = this.getSpeed();\n    this.playSound(...);\n}\n\n// After transformation with LocalCapture:\npublic void tick() {\n    int health = this.getHealth();\n    float speed = this.getSpeed();\n    // CAPTURE START\n    int captured_health = health;   // ILOAD 1\n    float captured_speed = speed;   // FLOAD 2\n    MyMixin.onTick(this, ci, captured_health, captured_speed);\n    // CAPTURE END\n    this.playSound(...);\n}\n```\n\n**Capture modes:**\n- `LocalCapture.NO_CAPTURE` - don't capture (default)\n- `LocalCapture.CAPTURE_FAILHARD` - fail if variables can't be captured\n- `LocalCapture.CAPTURE_FAILSOFT` - try to capture, skip if unable\n\nThe `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."}}