GetGlobalVariableInstr.java

  1. package org.jruby.ir.instructions;

  2. import org.jruby.ir.IRFlags;
  3. import org.jruby.ir.IRScope;
  4. import org.jruby.ir.IRVisitor;
  5. import org.jruby.ir.Operation;
  6. import org.jruby.ir.operands.GlobalVariable;
  7. import org.jruby.ir.operands.Operand;
  8. import org.jruby.ir.operands.Variable;
  9. import org.jruby.ir.transformations.inlining.CloneInfo;
  10. import org.jruby.parser.StaticScope;
  11. import org.jruby.runtime.DynamicScope;
  12. import org.jruby.runtime.ThreadContext;
  13. import org.jruby.runtime.builtin.IRubyObject;

  14. public class GetGlobalVariableInstr extends GetInstr  implements FixedArityInstr {
  15.     public GetGlobalVariableInstr(Variable dest, String gvarName) {
  16.         this(dest, new GlobalVariable(gvarName));
  17.     }

  18.     public GetGlobalVariableInstr(Variable dest, GlobalVariable gvar) {
  19.         super(Operation.GET_GLOBAL_VAR, dest, gvar, null);
  20.     }

  21.     @Override
  22.     public Operand[] getOperands() {
  23.         return new Operand[] { getSource() };
  24.     }

  25.     public boolean computeScopeFlags(IRScope scope) {
  26.         String name = ((GlobalVariable) getSource()).getName();

  27.         if (name.equals("$_") || name.equals("$~") || name.equals("$`") || name.equals("$'") ||
  28.             name.equals("$+") || name.equals("$LAST_READ_LINE") || name.equals("$LAST_MATCH_INFO") ||
  29.             name.equals("$PREMATCH") || name.equals("$POSTMATCH") || name.equals("$LAST_PAREN_MATCH")) {
  30.             scope.getFlags().add(IRFlags.USES_BACKREF_OR_LASTLINE);
  31.             return true;
  32.         }

  33.         return false;
  34.     }

  35.     @Override
  36.     public Instr clone(CloneInfo ii) {
  37.         return new GetGlobalVariableInstr(ii.getRenamedVariable(getResult()), ((GlobalVariable)getSource()).getName());
  38.     }

  39.     @Override
  40.     public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
  41.         return getSource().retrieve(context, self, currScope, currDynScope, temp);
  42.     }

  43.     @Override
  44.     public void visit(IRVisitor visitor) {
  45.         visitor.GetGlobalVariableInstr(this);
  46.     }
  47. }