Bignum.java

  1. package org.jruby.ir.operands;

  2. import org.jruby.RubyBignum;
  3. import org.jruby.ir.IRVisitor;
  4. import org.jruby.runtime.ThreadContext;

  5. import java.math.BigInteger;

  6. /**
  7.  * Represents a literal Bignum.
  8.  *
  9.  * We cache the value so that when the same Bignum Operand is copy-propagated
  10.  * across multiple instructions, the same RubyBignum object is created.  In a
  11.  * ddition, the same constant across loops should be the same object.
  12.  *
  13.  * So, in this example, the output should be false, true, true
  14.  * <pre>
  15.  *   n = 0
  16.  *   olda = nil
  17.  *   while (n < 3)
  18.  *     a = 81402749386839761113321
  19.  *     p a.equal?(olda)
  20.  *     olda = a
  21.  *     n += 1
  22.  *   end
  23.  * </pre>
  24.  *
  25.  */
  26. public class Bignum extends ImmutableLiteral {
  27.     final public BigInteger value;

  28.     public Bignum(BigInteger value) {
  29.         super(OperandType.BIGNUM);
  30.         this.value = value;
  31.     }

  32.     @Override
  33.     public Object createCacheObject(ThreadContext context) {
  34.         return RubyBignum.newBignum(context.runtime, value);
  35.     }

  36.     @Override
  37.     public void visit(IRVisitor visitor) {
  38.         visitor.Bignum(this);
  39.     }

  40.     @Override
  41.     public String toString() {
  42.         return "Bignum:" + value;
  43.     }
  44. }