File size: 3,359 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "mc-0010", "name": "CallbackInfo internals and how it wraps method calls"}
{"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": "Explain CallbackInfo and CallbackInfoReturnable internals. How do they wrap method calls at the bytecode level?"}}
{"type": "message", "message": {"role": "assistant", "content": "`CallbackInfo` and `CallbackInfoReturnable` are generated at bytecode level to wrap the original method call. They carry the method name, descriptor, and return value.\n\n**CallbackInfo** (for void methods):\n\n```java\npublic final class CallbackInfo {\n    private final String name;\n    private final String desc;\n    private final Object owner;\n    private boolean cancelled;\n    \n    public CallbackInfo(String name, String desc, Object owner) {\n        this.name = name;\n        this.desc = desc;\n        this.owner = owner;\n    }\n    \n    public String getId() { return name + desc; }\n    public boolean isCancellable() { return false; }\n    public void cancel() {\n        // Only works if Cancellable interface is implemented\n    }\n}\n```\n\n**CallbackInfoReturnable** extends CallbackInfo for methods with return values:\n\n```java\npublic final class CallbackInfoReturnable<T> extends CallbackInfo {\n    private T returnValue;\n    private boolean hasReturnValue;\n    \n    public T getReturnValue() { return returnValue; }\n    public void setReturnValue(T value) {\n        this.returnValue = value;\n        this.hasReturnValue = true;\n    }\n    \n    // Used by @Inject with cancellable = true\n    public void cancel() {\n        this.hasReturnValue = true;\n    }\n}\n```\n\n**Bytecode Generation** - For a method `int getValue()` with `@Inject(at = @At(\"HEAD\"))`, the mixin generates:\n\n```java\n// Original method:\npublic int getValue() {\n    return this.value + 1;\n}\n\n// After mixin transformation:\npublic int getValue() {\n    CallbackInfoReturnable<Integer> cir = new CallbackInfoReturnable<>(\"getValue\", \"()I\", this);\n    MyMixin.onGetValue(this, cir);\n    if (cir.hasReturnValue()) {\n        return cir.getReturnValue(); // Early return if cancelled\n    }\n    return this.value + 1;\n}\n```\n\nThe actual ASM bytecode for the generated wrapper:\n\n```bytecode\nALOAD 0  // load 'this'\nLDC \"getValue\"\nLDC \"()I\"\nALOAD 0\nINVOKESPECIAL CallbackInfoReturnable.<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V\nASTORE 2  // store cir\nALOAD 0  // load 'this' for mixin method\nALOAD 2  // load cir\nINVOKESTATIC MyMixin.onGetValue(LTargetClass;LCallbackInfoReturnable;)V\nALOAD 2\nINVOKEVIRTUAL CallbackInfoReturnable.hasReturnValue()Z\nIFEQ skip_return\nALOAD 2\nINVOKEVIRTUAL CallbackInfoReturnable.getReturnValue()Ljava/lang/Object;\nCHECKCAST java/lang/Integer\nINVOKEVIRTUAL Integer.intValue()I\nIRETURN\nskip_return:\n// original bytecode follows\n```\n\nThe `cancellable` parameter in `@Inject` controls whether the `CallbackInfo` supports cancellation. When `cancellable = true`, the mixin processor ensures the generated code checks for cancellation after the callback."}}