private static void fixnumStep( ThreadContext context, Ruby runtime, long from, long to, long step, Block block) { // We must avoid integer overflows in "i += step". if (step >= 0) { long tov = Long.MAX_VALUE - step; if (to < tov) tov = to; long i; for (i = from; i <= tov; i += step) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } if (i <= to) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } } else { long tov = Long.MIN_VALUE - step; if (to > tov) tov = to; long i; for (i = from; i >= tov; i += step) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } if (i >= to) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } } }
@JRubyMethod(name = "to_r") public IRubyObject to_r(ThreadContext context) { long[] exp = new long[1]; double f = frexp(value, exp); f = ldexp(f, DBL_MANT_DIG); long n = exp[0] - DBL_MANT_DIG; Ruby runtime = context.runtime; IRubyObject rf = RubyNumeric.dbl2num(runtime, f); IRubyObject rn = RubyFixnum.newFixnum(runtime, n); return f_mul(context, rf, f_expt(context, RubyFixnum.newFixnum(runtime, FLT_RADIX), rn)); }
public WarningGlobalVariable( Ruby runtime, String name, RubyInstanceConfig.Verbosity verbosity) { super( runtime, name, verbosity == RubyInstanceConfig.Verbosity.NIL ? RubyFixnum.newFixnum(runtime, 0) : verbosity == RubyInstanceConfig.Verbosity.FALSE ? RubyFixnum.newFixnum(runtime, 1) : verbosity == RubyInstanceConfig.Verbosity.TRUE ? RubyFixnum.newFixnum(runtime, 2) : runtime.getNil()); }
public static RubyClass createFloatClass(Ruby runtime) { RubyClass floatc = runtime.defineClass( "Float", runtime.getNumeric(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); runtime.setFloat(floatc); floatc.setClassIndex(ClassIndex.FLOAT); floatc.setReifiedClass(RubyFloat.class); floatc.kindOf = new RubyModule.JavaClassKindOf(RubyFloat.class); floatc.getSingletonClass().undefineMethod("new"); // Java Doubles are 64 bit long: floatc.defineConstant("ROUNDS", RubyFixnum.newFixnum(runtime, ROUNDS)); floatc.defineConstant("RADIX", RubyFixnum.newFixnum(runtime, RADIX)); floatc.defineConstant("MANT_DIG", RubyFixnum.newFixnum(runtime, MANT_DIG)); floatc.defineConstant("DIG", RubyFixnum.newFixnum(runtime, DIG)); // Double.MAX_EXPONENT since Java 1.6 floatc.defineConstant("MIN_EXP", RubyFixnum.newFixnum(runtime, MIN_EXP)); // Double.MAX_EXPONENT since Java 1.6 floatc.defineConstant("MAX_EXP", RubyFixnum.newFixnum(runtime, MAX_EXP)); floatc.defineConstant("MIN_10_EXP", RubyFixnum.newFixnum(runtime, MIN_10_EXP)); floatc.defineConstant("MAX_10_EXP", RubyFixnum.newFixnum(runtime, MAX_10_EXP)); floatc.defineConstant("MIN", RubyFloat.newFloat(runtime, Double.MIN_VALUE)); floatc.defineConstant("MAX", RubyFloat.newFloat(runtime, Double.MAX_VALUE)); floatc.defineConstant("EPSILON", RubyFloat.newFloat(runtime, EPSILON)); floatc.defineConstant("INFINITY", RubyFloat.newFloat(runtime, INFINITY)); floatc.defineConstant("NAN", RubyFloat.newFloat(runtime, NAN)); floatc.defineAnnotatedMethods(RubyFloat.class); return floatc; }
/** match_end */ @JRubyMethod(name = "end", compat = CompatVersion.RUBY1_8) public IRubyObject end(ThreadContext context, IRubyObject index) { int i = RubyNumeric.num2int(index); Ruby runtime = context.getRuntime(); int e = endCommon(runtime, i); return e < 0 ? runtime.getNil() : RubyFixnum.newFixnum(runtime, e); }
@JRubyMethod(name = "round", compat = CompatVersion.RUBY1_9) public IRubyObject round19(ThreadContext context, IRubyObject arg) { int ndigits = RubyNumeric.num2int(arg); if (ndigits > 0) return RubyKernel.new_float(this, this); if (ndigits == 0) return this; ndigits = -ndigits; Ruby runtime = context.getRuntime(); if (ndigits < 0) throw runtime.newArgumentError("ndigits out of range"); IRubyObject f = Numeric.int_pow(context, 10, ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = ((RubyFixnum) this).getLongValue(); long y = ((RubyFixnum) f).getLongValue(); boolean neg = x < 0; if (neg) x = -x; x = (x + y / 2) / y * y; if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } else { IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime)); IRubyObject r = callMethod(context, "%", f); IRubyObject n = callMethod(context, "-", r); if (!r.callMethod(context, "<", h).isTrue()) n = n.callMethod(context, "+", f); return n; } }
private static IRubyObject convertToNum(double val, Ruby runtime) { if (val >= (double) RubyFixnum.MAX || val < (double) RubyFixnum.MIN) { return RubyBignum.newBignum(runtime, val); } return RubyFixnum.newFixnum(runtime, (long) val); }
@JRubyMethod(name = "round", compat = CompatVersion.RUBY1_9) public IRubyObject round19(ThreadContext context, IRubyObject arg) { int ndigits = RubyNumeric.num2int(arg); if (ndigits > 0) return RubyKernel.new_float(this, this); if (ndigits == 0) return this; Ruby runtime = context.runtime; long bytes = (this instanceof RubyFixnum) ? 8 : RubyFixnum.fix2long(callMethod("size")); /* If 10**N/2 > this, return 0 */ /* We have log_256(10) > 0.415241 and log_256(1/2)=-0.125 */ if (-0.415241 * ndigits - 0.125 > bytes) { return RubyFixnum.zero(runtime); } IRubyObject f = Numeric.int_pow(context, 10, -ndigits); if (this instanceof RubyFixnum && f instanceof RubyFixnum) { long x = ((RubyFixnum) this).getLongValue(); long y = ((RubyFixnum) f).getLongValue(); boolean neg = x < 0; if (neg) x = -x; x = (x + y / 2) / y * y; if (neg) x = -x; return RubyFixnum.newFixnum(runtime, x); } else if (f instanceof RubyFloat) { return RubyFixnum.zero(runtime); } else { IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime)); IRubyObject r = callMethod(context, "%", f); IRubyObject n = callMethod(context, "-", r); String op = callMethod(context, "<", RubyFixnum.zero(runtime)).isTrue() ? "<=" : "<"; if (!r.callMethod(context, op, h).isTrue()) n = n.callMethod(context, "+", f); return n; } }
private void fixnumStep(ThreadContext context, Ruby runtime, long unit, Block block) { long e = ((RubyFixnum) end).getLongValue(); if (!isExclusive) e++; for (long i = ((RubyFixnum) begin).getLongValue(); i < e; i += unit) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } }
/** flo_is_infinite_p */ @JRubyMethod(name = "infinite?") public IRubyObject infinite_p() { if (Double.isInfinite(value)) { return RubyFixnum.newFixnum(getRuntime(), value < 0 ? -1 : 1); } return getRuntime().getNil(); }
@Override public RubyFixnum id() { if ((flags & FALSE_F) == 0) { return RubyFixnum.newFixnum(getRuntime(), 2); } else { return RubyFixnum.zero(getRuntime()); } }
/** int_succ */ @JRubyMethod(name = {"succ", "next"}) public IRubyObject succ(ThreadContext context) { if (this instanceof RubyFixnum) { return RubyFixnum.newFixnum(context.getRuntime(), getLongValue() + 1L); } else { return callMethod(context, "+", RubyFixnum.one(context.getRuntime())); } }
/** rb_num2fix */ public static IRubyObject num2fix(IRubyObject val) { if (val instanceof RubyFixnum) { return val; } if (val instanceof RubyBignum) { // any BigInteger is bigger than Fixnum and we don't have FIXABLE throw val.getRuntime().newRangeError("integer " + val + " out of range of fixnum"); } return RubyFixnum.newFixnum(val.getRuntime(), num2long(val)); }
void call(ThreadContext context, IRubyObject arg) { if (iter instanceof RubyFixnum) { iter = RubyFixnum.newFixnum(context.getRuntime(), ((RubyFixnum) iter).getLongValue() - 1); } else { iter = iter.callMethod(context, "-", RubyFixnum.one(context.getRuntime())); } if (iter == RubyFixnum.zero(context.getRuntime())) { block.yield(context, arg); iter = step; } }
private IRubyObject offsetCommon(ThreadContext context, int i, boolean is_19) { check(); Ruby runtime = context.getRuntime(); if (i < 0 || (regs == null ? 1 : regs.numRegs) <= i) throw runtime.newIndexError("index " + i + " out of matches"); int b, e; if (regs == null) { b = begin; e = end; } else { b = regs.beg[i]; e = regs.end[i]; } if (b < 0) return runtime.newArray(runtime.getNil(), runtime.getNil()); if (is_19 && !str.singleByteOptimizable()) { updateCharOffset(); b = charOffsets.beg[i]; e = charOffsets.end[i]; } return runtime.newArray(RubyFixnum.newFixnum(runtime, b), RubyFixnum.newFixnum(runtime, e)); }
@JRubyMethod(name = "begin") public IRubyObject begin19(ThreadContext context, IRubyObject index) { int i = backrefNumber(index); Ruby runtime = context.runtime; int b = beginCommon(runtime, i); if (b < 0) return runtime.getNil(); if (!str.singleByteOptimizable()) { updateCharOffset(); b = charOffsets.beg[i]; } return RubyFixnum.newFixnum(runtime, b); }
private void numericStep19(ThreadContext context, Ruby runtime, IRubyObject step, Block block) { final String method = isExclusive ? "<" : "<="; IRubyObject beg = begin; long i = 0; while (beg.callMethod(context, method, end).isTrue()) { block.yield(context, beg); i++; beg = begin.callMethod( context, "+", RubyFixnum.newFixnum(runtime, i).callMethod(context, "*", step)); } }
@JRubyMethod(name = "end", compat = CompatVersion.RUBY1_9) public IRubyObject end19(ThreadContext context, IRubyObject index) { int i = backrefNumber(index); Ruby runtime = context.getRuntime(); int e = endCommon(runtime, i); if (e < 0) return runtime.getNil(); if (!str.singleByteOptimizable()) { updateCharOffset(); e = charOffsets.end[i]; } return RubyFixnum.newFixnum(runtime, e); }
@Override public IRubyObject set(IRubyObject value) { // int level = RubyNumeric.fix2int(value); // if (level < runtime.getSafeLevel()) { // throw runtime.newSecurityError("tried to downgrade safe level from " + // runtime.getSafeLevel() + " to " + level); // } // runtime.setSafeLevel(level); // thread.setSafeLevel(level); runtime.getWarnings().warn(ID.SAFE_NOT_SUPPORTED, "SAFE levels are not supported in JRuby"); return RubyFixnum.newFixnum(runtime, runtime.getSafeLevel()); }
private static void duckStep( ThreadContext context, Ruby runtime, IRubyObject from, IRubyObject to, IRubyObject step, Block block) { IRubyObject i = from; String cmpString = step.callMethod(context, ">", RubyFixnum.newFixnum(context.runtime, 0)).isTrue() ? ">" : "<"; if (step.callMethod(context, "==", RubyFixnum.newFixnum(context.runtime, 0)).isTrue()) cmpString = "=="; while (true) { if (i.callMethod(context, cmpString, to).isTrue()) break; block.yield(context, i); i = i.callMethod(context, "+", step); } }
private static void fixnumDownto(ThreadContext context, long from, long to, Block block) { // We must avoid "i--" integer overflow when (to == Long.MIN_VALUE). Ruby runtime = context.runtime; if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) { IRubyObject nil = runtime.getNil(); long i; for (i = from; i > to; i--) { block.yield(context, nil); } if (i >= to) { block.yield(context, nil); } } else { long i; for (i = from; i > to; i--) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } if (i >= to) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } } }
private static void fixnumDownto(ThreadContext context, long from, long to, Block block) { Ruby runtime = context.getRuntime(); if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) { final IRubyObject nil = runtime.getNil(); for (long i = from; i >= to; i--) { block.yield(context, nil); } } else { for (long i = from; i >= to; i--) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } } }
private void fixnumEach(ThreadContext context, Ruby runtime, Block block) { long lim = ((RubyFixnum) end).getLongValue(); if (!isExclusive) lim++; if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) { final IRubyObject nil = runtime.getNil(); for (long i = ((RubyFixnum) begin).getLongValue(); i < lim; i++) { block.yield(context, nil); } } else { for (long i = ((RubyFixnum) begin).getLongValue(); i < lim; i++) { block.yield(context, RubyFixnum.newFixnum(runtime, i)); } } }
/** float_rationalize */ @JRubyMethod(name = "rationalize", optional = 1) public IRubyObject rationalize(ThreadContext context, IRubyObject[] args) { if (f_negative_p(context, this)) return f_negate(context, ((RubyFloat) f_abs(context, this)).rationalize(context, args)); Ruby runtime = context.runtime; RubyFixnum one = RubyFixnum.one(runtime); RubyFixnum two = RubyFixnum.two(runtime); IRubyObject eps, a, b; if (args.length != 0) { eps = f_abs(context, args[0]); a = f_sub(context, this, eps); b = f_add(context, this, eps); } else { long[] exp = new long[1]; double f = frexp(value, exp); f = ldexp(f, DBL_MANT_DIG); long n = exp[0] - DBL_MANT_DIG; IRubyObject rf = RubyNumeric.dbl2num(runtime, f); IRubyObject rn = RubyFixnum.newFixnum(runtime, n); if (f_zero_p(context, rf) || !(f_negative_p(context, rn) || f_zero_p(context, rn))) return RubyRational.newRationalRaw(runtime, f_lshift(context, rf, rn)); a = RubyRational.newRationalRaw( runtime, f_sub(context, f_mul(context, two, rf), one), f_lshift(context, one, f_sub(context, one, rn))); b = RubyRational.newRationalRaw( runtime, f_add(context, f_mul(context, two, rf), one), f_lshift(context, one, f_sub(context, one, rn))); } if (invokedynamic(context, a, OP_EQUAL, b).isTrue()) return f_to_r(context, this); IRubyObject[] ary = new IRubyObject[2]; ary[0] = a; ary[1] = b; IRubyObject[] ans = nurat_rationalize_internal(context, ary); return RubyRational.newRationalRaw(runtime, ans[0], ans[1]); }
@JRubyMethod(name = "priority=", required = 1) public IRubyObject priority_set(IRubyObject priority) { // FIXME: This should probably do some translation from Ruby priority levels to Java priority // levels (until we have green threads) int iPriority = RubyNumeric.fix2int(priority); if (iPriority < Thread.MIN_PRIORITY) { iPriority = Thread.MIN_PRIORITY; } else if (iPriority > Thread.MAX_PRIORITY) { iPriority = Thread.MAX_PRIORITY; } if (threadImpl.isAlive()) { threadImpl.setPriority(iPriority); } return RubyFixnum.newFixnum(getRuntime(), iPriority); }
@JRubyMethod(name = "max", frame = true, compat = CompatVersion.RUBY1_9) public IRubyObject max(ThreadContext context, Block block) { if (block.isGiven() || isExclusive && !(end instanceof RubyNumeric)) { return RuntimeHelpers.invokeSuper(context, this, block); } else { int c = RubyComparable.cmpint(context, begin.callMethod(context, "<=>", end), begin, end); Ruby runtime = context.getRuntime(); if (isExclusive) { if (!(end instanceof RubyInteger)) throw runtime.newTypeError("cannot exclude non Integer end value"); if (c == 0) return runtime.getNil(); if (end instanceof RubyFixnum) return RubyFixnum.newFixnum(runtime, ((RubyFixnum) end).getLongValue() - 1); return end.callMethod(context, "-", RubyFixnum.one(runtime)); } return end; } }
@JRubyMethod(name = "priority=", required = 1) public IRubyObject priority_set(IRubyObject priority) { int iPriority = RubyNumeric.fix2int(priority); if (iPriority < RUBY_MIN_THREAD_PRIORITY) { iPriority = RUBY_MIN_THREAD_PRIORITY; } else if (iPriority > RUBY_MAX_THREAD_PRIORITY) { iPriority = RUBY_MAX_THREAD_PRIORITY; } if (threadImpl.isAlive()) { int jPriority = rubyPriorityToJavaPriority(iPriority); if (jPriority < Thread.MIN_PRIORITY) { jPriority = Thread.MIN_PRIORITY; } else if (jPriority > Thread.MAX_PRIORITY) { jPriority = Thread.MAX_PRIORITY; } threadImpl.setPriority(jPriority); } return RubyFixnum.newFixnum(getRuntime(), iPriority); }
@JRubyMethod(name = "to_a", frame = true) public IRubyObject to_a(ThreadContext context, final Block block) { final Ruby runtime = context.getRuntime(); if (begin instanceof RubyFixnum && end instanceof RubyFixnum) { long lim = ((RubyFixnum) end).getLongValue(); if (!isExclusive) lim++; long base = ((RubyFixnum) begin).getLongValue(); long size = lim - base; if (size > Integer.MAX_VALUE) { throw runtime.newRangeError("Range size too large for to_a"); } IRubyObject[] array = new IRubyObject[(int) size]; for (int i = 0; i < size; i++) { array[i] = RubyFixnum.newFixnum(runtime, base + i); } return RubyArray.newArrayNoCopy(runtime, array); } else { return RubyEnumerable.to_a(context, this); } }
/** match_size */ @JRubyMethod(name = {"size", "length"}) public IRubyObject size(ThreadContext context) { check(); Ruby runtime = context.getRuntime(); return regs == null ? RubyFixnum.one(runtime) : RubyFixnum.newFixnum(runtime, regs.numRegs); }
@JRubyMethod(name = "size", meta = true, visibility = PUBLIC) public static IRubyObject size(ThreadContext context, IRubyObject recv) { return RubyFixnum.newFixnum( context.getRuntime(), Factory.getInstance().sizeOf(NativeType.POINTER)); }