Skip to content

Commit

Permalink
Fix: Resolve @Share initialization issues.
Browse files Browse the repository at this point in the history
Initialize them once per target method and do it after mixin's internal labels.
  • Loading branch information
LlamaLad7 committed May 8, 2023
1 parent bbe739b commit 568546d
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.llamalad7.mixinextras.utils.ASMUtils;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.VarInsnNode;
Expand All @@ -18,8 +19,6 @@
class ShareSugarApplicator extends SugarApplicator {
private final String id;
private Type innerType;
private int localRefIndex;
private boolean needsSetup;

ShareSugarApplicator(InjectionInfo info, SugarParameter parameter) {
super(info, parameter);
Expand All @@ -36,25 +35,32 @@ void validate(Target target, InjectionNodes.InjectionNode node) {

@Override
void prepare(Target target, InjectionNodes.InjectionNode node) {
}

@Override
void inject(Target target, InjectionNodes.InjectionNode node) {
Map<String, Integer> refIndices = TargetDecorations.getOrPut(target, "ShareSugar_LocalRefIndices", HashMap::new);
needsSetup = !refIndices.containsKey(id);
if (needsSetup) {
int localRefIndex;
if (!refIndices.containsKey(id)) {
localRefIndex = target.allocateLocal();
refIndices.put(id, localRefIndex);
target.addLocalVariable(localRefIndex, "sharedRef" + localRefIndex, paramType.getDescriptor());
InsnList init = new InsnList();
LocalRefUtils.generateWrapping(init, innerType, () -> init.add(new InsnNode(ASMUtils.getDummyOpcodeForType(innerType))));
init.add(new VarInsnNode(Opcodes.ASTORE, localRefIndex));
addToStart(target, init);
} else {
localRefIndex = refIndices.get(id);
}
target.insns.insertBefore(node.getCurrentTarget(), new VarInsnNode(Opcodes.ALOAD, localRefIndex));
}

@Override
void inject(Target target, InjectionNodes.InjectionNode node) {
if (needsSetup) {
InsnList init = new InsnList();
LocalRefUtils.generateWrapping(init, innerType, () -> init.add(new InsnNode(ASMUtils.getDummyOpcodeForType(innerType))));
init.add(new VarInsnNode(Opcodes.ASTORE, localRefIndex));
target.insertBefore(target.insns.getFirst(), init);
private void addToStart(Target target, InsnList insns) {
for (AbstractInsnNode existing : target) {
if (existing.getOpcode() != -1) {
target.insns.insertBefore(existing, insns);
return;
}
}
target.insns.insertBefore(node.getCurrentTarget(), new VarInsnNode(Opcodes.ALOAD, localRefIndex));
}
}

0 comments on commit 568546d

Please sign in to comment.