SystemYou 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
Explain CallbackInfo and CallbackInfoReturnable internals. How do they wrap method calls at the bytecode level?
Assistant
CallbackInfo and CallbackInfoReturnable are generated at bytecode level to wrap the original method call. They carry the method name, descriptor, and return value.
CallbackInfo (for void methods):
java
publicfinalclassCallbackInfo {
privatefinal String name;
privatefinal String desc;
privatefinal Object owner;
privateboolean cancelled;
publicCallbackInfo(String name, String desc, Object owner) {
this.name = name;
this.desc = desc;
this.owner = owner;
}
public String getId() { return name + desc; }
publicbooleanisCancellable() { returnfalse; }
publicvoidcancel() {
// Only works if Cancellable interface is implemented
}
}
CallbackInfoReturnable extends CallbackInfo for methods with return values:
java
publicfinalclassCallbackInfoReturnable<T> extendsCallbackInfo {
private T returnValue;
privateboolean hasReturnValue;
public T getReturnValue() { return returnValue; }
publicvoidsetReturnValue(T value) {
this.returnValue = value;
this.hasReturnValue = true;
}
// Used by @Inject with cancellable = truepublicvoidcancel() {
this.hasReturnValue = true;
}
}
Bytecode Generation - For a method int getValue() with @Inject(at = @At("HEAD")), the mixin generates:
java
// Original method:publicintgetValue() {
returnthis.value + 1;
}
// After mixin transformation:publicintgetValue() {
CallbackInfoReturnable<Integer> cir = newCallbackInfoReturnable<>("getValue", "()I", this);
MyMixin.onGetValue(this, cir);
if (cir.hasReturnValue()) {
return cir.getReturnValue(); // Early return if cancelled
}
returnthis.value + 1;
}
The actual ASM bytecode for the generated wrapper:
The 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.