tinycodinglm / mc /mc-0010.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
3.36 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

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
public final class CallbackInfo {
    private final String name;
    private final String desc;
    private final Object owner;
    private boolean cancelled;
    
    public CallbackInfo(String name, String desc, Object owner) {
        this.name = name;
        this.desc = desc;
        this.owner = owner;
    }
    
    public String getId() { return name + desc; }
    public boolean isCancellable() { return false; }
    public void cancel() {
        // Only works if Cancellable interface is implemented
    }
}

CallbackInfoReturnable extends CallbackInfo for methods with return values:

java
public final class CallbackInfoReturnable<T> extends CallbackInfo {
    private T returnValue;
    private boolean hasReturnValue;
    
    public T getReturnValue() { return returnValue; }
    public void setReturnValue(T value) {
        this.returnValue = value;
        this.hasReturnValue = true;
    }
    
    // Used by @Inject with cancellable = true
    public void cancel() {
        this.hasReturnValue = true;
    }
}

Bytecode Generation - For a method int getValue() with @Inject(at = @At("HEAD")), the mixin generates:

java
// Original method:
public int getValue() {
    return this.value + 1;
}

// After mixin transformation:
public int getValue() {
    CallbackInfoReturnable<Integer> cir = new CallbackInfoReturnable<>("getValue", "()I", this);
    MyMixin.onGetValue(this, cir);
    if (cir.hasReturnValue()) {
        return cir.getReturnValue(); // Early return if cancelled
    }
    return this.value + 1;
}

The actual ASM bytecode for the generated wrapper:

bytecode
ALOAD 0  // load 'this'
LDC "getValue"
LDC "()I"
ALOAD 0
INVOKESPECIAL CallbackInfoReturnable.<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V
ASTORE 2  // store cir
ALOAD 0  // load 'this' for mixin method
ALOAD 2  // load cir
INVOKESTATIC MyMixin.onGetValue(LTargetClass;LCallbackInfoReturnable;)V
ALOAD 2
INVOKEVIRTUAL CallbackInfoReturnable.hasReturnValue()Z
IFEQ skip_return
ALOAD 2
INVOKEVIRTUAL CallbackInfoReturnable.getReturnValue()Ljava/lang/Object;
CHECKCAST java/lang/Integer
INVOKEVIRTUAL Integer.intValue()I
IRETURN
skip_return:
// original bytecode follows

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.