| Method from org.jruby.RubyFixnum Detail: |
public IRubyObject abs() {
if (value < 0) {
// A gotcha for Long.MIN_VALUE: value = -value
if (value == Long.MIN_VALUE) {
return RubyBignum.newBignum(
getRuntime(), BigInteger.valueOf(value).negate());
}
return RubyFixnum.newFixnum(getRuntime(), -value);
}
return this;
}
|
public IRubyObject as(Class javaClass) {
return MiniJava.javaToRuby(getRuntime(), coerceToJavaType(getRuntime(), this, javaClass));
}
|
public String asJavaString() {
getRuntime().getWarnings().warn(ID.FIXNUMS_NOT_SYMBOLS, "do not use Fixnums as Symbols");
// FIXME: I think this chunk is equivalent to MRI id2name (and not our public method
// id2name). Make into method if used more than once.
RubySymbol symbol = RubySymbol.getSymbolLong(getRuntime(), value);
if (symbol == null) {
throw getRuntime().newArgumentError("" + value + " is not a symbol");
}
return symbol.asJavaString();
}
|
public static RubyClass createFixnumClass(Ruby runtime) {
RubyClass fixnum = runtime.defineClass("Fixnum", runtime.getInteger(),
ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.setFixnum(fixnum);
fixnum.index = ClassIndex.FIXNUM;
fixnum.kindOf = new RubyModule.KindOf() {
@Override
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyFixnum;
}
};
fixnum.includeModule(runtime.getPrecision());
fixnum.defineAnnotatedMethods(RubyFixnum.class);
for (int i = 0; i < runtime.fixnumCache.length; i++) {
runtime.fixnumCache[i] = new RubyFixnum(runtime, fixnum, i - 128);
}
return fixnum;
}
|
public IRubyObject div_div(ThreadContext context,
IRubyObject other) {
return idiv(context, other, "div");
}
fix_div
here is terrible MRI gotcha:
1.div 3.0 -> 0
1 / 3.0 -> 0.3333333333333333
MRI is also able to do it in one place by looking at current frame in rb_num_coerce_bin:
rb_funcall(x, ruby_frame->orig_func, 1, y);
also note that RubyFloat doesn't override Numeric.div |
public IRubyObject divmod(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
long x = value;
long y = ((RubyFixnum) other).value;
final Ruby runtime = context.getRuntime();
if (y == 0) {
throw runtime.newZeroDivisionError();
}
long div = x / y;
long mod = x % y;
if (mod < 0 && y > 0 || mod > 0 && y < 0) {
div -= 1;
mod += y;
}
IRubyObject fixDiv = RubyFixnum.newFixnum(runtime, div);
IRubyObject fixMod = RubyFixnum.newFixnum(runtime, mod);
return RubyArray.newArray(runtime, fixDiv, fixMod);
}
return coerceBin(context, "divmod", other);
}
|
public final boolean eql(IRubyObject other) {
return other instanceof RubyFixnum && value == ((RubyFixnum)other).value;
}
short circuit for Fixnum key comparison |
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other instanceof RubyFixnum) {
RubyFixnum num = (RubyFixnum)other;
if (num.value == value) {
return true;
}
}
return false;
}
|
public RubyBoolean even_p() {
if(value%2 == 0) {
return getRuntime().getTrue();
}
return getRuntime().getFalse();
}
|
public static RubyFixnum five(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET + 5];
}
|
public static RubyFixnum four(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET + 4];
}
|
public IRubyObject freeze(ThreadContext context) {
return this;
}
|
public double getDoubleValue() {
return value;
}
|
public Class getJavaClass() {
return Long.TYPE;
}
|
public long getLongValue() {
return value;
}
|
public int getNativeTypeIndex() {
return ClassIndex.FIXNUM;
}
|
public RubyClass getSingletonClass() {
throw getRuntime().newTypeError("can't define singleton");
}
|
public RubyFixnum hash() {
return newFixnum(hashCode());
}
|
public final int hashCode() {
return (int)(value ^ value > > > 32);
}
|
public IRubyObject id() {
if (value < = Long.MAX_VALUE / 2 && value >= Long.MIN_VALUE / 2) {
return newFixnum(2 * value + 1);
}
return super.id();
}
|
public IRubyObject id2name() {
RubySymbol symbol = RubySymbol.getSymbolLong(getRuntime(), value);
if (symbol != null) return getRuntime().newString(symbol.asJavaString());
return getRuntime().getNil();
}
|
public IRubyObject idiv(ThreadContext context,
IRubyObject other,
String method) {
if (other instanceof RubyFixnum) {
long x = value;
long y = ((RubyFixnum) other).value;
if (y == 0) {
throw context.getRuntime().newZeroDivisionError();
}
long div = x / y;
long mod = x % y;
if (mod < 0 && y > 0 || mod > 0 && y < 0) {
div -= 1;
}
return context.getRuntime().newFixnum(div);
}
return coerceBin(context, method, other);
}
|
public static IRubyObject induced_from(IRubyObject recv,
IRubyObject other) {
return RubyNumeric.num2fix(other);
}
|
public boolean isImmediate() {
return true;
}
|
public static RubyFixnum minus_one(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET - 1];
}
|
public RubyFixnum newFixnum(long newValue) {
return newFixnum(getRuntime(), newValue);
}
|
public static RubyFixnum newFixnum(Ruby runtime,
long value) {
if (isInCacheRange(value)) {
return runtime.fixnumCache[(int) value + CACHE_OFFSET];
}
return new RubyFixnum(runtime, value);
}
|
public RubyBoolean odd_p() {
if(value%2 != 0) {
return getRuntime().getTrue();
}
return getRuntime().getFalse();
}
|
public static RubyFixnum one(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET + 1];
}
|
public IRubyObject op_and(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum || (other = fixCoerce(other)) instanceof RubyFixnum) {
return newFixnum(context.getRuntime(), value & ((RubyFixnum) other).value);
}
return ((RubyBignum) other).op_and(context, this);
}
|
public IRubyObject op_aref(IRubyObject other) {
if(!(other instanceof RubyFixnum) && !((other = fixCoerce(other)) instanceof RubyFixnum)) {
RubyBignum big = (RubyBignum) other;
RubyObject tryFix = RubyBignum.bignorm(getRuntime(), big.getValue());
if (!(tryFix instanceof RubyFixnum)) {
return big.getValue().signum() == 0 || value >= 0 ? RubyFixnum.zero(getRuntime()) : RubyFixnum.one(getRuntime());
}
}
long otherValue = fix2long(other);
if (otherValue < 0) return RubyFixnum.zero(getRuntime());
if (BIT_SIZE - 1 < otherValue) {
return value < 0 ? RubyFixnum.one(getRuntime()) : RubyFixnum.zero(getRuntime());
}
return (value & (1L < < otherValue)) == 0 ? RubyFixnum.zero(getRuntime()) : RubyFixnum.one(getRuntime());
}
|
public IRubyObject op_cmp(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return compareFixnum(context, (RubyFixnum)other);
}
return coerceCmp(context, "< = >", other);
}
|
public IRubyObject op_div(ThreadContext context,
IRubyObject other) {
return idiv(context, other, "/");
}
|
public IRubyObject op_equal(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyBoolean.newBoolean(context.getRuntime(), value == ((RubyFixnum) other).value);
}
return super.op_num_equal(context, other);
}
|
public IRubyObject op_ge(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyBoolean.newBoolean(context.getRuntime(), value >= ((RubyFixnum) other).value);
}
return coerceRelOp(context, " >=", other);
}
|
public IRubyObject op_gt(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyBoolean.newBoolean(context.getRuntime(), value > ((RubyFixnum) other).value);
}
return coerceRelOp(context, " >", other);
}
|
public IRubyObject op_le(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyBoolean.newBoolean(context.getRuntime(), value < = ((RubyFixnum) other).value);
}
return coerceRelOp(context, "< =", other);
}
|
public IRubyObject op_lshift(IRubyObject other) {
if (!(other instanceof RubyFixnum)) return RubyBignum.newBignum(getRuntime(), value).op_lshift(other);
long width = ((RubyFixnum)other).getLongValue();
return width < 0 ? rshift(-width) : lshift(width);
}
|
public IRubyObject op_lt(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyBoolean.newBoolean(context.getRuntime(), value < ((RubyFixnum) other).value);
}
return coerceRelOp(context, "< ", other);
}
|
public IRubyObject op_minus(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return subtractFixnum(context, (RubyFixnum)other);
}
return subtractOther(context, other);
}
|
public IRubyObject op_mod(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
// Java / and % are not the same as ruby
long x = value;
long y = ((RubyFixnum) other).value;
if (y == 0) {
throw context.getRuntime().newZeroDivisionError();
}
long mod = x % y;
if (mod < 0 && y > 0 || mod > 0 && y < 0) {
mod += y;
}
return context.getRuntime().newFixnum(mod);
}
return coerceBin(context, "%", other);
}
|
public IRubyObject op_mul(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
long otherValue = ((RubyFixnum) other).value;
if (value == 0) {
return RubyFixnum.zero(context.getRuntime());
}
long result = value * otherValue;
IRubyObject r = newFixnum(context.getRuntime(),result);
if(RubyNumeric.fix2long(r) != result || result/value != otherValue) {
return (RubyNumeric) RubyBignum.newBignum(context.getRuntime(), value).op_mul(context, other);
}
return r;
} else if (other instanceof RubyBignum) {
return ((RubyBignum) other).op_mul(context, this);
} else if (other instanceof RubyFloat) {
return context.getRuntime().newFloat((double) value * ((RubyFloat) other).getDoubleValue());
}
return coerceBin(context, "*", other);
}
|
public IRubyObject op_neg() {
return newFixnum(~value);
}
|
public IRubyObject op_or(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum || (other = fixCoerce(other)) instanceof RubyFixnum) {
return newFixnum(context.getRuntime(), value | ((RubyFixnum) other).value);
}
return ((RubyBignum) other).op_or(context, this);
}
|
public IRubyObject op_plus(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return addFixnum(context, (RubyFixnum)other);
}
return addOther(context, other);
}
|
public IRubyObject op_pow(ThreadContext context,
IRubyObject other) {
if(other instanceof RubyFixnum) {
long b = ((RubyFixnum) other).value;
if (b == 0) {
return RubyFixnum.one(context.getRuntime());
}
if (b == 1) {
return this;
}
if (b > 0) {
return RubyBignum.newBignum(context.getRuntime(), value).op_pow(context, other);
}
return RubyFloat.newFloat(context.getRuntime(), Math.pow(value, b));
} else if (other instanceof RubyFloat) {
return RubyFloat.newFloat(context.getRuntime(), Math.pow(value, ((RubyFloat) other)
.getDoubleValue()));
}
return coerceBin(context, "**", other);
}
|
public IRubyObject op_rshift(IRubyObject other) {
if (!(other instanceof RubyFixnum)) return RubyBignum.newBignum(getRuntime(), value).op_rshift(other);
long width = ((RubyFixnum)other).getLongValue();
if (width == 0) return this;
return width < 0 ? lshift(-width) : rshift(width);
}
|
public IRubyObject op_uminus() {
if (value == MIN) { // a gotcha
return RubyBignum.newBignum(getRuntime(), BigInteger.valueOf(value).negate());
}
return RubyFixnum.newFixnum(getRuntime(), -value);
}
|
public IRubyObject op_xor(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum || (other = fixCoerce(other)) instanceof RubyFixnum) {
return newFixnum(context.getRuntime(), value ^ ((RubyFixnum) other).value);
}
return ((RubyBignum) other).op_xor(context, this);
}
|
public IRubyObject pred() {
return getRuntime().newFixnum(value-1);
}
|
public IRubyObject quo(ThreadContext context,
IRubyObject other) {
if (other instanceof RubyFixnum) {
return RubyFloat.newFloat(context.getRuntime(), (double) value / (double) ((RubyFixnum) other).value);
}
return coerceBin(context, "quo", other);
}
|
public IRubyObject size() {
return newFixnum((long) ((BIT_SIZE + 7) / 8));
}
|
public IRubyObject taint(ThreadContext context) {
return this;
}
|
public static RubyFixnum three(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET + 3];
}
|
public IRubyObject to_f() {
return RubyFloat.newFloat(getRuntime(), (double) value);
}
|
public IRubyObject to_java() {
return MiniJava.javaToRuby(getRuntime(), Long.valueOf(value));
}
|
public RubyString to_s() {
int base = 10;
return getRuntime().newString(Convert.longToByteList(value, base));
}
|
public RubyString to_s(IRubyObject[] args) {
switch (args.length) {
case 0: return to_s();
case 1: return to_s(args[0]);
default: throw getRuntime().newArgumentError(args.length, 1);
}
}
|
public RubyString to_s(IRubyObject arg0) {
int base = num2int(arg0);
if (base < 2 || base > 36) {
throw getRuntime().newArgumentError("illegal radix " + base);
}
return getRuntime().newString(Convert.longToByteList(value, base));
}
|
public IRubyObject to_sym() {
RubySymbol symbol = RubySymbol.getSymbolLong(getRuntime(), value);
return symbol != null ? symbol : getRuntime().getNil();
}
|
public static RubyFixnum two(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET + 2];
}
|
public static RubyFixnum unmarshalFrom(UnmarshalStream input) throws IOException {
return input.getRuntime().newFixnum(input.unmarshalInt());
}
|
public static RubyFixnum zero(Ruby runtime) {
return runtime.fixnumCache[CACHE_OFFSET];
}
|
public IRubyObject zero_p() {
return RubyBoolean.newBoolean(getRuntime(), value == 0);
}
|