SpongePowered / Mixin

Mixin is a trait/mixin and bytecode weaving framework for Java using ASM
MIT License
1.37k stars 185 forks source link

Allow `@ModifyVariable` to capture locals #649

Open ctrlaltmilk opened 8 months ago

ctrlaltmilk commented 8 months ago

I have a function with the structure

void foo() {
    int a = bar();
    int b = baz();

    if (a == 0) {
        doSomethingNontrivial();
    }
}

I would like to use Mixins to rewrite the function a bit:

void foo() {
    int a = bar();
    int b = baz();

    if (a == 0 || b == 0) {
        baz();
    }
}

To do this, the best option seems to be a @ModifyVariable on the value of a, but to do that I would need to capture the value of b as well. Currently, this is not possible. If the a == 0 was a method call, it would be possible to use an @Inject just before the comparison and set a to 0 if b is 0, but as the comparison becomes an IF_ICMPEQ it's impossible to target for an injection. @ModifyVariable already examines the LVT to determine the accesses it needs to inject bytecode at, so adding another local capture shouldn't make it any more fragile than it already is.

LlamaLad7 commented 8 months ago

Consider using https://github.com/LlamaLad7/MixinExtras/wiki/Local in the meantime.