GVarAliasInstr.java

  1. package org.jruby.ir.instructions;

  2. import org.jruby.ir.IRVisitor;
  3. import org.jruby.ir.Operation;
  4. import org.jruby.ir.operands.Operand;
  5. import org.jruby.ir.transformations.inlining.CloneInfo;
  6. import org.jruby.parser.StaticScope;
  7. import org.jruby.runtime.DynamicScope;
  8. import org.jruby.runtime.ThreadContext;
  9. import org.jruby.runtime.builtin.IRubyObject;

  10. import java.util.Map;

  11. public class GVarAliasInstr extends Instr implements FixedArityInstr {
  12.     private Operand newName;
  13.     private Operand oldName;

  14.     public GVarAliasInstr(Operand newName, Operand oldName) {
  15.         super(Operation.GVAR_ALIAS);

  16.         this.newName = newName;
  17.         this.oldName = oldName;
  18.     }

  19.     public Operand getNewName() {
  20.         return newName;
  21.     }

  22.     public Operand getOldName() {
  23.         return oldName;
  24.     }

  25.     @Override
  26.     public String toString() {
  27.         return getOperation().toString() + "(" + newName + ", " + oldName + ")";
  28.     }

  29.     @Override
  30.     public Operand[] getOperands() {
  31.         return new Operand[] { newName, oldName };
  32.     }

  33.     @Override
  34.     public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
  35.         oldName = oldName.getSimplifiedOperand(valueMap, force);
  36.         newName = newName.getSimplifiedOperand(valueMap, force);
  37.     }

  38.     @Override
  39.     public Instr clone(CloneInfo ii) {
  40.         return new GVarAliasInstr(newName.cloneForInlining(ii), oldName.cloneForInlining(ii));
  41.     }

  42.     @Override
  43.     public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
  44.         String newNameString = newName.retrieve(context, self, currScope, currDynScope, temp).toString();
  45.         String oldNameString = oldName.retrieve(context, self, currScope, currDynScope, temp).toString();

  46.         context.runtime.getGlobalVariables().alias(newNameString, oldNameString);
  47.         return null;
  48.     }

  49.     @Override
  50.     public void visit(IRVisitor visitor) {
  51.         visitor.GVarAliasInstr(this);
  52.     }
  53. }