PutFieldInstr.java

  1. package org.jruby.ir.instructions;

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

  11. public class PutFieldInstr extends PutInstr implements FixedArityInstr {
  12.     public PutFieldInstr(Operand obj, String fieldName, Operand value) {
  13.         super(Operation.PUT_FIELD, obj, fieldName, value);
  14.     }

  15.     @Override
  16.     public Instr clone(CloneInfo ii) {
  17.         return new PutFieldInstr(getTarget().cloneForInlining(ii), ref, getValue().cloneForInlining(ii));
  18.     }

  19.     @Override
  20.     public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
  21.         IRubyObject object = (IRubyObject) getTarget().retrieve(context, self, currScope, currDynScope, temp);

  22.         // We store instance variable offsets on the real class, since instance var tables are associated with the
  23.         // natural type of an object.
  24.         RubyClass clazz = object.getMetaClass().getRealClass();

  25.         // FIXME: Should add this as a field for instruction
  26.         clazz.getVariableAccessorForWrite(getRef()).set(object,
  27.                 getValue().retrieve(context, self, currScope, currDynScope, temp));
  28.         return null;
  29.     }

  30.     @Override
  31.     public void visit(IRVisitor visitor) {
  32.         visitor.PutFieldInstr(this);
  33.     }
  34. }