diff --git a/core/src/main/java/org/jruby/AbstractRubyMethod.java b/core/src/main/java/org/jruby/AbstractRubyMethod.java index 470a26010e6..7b3a8e6e399 100644 --- a/core/src/main/java/org/jruby/AbstractRubyMethod.java +++ b/core/src/main/java/org/jruby/AbstractRubyMethod.java @@ -35,6 +35,7 @@ import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.internal.runtime.methods.AliasMethod; import org.jruby.internal.runtime.methods.DelegatingDynamicMethod; import org.jruby.internal.runtime.methods.DynamicMethod; @@ -49,6 +50,9 @@ import org.jruby.runtime.callsite.CacheEntry; import org.jruby.runtime.marshal.DataType; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; + /** * @see RubyMethod * @see RubyUnboundMethod @@ -76,13 +80,18 @@ public DynamicMethod getMethod() { * @return the number of arguments of a method. */ @JRubyMethod(name = "arity") + public RubyFixnum arity(ThreadContext context) { + return asFixnum(context, method.getSignature().arityValue()); + } + + @Deprecated public RubyFixnum arity() { - return getRuntime().newFixnum(method.getSignature().arityValue()); + return arity(getCurrentContext()); } @JRubyMethod(name = "eql?") public IRubyObject op_eql(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, equals(other) ); + return asBoolean(context, equals(other) ); } @Override @@ -114,11 +123,10 @@ public IRubyObject owner(ThreadContext context) { @JRubyMethod(name = "source_location") public IRubyObject source_location(ThreadContext context) { - Ruby runtime = context.runtime; - String filename = getFilename(); + if (filename != null) { - return runtime.newArray(runtime.newString(filename), runtime.newFixnum(getLine())); + return context.runtime.newArray(Convert.asString(context, filename), asFixnum(context, getLine())); } return context.nil; diff --git a/core/src/main/java/org/jruby/BasicObjectStub.java b/core/src/main/java/org/jruby/BasicObjectStub.java index 609e4cf0573..820d384b03f 100644 --- a/core/src/main/java/org/jruby/BasicObjectStub.java +++ b/core/src/main/java/org/jruby/BasicObjectStub.java @@ -41,6 +41,7 @@ import org.jruby.runtime.builtin.RubyJavaObject; import org.jruby.runtime.builtin.Variable; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.invokedynamic.MethodNames.INSPECT; import static org.jruby.runtime.Helpers.invokedynamic; @@ -303,11 +304,11 @@ public static IRubyObject id(IRubyObject self) { } public static IRubyObject op_equal(IRubyObject self, ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, self == other); + return asBoolean(context, self == other); } public static IRubyObject op_eqq(IRubyObject self, ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, self == other); + return asBoolean(context, self == other); } public static boolean eql(IRubyObject self, IRubyObject other) { diff --git a/core/src/main/java/org/jruby/FiberScheduler.java b/core/src/main/java/org/jruby/FiberScheduler.java index 0a4af4b33ba..d2221193e84 100644 --- a/core/src/main/java/org/jruby/FiberScheduler.java +++ b/core/src/main/java/org/jruby/FiberScheduler.java @@ -8,6 +8,8 @@ import java.nio.ByteBuffer; +import static org.jruby.api.Convert.asFixnum; + public class FiberScheduler { // MRI: rb_fiber_scheduler_kernel_sleep public static IRubyObject kernelSleep(ThreadContext context, IRubyObject scheduler, IRubyObject timeout) { @@ -21,7 +23,7 @@ public static IRubyObject kernelSleep(ThreadContext context, IRubyObject schedul // MRI: rb_fiber_scheduler_process_wait public static IRubyObject processWait(ThreadContext context, IRubyObject scheduler, long pid, int flags) { - return Helpers.invokeChecked(context, scheduler, "process_wait", context.runtime.newFixnum(pid), context.runtime.newFixnum(flags)); + return Helpers.invokeChecked(context, scheduler, "process_wait", asFixnum(context, pid), asFixnum(context, flags)); } // MRI: rb_fiber_scheduler_block @@ -41,12 +43,12 @@ public static IRubyObject ioWait(ThreadContext context, IRubyObject scheduler, I // MRI: rb_fiber_scheduler_io_wait_readable public static IRubyObject ioWaitReadable(ThreadContext context, IRubyObject scheduler, IRubyObject io) { - return ioWait(context, scheduler, io, context.runtime.newFixnum(OpenFile.READABLE), context.nil); + return ioWait(context, scheduler, io, asFixnum(context, OpenFile.READABLE), context.nil); } // MRI: rb_fiber_scheduler_io_wait_writable public static IRubyObject ioWaitWritable(ThreadContext context, IRubyObject scheduler, IRubyObject io) { - return ioWait(context, scheduler, io, context.runtime.newFixnum(OpenFile.WRITABLE), context.nil); + return ioWait(context, scheduler, io, asFixnum(context, OpenFile.WRITABLE), context.nil); } // MRI: rb_fiber_scheduler_io_select @@ -62,7 +64,7 @@ public static IRubyObject ioSelectv(ThreadContext context, IRubyObject scheduler // MRI: rb_fiber_scheduler_io_read public static IRubyObject ioRead(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, int length, int offset) { Ruby runtime = context.runtime; - return Helpers.invokeChecked(context, scheduler, "io_read", io, buffer, runtime.newFixnum(length), runtime.newFixnum(offset)); + return Helpers.invokeChecked(context, scheduler, "io_read", io, buffer, asFixnum(context, length), asFixnum(context, offset)); } public static IRubyObject ioRead(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, RubyInteger length, RubyInteger offset) { @@ -71,7 +73,7 @@ public static IRubyObject ioRead(ThreadContext context, IRubyObject scheduler, I // MRI: rb_fiber_scheduler_io_pread public static IRubyObject ioPRead(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, int from, int length, int offset) { - return Helpers.invokeChecked(context, scheduler, "io_pread", io, buffer, context.runtime.newFixnum(from), context.runtime.newFixnum(length), context.runtime.newFixnum(offset)); + return Helpers.invokeChecked(context, scheduler, "io_pread", io, buffer, asFixnum(context, from), asFixnum(context, length), asFixnum(context, offset)); } public static IRubyObject ioPRead(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, RubyInteger from, RubyInteger length, RubyInteger offset) { @@ -80,8 +82,7 @@ public static IRubyObject ioPRead(ThreadContext context, IRubyObject scheduler, // MRI: rb_fiber_scheduler_io_write public static IRubyObject ioWrite(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, int length, int offset) { - Ruby runtime = context.runtime; - return Helpers.invokeChecked(context, scheduler, "io_write", io, buffer, runtime.newFixnum(length), runtime.newFixnum(offset)); + return Helpers.invokeChecked(context, scheduler, "io_write", io, buffer, asFixnum(context, length), asFixnum(context, offset)); } public static IRubyObject ioWrite(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, RubyInteger length, RubyInteger offset) { @@ -90,7 +91,7 @@ public static IRubyObject ioWrite(ThreadContext context, IRubyObject scheduler, // MRI: rb_fiber_scheduler_io_pwrite public static IRubyObject ioPWrite(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, int from, int length, int offset) { - return Helpers.invokeChecked(context, scheduler, "io_pwrite", io, buffer, context.runtime.newFixnum(from), context.runtime.newFixnum(length), context.runtime.newFixnum(offset)); + return Helpers.invokeChecked(context, scheduler, "io_pwrite", io, buffer, asFixnum(context, from), asFixnum(context, length), asFixnum(context, offset)); } public static IRubyObject ioPWrite(ThreadContext context, IRubyObject scheduler, IRubyObject io, IRubyObject buffer, RubyInteger from, RubyInteger length, RubyInteger offset) { diff --git a/core/src/main/java/org/jruby/RubyArgsFile.java b/core/src/main/java/org/jruby/RubyArgsFile.java index 3a76b0bc821..861400cc0d4 100644 --- a/core/src/main/java/org/jruby/RubyArgsFile.java +++ b/core/src/main/java/org/jruby/RubyArgsFile.java @@ -43,7 +43,7 @@ import static org.jruby.RubyArgsFile.Next.Stream; import static org.jruby.RubyEnumerator.enumeratorize; import static org.jruby.anno.FrameField.LASTLINE; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.runtime.ThreadContext.CALL_KEYWORD; import static org.jruby.runtime.ThreadContext.resetCallInfo; import static org.jruby.runtime.Visibility.PRIVATE; @@ -652,7 +652,7 @@ public static IRubyObject closed_p(ThreadContext context, IRubyObject recv) { data.next_argv(context); - return RubyBoolean.newBoolean(context, isClosed(context, data.currentFile)); + return asBoolean(context, isClosed(context, data.currentFile)); } private static boolean isClosed(ThreadContext context, IRubyObject currentFile) { @@ -678,7 +678,7 @@ public static IRubyObject op_binmode(ThreadContext context, IRubyObject recv) { @JRubyMethod(name = "lineno") public static IRubyObject lineno(ThreadContext context, IRubyObject recv) { - return context.runtime.newFixnum(context.runtime.getCurrentLine()); + return asFixnum(context, context.runtime.getCurrentLine()); } @JRubyMethod(name = "lineno=") @@ -698,7 +698,7 @@ public static IRubyObject rewind(ThreadContext context, IRubyObject recv) { RubyIO currentFile = getCurrentDataFile(context, "no stream to rewind"); RubyFixnum retVal = currentFile.rewind(context); - currentFile.lineno_set(context, context.runtime.newFixnum(0)); + currentFile.lineno_set(context, asFixnum(context, 0)); return retVal; } @@ -860,8 +860,6 @@ public static IRubyObject getc(ThreadContext context, IRubyObject recv) { @JRubyMethod(name = "read", optional = 2, checkArity = false) public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 0, 2); - - Ruby runtime = context.runtime; ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime); IRubyObject tmp, str, length; long len = 0; @@ -906,7 +904,7 @@ public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObj } else if (argc >= 1) { final int strLen = ((RubyString) str).getByteList().length(); if (strLen < len) { - args[0] = runtime.newFixnum(len - strLen); + args[0] = asFixnum(context, len - strLen); continue; } } diff --git a/core/src/main/java/org/jruby/RubyArithmeticSequence.java b/core/src/main/java/org/jruby/RubyArithmeticSequence.java index 52bff66536a..3f1cfc38f77 100644 --- a/core/src/main/java/org/jruby/RubyArithmeticSequence.java +++ b/core/src/main/java/org/jruby/RubyArithmeticSequence.java @@ -51,6 +51,7 @@ import static org.jruby.RubyNumeric.int2fix; import static org.jruby.RubyNumeric.num2dbl; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; import static org.jruby.runtime.Helpers.hashEnd; import static org.jruby.runtime.Helpers.hashStart; @@ -349,12 +350,8 @@ public RubyFixnum hash() { @JRubyMethod(name = "hash") public RubyFixnum hash(ThreadContext context) { - Ruby runtime = context.runtime; - - IRubyObject v; - - v = safeHash(context, excludeEnd); - long hash = hashStart(runtime, v.convertToInteger().getLongValue()); + IRubyObject v = safeHash(context, excludeEnd); + long hash = hashStart(context.runtime, v.convertToInteger().getLongValue()); v = safeHash(context, begin); hash = murmurCombine(hash, v.convertToInteger().getLongValue()); @@ -366,7 +363,7 @@ public RubyFixnum hash(ThreadContext context) { hash = murmurCombine(hash, v.convertToInteger().getLongValue()); hash = hashEnd(hash); - return runtime.newFixnum(hash); + return asFixnum(context, hash); } @Override @@ -627,12 +624,11 @@ public IRubyObject with_index(ThreadContext context, final Block block) { @JRubyMethod(name = "with_index") public IRubyObject with_index(ThreadContext context, IRubyObject arg, final Block block) { - final Ruby runtime = context.runtime; final int index = arg.isNil() ? 0 : RubyNumeric.num2int(arg); if ( ! block.isGiven() ) { return arg.isNil() ? - enumeratorizeWithSize(context, this, "with_index", RubyArithmeticSequence::size) : - enumeratorizeWithSize(context, this, "with_index", new IRubyObject[]{runtime.newFixnum(index)}, RubyArithmeticSequence::size); + enumeratorizeWithSize(context, this, "with_index", RubyArithmeticSequence::size) : + enumeratorizeWithSize(context, this, "with_index", new IRubyObject[]{asFixnum(context, index)}, RubyArithmeticSequence::size); } return RubyEnumerable.callEach(context, fiberSites(context).each, this, new RubyEnumerable.EachWithIndex(block, index)); diff --git a/core/src/main/java/org/jruby/RubyArray.java b/core/src/main/java/org/jruby/RubyArray.java index 9120229bf40..8ba1c32f592 100644 --- a/core/src/main/java/org/jruby/RubyArray.java +++ b/core/src/main/java/org/jruby/RubyArray.java @@ -93,8 +93,7 @@ import static org.jruby.RubyEnumerator.enumeratorize; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyEnumerator.enumWithSize; -import static org.jruby.api.Convert.checkInt; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.addBufferLength; import static org.jruby.runtime.Helpers.arrayOf; @@ -1421,8 +1420,13 @@ public IRubyObject subseq(RubyClass metaClass, long beg, long len, boolean light * */ @JRubyMethod(name = "length", alias = "size") + public RubyFixnum length(ThreadContext context) { + return asFixnum(context, realLength); + } + + @Deprecated public RubyFixnum length() { - return metaClass.runtime.newFixnum(realLength); + return length(getCurrentContext()); } /** @@ -1431,7 +1435,7 @@ public RubyFixnum length() { * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ protected static IRubyObject size(ThreadContext context, RubyArray self, IRubyObject[] args) { - return self.length(); + return self.length(context); } /** rb_ary_push - specialized rb_ary_store @@ -1601,7 +1605,7 @@ public IRubyObject unshift(IRubyObject[] items) { */ @JRubyMethod(name = "include?") public RubyBoolean include_p(ThreadContext context, IRubyObject item) { - return RubyBoolean.newBoolean(context, includes(context, item)); + return asBoolean(context, includes(context, item)); } /** rb_ary_frozen_p @@ -1610,7 +1614,7 @@ public RubyBoolean include_p(ThreadContext context, IRubyObject item) { @JRubyMethod(name = "frozen?") @Override public RubyBoolean frozen_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isFrozen() || (flags & TMPLOCK_ARR_F) != 0); + return asBoolean(context, isFrozen() || (flags & TMPLOCK_ARR_F) != 0); } /** @@ -1991,12 +1995,10 @@ public IRubyObject each_slice(ThreadContext context, IRubyObject arg, Block bloc * */ public IRubyObject eachIndex(ThreadContext context, Block block) { - Ruby runtime = context.runtime; - if (!block.isGiven()) { - throw runtime.newLocalJumpErrorNoBlock(); - } + if (!block.isGiven()) throw context.runtime.newLocalJumpErrorNoBlock(); + for (int i = 0; i < realLength; i++) { - block.yield(context, runtime.newFixnum(i)); + block.yield(context, asFixnum(context, i)); } return this; } @@ -2474,7 +2476,7 @@ protected IRubyObject fillCommon(ThreadContext context, int beg, long len, Block final Ruby runtime = context.runtime; for (int i = beg; i < end; i++) { - IRubyObject v = block.yield(context, runtime.newFixnum(i)); + IRubyObject v = block.yield(context, asFixnum(context, i)); if (i >= realLength) break; safeArraySet(runtime, values, begin + i, v); } @@ -2486,10 +2488,8 @@ protected IRubyObject fillCommon(ThreadContext context, int beg, long len, Block * */ public IRubyObject index(ThreadContext context, IRubyObject obj) { - Ruby runtime = context.runtime; - for (int i = 0; i < realLength; i++) { - if (equalInternal(context, eltOk(i), obj)) return runtime.newFixnum(i); + if (equalInternal(context, eltOk(i), obj)) return asFixnum(context, i); } return context.nil; @@ -2503,11 +2503,10 @@ public IRubyObject index(ThreadContext context, IRubyObject obj, Block unused) { @JRubyMethod(name = {"index", "find_index"}) public IRubyObject index(ThreadContext context, Block block) { - Ruby runtime = context.runtime; - if (!block.isGiven()) return enumeratorize(runtime, this, "index"); + if (!block.isGiven()) return enumeratorize(context.runtime, this, "index"); for (int i = 0; i < realLength; i++) { - if (block.yield(context, eltOk(i)).isTrue()) return runtime.newFixnum(i); + if (block.yield(context, eltOk(i)).isTrue()) return asFixnum(context, i); } return context.nil; @@ -2588,7 +2587,6 @@ private int bsearch_index_internal(ThreadContext context, Block block) { public IRubyObject rindex(ThreadContext context, IRubyObject obj) { unpack(); - Ruby runtime = context.runtime; int i = realLength; while (i-- > 0) { @@ -2596,10 +2594,10 @@ public IRubyObject rindex(ThreadContext context, IRubyObject obj) { i = realLength; continue; } - if (equalInternal(context, eltOk(i), obj)) return runtime.newFixnum(i); + if (equalInternal(context, eltOk(i), obj)) return asFixnum(context, i); } - return runtime.getNil(); + return context.nil; } @JRubyMethod @@ -2610,8 +2608,7 @@ public IRubyObject rindex(ThreadContext context, IRubyObject obj, Block unused) @JRubyMethod public IRubyObject rindex(ThreadContext context, Block block) { - Ruby runtime = context.runtime; - if (!block.isGiven()) return enumeratorize(runtime, this, "rindex"); + if (!block.isGiven()) return enumeratorize(context.runtime, this, "rindex"); int i = realLength; @@ -2620,10 +2617,10 @@ public IRubyObject rindex(ThreadContext context, Block block) { i = realLength; continue; } - if (block.yield(context, eltOk(i)).isTrue()) return runtime.newFixnum(i); + if (block.yield(context, eltOk(i)).isTrue()) return asFixnum(context, i); } - return runtime.getNil(); + return context.nil; } @Deprecated @@ -3468,14 +3465,19 @@ public IRubyObject count(ThreadContext context, IRubyObject obj, Block block) { * */ @JRubyMethod(name = "nitems") - public IRubyObject nitems() { + public IRubyObject nitems(ThreadContext context) { int n = 0; for (int i = 0; i < realLength; i++) { if (!eltOk(i).isNil()) n++; } - return metaClass.runtime.newFixnum(n); + return asFixnum(context, n); + } + + @Deprecated + public IRubyObject nitems() { + return nitems(getCurrentContext()); } /** rb_ary_plus @@ -4186,7 +4188,7 @@ private static IRubyObject cycleSize(ThreadContext context, RubyArray self, IRub return RubyFixnum.zero(runtime); } - RubyFixnum length = self.length(); + RubyFixnum length = self.length(context); return sites(context).op_times.call(context, length, length, RubyFixnum.newFixnum(runtime, multiple)); } @@ -4465,16 +4467,13 @@ public IRubyObject repeated_permutation(ThreadContext context, IRubyObject num, * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ private static IRubyObject repeatedPermutationSize(ThreadContext context, RubyArray self, IRubyObject[] args) { - RubyFixnum n = self.length(); + RubyFixnum n = self.length(context); assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #repeated_permutation ensures arg[0] is numeric long k = ((RubyNumeric) args[0]).getLongValue(); - Ruby runtime = context.runtime; - if (k < 0) { - return RubyFixnum.zero(runtime); - } + if (k < 0) return RubyFixnum.zero(context.runtime); - RubyFixnum v = RubyFixnum.newFixnum(runtime, k); + RubyFixnum v = asFixnum(context, k); return sites(context).op_exp.call(context, n, n, v); } @@ -5229,7 +5228,7 @@ public IRubyObject find_index(ThreadContext context, Block block) { if (!self_each.isBuiltin(this)) return RubyEnumerable.find_indexCommon(context, self_each, this, block, Signature.OPTIONAL); for (int i = 0; i < realLength; i++) { - if (block.yield(context, eltOk(i)).isTrue()) return context.runtime.newFixnum(i); + if (block.yield(context, eltOk(i)).isTrue()) return asFixnum(context, i); } return context.nil; @@ -5240,7 +5239,7 @@ public IRubyObject find_index(ThreadContext context, IRubyObject cond) { if (!self_each.isBuiltin(this)) return RubyEnumerable.find_indexCommon(context, self_each, this, cond); for (int i = 0; i < realLength; i++) { - if (eltOk(i).equals(cond)) return context.runtime.newFixnum(i); + if (eltOk(i).equals(cond)) return asFixnum(context, i); } return context.nil; diff --git a/core/src/main/java/org/jruby/RubyBasicObject.java b/core/src/main/java/org/jruby/RubyBasicObject.java index db6e4ff9fab..5d7701773d3 100644 --- a/core/src/main/java/org/jruby/RubyBasicObject.java +++ b/core/src/main/java/org/jruby/RubyBasicObject.java @@ -66,6 +66,7 @@ import org.jruby.runtime.Visibility; import static org.jruby.anno.FrameField.*; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Convert.castAsModule; import static org.jruby.api.Error.typeError; import static org.jruby.ir.runtime.IRRuntimeHelpers.dupIfKeywordRestAtCallsite; @@ -582,6 +583,16 @@ public final Ruby getRuntime() { return metaClass.runtime; } + // As part of reducing usage of getRuntime() this method was added so it is + // easier to know how many real uses of getRuntime() we still need to eliminate. + // IMPORTANT: This method should only be used in deprecated methods. If you do + // not have access then you should continue using getRuntime().getCurrentContext() + // until we can plumb ThreadContext into whatever method needs it. + @Deprecated + public final ThreadContext getCurrentContext() { + return getRuntime().getCurrentContext(); + } + /** * Will return the Java interface that most closely can represent * this object, when working through Java integration translations. @@ -1163,7 +1174,7 @@ private RubyString inspectObj(final Ruby runtime, RubyString part) { @JRubyMethod(name = "!") public IRubyObject op_not(ThreadContext context) { - return isTrue() ? context.fals : context.tru; + return asBoolean(context, !isTrue()); } /** @@ -1175,7 +1186,7 @@ public IRubyObject op_not(ThreadContext context) { */ @JRubyMethod(name = "!=") public IRubyObject op_not_equal(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, !sites(context).op_equal.call(context, this, this, other).isTrue()); + return asBoolean(context, !sites(context).op_equal.call(context, this, this, other).isTrue()); } /** @@ -1219,7 +1230,7 @@ public int compareTo(IRubyObject other) { @Override @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { - return this == obj ? context.tru : context.fals; + return asBoolean(context, this == obj); } @Override @@ -1997,7 +2008,7 @@ private void callFinalizer(IRubyObject finalizer) { */ @JRubyMethod(name = "equal?") public IRubyObject equal_p(ThreadContext context, IRubyObject other) { - return this == other ? context.tru : context.fals; + return asBoolean(context, this == other); } /** rb_obj_equal @@ -2183,7 +2194,7 @@ public IRubyObject freeze(ThreadContext context) { * a.frozen? #=> true */ public RubyBoolean frozen_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isFrozen()); + return asBoolean(context, isFrozen()); } /** rb_obj_is_instance_of @@ -2229,7 +2240,7 @@ public RubyBoolean instance_of_p(ThreadContext context, IRubyObject type) { * b.kind_of? M #=> true */ public RubyBoolean kind_of_p(ThreadContext context, IRubyObject type) { - return RubyBoolean.newBoolean(context, castAsModule(context, type, "class or module required").isInstance(this)); + return asBoolean(context, castAsModule(context, type, "class or module required").isInstance(this)); } /** rb_obj_methods @@ -2659,7 +2670,7 @@ public IRubyObject op_match(ThreadContext context, IRubyObject arg) { * @return */ public IRubyObject op_not_match(ThreadContext context, IRubyObject arg) { - return RubyBoolean.newBoolean(context, !sites(context).match.call(context, this, this, arg).isTrue()); + return asBoolean(context, !sites(context).match.call(context, this, this, arg).isTrue()); } @@ -2686,7 +2697,7 @@ public IRubyObject op_not_match(ThreadContext context, IRubyObject arg) { * fred.instance_variable_defined?("@c") #=> false */ public IRubyObject instance_variable_defined_p(ThreadContext context, IRubyObject name) { - return RubyBoolean.newBoolean(context, variableTableContains(validateInstanceVariable(name))); + return asBoolean(context, variableTableContains(validateInstanceVariable(name))); } /** rb_obj_ivar_get diff --git a/core/src/main/java/org/jruby/RubyBignum.java b/core/src/main/java/org/jruby/RubyBignum.java index b890b58dee8..c0f2790beff 100644 --- a/core/src/main/java/org/jruby/RubyBignum.java +++ b/core/src/main/java/org/jruby/RubyBignum.java @@ -52,7 +52,7 @@ import org.jruby.runtime.marshal.UnmarshalStream; import static org.jruby.RubyFixnum.zero; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; /** @@ -798,7 +798,7 @@ final RubyInteger op_or(ThreadContext context, RubyFixnum other) { @Override public IRubyObject bit_length(ThreadContext context) { - return context.runtime.newFixnum(value.bitLength()); + return asFixnum(context, value.bitLength()); } /** rb_big_xor @@ -1132,18 +1132,18 @@ public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { final BigInteger otherValue; - if (other instanceof RubyFixnum) { - otherValue = fix2big((RubyFixnum) other); - } else if (other instanceof RubyBignum) { - otherValue = ((RubyBignum) other).value; - } else if (other instanceof RubyFloat) { - double a = ((RubyFloat) other).value; + if (other instanceof RubyFixnum fixnum) { + otherValue = fix2big(fixnum); + } else if (other instanceof RubyBignum bignum) { + otherValue = bignum.value; + } else if (other instanceof RubyFloat flote) { + double a = flote.value; if (Double.isNaN(a)) return context.fals; - return RubyBoolean.newBoolean(context, a == big2dbl(this)); + return asBoolean(context, a == big2dbl(this)); } else { return other.op_eqq(context, this); } - return RubyBoolean.newBoolean(context, value.compareTo(otherValue) == 0); + return asBoolean(context, value.compareTo(otherValue) == 0); } /** rb_big_eql @@ -1204,12 +1204,12 @@ public IRubyObject abs(ThreadContext context) { */ @Override public IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum((value.bitLength() + 7) / 8); + return asFixnum(context, (value.bitLength() + 7) / 8); } @Override public IRubyObject zero_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isZero()); + return asBoolean(context, isZero()); } @Override @@ -1347,7 +1347,7 @@ public IRubyObject fdivFloat(ThreadContext context, RubyFloat y) { public IRubyObject isNegative(ThreadContext context) { CachingCallSite op_lt_site = sites(context).basic_op_lt; if (op_lt_site.isBuiltin(metaClass)) { - return RubyBoolean.newBoolean(context, value.signum() < 0); + return asBoolean(context, value.signum() < 0); } return op_lt_site.call(context, this, this, zero(context.runtime)); } @@ -1356,7 +1356,7 @@ public IRubyObject isNegative(ThreadContext context) { public IRubyObject isPositive(ThreadContext context) { CachingCallSite op_gt_site = sites(context).basic_op_gt; if (op_gt_site.isBuiltin(metaClass)) { - return RubyBoolean.newBoolean(context, value.signum() > 0); + return asBoolean(context, value.signum() > 0); } return op_gt_site.call(context, this, this, zero(context.runtime)); } diff --git a/core/src/main/java/org/jruby/RubyBinding.java b/core/src/main/java/org/jruby/RubyBinding.java index 864c3fe805a..a479c7ec53b 100644 --- a/core/src/main/java/org/jruby/RubyBinding.java +++ b/core/src/main/java/org/jruby/RubyBinding.java @@ -36,6 +36,7 @@ import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.ext.ripper.RubyLexer; import org.jruby.runtime.Arity; import org.jruby.runtime.Binding; @@ -47,6 +48,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.RubyStringBuilder.str; /** @@ -146,7 +149,7 @@ public IRubyObject eval(ThreadContext context, IRubyObject[] args) { @JRubyMethod(name = "local_variable_defined?") public IRubyObject local_variable_defined_p(ThreadContext context, IRubyObject symbol) { String id = checkLocalId(context, symbol); - return RubyBoolean.newBoolean(context, binding.getEvalScope(context.runtime).getStaticScope().isDefined(id) != -1); + return asBoolean(context, binding.getEvalScope(context.runtime).getStaticScope().isDefined(id) != -1); } @JRubyMethod @@ -198,9 +201,8 @@ public IRubyObject receiver(ThreadContext context) { @JRubyMethod public IRubyObject source_location(ThreadContext context) { - Ruby runtime = context.runtime; - IRubyObject filename = runtime.newString(binding.getFile()).freeze(context); - RubyFixnum line = runtime.newFixnum(binding.getLine() + 1); /* zero-based */ - return runtime.newArray(filename, line); + IRubyObject filename = Convert.asString(context, binding.getFile()).freeze(context); + RubyFixnum line = asFixnum(context, binding.getLine() + 1); /* zero-based */ + return context.runtime.newArray(filename, line); } } diff --git a/core/src/main/java/org/jruby/RubyBoolean.java b/core/src/main/java/org/jruby/RubyBoolean.java index 3adb9d3d7ef..b53c01314ef 100644 --- a/core/src/main/java/org/jruby/RubyBoolean.java +++ b/core/src/main/java/org/jruby/RubyBoolean.java @@ -45,6 +45,8 @@ import org.jruby.runtime.opto.OptoFactory; import org.jruby.util.ByteList; +import static org.jruby.api.Convert.asFixnum; + /** * * @author jpetersen @@ -132,11 +134,13 @@ public static RubyClass createTrueClass(Ruby runtime) { return trueClass; } - + + @Deprecated public static RubyBoolean newBoolean(Ruby runtime, boolean value) { return value ? runtime.getTrue() : runtime.getFalse(); } + @Deprecated public static RubyBoolean newBoolean(ThreadContext context, boolean value) { return value ? context.tru : context.fals; } @@ -234,7 +238,7 @@ public T toJava(Class target) { @JRubyMethod(name = "hash") public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCode()); + return asFixnum(context, hashCode()); } @Override diff --git a/core/src/main/java/org/jruby/RubyChain.java b/core/src/main/java/org/jruby/RubyChain.java index 7d9d40fddb8..e1f00cf12b1 100644 --- a/core/src/main/java/org/jruby/RubyChain.java +++ b/core/src/main/java/org/jruby/RubyChain.java @@ -42,6 +42,7 @@ import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyEnumerator.SizeFn; +import static org.jruby.api.Convert.asFixnum; /** * Implements Enumerator::Chain @@ -200,12 +201,11 @@ public IRubyObject with_index(ThreadContext context, final Block block) { @JRubyMethod(name = "with_index") public IRubyObject with_index(ThreadContext context, IRubyObject arg, final Block block) { - final Ruby runtime = context.runtime; final int index = arg.isNil() ? 0 : RubyNumeric.num2int(arg); if ( ! block.isGiven() ) { return arg.isNil() ? enumeratorizeWithSize(context, this, "with_index", RubyChain::size) : - enumeratorizeWithSize(context, this, "with_index", new IRubyObject[]{runtime.newFixnum(index)}, RubyChain::size); + enumeratorizeWithSize(context, this, "with_index", new IRubyObject[]{asFixnum(context, index)}, RubyChain::size); } return RubyEnumerable.callEach(context, fiberSites(context).each, this, new RubyEnumerable.EachWithIndex(block, index)); diff --git a/core/src/main/java/org/jruby/RubyClassPathVariable.java b/core/src/main/java/org/jruby/RubyClassPathVariable.java index eb7833162dc..810da1a3a39 100644 --- a/core/src/main/java/org/jruby/RubyClassPathVariable.java +++ b/core/src/main/java/org/jruby/RubyClassPathVariable.java @@ -38,6 +38,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asFixnum; + /** * @author Ola Bini */ @@ -99,8 +101,11 @@ private URL getURL(String target) throws MalformedURLException { } @JRubyMethod(name = {"size", "length"}) + public IRubyObject size(ThreadContext context) { + return asFixnum(context, context.runtime.getJRubyClassLoader().getURLs().length); + } public IRubyObject size() { - return getRuntime().newFixnum(getRuntime().getJRubyClassLoader().getURLs().length); + return size(getCurrentContext()); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/RubyComparable.java b/core/src/main/java/org/jruby/RubyComparable.java index 3b8f06a432f..58fcb2e7a77 100644 --- a/core/src/main/java/org/jruby/RubyComparable.java +++ b/core/src/main/java/org/jruby/RubyComparable.java @@ -41,6 +41,7 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Convert.castAsRange; /** Implementation of the Comparable module. @@ -66,20 +67,8 @@ public static RubyModule createComparable(Ruby runtime) { */ public static int cmpint(ThreadContext context, CallSite op_gt, CallSite op_lt, IRubyObject val, IRubyObject a, IRubyObject b) { if (val == context.nil) cmperr(a, b); - if (val instanceof RubyFixnum) { - final int asInt = RubyNumeric.fix2int((RubyFixnum) val); - - if (asInt > 0) { - return 1; - } - - if (asInt < 0) { - return -1; - } - - return 0; - } - if (val instanceof RubyBignum) return ((RubyBignum) val).signum() == -1 ? -1 : 1; + if (val instanceof RubyFixnum fixnum) return Integer.compare(RubyNumeric.fix2int(fixnum), 0); + if (val instanceof RubyBignum bignum) return bignum.signum() == -1 ? -1 : 1; RubyFixnum zero = RubyFixnum.zero(context.runtime); @@ -154,8 +143,6 @@ public static IRubyObject op_equal(ThreadContext context, IRubyObject recv, IRub } private static IRubyObject callCmpMethod(final ThreadContext context, final IRubyObject recv, final IRubyObject other, IRubyObject returnValueOnError) { - final Ruby runtime = context.runtime; - if (recv == other) return context.tru; IRubyObject result = context.safeRecurse( @@ -163,9 +150,9 @@ private static IRubyObject callCmpMethod(final ThreadContext context, final IRub other, recv, "<=>", true); // This is only to prevent throwing exceptions by cmperr - it has poor performance - if ( result.isNil() ) return returnValueOnError; + if (result.isNil()) return returnValueOnError; - return RubyBoolean.newBoolean(runtime, cmpint(context, result, recv, other) == 0); + return asBoolean(context, cmpint(context, result, recv, other) == 0); } /** cmp_gt @@ -178,7 +165,7 @@ public static RubyBoolean op_gt(ThreadContext context, IRubyObject recv, IRubyOb if (result.isNil()) cmperr(recv, other); - return RubyBoolean.newBoolean(context, cmpint(context, result, recv, other) > 0); + return asBoolean(context, cmpint(context, result, recv, other) > 0); } /** cmp_ge @@ -190,7 +177,7 @@ public static RubyBoolean op_ge(ThreadContext context, IRubyObject recv, IRubyOb if (result.isNil()) cmperr(recv, other); - return RubyBoolean.newBoolean(context, cmpint(context, result, recv, other) >= 0); + return asBoolean(context, cmpint(context, result, recv, other) >= 0); } /** cmp_lt @@ -202,7 +189,7 @@ public static RubyBoolean op_lt(ThreadContext context, IRubyObject recv, IRubyOb if (result.isNil()) cmperr(recv, other); - return RubyBoolean.newBoolean(context, cmpint(context, result, recv, other) < 0); + return asBoolean(context, cmpint(context, result, recv, other) < 0); } public static RubyBoolean op_lt(ThreadContext context, CallSite cmp, IRubyObject recv, IRubyObject other) { @@ -210,7 +197,7 @@ public static RubyBoolean op_lt(ThreadContext context, CallSite cmp, IRubyObject if (result.isNil()) cmperr(recv, other); - return RubyBoolean.newBoolean(context, cmpint(context, result, recv, other) < 0); + return asBoolean(context, cmpint(context, result, recv, other) < 0); } /** cmp_le @@ -222,7 +209,7 @@ public static RubyBoolean op_le(ThreadContext context, IRubyObject recv, IRubyOb if (result.isNil()) cmperr(recv, other); - return RubyBoolean.newBoolean(context, cmpint(context, result, recv, other) <= 0); + return asBoolean(context, cmpint(context, result, recv, other) <= 0); } /** cmp_between @@ -230,7 +217,7 @@ public static RubyBoolean op_le(ThreadContext context, IRubyObject recv, IRubyOb */ @JRubyMethod(name = "between?") public static RubyBoolean between_p(ThreadContext context, IRubyObject recv, IRubyObject first, IRubyObject second) { - return RubyBoolean.newBoolean(context, op_lt(context, recv, first).isFalse() && op_gt(context, recv, second).isFalse()); + return asBoolean(context, op_lt(context, recv, first).isFalse() && op_gt(context, recv, second).isFalse()); } @JRubyMethod(name = "clamp") diff --git a/core/src/main/java/org/jruby/RubyComplex.java b/core/src/main/java/org/jruby/RubyComplex.java index 550d9cff773..b1b646642b9 100644 --- a/core/src/main/java/org/jruby/RubyComplex.java +++ b/core/src/main/java/org/jruby/RubyComplex.java @@ -52,6 +52,7 @@ import java.util.Arrays; import java.util.function.BiFunction; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.invokedynamic; import static org.jruby.runtime.invokedynamic.MethodNames.HASH; @@ -833,18 +834,12 @@ else if (f_zero_p(context, xr)) { @JRubyMethod(name = "==") @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - if (other instanceof RubyComplex) { - RubyComplex otherComplex = (RubyComplex) other; - boolean test = f_equal(context, real, otherComplex.real).isTrue() && - f_equal(context, image, otherComplex.image).isTrue(); - - return RubyBoolean.newBoolean(context, test); + if (other instanceof RubyComplex comp) { + return asBoolean(context, f_equal(context, real, comp.real).isTrue() && f_equal(context, image, comp.image).isTrue()); } - if (other instanceof RubyNumeric && f_real_p(context, (RubyNumeric) other)) { - boolean test = f_equal(context, real, other).isTrue() && f_zero_p(context, image); - - return RubyBoolean.newBoolean(context, test); + if (other instanceof RubyNumeric num && f_real_p(context, num)) { + return asBoolean(context, f_equal(context, real, num).isTrue() && f_zero_p(context, image)); } return f_equal(context, other, this); @@ -1006,7 +1001,7 @@ public int hashCode() { @JRubyMethod(name = "eql?") @Override public IRubyObject eql_p(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, equals(context, other)); + return asBoolean(context, equals(context, other)); } private boolean equals(ThreadContext context, Object other) { diff --git a/core/src/main/java/org/jruby/RubyConverter.java b/core/src/main/java/org/jruby/RubyConverter.java index 4cd1bf50fa9..f008a928cf7 100644 --- a/core/src/main/java/org/jruby/RubyConverter.java +++ b/core/src/main/java/org/jruby/RubyConverter.java @@ -57,6 +57,8 @@ import java.util.HashMap; import java.util.Map; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.PRIVATE; @@ -361,16 +363,15 @@ public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args) @JRubyMethod public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) { - Ruby runtime = context.runtime; RubyString orig = srcBuffer.convertToString(); IRubyObject dest; IRubyObject[] newArgs = { orig.dup(), - dest = runtime.newString(), + dest = context.runtime.newString(), context.nil, context.nil, - runtime.newFixnum(EConvFlags.PARTIAL_INPUT) + asFixnum(context, EConvFlags.PARTIAL_INPUT) }; IRubyObject ret = primitive_convert(context, newArgs); @@ -382,15 +383,15 @@ public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) { if (retStr.equals(EConvResult.InvalidByteSequence.symbolicName()) || retStr.equals(EConvResult.UndefinedConversion.symbolicName()) || retStr.equals(EConvResult.IncompleteInput.symbolicName())) { - throw EncodingUtils.makeEconvException(runtime, ec); + throw EncodingUtils.makeEconvException(context.runtime, ec); } if (retStr.equals(EConvResult.Finished.symbolicName())) { - throw runtime.newArgumentError("converter already finished"); + throw context.runtime.newArgumentError("converter already finished"); } if (!retStr.equals(EConvResult.SourceBufferEmpty.symbolicName())) { - throw runtime.newRuntimeError("bug: unexpected result of primitive_convert: " + retSym); + throw context.runtime.newRuntimeError("bug: unexpected result of primitive_convert: " + retSym); } } @@ -399,15 +400,14 @@ public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) { @JRubyMethod public IRubyObject finish(ThreadContext context) { - Ruby runtime = context.runtime; IRubyObject dest; IRubyObject[] newArgs = { context.nil, - dest = runtime.newString(), + dest = context.runtime.newString(), context.nil, context.nil, - runtime.newFixnum(0) + asFixnum(context, 0) }; IRubyObject ret = primitive_convert(context, newArgs); @@ -419,11 +419,11 @@ public IRubyObject finish(ThreadContext context) { if (retStr.equals(EConvResult.InvalidByteSequence.symbolicName()) || retStr.equals(EConvResult.UndefinedConversion.symbolicName()) || retStr.equals(EConvResult.IncompleteInput.symbolicName())) { - throw EncodingUtils.makeEconvException(runtime, ec); + throw EncodingUtils.makeEconvException(context.runtime, ec); } if (!retStr.equals(EConvResult.Finished.symbolicName())) { - throw runtime.newRuntimeError("bug: unexpected result of primitive_convert"); + throw context.runtime.newRuntimeError("bug: unexpected result of primitive_convert"); } } @@ -636,15 +636,12 @@ public IRubyObject putback(ThreadContext context, IRubyObject[] argv) // econv_equal @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - EConv ec1 = ec; - EConv ec2; - int i; - if (!(other instanceof RubyConverter)) return context.nil; - ec2 = ((RubyConverter)other).ec; + EConv ec1 = ec; + EConv ec2 = ((RubyConverter)other).ec; - return RubyBoolean.newBoolean(context, ec1.equals(ec2)); + return asBoolean(context, ec1.equals(ec2)); } public static class EncodingErrorMethods { diff --git a/core/src/main/java/org/jruby/RubyDir.java b/core/src/main/java/org/jruby/RubyDir.java index 10b421243e4..4f1c50329a2 100644 --- a/core/src/main/java/org/jruby/RubyDir.java +++ b/core/src/main/java/org/jruby/RubyDir.java @@ -66,6 +66,8 @@ import static org.jruby.RubyEnumerator.enumeratorize; import static org.jruby.RubyFile.filePathConvert; import static org.jruby.RubyString.UTF8; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.RubyStringBuilder.str; import static org.jruby.util.io.EncodingUtils.newExternalStringWithEncoding; @@ -471,7 +473,7 @@ public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyOb } } else { runtime.setCurrentDirectory(adjustedPath); - result = runtime.newFixnum(0); + result = asFixnum(context, 0); } return result; @@ -838,9 +840,14 @@ public IRubyObject inspect() { * Returns the current position in the directory. */ @JRubyMethod(name = {"tell", "pos"}) - public RubyInteger tell() { + public RubyInteger tell(ThreadContext context) { checkDir(); - return getRuntime().newFixnum(pos); + return asFixnum(context, pos); + } + + @Deprecated + public RubyInteger tell() { + return tell(getCurrentContext()); } /** @@ -906,7 +913,7 @@ public static IRubyObject empty_p(ThreadContext context, IRubyObject recv, IRuby RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg)); RubyFileStat fileStat = runtime.newFileStat(path.asJavaString(), false); boolean isDirectory = fileStat.directory_p().isTrue(); - return runtime.newBoolean(isDirectory && entries(context, recv, arg).getLength() <= 2); + return asBoolean(context, isDirectory && entries(context, recv, arg).getLength() <= 2); } @JRubyMethod(name = "exist?", meta = true) @@ -921,7 +928,7 @@ public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyOb } catch (Exception e) { // Restore $! runtime.getGlobalVariables().set("$!", exception); - return runtime.newBoolean(false); + return context.fals; } } diff --git a/core/src/main/java/org/jruby/RubyEncoding.java b/core/src/main/java/org/jruby/RubyEncoding.java index c79f25b9b1b..3e234dde7bb 100755 --- a/core/src/main/java/org/jruby/RubyEncoding.java +++ b/core/src/main/java/org/jruby/RubyEncoding.java @@ -58,6 +58,7 @@ import static com.headius.backport9.buffer.Buffers.clearBuffer; import static com.headius.backport9.buffer.Buffers.flipBuffer; +import static org.jruby.api.Convert.asBoolean; @JRubyClass(name="Encoding") public class RubyEncoding extends RubyObject implements Constantizable { @@ -566,7 +567,7 @@ public static IRubyObject _load(ThreadContext context, IRubyObject recv, IRubyOb @JRubyMethod(name = "ascii_compatible?") public IRubyObject asciiCompatible_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getEncoding().isAsciiCompatible()); + return asBoolean(context, getEncoding().isAsciiCompatible()); } @JRubyMethod(name = {"to_s", "name"}) @@ -618,7 +619,7 @@ public IRubyObject names(ThreadContext context) { @JRubyMethod(name = "dummy?") public IRubyObject dummy_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isDummy); + return asBoolean(context, isDummy); } @JRubyMethod(name = "compatible?", meta = true) diff --git a/core/src/main/java/org/jruby/RubyEnumerable.java b/core/src/main/java/org/jruby/RubyEnumerable.java index 9d646ac9960..baaf467e81c 100644 --- a/core/src/main/java/org/jruby/RubyEnumerable.java +++ b/core/src/main/java/org/jruby/RubyEnumerable.java @@ -67,6 +67,7 @@ import static org.jruby.RubyEnumerator.enumeratorize; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyObject.equalInternal; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.arrayOf; @@ -1152,12 +1153,12 @@ static class EachWithIndex implements BlockCallback { } public IRubyObject call(ThreadContext context, IRubyObject[] iargs, Block block) { - return this.block.call(context, packEnumValues(context, iargs), context.runtime.newFixnum(index++)); + return this.block.call(context, packEnumValues(context, iargs), asFixnum(context, index++)); } @Override public IRubyObject call(ThreadContext context, IRubyObject iarg, Block block) { - return this.block.call(context, iarg, context.runtime.newFixnum(index++)); + return this.block.call(context, iarg, asFixnum(context, index++)); } } diff --git a/core/src/main/java/org/jruby/RubyEnumerator.java b/core/src/main/java/org/jruby/RubyEnumerator.java index 8738da0b080..b5128a96f90 100644 --- a/core/src/main/java/org/jruby/RubyEnumerator.java +++ b/core/src/main/java/org/jruby/RubyEnumerator.java @@ -45,6 +45,7 @@ import java.util.Spliterator; import java.util.stream.Stream; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.arrayOf; @@ -471,12 +472,11 @@ private static IRubyObject size(ThreadContext context, RubyEnumerator recv, IRub } private IRubyObject with_index_common(ThreadContext context, final Block block, final String rubyMethodName, IRubyObject arg) { - final Ruby runtime = context.runtime; final int index = arg.isNil() ? 0 : RubyNumeric.num2int(arg); if ( ! block.isGiven() ) { return arg.isNil() ? enumeratorizeWithSize(context, this, rubyMethodName, RubyEnumerator::size) : - enumeratorizeWithSize(context, this, rubyMethodName, new IRubyObject[]{runtime.newFixnum(index)}, RubyEnumerator::size); + enumeratorizeWithSize(context, this, rubyMethodName, new IRubyObject[]{asFixnum(context, index)}, RubyEnumerator::size); } return RubyEnumerable.callEach(context, sites(context).each, this, new RubyEnumerable.EachWithIndex(block, index)); diff --git a/core/src/main/java/org/jruby/RubyException.java b/core/src/main/java/org/jruby/RubyException.java index bd027ddee26..a85d4775d99 100644 --- a/core/src/main/java/org/jruby/RubyException.java +++ b/core/src/main/java/org/jruby/RubyException.java @@ -58,6 +58,7 @@ import java.io.PrintStream; import java.util.List; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.PRIVATE; import static org.jruby.util.RubyStringBuilder.str; @@ -383,11 +384,10 @@ public RubyString inspect(ThreadContext context) { public RubyBoolean op_equal(ThreadContext context, IRubyObject other) { if (this == other) return context.tru; - boolean equal = context.runtime.getException().isInstance(other) && + return asBoolean(context, context.runtime.getException().isInstance(other) && getMetaClass().getRealClass() == other.getMetaClass().getRealClass() && callMethod(context, "message").equals(other.callMethod(context, "message")) && - callMethod(context, "backtrace").equals(other.callMethod(context, "backtrace")); - return RubyBoolean.newBoolean(context, equal); + callMethod(context, "backtrace").equals(other.callMethod(context, "backtrace"))); } @JRubyMethod(name = "cause") diff --git a/core/src/main/java/org/jruby/RubyFile.java b/core/src/main/java/org/jruby/RubyFile.java index f09306c73d8..07c248e3dbc 100644 --- a/core/src/main/java/org/jruby/RubyFile.java +++ b/core/src/main/java/org/jruby/RubyFile.java @@ -81,7 +81,7 @@ import java.util.zip.ZipFile; import static org.jruby.RubyInteger.singleCharByteList; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.runtime.ThreadContext.hasKeywords; import static org.jruby.runtime.Visibility.PRIVATE; import static org.jruby.util.StringSupport.*; @@ -378,7 +378,7 @@ public IRubyObject chmod(ThreadContext context, IRubyObject arg) { throw context.runtime.newErrnoENOENTError(path); } - return context.runtime.newFixnum(context.runtime.getPosix().chmod(path, mode)); + return asFixnum(context, context.runtime.getPosix().chmod(path, mode)); } @JRubyMethod @@ -399,13 +399,13 @@ public IRubyObject chown(ThreadContext context, IRubyObject arg1, IRubyObject ar throw context.runtime.newErrnoENOENTError(path); } - return context.runtime.newFixnum(context.runtime.getPosix().chown(path, owner, group)); + return asFixnum(context, context.runtime.getPosix().chown(path, owner, group)); } @JRubyMethod public IRubyObject atime(ThreadContext context) { checkClosed(context); - return context.runtime.newFileStat(getPath(), false).atime(); + return context.runtime.newFileStat(getPath(), false).atime(context); } @JRubyMethod(name = "ctime") @@ -450,7 +450,7 @@ public IRubyObject lstat(ThreadContext context) { @JRubyMethod public IRubyObject mtime(ThreadContext context) { checkClosed(context); - return ((RubyFileStat) stat(context)).mtime(); + return ((RubyFileStat) stat(context)).mtime(context); } @JRubyMethod(meta = true) @@ -627,58 +627,48 @@ private static int getAltSeparatorChar(final RubyClass File) { @JRubyMethod(required = 2, rest = true, checkArity = false, meta = true) public static IRubyObject chmod(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 2, -1); - - Ruby runtime = context.runtime; - int count = 0; RubyInteger mode = args[0].convertToInteger(); + for (int i = 1; i < argc; i++) { JRubyFile filename = file(args[i]); - if (!filename.exists()) { - throw runtime.newErrnoENOENTError(filename.toString()); - } + if (!filename.exists()) throw context.runtime.newErrnoENOENTError(filename.toString()); - if (0 != runtime.getPosix().chmod(filename.getAbsolutePath(), (int) mode.getLongValue())) { - throw runtime.newErrnoFromLastPOSIXErrno(); + if (0 != context.runtime.getPosix().chmod(filename.getAbsolutePath(), (int) mode.getLongValue())) { + throw context.runtime.newErrnoFromLastPOSIXErrno(); } else { count++; } } - return runtime.newFixnum(count); + return asFixnum(context, count); } @JRubyMethod(required = 2, rest = true, checkArity = false, meta = true) public static IRubyObject chown(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 2, -1); - Ruby runtime = context.runtime; - int count = 0; + int owner = -1; - if (!args[0].isNil()) { - owner = RubyNumeric.num2int(args[0]); - } + if (!args[0].isNil()) owner = RubyNumeric.num2int(args[0]); int group = -1; - if (!args[1].isNil()) { - group = RubyNumeric.num2int(args[1]); - } + if (!args[1].isNil()) group = RubyNumeric.num2int(args[1]); + for (int i = 2; i < argc; i++) { JRubyFile filename = file(args[i]); - if (!filename.exists()) { - throw runtime.newErrnoENOENTError(filename.toString()); - } + if (!filename.exists()) throw context.runtime.newErrnoENOENTError(filename.toString()); - if (0 != runtime.getPosix().chown(filename.getAbsolutePath(), owner, group)) { - throw runtime.newErrnoFromLastPOSIXErrno(); + if (0 != context.runtime.getPosix().chown(filename.getAbsolutePath(), owner, group)) { + throw context.runtime.newErrnoFromLastPOSIXErrno(); } else { count++; } } - return runtime.newFixnum(count); + return asFixnum(context, count); } @JRubyMethod(meta = true) @@ -953,7 +943,7 @@ public static IRubyObject absolute_path_p(ThreadContext context, IRubyObject rec RubyString file = get_path(context, arg); // asJavaString should be ok here since windows drive shares will be charset representable and otherwise we look for "/" at front. - return context.runtime.newBoolean(isJRubyAbsolutePath(file.asJavaString())); + return asBoolean(context, isJRubyAbsolutePath(file.asJavaString())); } @JRubyMethod(meta = true) @@ -1085,7 +1075,7 @@ public static IRubyObject atime(ThreadContext context, IRubyObject recv, IRubyOb public static IRubyObject ctime(ThreadContext context, IRubyObject recv, IRubyObject filename) { Ruby runtime = context.runtime; String f = StringSupport.checkEmbeddedNulls(runtime, get_path(context, filename)).toString(); - return runtime.newFileStat(f, false).ctime(); + return runtime.newFileStat(f, false).ctime(context); } @JRubyMethod(name = "birthtime", meta = true) @@ -1098,28 +1088,24 @@ public static IRubyObject birthtime(ThreadContext context, IRubyObject recv, IRu @JRubyMethod(required = 1, rest = true, checkArity = false, meta = true) public static IRubyObject lchmod(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 1, -1); - - Ruby runtime = context.runtime; - int count = 0; RubyInteger mode = args[0].convertToInteger(); + for (int i = 1; i < argc; i++) { JRubyFile file = file(args[i]); - if (0 != runtime.getPosix().lchmod(file.toString(), (int) mode.getLongValue())) { - throw runtime.newErrnoFromLastPOSIXErrno(); + if (0 != context.runtime.getPosix().lchmod(file.toString(), (int) mode.getLongValue())) { + throw context.runtime.newErrnoFromLastPOSIXErrno(); } else { count++; } } - return runtime.newFixnum(count); + return asFixnum(context, count); } @JRubyMethod(required = 2, rest = true, checkArity = false, meta = true) public static IRubyObject lchown(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 2, -1); - - Ruby runtime = context.runtime; int owner = !args[0].isNil() ? RubyNumeric.num2int(args[0]) : -1; int group = !args[1].isNil() ? RubyNumeric.num2int(args[1]) : -1; int count = 0; @@ -1127,14 +1113,14 @@ public static IRubyObject lchown(ThreadContext context, IRubyObject recv, IRubyO for (int i = 2; i < argc; i++) { JRubyFile file = file(args[i]); - if (0 != runtime.getPosix().lchown(file.toString(), owner, group)) { - throw runtime.newErrnoFromLastPOSIXErrno(); + if (0 != context.runtime.getPosix().lchown(file.toString(), owner, group)) { + throw context.runtime.newErrnoFromLastPOSIXErrno(); } else { count++; } } - return runtime.newFixnum(count); + return asFixnum(context, count); } @JRubyMethod(meta = true) @@ -1155,14 +1141,13 @@ public static IRubyObject link(ThreadContext context, IRubyObject recv, IRubyObj throw runtime.newErrnoEEXISTError(fromStr + " or " + toStr); } } - return runtime.newFixnum(ret); + return asFixnum(context, ret); } @JRubyMethod(meta = true) public static IRubyObject mtime(ThreadContext context, IRubyObject recv, IRubyObject filename) { - Ruby runtime = context.runtime; - String f = StringSupport.checkEmbeddedNulls(runtime, get_path(context, filename)).toString(); - return runtime.newFileStat(f, false).mtime(); + String f = StringSupport.checkEmbeddedNulls(context.runtime, get_path(context, filename)).toString(); + return context.runtime.newFileStat(f, false).mtime(context); } @JRubyMethod(meta = true) @@ -1278,19 +1263,17 @@ public static IRubyObject truncate(ThreadContext context, IRubyObject recv, IRub @JRubyMethod(meta = true, optional = 1, checkArity = false) public static IRubyObject umask(ThreadContext context, IRubyObject recv, IRubyObject[] args) { int argc = Arity.checkArgumentCount(context, args, 0, 1); - - Ruby runtime = context.runtime; int oldMask; if (argc == 0) { - oldMask = PosixShim.umask(runtime.getPosix()); + oldMask = PosixShim.umask(context.runtime.getPosix()); } else if (argc == 1) { int newMask = (int) args[0].convertToInteger().getLongValue(); - oldMask = PosixShim.umask(runtime.getPosix(), newMask); + oldMask = PosixShim.umask(context.runtime.getPosix(), newMask); } else { - throw runtime.newArgumentError("wrong number of arguments"); + throw context.runtime.newArgumentError("wrong number of arguments"); } - return runtime.newFixnum(oldMask); + return asFixnum(context, oldMask); } @JRubyMethod(required = 2, rest = true, checkArity = false, meta = true) @@ -1321,7 +1304,7 @@ public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyO } } - return runtime.newFixnum(argc - 2); + return asFixnum(context, argc - 2); } @JRubyMethod(required = 2, rest = true, checkArity = false, meta = true) @@ -1362,7 +1345,7 @@ public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyOb } } - return runtime.newFixnum(argc - 2); + return asFixnum(context, argc - 2); } @JRubyMethod(rest = true, meta = true) @@ -1388,7 +1371,7 @@ public static IRubyObject delete(ThreadContext context, IRubyObject recv, IRubyO } } - return runtime.newFixnum(args.length); + return asFixnum(context, args.length); } private static boolean isSymlink(ThreadContext context, JRubyFile file) { @@ -1411,7 +1394,7 @@ public static IRubyObject unlink(ThreadContext context, IRubyObject recv, IRubyO } } - return runtime.newFixnum(args.length); + return asFixnum(context, args.length); } public static IRubyObject unlink(ThreadContext context, IRubyObject... args) { @@ -1617,10 +1600,9 @@ public static FileResource fileResource(ThreadContext context, IRubyObject pathO * * @param pathOrFile the string or IO to use for the path */ + @Deprecated public static FileResource fileResource(IRubyObject pathOrFile) { - ThreadContext context = pathOrFile.getRuntime().getCurrentContext(); - - return fileResource(context, pathOrFile); + return fileResource(((RubyBasicObject)pathOrFile).getCurrentContext(), pathOrFile); } private static String getDecodedPath(ThreadContext context, IRubyObject pathOrFile) { @@ -1648,7 +1630,7 @@ private static String getDecodedPath(ThreadContext context, IRubyObject pathOrFi @Deprecated // Use fileResource instead public static JRubyFile file(IRubyObject pathOrFile) { - return fileResource(pathOrFile).unwrap(JRubyFile.class); + return fileResource(((RubyBasicObject) pathOrFile).getCurrentContext(), pathOrFile).unwrap(JRubyFile.class); } @Override diff --git a/core/src/main/java/org/jruby/RubyFileStat.java b/core/src/main/java/org/jruby/RubyFileStat.java index 36fa8250691..61ca6337d77 100644 --- a/core/src/main/java/org/jruby/RubyFileStat.java +++ b/core/src/main/java/org/jruby/RubyFileStat.java @@ -42,6 +42,7 @@ import org.jruby.anno.JRubyMethod; import jnr.posix.FileStat; import jnr.posix.util.Platform; +import org.jruby.api.Convert; import org.jruby.runtime.Block; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; @@ -50,6 +51,8 @@ import org.jruby.util.JRubyFile; import org.jruby.util.StringSupport; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; /** @@ -67,8 +70,8 @@ public class RubyFileStat extends RubyObject { private FileResource file; private FileStat stat; - private void checkInitialized() { - if (stat == null) throw typeError(getRuntime().getCurrentContext(), "uninitialized File::Stat"); + private void checkInitialized(ThreadContext context) { + if (stat == null) throw typeError(context, "uninitialized File::Stat"); } public static RubyClass createFileStatClass(Ruby runtime) { @@ -160,149 +163,235 @@ public IRubyObject initialize(IRubyObject fname, Block unusedBlock) { } @JRubyMethod(name = "atime") - public IRubyObject atime() { - checkInitialized(); + public IRubyObject atime(ThreadContext context) { + checkInitialized(context); if (stat instanceof NanosecondFileStat) { - return RubyTime.newTimeFromNanoseconds(getRuntime(), stat.atime() * BILLION + ((NanosecondFileStat) stat).aTimeNanoSecs()); + return RubyTime.newTimeFromNanoseconds(context.runtime, stat.atime() * BILLION + ((NanosecondFileStat) stat).aTimeNanoSecs()); } - return getRuntime().newTime(stat.atime() * 1000); + return context.runtime.newTime(stat.atime() * 1000); + } + + @Deprecated + public IRubyObject atime() { + return atime(getCurrentContext()); } @JRubyMethod(name = "blksize") public IRubyObject blockSize(ThreadContext context) { - checkInitialized(); + checkInitialized(context); if (Platform.IS_WINDOWS) return context.nil; - return context.runtime.newFixnum(stat.blockSize()); + return asFixnum(context, stat.blockSize()); } @Deprecated public RubyFixnum blksize() { - checkInitialized(); - return getRuntime().newFixnum(stat.blockSize()); + ThreadContext context = getCurrentContext(); + checkInitialized(context); + return asFixnum(context, stat.blockSize()); } @JRubyMethod(name = "blockdev?") + public IRubyObject blockdev_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isBlockDev()); + } + + @Deprecated public IRubyObject blockdev_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isBlockDev()); + return blockdev_p(getCurrentContext()); } @JRubyMethod(name = "blocks") + public IRubyObject blocks(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.nil; + return asFixnum(context, stat.blocks()); + } + + @Deprecated public IRubyObject blocks() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getNil(); - return getRuntime().newFixnum(stat.blocks()); + return blocks(getCurrentContext()); } @JRubyMethod(name = "chardev?") + public IRubyObject chardev_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isCharDev()); + } + + @Deprecated public IRubyObject chardev_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isCharDev()); + return chardev_p(getCurrentContext()); } @JRubyMethod(name = "<=>") - public IRubyObject cmp(IRubyObject other) { - checkInitialized(); - if (!(other instanceof RubyFileStat)) return getRuntime().getNil(); - + public IRubyObject cmp(ThreadContext context, IRubyObject other) { + checkInitialized(context); + if (!(other instanceof RubyFileStat)) return context.nil; + long time1 = stat.mtime(); long time2 = ((RubyFileStat) other).stat.mtime(); - + if (time1 == time2) { - return getRuntime().newFixnum(0); + return asFixnum(context, 0); } else if (time1 < time2) { - return getRuntime().newFixnum(-1); - } + return asFixnum(context, -1); + } else { + return asFixnum(context, 1); + } + } - return getRuntime().newFixnum(1); + @Deprecated + public IRubyObject cmp(IRubyObject other) { + return cmp(getCurrentContext(), other); } @JRubyMethod(name = "ctime") - public IRubyObject ctime() { - checkInitialized(); + public IRubyObject ctime(ThreadContext context) { + checkInitialized(context); if (stat instanceof NanosecondFileStat) { - return RubyTime.newTimeFromNanoseconds(getRuntime(), stat.ctime() * BILLION + ((NanosecondFileStat) stat).cTimeNanoSecs()); + return RubyTime.newTimeFromNanoseconds(context.runtime, stat.ctime() * BILLION + ((NanosecondFileStat) stat).cTimeNanoSecs()); } - return getRuntime().newTime(stat.ctime() * 1000); + return context.runtime.newTime(stat.ctime() * 1000); + } + + @Deprecated + public IRubyObject ctime() { + return ctime(getCurrentContext()); } @JRubyMethod(name = "birthtime") - public IRubyObject birthtime() { - checkInitialized(); + public IRubyObject birthtime(ThreadContext context) { + checkInitialized(context); FileTime btime; if (Platform.IS_LINUX) { - throw getRuntime().newNotImplementedError("birthtime() function is unimplemented on this machine"); + throw context.runtime.newNotImplementedError("birthtime() function is unimplemented on this machine"); } if (file == null || (btime = RubyFile.getBirthtimeWithNIO(file.absolutePath())) == null) { return ctime(); } - return getRuntime().newTime(btime.toMillis()); + return context.runtime.newTime(btime.toMillis()); + } + + @Deprecated + public IRubyObject birthtime() { + return birthtime(getCurrentContext()); } @JRubyMethod(name = "dev") + public IRubyObject dev(ThreadContext context) { + checkInitialized(context); + return asFixnum(context, stat.dev()); + } + + @Deprecated public IRubyObject dev() { - checkInitialized(); - return getRuntime().newFixnum(stat.dev()); + return dev(getCurrentContext()); } @JRubyMethod(name = "dev_major") + public IRubyObject devMajor(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.nil; + return asFixnum(context, stat.major(stat.dev())); + } + + @Deprecated public IRubyObject devMajor() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getNil(); - return getRuntime().newFixnum(stat.major(stat.dev())); + return devMajor(getCurrentContext()); } @JRubyMethod(name = "dev_minor") + public IRubyObject devMinor(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.nil; + return asFixnum(context, stat.minor(stat.dev())); + } + + @Deprecated public IRubyObject devMinor() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getNil(); - return getRuntime().newFixnum(stat.minor(stat.dev())); + return devMinor(getCurrentContext()); } @JRubyMethod(name = "directory?") + public RubyBoolean directory_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isDirectory()); + } + + @Deprecated public RubyBoolean directory_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isDirectory()); + return directory_p(getCurrentContext()); } @JRubyMethod(name = "executable?") + public IRubyObject executable_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isExecutable()); + } + + @Deprecated public IRubyObject executable_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isExecutable()); + return executable_p(getCurrentContext()); } @JRubyMethod(name = "executable_real?") + public IRubyObject executableReal_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isExecutableReal()); + } + + @Deprecated public IRubyObject executableReal_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isExecutableReal()); + return executableReal_p(getCurrentContext()); } @JRubyMethod(name = "file?") + public RubyBoolean file_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isFile()); + } + + @Deprecated public RubyBoolean file_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isFile()); + return file_p(getCurrentContext()); } @JRubyMethod(name = "ftype") + public RubyString ftype(ThreadContext context) { + checkInitialized(context); + return Convert.asString(context, stat.ftype()); + } + + @Deprecated public RubyString ftype() { - checkInitialized(); - return getRuntime().newString(stat.ftype()); + return ftype(getCurrentContext()); } @JRubyMethod(name = "gid") + public IRubyObject gid(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return RubyFixnum.zero(context.runtime); + return asFixnum(context, stat.gid()); + } + + @Deprecated public IRubyObject gid() { - checkInitialized(); - if (Platform.IS_WINDOWS) return RubyFixnum.zero(getRuntime()); - return getRuntime().newFixnum(stat.gid()); + return gid(getCurrentContext()); } @JRubyMethod(name = "grpowned?") + public IRubyObject group_owned_p(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.fals; + return asBoolean(context, stat.isGroupOwned()); + } + + @Deprecated public IRubyObject group_owned_p() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getFalse(); - return getRuntime().newBoolean(stat.isGroupOwned()); + return group_owned_p(getCurrentContext()); } @JRubyMethod(name = "initialize_copy", visibility = Visibility.PRIVATE) @@ -321,20 +410,25 @@ public IRubyObject initialize_copy(IRubyObject original) { } @JRubyMethod(name = "ino") + public IRubyObject ino(ThreadContext context) { + checkInitialized(context); + return asFixnum(context, stat.ino()); + } + + @Deprecated public IRubyObject ino() { - checkInitialized(); - return metaClass.runtime.newFixnum(stat.ino()); + return ino(getCurrentContext()); } @JRubyMethod(name = "inspect") @Override public IRubyObject inspect() { + ThreadContext context = metaClass.runtime.getCurrentContext(); StringBuilder buf = new StringBuilder("#<"); buf.append(getMetaClass().getRealClass().getName()); if (stat == null) { buf.append(": uninitialized"); } else { - ThreadContext context = metaClass.runtime.getCurrentContext(); buf.append(' '); // FIXME: Obvious issue that not all platforms can display all attributes. Ugly hacks. // Using generic posix library makes pushing inspect behavior into specific system impls @@ -346,7 +440,7 @@ public IRubyObject inspect() { try { buf.append("uid=").append(stat.uid()); } catch (Exception e) {} finally { buf.append(", "); } try { buf.append("gid=").append(stat.gid()); } catch (Exception e) {} finally { buf.append(", "); } try { buf.append("rdev=0x").append(Long.toHexString(stat.rdev())); } catch (Exception e) {} finally { buf.append(", "); } - buf.append("size=").append(sizeInternal()).append(", "); + buf.append("size=").append(sizeInternal(context)).append(", "); try { buf.append("blksize=").append(blockSize(context).inspect().toString()); } catch (Exception e) {} finally { buf.append(", "); } try { buf.append("blocks=").append(blocks().inspect().toString()); } catch (Exception e) {} finally { buf.append(", "); } @@ -360,44 +454,63 @@ public IRubyObject inspect() { } buf.append('>'); - return getRuntime().newString(buf.toString()); + return Convert.asString(context, buf.toString()); } @JRubyMethod(name = "uid") + public IRubyObject uid(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return RubyFixnum.zero(context.runtime); + return asFixnum(context, stat.uid()); + } + + @Deprecated public IRubyObject uid() { - checkInitialized(); - if (Platform.IS_WINDOWS) return RubyFixnum.zero(getRuntime()); - return getRuntime().newFixnum(stat.uid()); + return uid(getCurrentContext()); } @JRubyMethod(name = "mode") + public IRubyObject mode(ThreadContext context) { + checkInitialized(context); + return asFixnum(context, stat.mode()); + } + + @Deprecated public IRubyObject mode() { - checkInitialized(); - return getRuntime().newFixnum(stat.mode()); + return mode(getCurrentContext()); } @JRubyMethod(name = "mtime") + public IRubyObject mtime(ThreadContext context) { + checkInitialized(context); + return stat instanceof NanosecondFileStat ? + RubyTime.newTimeFromNanoseconds(context.runtime, stat.mtime() * BILLION + ((NanosecondFileStat) stat).mTimeNanoSecs()) : + context.runtime.newTime(stat.mtime() * 1000); + } + + @Deprecated public IRubyObject mtime() { - checkInitialized(); - if (stat instanceof NanosecondFileStat) { - return RubyTime.newTimeFromNanoseconds(getRuntime(), stat.mtime() * BILLION + ((NanosecondFileStat) stat).mTimeNanoSecs()); - } - return getRuntime().newTime(stat.mtime() * 1000); + return mtime(getCurrentContext()); } - - public IRubyObject mtimeEquals(IRubyObject other) { - FileStat otherStat = newFileStat(getRuntime(), other.convertToString().toString(), false).stat; + + public IRubyObject mtimeEquals(ThreadContext context, IRubyObject other) { + FileStat otherStat = newFileStat(context.runtime, other.convertToString().toString(), false).stat; boolean equal = stat.mtime() == otherStat.mtime(); if (stat instanceof NanosecondFileStat && otherStat instanceof NanosecondFileStat) { equal = equal && ((NanosecondFileStat) stat).mTimeNanoSecs() == ((NanosecondFileStat) otherStat).mTimeNanoSecs(); } - return getRuntime().newBoolean(equal); + return asBoolean(context, equal); } - public IRubyObject mtimeGreaterThan(IRubyObject other) { - FileStat otherStat = newFileStat(getRuntime(), other.convertToString().toString(), false).stat; + @Deprecated + public IRubyObject mtimeEquals(IRubyObject other) { + return mtimeEquals(getCurrentContext(), other); + } + + public IRubyObject mtimeGreaterThan(ThreadContext context, IRubyObject other) { + FileStat otherStat = newFileStat(context.runtime, other.convertToString().toString(), false).stat; boolean gt; if (stat instanceof NanosecondFileStat && otherStat instanceof NanosecondFileStat) { @@ -407,11 +520,16 @@ public IRubyObject mtimeGreaterThan(IRubyObject other) { gt = stat.mtime() > otherStat.mtime(); } - return getRuntime().newBoolean(gt); + return asBoolean(context, gt); } - public IRubyObject mtimeLessThan(IRubyObject other) { - FileStat otherStat = newFileStat(getRuntime(), other.convertToString().toString(), false).stat; + @Deprecated + public IRubyObject mtimeGreaterThan(IRubyObject other) { + return mtimeGreaterThan(getCurrentContext(), other); + } + + public IRubyObject mtimeLessThan(ThreadContext context, IRubyObject other) { + FileStat otherStat = newFileStat(context.runtime, other.convertToString().toString(), false).stat; boolean lt; if (stat instanceof NanosecondFileStat && otherStat instanceof NanosecondFileStat) { @@ -421,73 +539,128 @@ public IRubyObject mtimeLessThan(IRubyObject other) { lt = stat.mtime() < otherStat.mtime(); } - return getRuntime().newBoolean(lt); + return asBoolean(context, lt); + } + + @Deprecated + public IRubyObject mtimeLessThan(IRubyObject other) { + return mtimeLessThan(getCurrentContext(), other); } @JRubyMethod(name = "nlink") + public IRubyObject nlink(ThreadContext context) { + checkInitialized(context); + return asFixnum(context, stat.nlink()); + } + + @Deprecated public IRubyObject nlink() { - checkInitialized(); - return getRuntime().newFixnum(stat.nlink()); + return nlink(getCurrentContext()); } @JRubyMethod(name = "owned?") + public IRubyObject owned_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isOwned()); + } + + @Deprecated public IRubyObject owned_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isOwned()); + return owned_p(getCurrentContext()); } @JRubyMethod(name = "pipe?") + public IRubyObject pipe_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isNamedPipe()); + } + + @Deprecated public IRubyObject pipe_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isNamedPipe()); + return pipe_p(getCurrentContext()); } @JRubyMethod(name = "rdev") + public IRubyObject rdev(ThreadContext context) { + checkInitialized(context); + return asFixnum(context, stat.rdev()); + } + + @Deprecated public IRubyObject rdev() { - checkInitialized(); - return getRuntime().newFixnum(stat.rdev()); + return rdev(getCurrentContext()); } @JRubyMethod(name = "rdev_major") + public IRubyObject rdevMajor(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.nil; + return asFixnum(context, stat.major(stat.rdev())); + } + + @Deprecated public IRubyObject rdevMajor() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getNil(); - return getRuntime().newFixnum(stat.major(stat.rdev())); + return rdevMajor(getCurrentContext()); } @JRubyMethod(name = "rdev_minor") + public IRubyObject rdevMinor(ThreadContext context) { + checkInitialized(context); + if (Platform.IS_WINDOWS) return context.nil; + return asFixnum(context, stat.minor(stat.rdev())); + } + + @Deprecated public IRubyObject rdevMinor() { - checkInitialized(); - if (Platform.IS_WINDOWS) return getRuntime().getNil(); - return getRuntime().newFixnum(stat.minor(stat.rdev())); + return rdevMinor(getCurrentContext()); } @JRubyMethod(name = "readable?") + public IRubyObject readable_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isReadable()); + } + + @Deprecated public IRubyObject readable_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isReadable()); + return readable_p(getCurrentContext()); } @JRubyMethod(name = "readable_real?") + public IRubyObject readableReal_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isReadableReal()); + } + + @Deprecated public IRubyObject readableReal_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isReadableReal()); + return readableReal_p(getCurrentContext()); } @JRubyMethod(name = "setgid?") + public IRubyObject setgid_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isSetgid()); + } + + @Deprecated public IRubyObject setgid_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isSetgid()); + return setgid_p(getCurrentContext()); } @JRubyMethod(name = "setuid?") + public IRubyObject setuid_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isSetuid()); + } + + @Deprecated public IRubyObject setuid_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isSetuid()); + return setuid_p(getCurrentContext()); } - private long sizeInternal() { - checkInitialized(); + private long sizeInternal(ThreadContext context) { + checkInitialized(context); // Workaround for JRUBY-4149 if (Platform.IS_WINDOWS && file != null) { try { @@ -501,59 +674,96 @@ private long sizeInternal() { } @JRubyMethod(name = "size") + public IRubyObject size(ThreadContext context) { + return asFixnum(context, sizeInternal(context)); + } + + @Deprecated public IRubyObject size() { - return getRuntime().newFixnum(sizeInternal()); + return size(getCurrentContext()); } @JRubyMethod(name = "size?") + public IRubyObject size_p(ThreadContext context) { + long size = sizeInternal(context); + + if (size == 0) return context.nil; + + return asFixnum(context, size); + } + + @Deprecated public IRubyObject size_p() { - long size = sizeInternal(); - - if (size == 0) return getRuntime().getNil(); - - return getRuntime().newFixnum(size); + return size_p(getCurrentContext()); } @JRubyMethod(name = "socket?") + public IRubyObject socket_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isSocket()); + } + + @Deprecated public IRubyObject socket_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isSocket()); + return socket_p(getCurrentContext()); } @JRubyMethod(name = "sticky?") + public IRubyObject sticky_p(ThreadContext context) { + checkInitialized(context); + + return context.runtime.getPosix().isNative() ? + asBoolean(context, stat.isSticky()) : + context.nil; + } + + @Deprecated public IRubyObject sticky_p() { - checkInitialized(); - Ruby runtime = getRuntime(); - - if (runtime.getPosix().isNative()) { - return runtime.newBoolean(stat.isSticky()); - } - - return runtime.getNil(); + return sticky_p(getCurrentContext()); } @JRubyMethod(name = "symlink?") + public IRubyObject symlink_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isSymlink()); + } + + @Deprecated public IRubyObject symlink_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isSymlink()); + return symlink_p(getCurrentContext()); } @JRubyMethod(name = "writable?") + public IRubyObject writable_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isWritable()); + } + + @Deprecated public IRubyObject writable_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isWritable()); + return writable_p(getCurrentContext()); } @JRubyMethod(name = "writable_real?") + public IRubyObject writableReal_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isWritableReal()); + } + + @Deprecated public IRubyObject writableReal_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isWritableReal()); + return writableReal_p(getCurrentContext()); } @JRubyMethod(name = "zero?") + public IRubyObject zero_p(ThreadContext context) { + checkInitialized(context); + return asBoolean(context, stat.isEmpty()); + } + + @Deprecated public IRubyObject zero_p() { - checkInitialized(); - return getRuntime().newBoolean(stat.isEmpty()); + return zero_p(getCurrentContext()); } @JRubyMethod(name = "world_readable?") @@ -567,7 +777,7 @@ public IRubyObject worldWritable(ThreadContext context) { } private IRubyObject getWorldMode(ThreadContext context, int mode) { - checkInitialized(); + checkInitialized(context); if ((stat.mode() & mode) == mode) { return RubyNumeric.int2fix(context.runtime, (stat.mode() & (S_IRUGO | S_IWUGO | S_IXUGO) )); diff --git a/core/src/main/java/org/jruby/RubyFileTest.java b/core/src/main/java/org/jruby/RubyFileTest.java index 38b1ff3712c..550921f753f 100644 --- a/core/src/main/java/org/jruby/RubyFileTest.java +++ b/core/src/main/java/org/jruby/RubyFileTest.java @@ -33,6 +33,8 @@ import static org.jruby.RubyFile.get_path; import static org.jruby.RubyFile.fileResource; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import java.io.File; import java.io.IOException; @@ -62,24 +64,35 @@ public static RubyModule createFileTestModule(Ruby runtime) { } @JRubyMethod(name = "blockdev?", module = true) - public static IRubyObject blockdev_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject blockdev_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isBlockDev()); + return asBoolean(context, stat != null && stat.isBlockDev()); + } + + @Deprecated + public static IRubyObject blockdev_p(IRubyObject recv, IRubyObject filename) { + return blockdev_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "chardev?", module = true) - public static IRubyObject chardev_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject chardev_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); + + return asBoolean(context, stat != null && stat.isCharDev()); + } - return recv.getRuntime().newBoolean(stat != null && stat.isCharDev()); + @Deprecated + public static IRubyObject chardev_p(IRubyObject recv, IRubyObject filename) { + return chardev_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @Deprecated public static IRubyObject directory_p(IRubyObject recv, IRubyObject filename) { - return directory_p(recv.getRuntime().getCurrentContext(), recv, filename); + return directory_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } + @Deprecated public static IRubyObject directory_p(Ruby ruby, IRubyObject filename) { return directory_p(ruby.getCurrentContext(), filename); } @@ -94,35 +107,43 @@ public static IRubyObject directory_p(ThreadContext context, IRubyObject filenam filename = TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } - return RubyBoolean.newBoolean(context, fileResource(context, filename).isDirectory()); + return asBoolean(context, fileResource(context, filename).isDirectory()); } @JRubyMethod(name = "executable?", module = true) + public static IRubyObject executable_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return asBoolean(context, fileResource(context, filename).canExecute()); + } + + @Deprecated public static IRubyObject executable_p(IRubyObject recv, IRubyObject filename) { - return recv.getRuntime().newBoolean(fileResource(filename).canExecute()); + return executable_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "executable_real?", module = true) - public static IRubyObject executable_real_p(IRubyObject recv, IRubyObject filename) { - if (recv.getRuntime().getPosix().isNative()) { - FileStat stat = fileResource(filename).stat(); - - return recv.getRuntime().newBoolean(stat != null && stat.isExecutableReal()); - } - else { - return executable_p(recv, filename); + public static IRubyObject executable_real_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + if (context.runtime.getPosix().isNative()) { + FileStat stat = fileResource(context, filename).stat(); + return asBoolean(context, stat != null && stat.isExecutableReal()); + } else { + return executable_p(context, recv, filename); } } + @Deprecated + public static IRubyObject executable_real_p(IRubyObject recv, IRubyObject filename) { + return executable_real_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); + } + @Deprecated public static IRubyObject exist_p(IRubyObject recv, IRubyObject filename) { - return exist_p(recv.getRuntime().getCurrentContext(), recv, filename); + return exist_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "exist?", module = true) public static IRubyObject exist_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { // We get_path here to prevent doing it both existsOnClasspath and fileResource (Only call to_path once). - return RubyBoolean.newBoolean(context, exist(context, get_path(context, filename))); + return asBoolean(context, exist(context, get_path(context, filename))); } static boolean exist(ThreadContext context, RubyString path) { @@ -135,38 +156,39 @@ static boolean exist(ThreadContext context, RubyString path) { @Deprecated public static RubyBoolean file_p(IRubyObject recv, IRubyObject filename) { - return file_p(recv.getRuntime().getCurrentContext(), recv, filename); + return file_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "file?", module = true) public static RubyBoolean file_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { - return RubyBoolean.newBoolean(context, fileResource(filename).isFile()); + return asBoolean(context, fileResource(context, filename).isFile()); } @JRubyMethod(name = "grpowned?", module = true) - public static IRubyObject grpowned_p(IRubyObject recv, IRubyObject filename) { - Ruby runtime = recv.getRuntime(); - FileStat stat = fileResource(filename).stat(); + public static IRubyObject grpowned_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); // JRUBY-4446, grpowned? always returns false on Windows - if (Platform.IS_WINDOWS) return runtime.getFalse(); - - return runtime.newBoolean(stat != null && stat.isGroupOwned()); + if (Platform.IS_WINDOWS) return context.fals; + + return asBoolean(context, stat != null && stat.isGroupOwned()); + } + + public static IRubyObject grpowned_p(IRubyObject recv, IRubyObject filename) { + return grpowned_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "identical?", module = true) public static IRubyObject identical_p(ThreadContext context, IRubyObject recv, IRubyObject filename1, IRubyObject filename2) { - Ruby runtime = context.runtime; - - FileResource file1 = fileResource(filename1); - FileResource file2 = fileResource(filename2); + FileResource file1 = fileResource(context, filename1); + FileResource file2 = fileResource(context, filename2); // Try posix first - if (!Platform.IS_WINDOWS && runtime.getPosix().isNative()) { + if (!Platform.IS_WINDOWS && context.runtime.getPosix().isNative()) { FileStat stat1 = file1.stat(); FileStat stat2 = file2.stat(); - return runtime.newBoolean(stat1 != null && stat2 != null && stat1.isIdentical(stat2)); + return asBoolean(context, stat1 != null && stat2 != null && stat1.isIdentical(stat2)); } // fallback to NIO2 to support more platforms @@ -174,7 +196,7 @@ public static IRubyObject identical_p(ThreadContext context, IRubyObject recv, I try { Path canon1 = new File(file1.absolutePath()).getCanonicalFile().toPath(); Path canon2 = new File(file2.absolutePath()).getCanonicalFile().toPath(); - return runtime.newBoolean(Files.isSameFile(canon1, canon2)); + return asBoolean(context, Files.isSameFile(canon1, canon2)); } catch (IOException canonicalizationError) { // fall through } @@ -184,22 +206,32 @@ public static IRubyObject identical_p(ThreadContext context, IRubyObject recv, I } @JRubyMethod(name = "owned?", module = true) - public static IRubyObject owned_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject owned_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isOwned()); + return asBoolean(context, stat != null && stat.isOwned()); + } + + @Deprecated + public static IRubyObject owned_p(IRubyObject recv, IRubyObject filename) { + return owned_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "pipe?", module = true) - public static IRubyObject pipe_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject pipe_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isNamedPipe()); + return asBoolean(context, stat != null && stat.isNamedPipe()); + } + + @Deprecated + public static IRubyObject pipe_p(IRubyObject recv, IRubyObject filename) { + return pipe_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @Deprecated public static IRubyObject readable_p(IRubyObject recv, IRubyObject filename) { - return readable_p(recv.getRuntime().getCurrentContext(), recv, filename); + return readable_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } // We use file test since it is faster than a stat; also euid == uid in Java always @@ -210,32 +242,48 @@ public static IRubyObject readable_p(ThreadContext context, IRubyObject recv, IR filename = get_path(context, filename); } - return runtime.newBoolean(fileResource(filename).canRead()); + return asBoolean(context, fileResource(context, filename).canRead()); } // Not exposed by filetest, but so similar in nature that it is stored here - public static IRubyObject rowned_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject rowned_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); + + return asBoolean(context, stat != null && stat.isROwned()); + } - return recv.getRuntime().newBoolean(stat != null && stat.isROwned()); + @Deprecated + public static IRubyObject rowned_p(IRubyObject recv, IRubyObject filename) { + return rowned_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "setgid?", module = true) - public static IRubyObject setgid_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject setgid_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isSetgid()); + return asBoolean(context, stat != null && stat.isSetgid()); + } + + @Deprecated + public static IRubyObject setgid_p(IRubyObject recv, IRubyObject filename) { + return setgid_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "setuid?", module = true) - public static IRubyObject setuid_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject setuid_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isSetuid()); + return asBoolean(context, stat != null && stat.isSetuid()); } + @Deprecated + public static IRubyObject setuid_p(IRubyObject recv, IRubyObject filename) { + return setuid_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); + } + + @Deprecated public static IRubyObject size(IRubyObject recv, IRubyObject filename) { - return size(recv.getRuntime().getCurrentContext(), recv, filename); + return size(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "size", module = true) @@ -244,62 +292,81 @@ public static IRubyObject size(ThreadContext context, IRubyObject recv, IRubyObj filename = TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } - FileStat stat = fileResource(filename).stat(); + FileStat stat = fileResource(context, filename).stat(); - if (stat == null) noFileError(filename); + if (stat == null) noFileError(context, filename); - return context.runtime.newFixnum(stat.st_size()); + return asFixnum(context, stat.st_size()); } @Deprecated public static IRubyObject size_p(IRubyObject recv, IRubyObject filename) { - return size_p(recv.getRuntime().getCurrentContext(), recv, filename); + return size_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "size?", module = true) public static IRubyObject size_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { - Ruby runtime = context.runtime; if (!(filename instanceof RubyFile) && filename.respondsTo("to_io")) { - filename = TypeConverter.convertToType(filename, runtime.getIO(), "to_io"); + filename = TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } - FileStat stat = fileResource(filename).stat(); + FileStat stat = fileResource(context, filename).stat(); - if (stat == null) return runtime.getNil(); + if (stat == null) return context.nil; long length = stat.st_size(); - return length > 0 ? runtime.newFixnum(length) : runtime.getNil(); + return length > 0 ? asFixnum(context, length) : context.nil; } @JRubyMethod(name = "socket?", module = true) - public static IRubyObject socket_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject socket_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isSocket()); + return asBoolean(context, stat != null && stat.isSocket()); + } + + @Deprecated + public static IRubyObject socket_p(IRubyObject recv, IRubyObject filename) { + return socket_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "sticky?", module = true) - public static IRubyObject sticky_p(IRubyObject recv, IRubyObject filename) { - FileStat stat = fileResource(filename).stat(); + public static IRubyObject sticky_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + FileStat stat = fileResource(context, filename).stat(); - return recv.getRuntime().newBoolean(stat != null && stat.isSticky()); + return asBoolean(context, stat != null && stat.isSticky()); + } + + @Deprecated + public static IRubyObject sticky_p(IRubyObject recv, IRubyObject filename) { + return sticky_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "symlink?", module = true) + public static RubyBoolean symlink_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return asBoolean(context, fileResource(context, filename).isSymLink()); + } + + @Deprecated public static RubyBoolean symlink_p(IRubyObject recv, IRubyObject filename) { - return recv.getRuntime().newBoolean(fileResource(filename).isSymLink()); + return symlink_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } // We do both writable and writable_real through the same method because // in our java process effective and real userid will always be the same. @JRubyMethod(name = {"writable?", "writable_real?"}, module = true) + public static RubyBoolean writable_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return asBoolean(context, fileResource(context, filename).canWrite()); + } + + @Deprecated public static RubyBoolean writable_p(IRubyObject recv, IRubyObject filename) { - return filename.getRuntime().newBoolean(fileResource(filename).canWrite()); + return writable_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @Deprecated public static RubyBoolean zero_p(IRubyObject recv, IRubyObject filename) { - return zero_p(recv.getRuntime().getCurrentContext(), recv, filename); + return zero_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = {"empty?", "zero?"}, module = true) @@ -308,15 +375,15 @@ public static RubyBoolean zero_p(ThreadContext context, IRubyObject recv, IRubyO // FIXME: Ultimately we should return a valid stat() from this but without massive NUL coverage // this is less risky. - if (resource.isNull()) return RubyBoolean.newBoolean(context, true); + if (resource.isNull()) return asBoolean(context, true); FileStat stat = resource.stat(); if (stat == null) return context.fals; // MRI behavior, enforced by RubySpecs. - if (stat.isDirectory()) return RubyBoolean.newBoolean(context, Platform.IS_WINDOWS); + if (stat.isDirectory()) return asBoolean(context, Platform.IS_WINDOWS); - return RubyBoolean.newBoolean(context, stat.st_size() == 0L); + return asBoolean(context, stat.st_size() == 0L); } @JRubyMethod(name = "world_readable?", module = true) @@ -344,13 +411,23 @@ public static IRubyObject worldWritable(ThreadContext context, IRubyObject recv, */ public static class FileTestFileMethods { @JRubyMethod(name = "blockdev?") + public static IRubyObject blockdev_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.blockdev_p(context, recv, filename); + } + + @Deprecated public static IRubyObject blockdev_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.blockdev_p(recv, filename); + return blockdev_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "chardev?") + public static IRubyObject chardev_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.chardev_p(context, recv, filename); + } + + @Deprecated public static IRubyObject chardev_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.chardev_p(recv, filename); + return chardev_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "directory?") @@ -359,13 +436,23 @@ public static IRubyObject directory_p(ThreadContext context, IRubyObject recv, I } @JRubyMethod(name = "executable?") + public static IRubyObject executable_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.executable_p(context, recv, filename); + } + + @Deprecated public static IRubyObject executable_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.executable_p(recv, filename); + return executable_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "executable_real?") + public static IRubyObject executable_real_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.executable_real_p(context, recv, filename); + } + + @Deprecated public static IRubyObject executable_real_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.executable_real_p(recv, filename); + return executable_real_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = {"exist?"}) @@ -379,8 +466,13 @@ public static RubyBoolean file_p(ThreadContext context, IRubyObject recv, IRubyO } @JRubyMethod(name = "grpowned?") + public static IRubyObject grpowned_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.grpowned_p(context, recv, filename); + } + + @Deprecated public static IRubyObject grpowned_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.grpowned_p(recv, filename); + return grpowned_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "identical?") @@ -389,13 +481,23 @@ public static IRubyObject identical_p(ThreadContext context, IRubyObject recv, I } @JRubyMethod(name = "owned?") + public static IRubyObject owned_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.owned_p(context, recv, filename); + } + + @Deprecated public static IRubyObject owned_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.owned_p(recv, filename); + return owned_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "pipe?") + public static IRubyObject pipe_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.pipe_p(context, recv, filename); + } + + @Deprecated public static IRubyObject pipe_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.pipe_p(recv, filename); + return pipe_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = {"readable?", "readable_real?"}) @@ -404,13 +506,22 @@ public static IRubyObject readable_p(ThreadContext context, IRubyObject recv, IR } @JRubyMethod(name = "setgid?") + public static IRubyObject setgid_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.setgid_p(context, recv, filename); + } + public static IRubyObject setgid_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.setgid_p(recv, filename); + return setgid_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "setuid?") + public static IRubyObject setuid_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.setuid_p(context, recv, filename); + } + + @Deprecated public static IRubyObject setuid_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.setuid_p(recv, filename); + return setuid_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "size") @@ -424,23 +535,43 @@ public static IRubyObject size_p(ThreadContext context, IRubyObject recv, IRubyO } @JRubyMethod(name = "socket?") + public static IRubyObject socket_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.socket_p(context, recv, filename); + } + + @Deprecated public static IRubyObject socket_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.socket_p(recv, filename); + return socket_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "sticky?") + public static IRubyObject sticky_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.sticky_p(context, recv, filename); + } + + @Deprecated public static IRubyObject sticky_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.sticky_p(recv, filename); + return sticky_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = "symlink?") + public static RubyBoolean symlink_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.symlink_p(context, recv, filename); + } + + @Deprecated public static RubyBoolean symlink_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.symlink_p(recv, filename); + return symlink_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = {"writable?", "writable_real?"}) + public static RubyBoolean writable_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { + return RubyFileTest.writable_p(context, recv, filename); + } + + @Deprecated public static RubyBoolean writable_p(IRubyObject recv, IRubyObject filename) { - return RubyFileTest.writable_p(recv, filename); + return writable_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename); } @JRubyMethod(name = {"empty?", "zero?"}) @@ -460,7 +591,7 @@ public static IRubyObject worldWritable(ThreadContext context, IRubyObject recv, @Deprecated public static IRubyObject identical_p(IRubyObject recv, IRubyObject filename1, IRubyObject filename2) { - return RubyFileTest.identical_p(recv, filename1, filename2); + return RubyFileTest.identical_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename1, filename2); } } @@ -505,13 +636,12 @@ private static boolean existsOnClasspath(ThreadContext context, String path) { return false; } - private static void noFileError(IRubyObject filename) { - throw filename.getRuntime().newErrnoENOENTError("No such file or directory - " + - filename.convertToString()); + private static void noFileError(ThreadContext context, IRubyObject filename) { + throw context.runtime.newErrnoENOENTError("No such file or directory - " + filename.convertToString()); } @Deprecated public static IRubyObject identical_p(IRubyObject recv, IRubyObject filename1, IRubyObject filename2) { - return identical_p(recv.getRuntime().getCurrentContext(), recv, filename1, filename2); + return identical_p(((RubyBasicObject) recv).getCurrentContext(), recv, filename1, filename2); } } diff --git a/core/src/main/java/org/jruby/RubyFixnum.java b/core/src/main/java/org/jruby/RubyFixnum.java index 1c5589db83d..1ebf41736da 100644 --- a/core/src/main/java/org/jruby/RubyFixnum.java +++ b/core/src/main/java/org/jruby/RubyFixnum.java @@ -58,6 +58,8 @@ import org.jruby.util.StringSupport; import org.jruby.util.cli.Options; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.util.Numeric.f_odd_p; @@ -155,9 +157,7 @@ public final boolean eql(IRubyObject other) { public IRubyObject equal_p(ThreadContext context, IRubyObject obj) { long value = this.value; - if (fixnumable(value)) { - return RubyBoolean.newBoolean(context, this == obj || eql(obj)); - } + if (fixnumable(value)) return asBoolean(context, this == obj || eql(obj)); return super.equal_p(context, obj); } @@ -696,10 +696,8 @@ public IRubyObject idiv(ThreadContext context, long y, String method) { } private RubyInteger idivLong(ThreadContext context, long x, long y) { - final Ruby runtime = context.runtime; - if (y == 0) { - throw runtime.newZeroDivisionError(); - } + if (y == 0) throw context.runtime.newZeroDivisionError(); + long result; if (y > 0) { if (x >= 0) { @@ -710,14 +708,12 @@ private RubyInteger idivLong(ThreadContext context, long x, long y) { } else if (x > 0) { result = (x - 1) / y - 1; // x > 0, y < 0; } else if (y == -1) { - if (x == MIN) { - return RubyBignum.newBignum(runtime, BigInteger.valueOf(x).negate()); - } + if (x == MIN) return RubyBignum.newBignum(context.runtime, BigInteger.valueOf(x).negate()); result = -x; } else { result = x / y; // x <= 0, y < 0; } - return runtime.newFixnum(result); + return asFixnum(context, result); } /** fix_mod @@ -755,14 +751,13 @@ private IRubyObject moduloFixnum(ThreadContext context, long other) { // Java / and % are not the same as ruby long x = value; long y = other; - if (y == 0) { - throw context.runtime.newZeroDivisionError(); - } + if (y == 0) throw context.runtime.newZeroDivisionError(); + long mod = x % y; if (mod < 0 && y > 0 || mod > 0 && y < 0) { mod += y; } - return context.runtime.newFixnum(mod); + return asFixnum(context, mod); } /** fix_divmod @@ -884,13 +879,11 @@ private RubyNumeric powerFixnum(ThreadContext context, RubyFixnum other) { // MRI: int_pow_tmp1 protected IRubyObject intPowTmp1(ThreadContext context, RubyInteger y, long mm, boolean negaFlg) { - Ruby runtime = context.runtime; - long xx = this.value; long tmp = 1L; long yy; - for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(runtime))) { + for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(context.runtime))) { if (f_odd_p(context, y)) { tmp = (tmp * xx) % mm; } @@ -906,7 +899,7 @@ protected IRubyObject intPowTmp1(ThreadContext context, RubyInteger y, long mm, if (negaFlg && (tmp != 0)) { tmp -= mm; } - return runtime.newFixnum(tmp); + return asFixnum(context, tmp); } @Deprecated @@ -916,15 +909,13 @@ protected IRubyObject intPowTmp2(ThreadContext context, IRubyObject y, final lon // MRI: int_pow_tmp2 IRubyObject intPowTmp2(ThreadContext context, RubyInteger y, final long mm, boolean negaFlg) { - Ruby runtime = context.runtime; - long tmp = 1L; long yy; - RubyFixnum tmp2 = runtime.newFixnum(tmp); + RubyFixnum tmp2 = asFixnum(context, tmp); RubyFixnum xx = this; - for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(runtime))) { + for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(context.runtime))) { if (f_odd_p(context, y)) { tmp2 = mulModulo(context, tmp2, xx, mm); } @@ -941,7 +932,7 @@ IRubyObject intPowTmp2(ThreadContext context, RubyInteger y, final long mm, bool if (negaFlg && (tmp != 0)) { tmp -= mm; } - return runtime.newFixnum(tmp); + return asFixnum(context, tmp); } // MRI: MUL_MODULO macro defined within int_pow_tmp2 in numeric.c @@ -982,11 +973,11 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) { } public IRubyObject op_equal(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value == other); + return asBoolean(context, value == other); } public IRubyObject op_equal(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, (double) value == other); + return asBoolean(context, (double) value == other); } /** fix_not_equal @@ -999,11 +990,11 @@ public IRubyObject op_not_equal(ThreadContext context, IRubyObject other) { } public IRubyObject op_not_equal(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value != other); + return asBoolean(context, value != other); } public IRubyObject op_not_equal(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, (double) value != other); + return asBoolean(context, (double) value != other); } public boolean op_equal_boolean(ThreadContext context, long other) { @@ -1016,7 +1007,7 @@ public final boolean fastEqual(RubyFixnum other) { private IRubyObject op_equalOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { - return RubyBoolean.newBoolean(context, + return asBoolean(context, BigInteger.valueOf(this.value).compareTo(((RubyBignum) other).value) == 0); } if (other instanceof RubyFloat) { @@ -1085,14 +1076,14 @@ private IRubyObject compareOther(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_gt(ThreadContext context, IRubyObject other) { if (other instanceof RubyFixnum) { - return RubyBoolean.newBoolean(context, value > ((RubyFixnum) other).value); + return asBoolean(context, value > ((RubyFixnum) other).value); } return op_gtOther(context, other); } public IRubyObject op_gt(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value > other); + return asBoolean(context, value > other); } public boolean op_gt_boolean(ThreadContext context, long other) { @@ -1101,11 +1092,11 @@ public boolean op_gt_boolean(ThreadContext context, long other) { private IRubyObject op_gtOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { - return RubyBoolean.newBoolean(context, + return asBoolean(context, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) > 0); } if (other instanceof RubyFloat) { - return RubyBoolean.newBoolean(context, (double) value > ((RubyFloat) other).value); + return asBoolean(context, (double) value > ((RubyFloat) other).value); } return coerceRelOp(context, sites(context).op_gt, other); } @@ -1116,13 +1107,13 @@ private IRubyObject op_gtOther(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_ge(ThreadContext context, IRubyObject other) { if (other instanceof RubyFixnum) { - return RubyBoolean.newBoolean(context, value >= ((RubyFixnum) other).value); + return asBoolean(context, value >= ((RubyFixnum) other).value); } return op_geOther(context, other); } public IRubyObject op_ge(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value >= other); + return asBoolean(context, value >= other); } public boolean op_ge_boolean(ThreadContext context, long other) { @@ -1131,11 +1122,11 @@ public boolean op_ge_boolean(ThreadContext context, long other) { private IRubyObject op_geOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { - return RubyBoolean.newBoolean(context, + return asBoolean(context, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) >= 0); } if (other instanceof RubyFloat) { - return RubyBoolean.newBoolean(context, (double) value >= ((RubyFloat) other).value); + return asBoolean(context, (double) value >= ((RubyFloat) other).value); } return coerceRelOp(context, sites(context).op_ge, other); } @@ -1152,7 +1143,7 @@ public IRubyObject op_lt(ThreadContext context, IRubyObject other) { } public IRubyObject op_lt(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value < other); + return asBoolean(context, value < other); } public boolean op_lt_boolean(ThreadContext context, long other) { @@ -1161,11 +1152,11 @@ public boolean op_lt_boolean(ThreadContext context, long other) { private IRubyObject op_ltOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { - return RubyBoolean.newBoolean(context, + return asBoolean(context, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) < 0); } if (other instanceof RubyFloat) { - return RubyBoolean.newBoolean(context, (double) value < ((RubyFloat) other).value); + return asBoolean(context, (double) value < ((RubyFloat) other).value); } return coerceRelOp(context, sites(context).op_lt, other); } @@ -1176,13 +1167,13 @@ private IRubyObject op_ltOther(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_le(ThreadContext context, IRubyObject other) { if (other instanceof RubyFixnum) { - return RubyBoolean.newBoolean(context, value <= ((RubyFixnum) other).value); + return asBoolean(context, value <= ((RubyFixnum) other).value); } return op_leOther(context, other); } public IRubyObject op_le(ThreadContext context, long other) { - return RubyBoolean.newBoolean(context, value <= other); + return asBoolean(context, value <= other); } public boolean op_le_boolean(ThreadContext context, long other) { @@ -1191,11 +1182,11 @@ public boolean op_le_boolean(ThreadContext context, long other) { private IRubyObject op_leOther(ThreadContext context, IRubyObject other) { if (other instanceof RubyBignum) { - return RubyBoolean.newBoolean(context, + return asBoolean(context, BigInteger.valueOf(value).compareTo(((RubyBignum) other).value) <= 0); } if (other instanceof RubyFloat) { - return RubyBoolean.newBoolean(context, (double) value <= ((RubyFloat) other).value); + return asBoolean(context, (double) value <= ((RubyFloat) other).value); } return coerceRelOp(context, sites(context).op_le, other); } @@ -1213,12 +1204,9 @@ public IRubyObject op_neg(ThreadContext context) { */ @Override public IRubyObject op_and(ThreadContext context, IRubyObject other) { - if (other instanceof RubyFixnum) { - return context.runtime.newFixnum(value & ((RubyFixnum) other).value); - } - if (other instanceof RubyBignum) { - return ((RubyBignum) other).op_and(context, this); - } + if (other instanceof RubyFixnum) return asFixnum(context, value & ((RubyFixnum) other).value); + if (other instanceof RubyBignum) return ((RubyBignum) other).op_and(context, this); + return coerceBit(context, sites(context).checked_op_and, other); } @@ -1231,12 +1219,9 @@ public IRubyObject op_and(ThreadContext context, long other) { */ @Override public IRubyObject op_or(ThreadContext context, IRubyObject other) { - if (other instanceof RubyFixnum) { - return context.runtime.newFixnum(value | ((RubyFixnum) other).value); - } - if (other instanceof RubyBignum) { - return ((RubyBignum) other).op_or(context, this); - } + if (other instanceof RubyFixnum) return asFixnum(context, value | ((RubyFixnum) other).value); + if (other instanceof RubyBignum) return ((RubyBignum) other).op_or(context, this); + return coerceBit(context, sites(context).checked_op_or, other); } @@ -1249,12 +1234,9 @@ public IRubyObject op_or(ThreadContext context, long other) { */ @Override public IRubyObject op_xor(ThreadContext context, IRubyObject other) { - if (other instanceof RubyFixnum) { - return context.runtime.newFixnum(value ^ ((RubyFixnum) other).value); - } - if (other instanceof RubyBignum) { - return ((RubyBignum) other).op_xor(context, this); - } + if (other instanceof RubyFixnum) return asFixnum(context, value ^ ((RubyFixnum) other).value); + if (other instanceof RubyBignum) return ((RubyBignum) other).op_xor(context, this); + return coerceBit(context, sites(context).checked_op_xor, other); } @@ -1377,7 +1359,7 @@ public IRubyObject zero_p() { */ @Override public IRubyObject zero_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, value == 0); + return asBoolean(context, value == 0); } @Override @@ -1403,11 +1385,9 @@ public IRubyObject succ(ThreadContext context) { @Override public IRubyObject bit_length(ThreadContext context) { long tmpValue = value; - if (value < 0) { - tmpValue = ~value; - } + if (tmpValue < 0) tmpValue = ~value; - return context.runtime.newFixnum(64 - Long.numberOfLeadingZeros(tmpValue)); + return asFixnum(context, 64 - Long.numberOfLeadingZeros(tmpValue)); } @Override @@ -1461,7 +1441,7 @@ public IRubyObject fdivDouble(ThreadContext context, IRubyObject y) { public IRubyObject isNegative(ThreadContext context) { CachingCallSite op_lt_site = sites(context).basic_op_lt; if (op_lt_site.isBuiltin(metaClass)) { - return RubyBoolean.newBoolean(context, value < 0); + return asBoolean(context, value < 0); } return op_lt_site.call(context, this, this, RubyFixnum.zero(context.runtime)); } @@ -1470,7 +1450,7 @@ public IRubyObject isNegative(ThreadContext context) { public IRubyObject isPositive(ThreadContext context) { CachingCallSite op_gt_site = sites(context).basic_op_gt; if (op_gt_site.isBuiltin(metaClass)) { - return RubyBoolean.newBoolean(context, value > 0); + return asBoolean(context, value > 0); } return op_gt_site.call(context, this, this, RubyFixnum.zero(context.runtime)); } diff --git a/core/src/main/java/org/jruby/RubyFloat.java b/core/src/main/java/org/jruby/RubyFloat.java index 0e14be5e500..8c0fb9748e3 100644 --- a/core/src/main/java/org/jruby/RubyFloat.java +++ b/core/src/main/java/org/jruby/RubyFloat.java @@ -62,6 +62,7 @@ import org.jruby.util.Numeric; import org.jruby.util.Sprintf; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.util.Numeric.f_abs; import static org.jruby.util.Numeric.f_add; @@ -208,13 +209,13 @@ public int signum() { @Override @JRubyMethod(name = "negative?") public IRubyObject isNegative(ThreadContext context) { - return RubyBoolean.newBoolean(context, isNegative()); + return asBoolean(context, isNegative()); } @Override @JRubyMethod(name = "positive?") public IRubyObject isPositive(ThreadContext context) { - return RubyBoolean.newBoolean(context, isPositive()); + return asBoolean(context, isPositive()); } @Override @@ -496,7 +497,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) { switch (other.getMetaClass().getClassIndex()) { case INTEGER: case FLOAT: - return RubyBoolean.newBoolean(context, value == ((RubyNumeric) other).getDoubleValue()); + return asBoolean(context, value == ((RubyNumeric) other).getDoubleValue()); default: // Numeric.equal return super.op_num_equal(context, other); @@ -507,14 +508,14 @@ public IRubyObject op_equal(ThreadContext context, double other) { if (Double.isNaN(value)) { return context.fals; } - return RubyBoolean.newBoolean(context, value == other); + return asBoolean(context, value == other); } public IRubyObject op_not_equal(ThreadContext context, double other) { if (Double.isNaN(value)) { return context.tru; } - return RubyBoolean.newBoolean(context, value != other); + return asBoolean(context, value != other); } public boolean fastEqual(RubyFloat other) { @@ -581,14 +582,14 @@ public IRubyObject op_gt(ThreadContext context, IRubyObject other) { case INTEGER: case FLOAT: double b = ((RubyNumeric) other).getDoubleValue(); - return RubyBoolean.newBoolean(context, !Double.isNaN(b) && value > b); + return asBoolean(context, !Double.isNaN(b) && value > b); default: return coerceRelOp(context, sites(context).op_gt, other); } } public IRubyObject op_gt(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, !Double.isNaN(other) && value > other); + return asBoolean(context, !Double.isNaN(other) && value > other); } /** flo_ge @@ -600,14 +601,14 @@ public IRubyObject op_ge(ThreadContext context, IRubyObject other) { case INTEGER: case FLOAT: double b = ((RubyNumeric) other).getDoubleValue(); - return RubyBoolean.newBoolean(context, !Double.isNaN(b) && value >= b); + return asBoolean(context, !Double.isNaN(b) && value >= b); default: return coerceRelOp(context, sites(context).op_ge, other); } } public IRubyObject op_ge(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, !Double.isNaN(other) && value >= other); + return asBoolean(context, !Double.isNaN(other) && value >= other); } /** flo_lt @@ -619,14 +620,14 @@ public IRubyObject op_lt(ThreadContext context, IRubyObject other) { case INTEGER: case FLOAT: double b = ((RubyNumeric) other).getDoubleValue(); - return RubyBoolean.newBoolean(context, !Double.isNaN(b) && value < b); + return asBoolean(context, !Double.isNaN(b) && value < b); default: return coerceRelOp(context, sites(context).op_lt, other); } } public IRubyObject op_lt(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, !Double.isNaN(other) && value < other); + return asBoolean(context, !Double.isNaN(other) && value < other); } /** flo_le @@ -638,14 +639,14 @@ public IRubyObject op_le(ThreadContext context, IRubyObject other) { case INTEGER: case FLOAT: double b = ((RubyNumeric) other).getDoubleValue(); - return RubyBoolean.newBoolean(context, !Double.isNaN(b) && value <= b); + return asBoolean(context, !Double.isNaN(b) && value <= b); default: return coerceRelOp(context, sites(context).op_le, other); } } public IRubyObject op_le(ThreadContext context, double other) { - return RubyBoolean.newBoolean(context, !Double.isNaN(other) && value <= other); + return asBoolean(context, !Double.isNaN(other) && value <= other); } /** flo_eql @@ -733,7 +734,7 @@ public IRubyObject magnitude(ThreadContext context) { @JRubyMethod(name = "zero?") @Override public IRubyObject zero_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, value == 0.0); + return asBoolean(context, value == 0.0); } @Override @@ -1166,8 +1167,13 @@ private IRubyObject floatRoundByRational(ThreadContext context, IRubyObject ndig * */ @JRubyMethod(name = "nan?") + public IRubyObject nan_p(ThreadContext context) { + return asBoolean(context, isNaN()); + } + + @Deprecated public IRubyObject nan_p() { - return RubyBoolean.newBoolean(metaClass.runtime, isNaN()); + return nan_p(getRuntime().getCurrentContext()); } public boolean isNaN() { @@ -1290,7 +1296,7 @@ public IRubyObject id() { public IRubyObject equal_p(ThreadContext context, IRubyObject obj) { // if flonum, simlulate identity if (flonumable(value)) { - return RubyBoolean.newBoolean(context, this == obj || eql(obj)); + return asBoolean(context, this == obj || eql(obj)); } else { return super.equal_p(context, obj); } diff --git a/core/src/main/java/org/jruby/RubyGC.java b/core/src/main/java/org/jruby/RubyGC.java index f5630f964a5..286e10e41c7 100644 --- a/core/src/main/java/org/jruby/RubyGC.java +++ b/core/src/main/java/org/jruby/RubyGC.java @@ -38,6 +38,9 @@ import org.jruby.common.IRubyWarnings.ID; import org.jruby.runtime.Arity; import org.jruby.runtime.ThreadContext; + +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.runtime.Visibility.*; import org.jruby.runtime.builtin.IRubyObject; @@ -84,7 +87,7 @@ public static IRubyObject garbage_collect(ThreadContext context, IRubyObject rec public static IRubyObject measure_total_time(ThreadContext context, IRubyObject self) { // JVM just keeps track of this so we do not have a toggle here. If we need to show incremental time // from a particular point we will need to record time and do some extra math. - return context.runtime.newBoolean(measureTotalTime); + return asBoolean(context, measureTotalTime); } @JRubyMethod(module = true, name = "measure_total_time=", visibility = PRIVATE) @@ -97,7 +100,7 @@ public static IRubyObject measure_total_time_set(ThreadContext context, IRubyObj @JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject total_time(ThreadContext context, IRubyObject self) { - return context.runtime.newFixnum(getCollectionTime()); + return asFixnum(context, getCollectionTime()); } @JRubyMethod(module = true, visibility = PRIVATE) @@ -106,7 +109,7 @@ public static IRubyObject enable(ThreadContext context, IRubyObject recv) { emptyImplementationWarning(runtime, ID.GC_ENABLE_UNIMPLEMENTED, "GC.enable"); boolean old = gcDisabled; gcDisabled = false; - return runtime.newBoolean(old); + return asBoolean(context, old); } @JRubyMethod(module = true, visibility = PRIVATE) @@ -115,27 +118,25 @@ public static IRubyObject disable(ThreadContext context, IRubyObject recv) { emptyImplementationWarning(runtime, ID.GC_DISABLE_UNIMPLEMENTED, "GC.disable"); boolean old = gcDisabled; gcDisabled = true; - return runtime.newBoolean(old); + return asBoolean(context, old); } @JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject stress(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, stress); + return asBoolean(context, stress); } @JRubyMethod(name = "stress=", module = true, visibility = PRIVATE) public static IRubyObject stress_set(ThreadContext context, IRubyObject recv, IRubyObject arg) { - Ruby runtime = context.runtime; - emptyImplementationWarning(runtime, ID.GC_STRESS_UNIMPLEMENTED, "GC.stress="); + emptyImplementationWarning(context.runtime, ID.GC_STRESS_UNIMPLEMENTED, "GC.stress="); stress = arg.isTrue(); - return runtime.newBoolean(stress); + return asBoolean(context, stress); } @JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject count(ThreadContext context, IRubyObject recv) { try { - int count = getCollectionCount(); - return context.runtime.newFixnum(count); + return asFixnum(context, getCollectionCount()); } catch (Throwable t) { return RubyFixnum.minus_one(context.runtime); } @@ -145,7 +146,7 @@ public static IRubyObject count(ThreadContext context, IRubyObject recv) { public static IRubyObject auto_compact(ThreadContext context, IRubyObject recv) { emptyImplementationWarning(context.runtime, ID.GC_ENABLE_UNIMPLEMENTED, "GC.auto_compact"); - return RubyBoolean.newBoolean(context, autoCompact); + return asBoolean(context, autoCompact); } @JRubyMethod(name = "auto_compact=", module = true, visibility = PRIVATE) diff --git a/core/src/main/java/org/jruby/RubyHash.java b/core/src/main/java/org/jruby/RubyHash.java index 1ba169c57a1..f33c7a0fb07 100644 --- a/core/src/main/java/org/jruby/RubyHash.java +++ b/core/src/main/java/org/jruby/RubyHash.java @@ -79,6 +79,8 @@ import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyEnumerator.SizeFn; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.PRIVATE; import static org.jruby.util.Inspector.*; @@ -960,7 +962,7 @@ public IRubyObject inspect(ThreadContext context) { */ @JRubyMethod(name = {"size", "length"}) public RubyFixnum rb_size(ThreadContext context) { - return context.runtime.newFixnum(size); + return asFixnum(context, size); } /** @@ -1320,7 +1322,7 @@ public IRubyObject op_lt(ThreadContext context, IRubyObject other) { final RubyHash otherHash = ((RubyBasicObject) other).convertToHash(); if (size() >= otherHash.size()) return context.fals; - return RubyBoolean.newBoolean(context, hash_le(otherHash)); + return asBoolean(context, hash_le(otherHash)); } @JRubyMethod(name = "<=") @@ -1328,7 +1330,7 @@ public IRubyObject op_le(ThreadContext context, IRubyObject other) { final RubyHash otherHash = other.convertToHash(); if (size() > otherHash.size()) return context.fals; - return RubyBoolean.newBoolean(context, hash_le(otherHash)); + return asBoolean(context, hash_le(otherHash)); } @JRubyMethod(name = ">") @@ -1365,7 +1367,7 @@ public RubyFixnum hash(ThreadContext context) { hash = hval[0]; } - return context.runtime.newFixnum(hash); + return asFixnum(context, hash); } private static final ThreadLocal HASH_16_BYTE = ThreadLocal.withInitial(() -> ByteBuffer.allocate(16)); @@ -1514,7 +1516,7 @@ public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyOb */ @JRubyMethod(name = {"has_value?", "value?"}) public RubyBoolean has_value_p(ThreadContext context, IRubyObject expected) { - return RubyBoolean.newBoolean(context, hasValue(context, expected)); + return asBoolean(context, hasValue(context, expected)); } private volatile int iteratorCount; @@ -2284,7 +2286,7 @@ public IRubyObject compare_by_identity(ThreadContext context) { @JRubyMethod(name = "compare_by_identity?") public IRubyObject compare_by_identity_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isComparedByIdentity()); + return asBoolean(context, isComparedByIdentity()); } @JRubyMethod @@ -2461,7 +2463,7 @@ public static IRubyObject ruby2_keywords_hash(ThreadContext context, IRubyObject public static IRubyObject ruby2_keywords_hash_p(ThreadContext context, IRubyObject _self, IRubyObject arg) { TypeConverter.checkType(context, arg, context.runtime.getHash()); - return context.runtime.newBoolean(((RubyHash) arg).isRuby2KeywordHash()); + return asBoolean(context, ((RubyHash) arg).isRuby2KeywordHash()); } private static class VisitorIOException extends RuntimeException { @@ -3016,13 +3018,12 @@ public IRubyObject set_default_proc(IRubyObject proc) { @Deprecated public RubyFixnum rb_size() { - return metaClass.runtime.newFixnum(size()); + return rb_size(getCurrentContext()); } @Deprecated public RubyBoolean empty_p() { - Ruby runtime = metaClass.runtime; - return isEmpty() ? runtime.getTrue() : runtime.getFalse(); + return asBoolean(getCurrentContext(), isEmpty()); } @Deprecated diff --git a/core/src/main/java/org/jruby/RubyIO.java b/core/src/main/java/org/jruby/RubyIO.java index f7af111666a..2488ebb7fcc 100644 --- a/core/src/main/java/org/jruby/RubyIO.java +++ b/core/src/main/java/org/jruby/RubyIO.java @@ -960,7 +960,7 @@ private IRubyObject initializeCommon(ThreadContext context, ChannelFD fd, IRubyO throw runtime.newErrnoEBADFError(); } - API.ModeAndPermission pm = EncodingUtils.vmodeVperm(vmodeArg, runtime.newFixnum(0)); + API.ModeAndPermission pm = EncodingUtils.vmodeVperm(vmodeArg, asFixnum(context, 0)); int[] fmode_p = {0}; ConvConfig convconfig = new ConvConfig(); EncodingUtils.extractModeEncoding(context, convconfig, pm, opt, oflags_p, fmode_p); @@ -976,7 +976,7 @@ private IRubyObject initializeCommon(ThreadContext context, ChannelFD fd, IRubyO } } - if (opt != null && !opt.isNil() && ((RubyHash)opt).op_aref(context, runtime.newSymbol("autoclose")) == runtime.getFalse()) { + if (opt != null && !opt.isNil() && ((RubyHash)opt).op_aref(context, runtime.newSymbol("autoclose")) == context.fals) { fmode_p[0] |= OpenFile.PREP; } @@ -1242,7 +1242,7 @@ public static IRubyObject sysopen(ThreadContext context, IRubyObject recv, IRuby if (vmode.isNil()) oflags = OpenFlags.O_RDONLY.intValue(); else if (!(intmode = checkToInteger(context, vmode)).isNil()) - oflags = integerAsInt(context, (RubyInteger) intmode); + oflags = asInt(context, (RubyInteger) intmode); else { vmode = vmode.convertToString(); oflags = OpenFile.ioModestrOflags(runtime, vmode.toString()); @@ -1252,7 +1252,7 @@ else if (!(intmode = checkToInteger(context, vmode)).isNil()) StringSupport.checkStringSafety(context.runtime, fname); fname = ((RubyString)fname).dupFrozen(); fd = sysopen(runtime, fname.toString(), oflags, perm); - return runtime.newFixnum(fd.bestFileno(true)); + return asFixnum(context, fd.bestFileno(true)); } public static class Sysopen { @@ -1335,7 +1335,7 @@ public void setAutoclose(boolean autoclose) { @JRubyMethod(name = "autoclose?") public IRubyObject autoclose(ThreadContext context) { - return RubyBoolean.newBoolean(context, isAutoclose()); + return asBoolean(context, isAutoclose()); } @JRubyMethod(name = "autoclose=") @@ -1359,18 +1359,16 @@ public IRubyObject binmode() { // MRI: rb_io_binmode_p @JRubyMethod(name = "binmode?") public IRubyObject op_binmode(ThreadContext context) { - return RubyBoolean.newBoolean(context, getOpenFileChecked().isBinmode()); + return asBoolean(context, getOpenFileChecked().isBinmode()); } // rb_io_syswrite @JRubyMethod(name = "syswrite") public IRubyObject syswrite(ThreadContext context, IRubyObject str) { - Ruby runtime = context.runtime; OpenFile fptr; long n; - if (!(str instanceof RubyString)) - str = str.asString(); + if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); @@ -1381,19 +1379,17 @@ public IRubyObject syswrite(ThreadContext context, IRubyObject str) { str = str.convertToString().newFrozen(); - if (fptr.wbuf.len != 0) { - runtime.getWarnings().warn("syswrite for buffered IO"); - } + if (fptr.wbuf.len != 0) context.runtime.getWarnings().warn("syswrite for buffered IO"); ByteList strByteList = ((RubyString) str).getByteList(); n = OpenFile.writeInternal(context, fptr, strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize()); - if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); + if (n == -1) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } - return runtime.newFixnum(n); + return asFixnum(context, n); } // MRI: rb_io_write_nonblock @@ -1448,7 +1444,7 @@ private IRubyObject ioWriteNonblock(ThreadContext context, Ruby runtime, IRubyOb if (locked) fptr.unlock(); } - return runtime.newFixnum(n); + return asFixnum(context, n); } public RubyIO GetWriteIO() { @@ -1717,7 +1713,7 @@ public IRubyObject op_append(ThreadContext context, IRubyObject anObject) { @JRubyMethod(name = "fileno", alias = "to_i") public RubyFixnum fileno(ThreadContext context) { - return context.runtime.newFixnum(getOpenFileChecked().getFileno()); + return asFixnum(context, getOpenFileChecked().getFileno()); } /** Returns the current line number. @@ -1726,7 +1722,7 @@ public RubyFixnum fileno(ThreadContext context) { */ @JRubyMethod(name = "lineno") public RubyFixnum lineno(ThreadContext context) { - return context.runtime.newFixnum(getOpenFileChecked().getLineNumber()); + return asFixnum(context, getOpenFileChecked().getLineNumber()); } /** Sets the current line number. @@ -1737,7 +1733,7 @@ public RubyFixnum lineno(ThreadContext context) { public RubyFixnum lineno_set(ThreadContext context, IRubyObject newLineNumber) { getOpenFileChecked().setLineNumber(RubyNumeric.fix2int(newLineNumber)); - return context.runtime.newFixnum(getOpenFileChecked().getLineNumber()); + return asFixnum(context, getOpenFileChecked().getLineNumber()); } /** Returns the current sync mode. @@ -1779,7 +1775,7 @@ public IRubyObject pid(ThreadContext context) { // Of course this isn't particularly useful. long pid = myOpenFile.getPid(); - return context.runtime.newFixnum(pid); + return asFixnum(context, pid); } // rb_io_pos @@ -1792,7 +1788,7 @@ public RubyFixnum pos(ThreadContext context) { long pos = fptr.tell(context); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); pos -= fptr.rbuf.len; - return context.runtime.newFixnum(pos); + return asFixnum(context, pos); } finally { if (locked) fptr.unlock(); } @@ -1801,13 +1797,10 @@ public RubyFixnum pos(ThreadContext context) { // rb_io_set_pos @JRubyMethod(name = "pos=") public RubyFixnum pos_set(ThreadContext context, IRubyObject offset) { - OpenFile fptr; - long pos; - - pos = offset.convertToInteger().getLongValue(); - fptr = getOpenFileChecked(); - + long pos = offset.convertToInteger().getLongValue(); + OpenFile fptr = getOpenFileChecked(); boolean locked = fptr.lock(); + try { pos = fptr.seek(context, pos, PosixShim.SEEK_SET); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); @@ -1815,7 +1808,7 @@ public RubyFixnum pos_set(ThreadContext context, IRubyObject offset) { if (locked) fptr.unlock(); } - return context.runtime.newFixnum(pos); + return asFixnum(context, pos); } /** Print some objects to the stream. @@ -1978,14 +1971,12 @@ public RubyFixnum seek(ThreadContext context, IRubyObject[] args) { @JRubyMethod public RubyFixnum seek(ThreadContext context, IRubyObject off) { - long ret = doSeek(context, numericToLong(context, off), PosixShim.SEEK_SET); - return context.runtime.newFixnum(ret); + return asFixnum(context, doSeek(context, numericToLong(context, off), PosixShim.SEEK_SET)); } @JRubyMethod public RubyFixnum seek(ThreadContext context, IRubyObject off, IRubyObject whence) { - long ret = doSeek(context, numericToLong(context, off), interpretSeekWhence(whence)); - return context.runtime.newFixnum(ret); + return asFixnum(context, doSeek(context, numericToLong(context, off), interpretSeekWhence(whence))); } // rb_io_seek @@ -2253,7 +2244,7 @@ public IRubyObject initialize_copy(IRubyObject _io){ @JRubyMethod(name = "closed?") public RubyBoolean closed_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isClosed()); + return asBoolean(context, isClosed()); } /** @@ -2302,15 +2293,14 @@ protected static IRubyObject ioClose(ThreadContext context, IRubyObject io) { IOSites sites = sites(context); IRubyObject closed = io.checkCallMethod(context, sites.closed_checked); if (closed != null && closed.isTrue()) return io; - final Ruby runtime = context.runtime; - IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! + IRubyObject oldExc = context.runtime.getGlobalVariables().get("$!"); // Save $! try { closed = io.checkCallMethod(context, sites.close_checked); - return runtime.newBoolean(closed != null && closed.isTrue()); + return asBoolean(context, closed != null && closed.isTrue()); } catch (RaiseException re) { if (re.getMessage().contains(CLOSED_STREAM_MSG)) { // ignore - runtime.getGlobalVariables().set("$!", oldExc); // Restore $! + context.runtime.getGlobalVariables().set("$!", oldExc); // Restore $! return context.nil; } else { throw re; @@ -2725,16 +2715,16 @@ private IRubyObject ctl(ThreadContext context, IRubyObject cmd, IRubyObject arg) if (realCmd == Fcntl.F_GETFL.intValue()) { OpenFile myOpenFile = getOpenFileChecked(); - return runtime.newFixnum(OpenFile.ioFmodeOflags(myOpenFile.getMode())); + return asFixnum(context, OpenFile.ioFmodeOflags(myOpenFile.getMode())); } // FIXME: Arg may also be true, false, and nil and still be valid. Strangely enough, // protocol conversion is not happening in Ruby on this arg? - if (arg == null || arg.isNil() || arg == runtime.getFalse()) { + if (arg == null || arg.isNil() || arg == context.fals) { nArg = 0; } else if (arg instanceof RubyFixnum) { nArg = RubyFixnum.fix2long(arg); - } else if (arg == runtime.getTrue()) { + } else if (arg == context.tru) { nArg = 1; } else { throw runtime.newNotImplementedError("JRuby does not support string for second fcntl/ioctl argument yet"); @@ -2752,7 +2742,7 @@ private IRubyObject ctl(ThreadContext context, IRubyObject cmd, IRubyObject arg) // for mode changes which should persist across fork() boundaries. Since JVM has no fork // this is not a problem for us. if (realCmd == FcntlLibrary.FD_CLOEXEC) { - close_on_exec_set(context, runtime.getTrue()); + close_on_exec_set(context, context.tru); } else if (realCmd == Fcntl.F_SETFD.intValue()) { if (arg != null && (nArg & FcntlLibrary.FD_CLOEXEC) == FcntlLibrary.FD_CLOEXEC) { close_on_exec_set(context, arg); @@ -2760,7 +2750,7 @@ private IRubyObject ctl(ThreadContext context, IRubyObject cmd, IRubyObject arg) throw runtime.newNotImplementedError("F_SETFD only supports FD_CLOEXEC"); } } else if (realCmd == Fcntl.F_GETFD.intValue()) { - return runtime.newFixnum(close_on_exec_p(context).isTrue() ? FD_CLOEXEC : 0); + return asFixnum(context, close_on_exec_p(context).isTrue() ? FD_CLOEXEC : 0); } else if (realCmd == Fcntl.F_SETFL.intValue()) { if ((nArg & OpenFlags.O_NONBLOCK.intValue()) != 0) { fptr.setBlocking(runtime, false); @@ -2774,14 +2764,14 @@ private IRubyObject ctl(ThreadContext context, IRubyObject cmd, IRubyObject arg) close_on_exec_set(context, context.fals); } } else if (realCmd == Fcntl.F_GETFL.intValue()) { - return runtime.newFixnum( + return asFixnum(context, (fptr.isBlocking() ? 0 : OpenFlags.O_NONBLOCK.intValue()) | (close_on_exec_p(context).isTrue() ? FD_CLOEXEC : 0)); } else { throw runtime.newNotImplementedError("JRuby only supports F_SETFL and F_GETFL with NONBLOCK for fcntl/ioctl"); } - return runtime.newFixnum(0); + return asFixnum(context, 0); } @JRubyMethod(name = "puts") @@ -3571,20 +3561,18 @@ public IRubyObject stat(ThreadContext context) { * MRI: rb_io_each_byte */ public IRubyObject each_byteInternal(ThreadContext context, Block block) { - Ruby runtime = context.runtime; - OpenFile fptr; - if (!block.isGiven()) return enumeratorize(context.runtime, this, "each_byte"); - fptr = getOpenFileChecked(); + OpenFile fptr = getOpenFileChecked(); boolean locked = fptr.lock(); + try { do { while (fptr.rbuf.len > 0) { byte[] pBytes = fptr.rbuf.ptr; int p = fptr.rbuf.off++; fptr.rbuf.len--; - block.yield(context, runtime.newFixnum(pBytes[p] & 0xFF)); + block.yield(context, asFixnum(context, pBytes[p] & 0xFF)); fptr.errno(null); } fptr.checkByteReadable(context); @@ -3711,7 +3699,7 @@ private IRubyObject eachCodePointCommon(ThreadContext context, Block block, Stri } fptr.cbuf.off += n; fptr.cbuf.len -= n; - block.yield(context, runtime.newFixnum(c & 0xFFFFFFFF)); + block.yield(context, asFixnum(context, c & 0xFFFFFFFF)); } } fptr.NEED_NEWLINE_DECORATOR_ON_READ_CHECK(); @@ -3723,7 +3711,7 @@ private IRubyObject eachCodePointCommon(ThreadContext context, Block block, Stri c = StringSupport.codePoint(runtime, enc, fptr.rbuf.ptr, fptr.rbuf.off, fptr.rbuf.off + fptr.rbuf.len); fptr.rbuf.off += n; fptr.rbuf.len -= n; - block.yield(context, runtime.newFixnum(c & 0xFFFFFFFF)); + block.yield(context, asFixnum(context, c & 0xFFFFFFFF)); } else if (StringSupport.MBCLEN_INVALID_P(r)) { throw runtime.newArgumentError("invalid byte sequence in " + enc); } else if (StringSupport.MBCLEN_NEEDMORE_P(r)) { @@ -3743,7 +3731,7 @@ private IRubyObject eachCodePointCommon(ThreadContext context, Block block, Stri r = enc.length(cbuf, 0, p); if (!StringSupport.MBCLEN_CHARFOUND_P(r)) throw runtime.newArgumentError("invalid byte sequence in " + enc); c = enc.mbcToCode(cbuf, 0, p); - block.yield(context, runtime.newFixnum(c)); + block.yield(context, asFixnum(context, c)); } else { continue; } @@ -4188,7 +4176,7 @@ static PosixFadvise adviceArgCheck(ThreadContext context, IRubyObject advice) { } public static void failIfDirectory(Ruby runtime, RubyString pathStr) { - if (RubyFileTest.directory_p(runtime, pathStr).isTrue()) { + if (RubyFileTest.directory_p(runtime.getCurrentContext(), pathStr).isTrue()) { if (Platform.IS_WINDOWS) { throw runtime.newErrnoEACCESError(pathStr.asJavaString()); } else { @@ -4207,8 +4195,8 @@ private static RubyIO openKeyArgs(ThreadContext context, IRubyObject recv, IRuby // MRI increments args past 0 now, so remaining uses of args only see non-path args if (opt == context.nil) { - vmode = runtime.newFixnum(ModeFlags.RDONLY); - vperm = runtime.newFixnum(0666); + vmode = asFixnum(context, ModeFlags.RDONLY); + vperm = asFixnum(context, 0666); } else if ((v = ((RubyHash) opt).op_aref(context, runtime.newSymbol("open_args"))) != context.nil) { RubyArray vAry = v.convertToArray(); int n = vAry.size(); @@ -4411,7 +4399,7 @@ public static IRubyObject ioStaticWrite(ThreadContext context, IRubyObject recv, if ( binary ) mode |= OpenFlags.O_BINARY.intValue(); } if ( offset == context.nil ) mode |= OpenFlags.O_TRUNC.intValue(); - optHash.op_aset(context, modeSym, runtime.newFixnum(mode)); + optHash.op_aset(context, modeSym, asFixnum(context, mode)); } IRubyObject _io = openKeyArgs(context, recv, argv, optHash); @@ -4606,7 +4594,7 @@ public static IRubyObject popen(ThreadContext context, IRubyObject recv, IRubyOb io.MakeOpenFile(); - API.ModeAndPermission pm = vmodeVperm(pmode, runtime.newFixnum(0)); + API.ModeAndPermission pm = vmodeVperm(pmode, asFixnum(context, 0)); int[] oflags_p = {0}, fmode_p = {0}; EncodingUtils.extractModeEncoding(context, io, pm, options, oflags_p, fmode_p); ModeFlags modes = ModeFlags.createModeFlags(oflags_p[0]); @@ -4831,7 +4819,7 @@ private static RubyFixnum copyStreamCommon(ThreadContext context, IRubyObject ar arg1 = tmpIO; } else if (!(arg1 instanceof RubyFile)) { RubyString path = RubyFile.get_path(context, arg1); - arg1 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, runtime.newFixnum(ModeFlags.RDONLY)}, Block.NULL_BLOCK); + arg1 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, asFixnum(context, ModeFlags.RDONLY)}, Block.NULL_BLOCK); local1 = true; } @@ -4855,7 +4843,7 @@ private static RubyFixnum copyStreamCommon(ThreadContext context, IRubyObject ar arg2 = tmpIO; } else if (!(arg2 instanceof RubyFile)) { RubyString path = RubyFile.get_path(context, arg2); - arg2 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, runtime.newFixnum(ModeFlags.WRONLY | ModeFlags.CREAT | ModeFlags.TRUNC)}, Block.NULL_BLOCK); + arg2 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, asFixnum(context, ModeFlags.WRONLY | ModeFlags.CREAT | ModeFlags.TRUNC)}, Block.NULL_BLOCK); local2 = true; } @@ -4915,10 +4903,10 @@ private static RubyFixnum copyStreamCommon(ThreadContext context, IRubyObject ar } } - return context.runtime.newFixnum(size); + return asFixnum(context, size); } catch (EOFError eof) { // ignore EOF, reached end of input - return context.runtime.newFixnum(size); + return asFixnum(context, size); } catch (IOException ioe) { throw runtime.newIOErrorFromException(ioe); } finally { @@ -5121,11 +5109,9 @@ public IRubyObject pwrite(ThreadContext context, IRubyObject str, IRubyObject of int length = strByteList.realSize(); ByteBuffer wrap = ByteBuffer.wrap(strByteList.unsafeBytes(), strByteList.begin(), length); - int written; - - written = OpenFile.pwriteInternal(context, fptr, fd, wrap, off, length); + int written = OpenFile.pwriteInternal(context, fptr, fd, wrap, off, length); - return context.runtime.newFixnum(written); + return asFixnum(context, written); } @JRubyMethod(optional = 1) @@ -5709,7 +5695,7 @@ public void run() { runtime.getThreadService().registerNewThread(rubyThread); - rubyThread.op_aset(runtime.newSymbol("pid"), runtime.newFixnum(pid)); + rubyThread.op_aset(runtime.newSymbol("pid"), asFixnum(context, pid)); try { int exitValue = tuple.process.waitFor(); @@ -5758,7 +5744,7 @@ public static IRubyObject popen4(ThreadContext context, IRubyObject recv, IRubyO POpenTuple tuple = popenSpecial(context, args); RubyArray yieldArgs = RubyArray.newArrayLight(runtime, - runtime.newFixnum(ShellLauncher.getPidFromProcess(tuple.process)), + asFixnum(context, ShellLauncher.getPidFromProcess(tuple.process)), tuple.output, tuple.input, tuple.error); diff --git a/core/src/main/java/org/jruby/RubyIOBuffer.java b/core/src/main/java/org/jruby/RubyIOBuffer.java index 116d702dd43..a7c8ea9279c 100644 --- a/core/src/main/java/org/jruby/RubyIOBuffer.java +++ b/core/src/main/java/org/jruby/RubyIOBuffer.java @@ -25,7 +25,8 @@ import java.nio.channels.FileChannel; import java.util.Arrays; -import static org.jruby.RubyBoolean.newBoolean; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; public class RubyIOBuffer extends RubyObject { @@ -546,12 +547,12 @@ public RubyString to_s(ThreadContext context) { @JRubyMethod(name = "size") public IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum(size); + return asFixnum(context, size); } @JRubyMethod(name = "valid?") public IRubyObject valid_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, validate()); + return asBoolean(context, validate()); } @JRubyMethod(name = "transfer") @@ -580,17 +581,17 @@ private void zero(ThreadContext context) { @JRubyMethod(name = "null?") public IRubyObject null_p(ThreadContext context) { - return newBoolean(context, base == null); + return asBoolean(context, base == null); } @JRubyMethod(name = "empty?") public IRubyObject empty_p(ThreadContext context) { - return newBoolean(context, size == 0); + return asBoolean(context, size == 0); } @JRubyMethod(name = "external?") public IRubyObject external_p(ThreadContext context) { - return newBoolean(context, isExternal()); + return asBoolean(context, isExternal()); } private boolean isExternal() { @@ -599,7 +600,7 @@ private boolean isExternal() { @JRubyMethod(name = "internal?") public IRubyObject internal_p(ThreadContext context) { - return newBoolean(context, isInternal()); + return asBoolean(context, isInternal()); } private boolean isInternal() { @@ -608,7 +609,7 @@ private boolean isInternal() { @JRubyMethod(name = "mapped?") public IRubyObject mapped_p(ThreadContext context) { - return newBoolean(context, isMapped()); + return asBoolean(context, isMapped()); } private boolean isMapped() { @@ -618,7 +619,7 @@ private boolean isMapped() { @JRubyMethod(name = "shared?") public IRubyObject shared_p(ThreadContext context) { // no support for shared yet - return newBoolean(context, false); + return asBoolean(context, false); } private boolean isShared() { @@ -627,7 +628,7 @@ private boolean isShared() { @JRubyMethod(name = "locked?") public IRubyObject locked_p(ThreadContext context) { - return newBoolean(context, isLocked()); + return asBoolean(context, isLocked()); } private boolean isLocked() { @@ -636,7 +637,7 @@ private boolean isLocked() { @JRubyMethod(name = "readonly?") public IRubyObject readonly_p(ThreadContext context) { - return newBoolean(context, isReadonly()); + return asBoolean(context, isReadonly()); } private boolean isReadonly() { @@ -744,7 +745,7 @@ private void validateRange(ThreadContext context, int offset, int length) { @JRubyMethod(name = "<=>") public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { - return context.runtime.newFixnum(base.compareTo(((RubyIOBuffer) other).base)); + return asFixnum(context, base.compareTo(((RubyIOBuffer) other).base)); } @JRubyMethod(name = "resize") diff --git a/core/src/main/java/org/jruby/RubyInteger.java b/core/src/main/java/org/jruby/RubyInteger.java index e550d79c822..e0f19f681aa 100644 --- a/core/src/main/java/org/jruby/RubyInteger.java +++ b/core/src/main/java/org/jruby/RubyInteger.java @@ -129,12 +129,12 @@ public RubyInteger negate() { // abstract - Fixnum/Bignum do override @Override public IRubyObject isNegative(ThreadContext context) { - return RubyBoolean.newBoolean(context, isNegative()); + return asBoolean(context, isNegative()); } @Override public IRubyObject isPositive(ThreadContext context) { - return RubyBoolean.newBoolean(context, isPositive()); + return asBoolean(context, isPositive()); } @Override @@ -989,17 +989,17 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject index) { if (beg.isNil()) { if (!negativeInt(context, end)) { if (!isExclusive) { - end = ((RubyInteger) end).op_plus(context, context.runtime.newFixnum(1)); + end = ((RubyInteger) end).op_plus(context, asFixnum(context, 1)); } RubyInteger mask = generateMask(context, end); if (((RubyInteger) op_and(context, mask)).isZero()) { - return context.runtime.newFixnum(0); + return asFixnum(context, 0); } else { throw context.runtime.newArgumentError("The beginless range for Integer#[] results in infinity"); } } else { - return context.runtime.newFixnum(0); + return asFixnum(context, 0); } } beg = beg.convertToInteger(); @@ -1008,13 +1008,13 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject index) { if (!end.isNil() && cmp < 0) { IRubyObject length = ((RubyInteger) end).op_minus(context, beg); if (!isExclusive) { - length = ((RubyInteger) length).op_plus(context, context.runtime.newFixnum(1)); + length = ((RubyInteger) length).op_plus(context, asFixnum(context, 1)); } RubyInteger mask = generateMask(context, length); num = (((RubyInteger) num).op_and(context, mask)); return num; } else if (cmp == 0) { - if (isExclusive) return context.runtime.newFixnum(0); + if (isExclusive) return asFixnum(context, 0); index = beg; } else { return num; @@ -1043,7 +1043,7 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject index, IRubyObject } RubyInteger generateMask(ThreadContext context, IRubyObject length) { - RubyFixnum one = context.runtime.newFixnum(1); + RubyFixnum one = asFixnum(context, 1); return (RubyInteger) ((RubyInteger) one.op_lshift(context, length)).op_minus(context, one); } diff --git a/core/src/main/java/org/jruby/RubyKernel.java b/core/src/main/java/org/jruby/RubyKernel.java index d4bcb3f90a9..73458d6d79a 100644 --- a/core/src/main/java/org/jruby/RubyKernel.java +++ b/core/src/main/java/org/jruby/RubyKernel.java @@ -108,6 +108,8 @@ import static org.jruby.anno.FrameField.SCOPE; import static org.jruby.anno.FrameField.SELF; import static org.jruby.anno.FrameField.VISIBILITY; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.ir.runtime.IRRuntimeHelpers.dupIfKeywordRestAtCallsite; import static org.jruby.runtime.ThreadContext.hasKeywords; @@ -871,7 +873,7 @@ private static RubyFixnum sleepCommon(ThreadContext context, long milliseconds) } } - return context.runtime.newFixnum(Math.round((System.currentTimeMillis() - startTime) / 1000.0)); + return asFixnum(context, Math.round((System.currentTimeMillis() - startTime) / 1000.0)); } // FIXME: Add at_exit and finalizers to exit, then make exit_bang not call those. @@ -966,11 +968,11 @@ public static RubyBinding binding(ThreadContext context, IRubyObject recv, Block @JRubyMethod(name = {"block_given?", "iterator?"}, module = true, visibility = PRIVATE, reads = BLOCK) public static RubyBoolean block_given_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, context.getCurrentFrame().getBlock().isGiven()); + return asBoolean(context, context.getCurrentFrame().getBlock().isGiven()); } public static RubyBoolean blockGiven(ThreadContext context, IRubyObject recv, Block frameBlock) { - return RubyBoolean.newBoolean(context, frameBlock.isGiven()); + return asBoolean(context, frameBlock.isGiven()); } @JRubyMethod(name = {"sprintf", "format"}, required = 1, rest = true, checkArity = false, module = true, visibility = PRIVATE) @@ -1165,15 +1167,14 @@ private static void printExceptionSummary(Ruby runtime, RubyException rEx) { @JRubyMethod(name = "require", module = true, visibility = PRIVATE) public static IRubyObject require(ThreadContext context, IRubyObject recv, IRubyObject name, Block block) { IRubyObject tmp = name.checkStringType(); + RubyString requireName = tmp != context.nil ? (RubyString) tmp : RubyFile.get_path(context, name); - if (tmp != context.nil) return requireCommon(context.runtime, (RubyString) tmp, block); - - return requireCommon(context.runtime, RubyFile.get_path(context, name), block); + return requireCommon(context, requireName, block); } - private static IRubyObject requireCommon(Ruby runtime, RubyString name, Block block) { - RubyString path = StringSupport.checkEmbeddedNulls(runtime, name); - return runtime.newBoolean(runtime.getLoadService().require(path.toString())); + private static IRubyObject requireCommon(ThreadContext context, RubyString name, Block block) { + RubyString path = StringSupport.checkEmbeddedNulls(context.runtime, name); + return asBoolean(context, context.runtime.getLoadService().require(path.toString())); } @JRubyMethod(name = "require_relative", module = true, visibility = PRIVATE, reads = SCOPE) @@ -1725,13 +1726,13 @@ private static IRubyObject testCommon(ThreadContext context, IRubyObject recv, i switch (cmd) { case 'A': // ?A | Time | Last access time for file1 - return context.runtime.newFileStat(fileResource(arg1).path(), false).atime(); + return context.runtime.newFileStat(fileResource(arg1).path(), false).atime(context); case 'b': // ?b | boolean | True if file1 is a block device - return RubyFileTest.blockdev_p(recv, arg1); + return RubyFileTest.blockdev_p(context, recv, arg1); case 'c': // ?c | boolean | True if file1 is a character device - return RubyFileTest.chardev_p(recv, arg1); + return RubyFileTest.chardev_p(context, recv, arg1); case 'C': // ?C | Time | Last change time for file1 - return context.runtime.newFileStat(fileResource(arg1).path(), false).ctime(); + return context.runtime.newFileStat(fileResource(arg1).path(), false).ctime(context); case 'd': // ?d | boolean | True if file1 exists and is a directory return RubyFileTest.directory_p(context, recv, arg1); case 'e': // ?e | boolean | True if file1 exists @@ -1739,21 +1740,21 @@ private static IRubyObject testCommon(ThreadContext context, IRubyObject recv, i case 'f': // ?f | boolean | True if file1 exists and is a regular file return RubyFileTest.file_p(context, recv, arg1); case 'g': // ?g | boolean | True if file1 has the \CF{setgid} bit - return RubyFileTest.setgid_p(recv, arg1); + return RubyFileTest.setgid_p(context, recv, arg1); case 'G': // ?G | boolean | True if file1 exists and has a group ownership equal to the caller's group - return RubyFileTest.grpowned_p(recv, arg1); + return RubyFileTest.grpowned_p(context, recv, arg1); case 'k': // ?k | boolean | True if file1 exists and has the sticky bit set - return RubyFileTest.sticky_p(recv, arg1); + return RubyFileTest.sticky_p(context, recv, arg1); case 'M': // ?M | Time | Last modification time for file1 - return context.runtime.newFileStat(fileResource(arg1).path(), false).mtime(); + return context.runtime.newFileStat(fileResource(arg1).path(), false).mtime(context); case 'l': // ?l | boolean | True if file1 exists and is a symbolic link - return RubyFileTest.symlink_p(recv, arg1); + return RubyFileTest.symlink_p(context, recv, arg1); case 'o': // ?o | boolean | True if file1 exists and is owned by the caller's effective uid - return RubyFileTest.owned_p(recv, arg1); + return RubyFileTest.owned_p(context, recv, arg1); case 'O': // ?O | boolean | True if file1 exists and is owned by the caller's real uid - return RubyFileTest.rowned_p(recv, arg1); + return RubyFileTest.rowned_p(context, recv, arg1); case 'p': // ?p | boolean | True if file1 exists and is a fifo - return RubyFileTest.pipe_p(recv, arg1); + return RubyFileTest.pipe_p(context, recv, arg1); case 'r': // ?r | boolean | True if file1 is readable by the effective uid/gid of the caller return RubyFileTest.readable_p(context, recv, arg1); case 'R': // ?R | boolean | True if file is readable by the real uid/gid of the caller @@ -1761,26 +1762,26 @@ private static IRubyObject testCommon(ThreadContext context, IRubyObject recv, i case 's': // ?s | int/nil | If file1 has nonzero size, return the size, otherwise nil return RubyFileTest.size_p(context, recv, arg1); case 'S': // ?S | boolean | True if file1 exists and is a socket - return RubyFileTest.socket_p(recv, arg1); + return RubyFileTest.socket_p(context, recv, arg1); case 'u': // ?u | boolean | True if file1 has the setuid bit set - return RubyFileTest.setuid_p(recv, arg1); + return RubyFileTest.setuid_p(context, recv, arg1); case 'w': // ?w | boolean | True if file1 exists and is writable by effective uid/gid - return RubyFileTest.writable_p(recv, arg1); + return RubyFileTest.writable_p(context, recv, arg1); case 'W': // ?W | boolean | True if file1 exists and is writable by the real uid/gid // FIXME: Need to implement an writable_real_p in FileTest - return RubyFileTest.writable_p(recv, arg1); + return RubyFileTest.writable_p(context, recv, arg1); case 'x': // ?x | boolean | True if file1 exists and is executable by the effective uid/gid - return RubyFileTest.executable_p(recv, arg1); + return RubyFileTest.executable_p(context, recv, arg1); case 'X': // ?X | boolean | True if file1 exists and is executable by the real uid/gid - return RubyFileTest.executable_real_p(recv, arg1); + return RubyFileTest.executable_real_p(context, recv, arg1); case 'z': // ?z | boolean | True if file1 exists and has a zero length return RubyFileTest.zero_p(context, recv, arg1); case '=': // ?= | boolean | True if the modification times of file1 and file2 are equal - return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeEquals(arg2); + return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeEquals(context, arg2); case '<': // ?< | boolean | True if the modification time of file1 is prior to that of file2 - return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeLessThan(arg2); + return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeLessThan(context, arg2); case '>': // ?> | boolean | True if the modification time of file1 is after that of file2 - return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeGreaterThan(arg2); + return context.runtime.newFileStat(arg1.convertToString().toString(), false).mtimeGreaterThan(context, arg2); case '-': // ?- | boolean | True if file1 and file2 are identical return RubyFileTest.identical_p(context, recv, arg1, arg2); default: @@ -2087,7 +2088,7 @@ private static IRubyObject execCommon(ThreadContext context, IRubyObject env, IR // FIXME: Make jnr-posix Pure-Java backend do this as well int resultCode = ShellLauncher.execAndWait(runtime, args); - exit(runtime, new IRubyObject[] {runtime.newFixnum(resultCode)}, true); + exit(runtime, new IRubyObject[] {asFixnum(context, resultCode)}, true); // not reached return runtime.getNil(); diff --git a/core/src/main/java/org/jruby/RubyMatchData.java b/core/src/main/java/org/jruby/RubyMatchData.java index 1b6c1271e0d..4d75c87526f 100644 --- a/core/src/main/java/org/jruby/RubyMatchData.java +++ b/core/src/main/java/org/jruby/RubyMatchData.java @@ -60,6 +60,7 @@ import org.jruby.util.RegexpOptions; import org.jruby.util.StringSupport; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.util.RubyStringBuilder.str; @@ -381,7 +382,7 @@ public IRubyObject byteoffset(ThreadContext context, IRubyObject group) { int end = regs.getEnd(index); - return runtime.newArray(runtime.newFixnum(start), runtime.newFixnum(end)); + return runtime.newArray(asFixnum(context, start), asFixnum(context, end)); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/RubyMethod.java b/core/src/main/java/org/jruby/RubyMethod.java index d7b09dfbfda..bec28961241 100644 --- a/core/src/main/java/org/jruby/RubyMethod.java +++ b/core/src/main/java/org/jruby/RubyMethod.java @@ -34,6 +34,7 @@ import org.jruby.anno.JRubyMethod; import org.jruby.anno.JRubyClass; +import org.jruby.api.Convert; import org.jruby.internal.runtime.methods.AliasMethod; import org.jruby.internal.runtime.methods.DelegatingDynamicMethod; import org.jruby.internal.runtime.methods.DynamicMethod; @@ -52,6 +53,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.callsite.CacheEntry; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.ir.runtime.IRRuntimeHelpers.dupIfKeywordRestAtCallsite; /** @@ -161,7 +164,7 @@ public IRubyObject op_eql(ThreadContext context, IRubyObject other) { @Override @JRubyMethod(name = "==") public RubyBoolean op_equal(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, equals(other) ); + return asBoolean(context, equals(other) ); } @Override @@ -198,7 +201,7 @@ private boolean isSerialMatch(DynamicMethod otherMethod) { @JRubyMethod public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCodeImpl()); + return asFixnum(context, hashCodeImpl()); } @Override @@ -273,11 +276,9 @@ public IRubyObject receiver(ThreadContext context) { @JRubyMethod public IRubyObject source_location(ThreadContext context) { - Ruby runtime = context.runtime; - String filename = getFilename(); if (filename != null) { - return runtime.newArray(runtime.newString(filename), runtime.newFixnum(getLine())); + return context.runtime.newArray(Convert.asString(context, filename), asFixnum(context, getLine())); } return context.nil; diff --git a/core/src/main/java/org/jruby/RubyModule.java b/core/src/main/java/org/jruby/RubyModule.java index e956cbeeb7a..faa2f7bbd13 100644 --- a/core/src/main/java/org/jruby/RubyModule.java +++ b/core/src/main/java/org/jruby/RubyModule.java @@ -1551,7 +1551,7 @@ public IRubyObject include_p(ThreadContext context, IRubyObject arg) { @JRubyMethod(name = "singleton_class?") public IRubyObject singleton_class_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isSingleton()); + return asBoolean(context, isSingleton()); } private String frozenType() { @@ -2887,7 +2887,7 @@ public RubyString to_s() { @JRubyMethod(name = "===") @Override public RubyBoolean op_eqq(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, isInstance(obj)); + return asBoolean(context, isInstance(obj)); } /** @@ -2908,9 +2908,9 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) { RubyModule otherModule = (RubyModule) other; if(otherModule.isIncluded()) { - return RubyBoolean.newBoolean(context, otherModule.isSame(this)); + return asBoolean(context, otherModule.isSame(this)); } else { - return RubyBoolean.newBoolean(context, isSame(otherModule)); + return asBoolean(context, isSame(otherModule)); } } @@ -2984,16 +2984,21 @@ public IRubyObject op_gt(IRubyObject obj) { * */ @JRubyMethod(name = "<=>") - public IRubyObject op_cmp(IRubyObject obj) { - if (this == obj) return getRuntime().newFixnum(0); - if (!(obj instanceof RubyModule)) return getRuntime().getNil(); + public IRubyObject op_cmp(ThreadContext context, IRubyObject obj) { + if (this == obj) return asFixnum(context, 0); + if (!(obj instanceof RubyModule)) return context.nil; RubyModule module = (RubyModule) obj; - if (module.isKindOfModule(this)) return getRuntime().newFixnum(1); - if (this.isKindOfModule(module)) return getRuntime().newFixnum(-1); + if (module.isKindOfModule(this)) return asFixnum(context, 1); + if (this.isKindOfModule(module)) return asFixnum(context, -1); - return getRuntime().getNil(); + return context.nil; + } + + @Deprecated + public IRubyObject op_cmp(IRubyObject obj) { + return op_cmp(getCurrentContext(), obj); } public boolean isKindOfModule(RubyModule type) { @@ -3589,7 +3594,7 @@ public IRubyObject method_undefined(ThreadContext context, IRubyObject nothing) @JRubyMethod(name = "method_defined?") public RubyBoolean method_defined_p(ThreadContext context, IRubyObject symbol) { - return isMethodBound(TypeConverter.checkID(symbol).idString(), true) ? context.tru : context.fals; + return asBoolean(context, isMethodBound(TypeConverter.checkID(symbol).idString(), true)); } @JRubyMethod(name = "method_defined?") @@ -3599,43 +3604,43 @@ public RubyBoolean method_defined_p(ThreadContext context, IRubyObject symbol, I if (parents) return method_defined_p(context, symbol); Visibility visibility = checkMethodVisibility(context, symbol, parents); - return RubyBoolean.newBoolean(context, visibility != UNDEFINED && visibility != PRIVATE); + return asBoolean(context, visibility != UNDEFINED && visibility != PRIVATE); } @JRubyMethod(name = "public_method_defined?") public IRubyObject public_method_defined(ThreadContext context, IRubyObject symbol) { - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, true) == PUBLIC); + return asBoolean(context, checkMethodVisibility(context, symbol, true) == PUBLIC); } @JRubyMethod(name = "public_method_defined?") public IRubyObject public_method_defined(ThreadContext context, IRubyObject symbol, IRubyObject includeSuper) { boolean parents = includeSuper.isTrue(); - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, parents) == PUBLIC); + return asBoolean(context, checkMethodVisibility(context, symbol, parents) == PUBLIC); } @JRubyMethod(name = "protected_method_defined?") public IRubyObject protected_method_defined(ThreadContext context, IRubyObject symbol) { - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, true) == PROTECTED); + return asBoolean(context, checkMethodVisibility(context, symbol, true) == PROTECTED); } @JRubyMethod(name = "protected_method_defined?") public IRubyObject protected_method_defined(ThreadContext context, IRubyObject symbol, IRubyObject includeSuper) { boolean parents = includeSuper.isTrue(); - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, parents) == PROTECTED); + return asBoolean(context, checkMethodVisibility(context, symbol, parents) == PROTECTED); } @JRubyMethod(name = "private_method_defined?") public IRubyObject private_method_defined(ThreadContext context, IRubyObject symbol) { - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, true) == PRIVATE); + return asBoolean(context, checkMethodVisibility(context, symbol, true) == PRIVATE); } @JRubyMethod(name = "private_method_defined?") public IRubyObject private_method_defined(ThreadContext context, IRubyObject symbol, IRubyObject includeSuper) { boolean parents = includeSuper.isTrue(); - return RubyBoolean.newBoolean(context, checkMethodVisibility(context, symbol, parents) == PRIVATE); + return asBoolean(context, checkMethodVisibility(context, symbol, parents) == PRIVATE); } private Visibility checkMethodVisibility(ThreadContext context, IRubyObject symbol, boolean parents) { @@ -4123,13 +4128,12 @@ private Collection classVariablesCommon(boolean inherit) { */ @JRubyMethod(name = "const_defined?") public RubyBoolean const_defined_p(ThreadContext context, IRubyObject name) { - - return constDefined(context, name, true) ? context.tru : context.fals; + return asBoolean(context, constDefined(context, name, true)); } @JRubyMethod(name = "const_defined?") public RubyBoolean const_defined_p(ThreadContext context, IRubyObject name, IRubyObject recurse) { - return constDefined(context, name, recurse.isTrue()) ? context.tru : context.fals; + return asBoolean(context, constDefined(context, name, recurse.isTrue())); } private boolean constDefined(ThreadContext context, IRubyObject name, boolean inherit) { @@ -4350,7 +4354,7 @@ public IRubyObject const_source_location(ThreadContext context, IRubyObject[] ar if (location.getFile().equals(BUILTIN_CONSTANT)) { return RubyArray.newEmptyArray(context.runtime); } - return RubyArray.newArray(context.runtime, runtime.newString(location.getFile()), runtime.newFixnum(location.getLine())); + return RubyArray.newArray(context.runtime, runtime.newString(location.getFile()), asFixnum(context, location.getLine())); } return context.nil; diff --git a/core/src/main/java/org/jruby/RubyNameError.java b/core/src/main/java/org/jruby/RubyNameError.java index bdb59285c9c..67cdcbbb0d3 100644 --- a/core/src/main/java/org/jruby/RubyNameError.java +++ b/core/src/main/java/org/jruby/RubyNameError.java @@ -44,6 +44,8 @@ import org.jruby.util.ByteList; import org.jruby.util.Sprintf; +import static org.jruby.api.Convert.asBoolean; + /** * The Java representation of a Ruby NameError. * @@ -280,7 +282,7 @@ public IRubyObject receiver(ThreadContext context) { @JRubyMethod(name = "private_call?") public IRubyObject private_call_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isPrivateCall()); + return asBoolean(context, isPrivateCall()); } @Override diff --git a/core/src/main/java/org/jruby/RubyNil.java b/core/src/main/java/org/jruby/RubyNil.java index c56df3dadde..344d316cbee 100644 --- a/core/src/main/java/org/jruby/RubyNil.java +++ b/core/src/main/java/org/jruby/RubyNil.java @@ -45,6 +45,9 @@ import org.jruby.runtime.opto.OptoFactory; import org.jruby.util.ByteList; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; + /** * * @author jpetersen @@ -189,7 +192,7 @@ public static RubyBoolean op_and(ThreadContext context, IRubyObject recv, IRubyO */ @JRubyMethod(name = "|") public static RubyBoolean op_or(ThreadContext context, IRubyObject recv, IRubyObject obj) { - return RubyBoolean.newBoolean(context, obj.isTrue()); + return asBoolean(context, obj.isTrue()); } /** nil_xor @@ -197,7 +200,7 @@ public static RubyBoolean op_or(ThreadContext context, IRubyObject recv, IRubyOb */ @JRubyMethod(name = "^") public static RubyBoolean op_xor(ThreadContext context, IRubyObject recv, IRubyObject obj) { - return RubyBoolean.newBoolean(context, obj.isTrue()); + return asBoolean(context, obj.isTrue()); } @Override @@ -213,7 +216,7 @@ public IRubyObject nil_p() { @JRubyMethod public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCode()); + return asFixnum(context, hashCode()); } @Override diff --git a/core/src/main/java/org/jruby/RubyNumeric.java b/core/src/main/java/org/jruby/RubyNumeric.java index ff249248679..e0022ff845a 100644 --- a/core/src/main/java/org/jruby/RubyNumeric.java +++ b/core/src/main/java/org/jruby/RubyNumeric.java @@ -60,6 +60,8 @@ import java.math.RoundingMode; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.util.Numeric.f_abs; import static org.jruby.util.Numeric.f_arg; @@ -931,14 +933,14 @@ public IRubyObject to_int(ThreadContext context) { */ @JRubyMethod(name = "real?") public IRubyObject real_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isReal()); + return asBoolean(context, isReal()); } public boolean isReal() { return true; } // only RubyComplex isn't real @Deprecated public IRubyObject scalar_p() { - return getRuntime().newBoolean(isReal()); + return asBoolean(getRuntime().getCurrentContext(), isReal()); } /** num_int_p @@ -1026,7 +1028,7 @@ public IRubyObject step(ThreadContext context, IRubyObject[] args, Block block) if (step.isNil()) { step = RubyFixnum.one(context.runtime); - } else if (step.op_equal(context, context.runtime.newFixnum(0)).isTrue()) { + } else if (step.op_equal(context, asFixnum(context, 0)).isTrue()) { throw context.runtime.newArgumentError("step can't be 0"); } @@ -1269,15 +1271,13 @@ public static RubyNumeric intervalStepSize(ThreadContext context, IRubyObject fr if (excl) { delta--; } - if (delta < 0) { - return runtime.newFixnum(0); - } + if (delta < 0) return asFixnum(context, 0); // overflow checking long steps = delta / diff; long stepSize = steps + 1; if (stepSize != Long.MIN_VALUE) { - return new RubyFixnum(runtime, delta / diff + 1); + return asFixnum(context, delta / diff + 1); } else { return RubyBignum.newBignum(runtime, BigInteger.valueOf(steps).add(BigInteger.ONE)); } diff --git a/core/src/main/java/org/jruby/RubyObject.java b/core/src/main/java/org/jruby/RubyObject.java index a94da04b93d..cb2b16efe86 100644 --- a/core/src/main/java/org/jruby/RubyObject.java +++ b/core/src/main/java/org/jruby/RubyObject.java @@ -56,6 +56,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.marshal.DataType; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.invokedynamic; import static org.jruby.runtime.Helpers.throwException; @@ -386,7 +387,7 @@ public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObje */ @Override public IRubyObject op_eqq(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, equalInternal(context, this, other)); + return asBoolean(context, equalInternal(context, this, other)); } /** diff --git a/core/src/main/java/org/jruby/RubyObjectSpace.java b/core/src/main/java/org/jruby/RubyObjectSpace.java index 6ad8c6c3895..7cb736987eb 100644 --- a/core/src/main/java/org/jruby/RubyObjectSpace.java +++ b/core/src/main/java/org/jruby/RubyObjectSpace.java @@ -49,7 +49,7 @@ import org.jruby.runtime.Block; import org.jruby.runtime.ThreadContext; -import static org.jruby.api.Convert.castAsFixnum; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.*; import static org.jruby.util.Inspector.inspectPrefix; @@ -122,16 +122,17 @@ public static IRubyObject undefine_finalizer(IRubyObject recv, IRubyObject obj, @JRubyMethod(name = "_id2ref", module = true, visibility = PRIVATE) public static IRubyObject id2ref(IRubyObject recv, IRubyObject id) { final Ruby runtime = id.getRuntime(); - long longId = Convert.castAsFixnum(runtime.getCurrentContext(), id).getLongValue(); + ThreadContext context = runtime.getCurrentContext(); + long longId = castAsFixnum(context, id).getLongValue(); if (longId == 0) { - return runtime.getFalse(); + return context.fals; } else if (longId == 20) { - return runtime.getTrue(); + return context.tru; } else if (longId == 8) { - return runtime.getNil(); + return context.nil; } else if ((longId & 0b01) == 0b01) { // fixnum - return runtime.newFixnum((longId - 1) / 2); + return asFixnum(context, (longId - 1) / 2); } else if ((longId & 0b11) == 0b10) { // flonum double d = 0.0; @@ -148,7 +149,7 @@ public static IRubyObject id2ref(IRubyObject recv, IRubyObject id) { if (runtime.isObjectSpaceEnabled()) { IRubyObject object = runtime.getObjectSpace().id2ref(longId); if (object == null) { - return runtime.getNil(); + return context.nil; } return object; } else { @@ -185,7 +186,7 @@ public static IRubyObject each_objectInternal(final ThreadContext context, IRuby for (int i = 0; i weakMap = getWeakMapFor(key); weakMap.put(key, value); - return runtime.newFixnum(System.identityHashCode(value)); + return asFixnum(context, System.identityHashCode(value)); } @JRubyMethod(name = "key?") public IRubyObject key_p(ThreadContext context, IRubyObject key) { Map weakMap = getWeakMapFor(key); - return RubyBoolean.newBoolean(context, weakMap.get(key) != null); + return asBoolean(context, weakMap.get(key) != null); } @JRubyMethod(name = "keys") @@ -309,7 +308,7 @@ public IRubyObject each_value(ThreadContext context, Block block) { @JRubyMethod(name = {"include?", "member?"}) public IRubyObject member_p(ThreadContext context, IRubyObject key) { - return RubyBoolean.newBoolean(context, getWeakMapFor(key).containsKey(key)); + return asBoolean(context, getWeakMapFor(key).containsKey(key)); } @JRubyMethod(name = "delete") @@ -355,7 +354,7 @@ protected Stream> getEntryStream() { } public IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum(identityMap.size() + valueMap.size()); + return asFixnum(context, identityMap.size() + valueMap.size()); } public IRubyObject inspect(ThreadContext context) { @@ -420,13 +419,11 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject key, IRubyObject v } public IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum(weakMap.size()); + return asFixnum(context, weakMap.size()); } public IRubyObject inspect(ThreadContext context) { - Ruby runtime = context.runtime; - - RubyString part = inspectPrefix(runtime.getCurrentContext(), metaClass.getRealClass(), inspectHashCode()); + RubyString part = inspectPrefix(context, metaClass.getRealClass(), inspectHashCode()); part.cat(Inspector.SPACE); part.cat(Inspector.SIZE_EQUALS); diff --git a/core/src/main/java/org/jruby/RubyProc.java b/core/src/main/java/org/jruby/RubyProc.java index ab5b2de9e2c..720009fc7dd 100644 --- a/core/src/main/java/org/jruby/RubyProc.java +++ b/core/src/main/java/org/jruby/RubyProc.java @@ -37,6 +37,7 @@ import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.ast.util.ArgsUtil; import org.jruby.common.IRubyWarnings; import org.jruby.ir.runtime.IRRuntimeHelpers; @@ -56,6 +57,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.marshal.DataType; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.RubyStringBuilder.types; /** @@ -293,7 +296,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { if (type != other.type) return context.fals; - return context.runtime.newBoolean(getBlock().equals(other.block)); + return asBoolean(context, getBlock().equals(other.block)); } /** @@ -401,7 +404,7 @@ public RubyProc to_proc() { public IRubyObject source_location(ThreadContext context) { Ruby runtime = context.runtime; if (file != null) { - return runtime.newArray(runtime.newString(file), runtime.newFixnum(line + 1 /*zero-based*/)); + return runtime.newArray(runtime.newString(file), asFixnum(context, line + 1 /*zero-based*/)); } if (block != null) { @@ -410,8 +413,8 @@ public IRubyObject source_location(ThreadContext context) { // block+binding may exist for a core method, which will have a null filename if (binding.getFile() != null) { return runtime.newArray( - runtime.newString(binding.getFile()), - runtime.newFixnum(binding.getLine() + 1 /*zero-based*/)); + Convert.asString(context, binding.getFile()), + asFixnum(context, binding.getLine() + 1 /*zero-based*/)); } } @@ -442,7 +445,7 @@ private IRubyObject parametersCommon(ThreadContext context, boolean isLambda) { @JRubyMethod(name = "lambda?") public IRubyObject lambda_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isLambda()); + return asBoolean(context, isLambda()); } private boolean isLambda() { diff --git a/core/src/main/java/org/jruby/RubyProcess.java b/core/src/main/java/org/jruby/RubyProcess.java index 0da899dd86b..96a60f99cab 100644 --- a/core/src/main/java/org/jruby/RubyProcess.java +++ b/core/src/main/java/org/jruby/RubyProcess.java @@ -53,6 +53,8 @@ import org.jruby.runtime.Signature; import org.jruby.runtime.ThreadContext; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.runtime.Helpers.throwException; import static org.jruby.runtime.Visibility.*; import org.jruby.runtime.builtin.IRubyObject; @@ -269,7 +271,7 @@ public IRubyObject op_and(ThreadContext context, IRubyObject arg) { break; } - return getRuntime().newFixnum(status & mask); + return asFixnum(context, status & mask); } private static void deprecateAndSuggest(ThreadContext context, String method, String suggest) { @@ -277,18 +279,32 @@ private static void deprecateAndSuggest(ThreadContext context, String method, St } @JRubyMethod(name = "stopped?") + public IRubyObject stopped_p(ThreadContext context) { + return asBoolean(context, PosixShim.WAIT_MACROS.WIFSTOPPED(status)); + } + + @Deprecated public IRubyObject stopped_p() { - return RubyBoolean.newBoolean(getRuntime(), PosixShim.WAIT_MACROS.WIFSTOPPED(status)); + return stopped_p(getRuntime().getCurrentContext()); } @JRubyMethod(name = "signaled?") + public IRubyObject signaled(ThreadContext context) { + return asBoolean(context, PosixShim.WAIT_MACROS.WIFSIGNALED(status)); + } + + @Deprecated public IRubyObject signaled() { - return RubyBoolean.newBoolean(getRuntime(), PosixShim.WAIT_MACROS.WIFSIGNALED(status)); + return signaled(getRuntime().getCurrentContext()); } @JRubyMethod(name = "exited?") + public IRubyObject exited(ThreadContext context) { + return asBoolean(context, PosixShim.WAIT_MACROS.WIFEXITED(status)); + } + public IRubyObject exited() { - return RubyBoolean.newBoolean(getRuntime(), PosixShim.WAIT_MACROS.WIFEXITED(status)); + return exited(getRuntime().getCurrentContext()); } @JRubyMethod @@ -339,16 +355,14 @@ public IRubyObject op_rshift(ThreadContext context, IRubyObject other) { break; } - return context.runtime.newFixnum(status >> places); + return asFixnum(context, status >> places); } @Override @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - Ruby runtime = context.runtime; - - if (this == other) return runtime.getTrue(); - return invokedynamic(context, runtime.newFixnum(status), MethodNames.OP_EQUAL, other); + if (this == other) return context.tru; + return invokedynamic(context, asFixnum(context, status), MethodNames.OP_EQUAL, other); } @JRubyMethod @@ -368,20 +382,23 @@ public IRubyObject inspect(ThreadContext context) { @JRubyMethod(name = "success?") public IRubyObject success_p(ThreadContext context) { - if (!PosixShim.WAIT_MACROS.WIFEXITED(status)) { - return context.nil; - } - return RubyBoolean.newBoolean(context, PosixShim.WAIT_MACROS.WEXITSTATUS(status) == EXIT_SUCCESS); + if (!PosixShim.WAIT_MACROS.WIFEXITED(status)) return context.nil; + + return asBoolean(context, PosixShim.WAIT_MACROS.WEXITSTATUS(status) == EXIT_SUCCESS); } @JRubyMethod(name = "coredump?") + public IRubyObject coredump_p(ThreadContext context) { + return asBoolean(context, PosixShim.WAIT_MACROS.WCOREDUMP(status)); + } + public IRubyObject coredump_p() { - return RubyBoolean.newBoolean(getRuntime(), PosixShim.WAIT_MACROS.WCOREDUMP(status)); + return coredump_p(getRuntime().getCurrentContext()); } @JRubyMethod public IRubyObject pid(ThreadContext context) { - return context.runtime.newFixnum(pid); + return asFixnum(context, pid); } public long getStatus() { @@ -476,7 +493,7 @@ public IRubyObject to_i() { @Deprecated public IRubyObject op_rshift(Ruby runtime, IRubyObject other) { long shiftValue = other.convertToInteger().getLongValue(); - return runtime.newFixnum(status >> shiftValue); + return asFixnum(runtime.getCurrentContext(), status >> shiftValue); } @Deprecated @@ -498,7 +515,7 @@ public static IRubyObject eid(IRubyObject self) { } @JRubyMethod(name = "eid", module = true, visibility = PRIVATE) public static IRubyObject eid(ThreadContext context, IRubyObject self) { - return euid(context.runtime); + return euid(context, self); } @Deprecated @@ -507,7 +524,7 @@ public static IRubyObject eid(IRubyObject self, IRubyObject arg) { } @JRubyMethod(name = "eid=", module = true, visibility = PRIVATE) public static IRubyObject eid(ThreadContext context, IRubyObject self, IRubyObject arg) { - return eid(context.runtime, arg); + return eid(context, self, arg); } public static IRubyObject eid(Ruby runtime, IRubyObject arg) { return euid_set(runtime, arg); @@ -666,16 +683,16 @@ public static IRubyObject getegid(IRubyObject self) { } @JRubyMethod(name = "getegid", module = true, visibility = PRIVATE) public static IRubyObject getegid(ThreadContext context, IRubyObject self) { - return egid(context.runtime); + return egid(context, self); } @Deprecated public static IRubyObject geteuid(IRubyObject self) { - return euid(self.getRuntime()); + return euid(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "geteuid", module = true, visibility = PRIVATE) public static IRubyObject geteuid(ThreadContext context, IRubyObject self) { - return euid(context.runtime); + return euid(context, self); } @Deprecated @@ -684,7 +701,7 @@ public static IRubyObject getgid(IRubyObject self) { } @JRubyMethod(name = "getgid", module = true, visibility = PRIVATE) public static IRubyObject getgid(ThreadContext context, IRubyObject self) { - return gid(context.runtime); + return gid(context, self); } @Deprecated @@ -693,7 +710,7 @@ public static IRubyObject getuid(IRubyObject self) { } @JRubyMethod(name = "getuid", module = true, visibility = PRIVATE) public static IRubyObject getuid(ThreadContext context, IRubyObject self) { - return uid(context.runtime); + return uid(context, self); } @Deprecated @@ -702,7 +719,7 @@ public static IRubyObject setegid(IRubyObject recv, IRubyObject arg) { } @JRubyMethod(name = "setegid", module = true, visibility = PRIVATE) public static IRubyObject setegid(ThreadContext context, IRubyObject recv, IRubyObject arg) { - return egid_set(context.runtime, arg); + return egid_set(context, recv, arg); } @Deprecated @@ -1021,10 +1038,12 @@ public static IRubyObject getpgrp(IRubyObject recv) { } @JRubyMethod(name = "getpgrp", module = true, visibility = PRIVATE) public static IRubyObject getpgrp(ThreadContext context, IRubyObject recv) { - return getpgrp(context.runtime); + return asFixnum(context, context.runtime.getPosix().getpgrp()); } + + @Deprecated public static IRubyObject getpgrp(Ruby runtime) { - return runtime.newFixnum(runtime.getPosix().getpgrp()); + return asFixnum(runtime.getCurrentContext(), runtime.getPosix().getpgrp()); } @JRubyMethod(name = "groups=", module = true, visibility = PRIVATE) @@ -1199,7 +1218,7 @@ public static IRubyObject wait(Ruby runtime, IRubyObject[] args) { checkErrno(runtime, pid, ECHILD); context.setLastExitStatus(RubyProcess.RubyStatus.newProcessStatus(runtime, status[0], pid)); - return runtime.newFixnum(pid); + return asFixnum(context, pid); } @@ -1216,14 +1235,14 @@ public static IRubyObject waitall(Ruby runtime) { RubyArray results = runtime.newArray(); int[] status = new int[1]; - ThreadContext currentContext = runtime.getCurrentContext(); + ThreadContext context = runtime.getCurrentContext(); - int result = pthreadKillable(currentContext, ctx -> posix.wait(status)); + int result = pthreadKillable(context, ctx -> posix.wait(status)); while (result != -1) { - results.append(runtime.newArray(runtime.newFixnum(result), RubyProcess.RubyStatus.newProcessStatus(runtime, status[0], result))); + results.append(runtime.newArray(asFixnum(context, result), RubyProcess.RubyStatus.newProcessStatus(runtime, status[0], result))); - result = pthreadKillable(currentContext, ctx -> posix.wait(status)); + result = pthreadKillable(context, ctx -> posix.wait(status)); } return results; @@ -1235,10 +1254,12 @@ public static IRubyObject setsid(IRubyObject recv) { } @JRubyMethod(name = "setsid", module = true, visibility = PRIVATE) public static IRubyObject setsid(ThreadContext context, IRubyObject recv) { - return setsid(context.runtime); + return asFixnum(context, checkErrno(context.runtime, context.runtime.getPosix().setsid())); } + + @Deprecated public static IRubyObject setsid(Ruby runtime) { - return runtime.newFixnum(checkErrno(runtime, runtime.getPosix().setsid())); + return setsid(runtime.getCurrentContext(), null); } @Deprecated @@ -1247,10 +1268,12 @@ public static IRubyObject setpgrp(IRubyObject recv) { } @JRubyMethod(name = "setpgrp", module = true, visibility = PRIVATE) public static IRubyObject setpgrp(ThreadContext context, IRubyObject recv) { - return setpgrp(context.runtime); + return asFixnum(context, checkErrno(context.runtime, context.runtime.getPosix().setpgid(0, 0))); } + + @Deprecated public static IRubyObject setpgrp(Ruby runtime) { - return runtime.newFixnum(checkErrno(runtime, runtime.getPosix().setpgid(0, 0))); + return setpgrp(runtime.getCurrentContext(), null); } @Deprecated @@ -1282,10 +1305,12 @@ public static IRubyObject euid(IRubyObject recv) { } @JRubyMethod(name = "euid", module = true, visibility = PRIVATE) public static IRubyObject euid(ThreadContext context, IRubyObject recv) { - return euid(context.runtime); + return asFixnum(context, checkErrno(context.runtime, context.runtime.getPosix().geteuid())); } + + @Deprecated public static IRubyObject euid(Ruby runtime) { - return runtime.newFixnum(checkErrno(runtime, runtime.getPosix().geteuid())); + return euid(runtime.getCurrentContext(), null); } @Deprecated diff --git a/core/src/main/java/org/jruby/RubyRandom.java b/core/src/main/java/org/jruby/RubyRandom.java index 1e277b94c4b..53cb1125353 100644 --- a/core/src/main/java/org/jruby/RubyRandom.java +++ b/core/src/main/java/org/jruby/RubyRandom.java @@ -33,8 +33,7 @@ import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.ThreadContext; -import static org.jruby.api.Convert.castAsBignum; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.PRIVATE; import org.jruby.runtime.builtin.IRubyObject; @@ -327,7 +326,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { if (!getType().equals(obj.getType())) { return context.fals; } - return RubyBoolean.newBoolean(context, random.equals(((RubyRandom) obj).random)); + return asBoolean(context, random.equals(((RubyRandom) obj).random)); } // c: random_state diff --git a/core/src/main/java/org/jruby/RubyRandomBase.java b/core/src/main/java/org/jruby/RubyRandomBase.java index ae7649e38c0..ba0a930a14c 100644 --- a/core/src/main/java/org/jruby/RubyRandomBase.java +++ b/core/src/main/java/org/jruby/RubyRandomBase.java @@ -13,8 +13,7 @@ import java.math.BigInteger; import java.nio.ByteBuffer; -import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.runtime.Visibility.PRIVATE; import static org.jruby.util.TypeConverter.toFloat; @@ -523,7 +522,7 @@ public static long randomLongLimited(ThreadContext context, IRubyObject obj, lon RubyRandom.RandomType rnd = tryGetRandomType(context, obj); if (rnd == null) { - RubyInteger v = Helpers.invokePublic(context, obj, "rand", context.runtime.newFixnum(limit + 1)).convertToInteger(); + RubyInteger v = Helpers.invokePublic(context, obj, "rand", asFixnum(context, limit + 1)).convertToInteger(); long r = numericToLong(context, v); if (r < 0) throw context.runtime.newRangeError("random number too small " + r); if (r > limit) throw context.runtime.newRangeError("random number too big " + r); diff --git a/core/src/main/java/org/jruby/RubyRange.java b/core/src/main/java/org/jruby/RubyRange.java index 8f11431c912..e4838b0557d 100644 --- a/core/src/main/java/org/jruby/RubyRange.java +++ b/core/src/main/java/org/jruby/RubyRange.java @@ -59,8 +59,7 @@ import static org.jruby.RubyEnumerator.enumeratorizeWithSize; import static org.jruby.RubyNumeric.*; -import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.hashEnd; import static org.jruby.runtime.Helpers.hashStart; @@ -133,7 +132,7 @@ public static RubyRange newRange(ThreadContext context, IRubyObject begin, IRuby public static RubyRange newBeginlessRange(ThreadContext context, long end, boolean isExclusive) { Ruby runtime = context.runtime; RubyRange range = new RubyRange(runtime, runtime.getRange()); - range.init(context, context.nil, runtime.newFixnum(end), isExclusive); + range.init(context, context.nil, asFixnum(context, end), isExclusive); range.isInited = true; return range; } @@ -141,7 +140,7 @@ public static RubyRange newBeginlessRange(ThreadContext context, long end, boole public static RubyRange newEndlessRange(ThreadContext context, long begin, boolean isExclusive) { Ruby runtime = context.runtime; RubyRange range = new RubyRange(runtime, runtime.getRange()); - range.init(context, runtime.newFixnum(begin), context.nil, isExclusive); + range.init(context, asFixnum(context, begin), context.nil, isExclusive); range.isInited = true; return range; } @@ -149,7 +148,7 @@ public static RubyRange newEndlessRange(ThreadContext context, long begin, boole public static RubyRange newRange(ThreadContext context, long begin, long end, boolean isExclusive) { Ruby runtime = context.runtime; RubyRange range = new RubyRange(runtime, runtime.getRange()); - range.init(context, runtime.newFixnum(begin), runtime.newFixnum(end), isExclusive); + range.init(context, asFixnum(context, begin), asFixnum(context, end), isExclusive); range.isInited = true; return range; } @@ -353,21 +352,18 @@ public RubyFixnum hash() { @JRubyMethod(name = "hash") public RubyFixnum hash(ThreadContext context) { - Ruby runtime = context.runtime; - int exclusiveBit = isExclusive ? 1 : 0; long hash = exclusiveBit; - IRubyObject v; - hash = hashStart(runtime, hash); - v = safeHash(context, begin); + hash = hashStart(context.runtime, hash); + IRubyObject v = safeHash(context, begin); hash = murmurCombine(hash, v.convertToInteger().getLongValue()); v = safeHash(context, end); hash = murmurCombine(hash, v.convertToInteger().getLongValue()); hash = murmurCombine(hash, exclusiveBit << 24); hash = hashEnd(hash); - return runtime.newFixnum(hash); + return asFixnum(context, hash); } private static RubyString inspectValue(final ThreadContext context, IRubyObject value) { @@ -417,8 +413,13 @@ private RubyString to_s(final Ruby runtime) { } @JRubyMethod(name = "exclude_end?") + public RubyBoolean exclude_end_p(ThreadContext context) { + return asBoolean(context, isExclusive); + } + + @Deprecated public RubyBoolean exclude_end_p() { - return getRuntime().newBoolean(isExclusive); + return exclude_end_p(getRuntime().getCurrentContext()); } @JRubyMethod(name = "eql?") @@ -437,7 +438,7 @@ private IRubyObject equalityInner(ThreadContext context, IRubyObject other, Meth RubyRange otherRange = (RubyRange) other; - return RubyBoolean.newBoolean(context, isExclusive == otherRange.isExclusive && + return asBoolean(context, isExclusive == otherRange.isExclusive && invokedynamic(context, this.begin, equalityCheck, otherRange.begin).isTrue() && invokedynamic(context, this.end, equalityCheck, otherRange.end).isTrue()); } @@ -1006,7 +1007,7 @@ private IRubyObject includeCommon(ThreadContext context, final IRubyObject val, if (iterable || !TypeConverter.convertToTypeWithCheck(context, begin, runtime.getInteger(), to_int_checked).isNil() || !TypeConverter.convertToTypeWithCheck(context, end, runtime.getInteger(), to_int_checked).isNil()) { - return RubyBoolean.newBoolean(context, rangeIncludes(context, val)); + return asBoolean(context, rangeIncludes(context, val)); } else if ((begin instanceof RubyString) || (end instanceof RubyString)) { if ((begin instanceof RubyString) && (end instanceof RubyString)) { if (useStringCover) { @@ -1047,16 +1048,16 @@ private static boolean linearObject(ThreadContext context, IRubyObject obj) { public IRubyObject eqq_p(ThreadContext context, IRubyObject obj) { IRubyObject result = includeCommon(context, obj, true); if (result != UNDEF) return result; - return RubyBoolean.newBoolean(context, rangeIncludes(context, obj)); + return asBoolean(context, rangeIncludes(context, obj)); } @JRubyMethod(name = "cover?") public RubyBoolean cover_p(ThreadContext context, IRubyObject obj) { if (obj instanceof RubyRange) { - return RubyBoolean.newBoolean(context, coverRange(context, (RubyRange) obj)); + return asBoolean(context, coverRange(context, (RubyRange) obj)); } - return RubyBoolean.newBoolean(context, rangeIncludes(context, obj)); + return asBoolean(context, rangeIncludes(context, obj)); } // MRI: r_cover_p @@ -1475,7 +1476,7 @@ public static IRubyObject double_to_long_bits(ThreadContext context, IRubyObject longBits = Double.doubleToLongBits(((RubyFloat) flote).getDoubleValue()); } - return context.runtime.newFixnum(longBits); + return asFixnum(context, longBits); } @JRubyMethod(meta = true) diff --git a/core/src/main/java/org/jruby/RubyRational.java b/core/src/main/java/org/jruby/RubyRational.java index 042cb9f4b66..b2e67c7de5e 100644 --- a/core/src/main/java/org/jruby/RubyRational.java +++ b/core/src/main/java/org/jruby/RubyRational.java @@ -52,7 +52,7 @@ import org.jruby.util.Numeric; import org.jruby.util.TypeConverter; -import static org.jruby.api.Convert.checkToInteger; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.ast.util.ArgsUtil.hasExceptionOption; import static org.jruby.runtime.Helpers.invokedynamic; @@ -231,13 +231,13 @@ private static RubyNumeric canonicalizeInternal(ThreadContext context, RubyClass } private static RubyNumeric canonicalizeInternal(ThreadContext context, RubyClass clazz, long num, long den) { - if (den == 0) - throw context.runtime.newZeroDivisionError(); + if (den == 0) throw context.runtime.newZeroDivisionError(); + if (num == Long.MIN_VALUE && den == Long.MIN_VALUE) - canonicalizeInternal(context, clazz, context.runtime.newFixnum(num), context.runtime.newFixnum(den)); + canonicalizeInternal(context, clazz, asFixnum(context, num), asFixnum(context, den)); long gcd = i_gcd(num, den); - RubyInteger _num = (RubyInteger) context.runtime.newFixnum(num).idiv(context, gcd); - RubyInteger _den = (RubyInteger) context.runtime.newFixnum(den).idiv(context, gcd); + RubyInteger _num = (RubyInteger) asFixnum(context, num).idiv(context, gcd); + RubyInteger _den = (RubyInteger) asFixnum(context, den).idiv(context, gcd); if (Numeric.CANON && canonicalization && _den.getLongValue() == 1) return _num; @@ -574,7 +574,7 @@ public RubyInteger getDenominator() { @Override public IRubyObject zero_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isZero()); + return asBoolean(context, isZero()); } @Override @@ -589,12 +589,12 @@ public IRubyObject nonzero_p(ThreadContext context) { @Override public IRubyObject isNegative(ThreadContext context) { - return RubyBoolean.newBoolean(context, signum() < 0); + return asBoolean(context, signum() < 0); } @Override public IRubyObject isPositive(ThreadContext context) { - return RubyBoolean.newBoolean(context, signum() > 0); + return asBoolean(context, signum() > 0); } @Override @@ -969,14 +969,14 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) { } public final IRubyObject op_equal(ThreadContext context, RubyInteger other) { - if (num.isZero()) return RubyBoolean.newBoolean(context, other.isZero()); + if (num.isZero()) return asBoolean(context, other.isZero()); if (!(den instanceof RubyFixnum) || den.getLongValue() != 1) return context.fals; return f_equal(context, num, other); } final RubyBoolean op_equal(ThreadContext context, RubyRational other) { - if (num.isZero()) return RubyBoolean.newBoolean(context, other.num.isZero()); - return RubyBoolean.newBoolean(context, + if (num.isZero()) return asBoolean(context, other.num.isZero()); + return asBoolean(context, f_equal(context, num, other.num).isTrue() && f_equal(context, den, other.den).isTrue()); } @@ -1163,7 +1163,7 @@ public IRubyObject roundCommon(ThreadContext context, final IRubyObject n, Round final int nsign = ((RubyInteger) n).signum(); - RubyNumeric b = f_expt(context, context.runtime.newFixnum(10), (RubyInteger) n); + RubyNumeric b = f_expt(context, asFixnum(context, 10), (RubyInteger) n); IRubyObject s = nsign >= 0 ? op_mul(context, (RubyInteger) b) : op_mul(context, b); // (RubyRational) b diff --git a/core/src/main/java/org/jruby/RubyRegexp.java b/core/src/main/java/org/jruby/RubyRegexp.java index 6d85480e03a..05046245467 100755 --- a/core/src/main/java/org/jruby/RubyRegexp.java +++ b/core/src/main/java/org/jruby/RubyRegexp.java @@ -79,7 +79,7 @@ import org.jruby.util.io.EncodingUtils; import org.jruby.util.collections.WeakValuedMap; -import static org.jruby.api.Convert.castAsHash; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.ThreadContext.resetCallInfo; import static org.jruby.util.RubyStringBuilder.str; @@ -1041,23 +1041,19 @@ public RubyFixnum hash() { @JRubyMethod(name = {"==", "eql?"}) @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - if (this == other) { - return context.tru; - } - if (!(other instanceof RubyRegexp)) { - return context.fals; - } + if (this == other) return context.tru; + if (!(other instanceof RubyRegexp)) return context.fals; + RubyRegexp otherRegex = (RubyRegexp) other; check(); otherRegex.check(); - return RubyBoolean.newBoolean(context, str.equal(otherRegex.str) && options.equals(otherRegex.options)); + return asBoolean(context, str.equal(otherRegex.str) && options.equals(otherRegex.options)); } @JRubyMethod(name = "~", reads = {LASTLINE}, writes = BACKREF) public IRubyObject op_match2(ThreadContext context) { - Ruby runtime = context.runtime; IRubyObject line = context.getLastLine(); if (line instanceof RubyString) { int start = searchString(context, (RubyString) line, 0, false); @@ -1065,7 +1061,7 @@ public IRubyObject op_match2(ThreadContext context) { // set backref for user context.updateBackref(); - return runtime.newFixnum(start); + return asFixnum(context, start); } } @@ -1374,13 +1370,18 @@ static RubyMatchData createMatchData(ThreadContext context, RubyString str, int } @JRubyMethod + public IRubyObject options(ThreadContext context) { + return asFixnum(context, getOptions().toOptions()); + } + + @Deprecated public IRubyObject options() { - return metaClass.runtime.newFixnum(getOptions().toOptions()); + return options(getCurrentContext()); } @JRubyMethod(name = "casefold?") public IRubyObject casefold_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getOptions().isIgnorecase()); + return asBoolean(context, getOptions().isIgnorecase()); } /** rb_reg_source @@ -1574,7 +1575,7 @@ public IRubyObject encoding(ThreadContext context) { @JRubyMethod(name = "fixed_encoding?") public IRubyObject fixed_encoding_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, options.isFixed()); + return asBoolean(context, options.isFixed()); } private record RegexpArgs(RubyString string, int options, IRubyObject timeout) {} diff --git a/core/src/main/java/org/jruby/RubySignal.java b/core/src/main/java/org/jruby/RubySignal.java index cfbd55d4859..d6aa0b5a32e 100644 --- a/core/src/main/java/org/jruby/RubySignal.java +++ b/core/src/main/java/org/jruby/RubySignal.java @@ -40,6 +40,8 @@ import java.util.*; +import static org.jruby.api.Convert.asFixnum; + @JRubyModule(name="Signal") public class RubySignal { @@ -143,9 +145,9 @@ public static IRubyObject list(ThreadContext context, IRubyObject recv) { if (names == null) { names = RubyHash.newHash(runtime); for (Map.Entry sig : RubySignal.list().entrySet()) { - names.op_aset(context, runtime.newDeduplicatedString(sig.getKey()), runtime.newFixnum(sig.getValue())); + names.op_aset(context, runtime.newDeduplicatedString(sig.getKey()), asFixnum(context, sig.getValue())); } - names.op_aset(context, runtime.newDeduplicatedString("EXIT"), runtime.newFixnum(0)); + names.op_aset(context, runtime.newDeduplicatedString("EXIT"), asFixnum(context, 0)); recv.getInternalVariables().setInternalVariable("signal_list", names); } else { names.dup(context); diff --git a/core/src/main/java/org/jruby/RubySignalException.java b/core/src/main/java/org/jruby/RubySignalException.java index 4410b9651a5..3bd28dd5de1 100644 --- a/core/src/main/java/org/jruby/RubySignalException.java +++ b/core/src/main/java/org/jruby/RubySignalException.java @@ -30,18 +30,17 @@ import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.exceptions.RaiseException; import org.jruby.exceptions.SignalException; import org.jruby.runtime.Arity; import org.jruby.runtime.Block; import org.jruby.runtime.ThreadContext; -import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.integerAsLong; +import static org.jruby.api.Convert.*; import static org.jruby.runtime.Visibility.PRIVATE; import org.jruby.runtime.builtin.IRubyObject; -import org.jruby.util.TypeConverter; /** * The Java representation of a Ruby SignalException. @@ -69,15 +68,12 @@ static RubyClass define(Ruby runtime, RubyClass exceptionClass) { @JRubyMethod(required = 1, optional = 2, checkArity = false, visibility = PRIVATE) public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block) { int argc = Arity.checkArgumentCount(context, args, 1, 2); - - final Ruby runtime = context.runtime; int argnum = 1; - IRubyObject sig = checkToInteger(context, args[0]); if (sig.isNil()) { sig = args[0]; - Arity.checkArgumentCount(runtime, args, 1, argnum); + Arity.checkArgumentCount(context.runtime, args, 1, argnum); } else { argnum = 2; } @@ -85,29 +81,25 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block b long _signo; if (argnum == 2) { - _signo = integerAsLong(context, (RubyInteger) sig); - if (_signo < 0 || _signo > NSIG.longValue()) { - throw runtime.newArgumentError("invalid signal number (" + _signo + ")"); - } + _signo = asLong(context, (RubyInteger) sig); + if (_signo < 0 || _signo > NSIG.longValue()) throw context.runtime.newArgumentError("invalid signal number (" + _signo + ")"); if (argc > 1) { sig = args[1]; } else { - sig = runtime.newString(RubySignal.signmWithPrefix(RubySignal.signo2signm(_signo))); + sig = Convert.asString(context, RubySignal.signmWithPrefix(RubySignal.signo2signm(_signo))); } } else { String signm = sig.toString(); _signo = RubySignal.signm2signo(RubySignal.signmWithoutPrefix(signm)); - if (_signo == 0) { - throw runtime.newArgumentError("unsupported name " + sig); - } + if (_signo == 0) throw context.runtime.newArgumentError("unsupported name " + sig); - sig = runtime.newString(RubySignal.signmWithPrefix(signm)); + sig = Convert.asString(context, RubySignal.signmWithPrefix(signm)); } super.initialize(new IRubyObject[]{sig}, block); - this.signo = runtime.newFixnum(_signo); + this.signo = asFixnum(context, _signo); return this; } diff --git a/core/src/main/java/org/jruby/RubyString.java b/core/src/main/java/org/jruby/RubyString.java index 09d389ce708..88df60e4d35 100644 --- a/core/src/main/java/org/jruby/RubyString.java +++ b/core/src/main/java/org/jruby/RubyString.java @@ -1278,12 +1278,12 @@ public final int compareTo(IRubyObject other) { @JRubyMethod(name = "<=>") @Override public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { - if (other instanceof RubyString otherStr) return context.runtime.newFixnum(op_cmp(otherStr)); + if (other instanceof RubyString otherStr) return asFixnum(context, op_cmp(otherStr)); JavaSites.CheckedSites sites = sites(context).to_str_checked; if (sites.respond_to_X.respondsTo(context, this, other)) { IRubyObject tmp = TypeConverter.checkStringType(context, sites, other); - if (tmp instanceof RubyString otherStr) return context.runtime.newFixnum(op_cmp(otherStr)); + if (tmp instanceof RubyString otherStr) return asFixnum(context, op_cmp(otherStr)); return context.nil; } else { return invcmp(context, sites(context).recursive_cmp, this, other); @@ -1298,15 +1298,14 @@ public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { public IRubyObject op_equal(ThreadContext context, IRubyObject other) { if (this == other) return context.tru; - if (other instanceof RubyString otherStr) { - return StringSupport.areComparable(this, otherStr) && value.equal(otherStr.value) ? context.tru : context.fals; - } - return op_equalCommon(context, other); + return other instanceof RubyString otherStr ? + asBoolean(context, StringSupport.areComparable(this, otherStr) && value.equal(otherStr.value)) : + op_equalCommon(context, other); } private IRubyObject op_equalCommon(ThreadContext context, IRubyObject other) { if (!sites(context).respond_to_to_str.respondsTo(context, this, other)) return context.fals; - return sites(context).equals.call(context, this, other, this).isTrue() ? context.tru : context.fals; + return asBoolean(context, sites(context).equals.call(context, this, other, this).isTrue()); } @JRubyMethod(name = {"-@", "dedup"}) // -'foo' returns frozen string @@ -1818,7 +1817,7 @@ public IRubyObject casecmp_p(ThreadContext context, IRubyObject other) { down.downcase_bang(context, flags); RubyString otherDown = otherStr.strDup(runtime); otherDown.downcase_bang(context, flags); - return down.equals(otherDown) ? context.tru : context.fals; + return asBoolean(context, down.equals(otherDown)); } /** rb_str_match @@ -1897,10 +1896,8 @@ public IRubyObject op_ge19(ThreadContext context, IRubyObject other) { @JRubyMethod(name = ">=") public IRubyObject op_ge(ThreadContext context, IRubyObject other) { - if (other instanceof RubyString otherStr && cmpIsBuiltin(context)) { - return RubyBoolean.newBoolean(context, op_cmp(otherStr) >= 0); - } - return RubyComparable.op_ge(context, this, other); + return other instanceof RubyString otherStr && cmpIsBuiltin(context) ? + asBoolean(context, op_cmp(otherStr) >= 0) : RubyComparable.op_ge(context, this, other); } @Deprecated @@ -1910,10 +1907,8 @@ public IRubyObject op_gt19(ThreadContext context, IRubyObject other) { @JRubyMethod(name = ">") public IRubyObject op_gt(ThreadContext context, IRubyObject other) { - if (other instanceof RubyString otherStr && cmpIsBuiltin(context)) { - return RubyBoolean.newBoolean(context, op_cmp(otherStr) > 0); - } - return RubyComparable.op_gt(context, this, other); + return other instanceof RubyString otherStr && cmpIsBuiltin(context) ? + asBoolean(context, op_cmp(otherStr) > 0) : RubyComparable.op_gt(context, this, other); } @Deprecated @@ -1923,10 +1918,8 @@ public IRubyObject op_le19(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "<=") public IRubyObject op_le(ThreadContext context, IRubyObject other) { - if (other instanceof RubyString otherStr && cmpIsBuiltin(context)) { - return RubyBoolean.newBoolean(context, op_cmp(otherStr) <= 0); - } - return RubyComparable.op_le(context, this, other); + return other instanceof RubyString otherStr && cmpIsBuiltin(context) ? + asBoolean(context, op_cmp(otherStr) <= 0) : RubyComparable.op_le(context, this, other); } @Deprecated @@ -1936,10 +1929,8 @@ public IRubyObject op_lt19(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "<") public IRubyObject op_lt(ThreadContext context, IRubyObject other) { - if (other instanceof RubyString otherStr && cmpIsBuiltin(context)) { - return RubyBoolean.newBoolean(context, op_cmp(otherStr) < 0); - } - return RubyComparable.op_lt(context, sites(context).cmp, this, other); + return other instanceof RubyString otherStr && cmpIsBuiltin(context) ? + asBoolean(context, op_cmp(otherStr) < 0) : RubyComparable.op_lt(context, sites(context).cmp, this, other); } private boolean cmpIsBuiltin(ThreadContext context) { @@ -1953,9 +1944,8 @@ public IRubyObject str_eql_p19(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "eql?") public IRubyObject str_eql_p(ThreadContext context, IRubyObject other) { - return other instanceof RubyString otherStr && - StringSupport.areComparable(this, otherStr) && - value.equal(otherStr.value) ? context.tru : context.fals; + return asBoolean(context, + other instanceof RubyString otherStr && StringSupport.areComparable(this, otherStr) && value.equal(otherStr.value)); } private int caseMap(Ruby runtime, int flags, Encoding enc) { @@ -2662,19 +2652,19 @@ public int size() { // MRI: rb_str_length @JRubyMethod(name = {"length", "size"}) public RubyFixnum rubyLength(final ThreadContext context) { - return rubyLength(context.runtime); + return asFixnum(context, strLength()); } - private RubyFixnum rubyLength(final Ruby runtime) { - return runtime.newFixnum(strLength()); + @JRubyMethod(name = "bytesize") + public RubyFixnum bytesize(ThreadContext context) { + return asFixnum(context, value.getRealSize()); } - @JRubyMethod(name = "bytesize") + @Deprecated public RubyFixnum bytesize() { - return getRuntime().newFixnum(value.getRealSize()); + return bytesize(getCurrentContext()); } - // CharSequence @Override @@ -2720,7 +2710,7 @@ private static IRubyObject byteSize(ThreadContext context, RubyString recv, IRub */ @JRubyMethod(name = "empty?") public RubyBoolean empty_p(ThreadContext context) { - return isEmpty() ? context.tru : context.fals; + return asBoolean(context, isEmpty()); } public boolean isEmpty() { @@ -3522,7 +3512,8 @@ private IRubyObject indexCommon(ThreadContext context, IRubyObject sub, int pos) } private IRubyObject byteIndexCommon(ThreadContext context, IRubyObject sub, int pos) { - if (pos == this.value.getRealSize()) return context.runtime.newFixnum(pos); + if (pos == this.value.getRealSize()) return asFixnum(context, pos); + if (sub instanceof RubyRegexp regexp) { if (pos > value.realSize()) return context.nil; pos = regexp.search(context, this, pos, false); @@ -3532,7 +3523,7 @@ private IRubyObject byteIndexCommon(ThreadContext context, IRubyObject sub, int pos = StringSupport.byteindex(this, str, pos, checkEncoding(str)); } - return pos < 0 ? context.nil : RubyFixnum.newFixnum(context.runtime, pos); + return pos < 0 ? context.nil : asFixnum(context, pos); } // MRI: rb_strseq_index @@ -3851,7 +3842,7 @@ private void replaceInternal19(Ruby runtime, int beg, int len, RubyString repl) public IRubyObject op_aref(ThreadContext context, IRubyObject arg) { Ruby runtime = context.runtime; if (arg instanceof RubyFixnum fixnum) { - return op_aref(runtime, integerAsInt(context, fixnum)); + return op_aref(runtime, asInt(context, fixnum)); } else if (arg instanceof RubyRegexp regexp) { return subpat(context, regexp); } else if (arg instanceof RubyString str) { @@ -4193,7 +4184,7 @@ private IRubyObject subpat(ThreadContext context, RubyRegexp regex) { @JRubyMethod(name = "[]=", writes = BACKREF) public IRubyObject op_aset(ThreadContext context, IRubyObject arg0, IRubyObject arg1) { if (arg0 instanceof RubyFixnum fixnum) { - return op_aset(context, integerAsInt(context, fixnum), arg1); + return op_aset(context, asInt(context, fixnum), arg1); } else if (arg0 instanceof RubyRegexp regexp) { subpatSet(context, regexp, null, arg1); return arg1; @@ -4465,7 +4456,7 @@ final IRubyObject uptoEndless(ThreadContext context, Block block) { @JRubyMethod(name = "include?") public RubyBoolean include_p(ThreadContext context, IRubyObject obj) { RubyString coerced = obj.convertToString(); - return StringSupport.index(this, coerced, 0, this.checkEncoding(coerced)) == -1 ? context.fals : context.tru; + return asBoolean(context, StringSupport.index(this, coerced, 0, this.checkEncoding(coerced)) != -1); } @JRubyMethod @@ -5183,10 +5174,8 @@ public IRubyObject start_with_p(ThreadContext context) { @JRubyMethod(name = "start_with?") public IRubyObject start_with_p(ThreadContext context, IRubyObject arg) { - if (arg instanceof RubyRegexp regexp) { - return regexp.startsWith(context, this) ? context.tru : context.fals; - } - return startsWith(arg.convertToString()) ? context.tru : context.fals; + return asBoolean(context, arg instanceof RubyRegexp regexp ? + regexp.startsWith(context, this) : startsWith(arg.convertToString())); } @JRubyMethod(name = "start_with?", rest = true) @@ -5216,7 +5205,7 @@ public IRubyObject end_with_p(ThreadContext context) { @JRubyMethod(name = "end_with?") public IRubyObject end_with_p(ThreadContext context, IRubyObject arg) { - return endWith(arg) ? context.tru : context.fals; + return asBoolean(context, endWith(arg)); } @JRubyMethod(name = "end_with?", rest = true) @@ -5920,13 +5909,13 @@ public IRubyObject count(ThreadContext context, IRubyObject arg) { while ( i < end ) { if ( ( bytes[i++] & 0xff ) == c ) n++; } - return RubyFixnum.newFixnum(runtime, n); + return asFixnum(context, n); } } final boolean[] table = new boolean[StringSupport.TRANS_SIZE + 1]; StringSupport.TrTables tables = StringSupport.trSetupTable(countValue, runtime, table, null, true, enc); - return runtime.newFixnum(StringSupport.strCount(value, runtime, table, tables, enc)); + return asFixnum(context, StringSupport.strCount(value, runtime, table, tables, enc)); } // MRI: rb_str_count for arity > 1, first half @@ -5949,7 +5938,7 @@ public IRubyObject count(ThreadContext context, IRubyObject[] args) { tables = StringSupport.trSetupTable(countStr.value, runtime, table, tables, false, enc); } - return runtime.newFixnum(StringSupport.strCount(value, runtime, table, tables, enc)); + return asFixnum(context, StringSupport.strCount(value, runtime, table, tables, enc)); } /** rb_str_delete / rb_str_delete_bang @@ -6304,7 +6293,7 @@ public IRubyObject chars(ThreadContext context, Block block) { * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ private static IRubyObject eachCharSize(ThreadContext context, RubyString recv, IRubyObject[] args) { - return recv.rubyLength(context.runtime); + return recv.rubyLength(context); } /** rb_str_each_codepoint @@ -6442,7 +6431,7 @@ else if (!wantarray) { * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ private static IRubyObject codepointSize(ThreadContext context, RubyString recv, IRubyObject[] args) { - return recv.rubyLength(context.runtime); + return recv.rubyLength(context); } private static final ByteList GRAPHEME_CLUSTER_PATTERN = new ByteList(new byte[] {(byte)'\\', (byte)'X'}, false); @@ -6456,7 +6445,7 @@ private static IRubyObject eachGraphemeClusterSize(ThreadContext context, RubySt Ruby runtime = context.runtime; ByteList value = self.getByteList(); Encoding enc = value.getEncoding(); - if (!enc.isUnicode()) return self.rubyLength(runtime); + if (!enc.isUnicode()) return self.rubyLength(context); Regex reg = RubyRegexp.getRegexpFromCache(runtime, GRAPHEME_CLUSTER_PATTERN, enc, RegexpOptions.NULL_OPTIONS); int beg = value.getBegin(); @@ -6755,12 +6744,12 @@ private IRubyObject force_encoding(Encoding encoding) { @JRubyMethod(name = "valid_encoding?") public IRubyObject valid_encoding_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, scanForCodeRange() != CR_BROKEN); + return asBoolean(context, scanForCodeRange() != CR_BROKEN); } @JRubyMethod(name = "ascii_only?") public IRubyObject ascii_only_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, scanForCodeRange() == CR_7BIT); + return asBoolean(context, scanForCodeRange() == CR_7BIT); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/RubyStruct.java b/core/src/main/java/org/jruby/RubyStruct.java index aa8c50f8b63..673dd4c8b59 100644 --- a/core/src/main/java/org/jruby/RubyStruct.java +++ b/core/src/main/java/org/jruby/RubyStruct.java @@ -59,6 +59,7 @@ import org.jruby.util.RecursiveComparator; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.invokedynamic; @@ -167,20 +168,18 @@ public IRubyObject deconstruct_keys(ThreadContext context, IRubyObject keysArg) @JRubyMethod public RubyFixnum hash(ThreadContext context) { - Ruby runtime = context.runtime; - int h = getType().hashCode(); - IRubyObject[] values = this.values; + for (int i = 0; i < values.length; i++) { h = (h << 1) | (h < 0 ? 1 : 0); IRubyObject hash = context.safeRecurse( (ctx, runtime1, obj, recur) -> recur ? RubyFixnum.zero(runtime1) : invokedynamic(ctx, obj, HASH), - runtime, values[i], "hash", true); + context.runtime, values[i], "hash", true); h ^= numericToLong(context, hash); } - return runtime.newFixnum(h); + return asFixnum(context, h); } private IRubyObject setByName(String name, IRubyObject value) { @@ -307,7 +306,7 @@ public static RubyClass newInstance(IRubyObject recv, IRubyObject[] args, Block newStruct.setReifiedClass(RubyStruct.class); newStruct.setClassIndex(ClassIndex.STRUCT); - newStruct.setInternalVariable(SIZE_VAR, member.length()); + newStruct.setInternalVariable(SIZE_VAR, member.length(runtime.getCurrentContext())); newStruct.setInternalVariable(MEMBER_VAR, member); newStruct.setInternalVariable(KEYWORD_INIT_VAR, keywordInitValue); @@ -594,7 +593,7 @@ public IRubyObject select(ThreadContext context, Block block) { * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ private static IRubyObject size(ThreadContext context, RubyStruct recv, IRubyObject[] args) { - return recv.size(); + return recv.size(context); } public IRubyObject set(IRubyObject value, int index) { @@ -750,8 +749,13 @@ public RubyHash to_h(ThreadContext context, Block block) { } @JRubyMethod(name = {"size", "length"} ) + public RubyFixnum size(ThreadContext context) { + return asFixnum(context, values.length); + } + + @Deprecated public RubyFixnum size() { - return getRuntime().newFixnum(values.length); + return size(getCurrentContext()); } public IRubyObject eachInternal(ThreadContext context, Block block) { diff --git a/core/src/main/java/org/jruby/RubySymbol.java b/core/src/main/java/org/jruby/RubySymbol.java index 45c04da2582..7be6d02c78a 100644 --- a/core/src/main/java/org/jruby/RubySymbol.java +++ b/core/src/main/java/org/jruby/RubySymbol.java @@ -76,6 +76,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.util.RubyStringBuilder.str; import static org.jruby.util.RubyStringBuilder.ids; @@ -524,13 +526,13 @@ public RubyString asString() { @JRubyMethod(name = "===") @Override public IRubyObject op_eqq(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, this == other); + return asBoolean(context, this == other); } @JRubyMethod(name = "==") @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, this == other); + return asBoolean(context, this == other); } @Deprecated @@ -541,7 +543,7 @@ public RubyFixnum hash() { @JRubyMethod public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCode()); + return asFixnum(context, hashCode()); } @Override diff --git a/core/src/main/java/org/jruby/RubyThread.java b/core/src/main/java/org/jruby/RubyThread.java index e23efb88cb4..5f2b0cbb18b 100644 --- a/core/src/main/java/org/jruby/RubyThread.java +++ b/core/src/main/java/org/jruby/RubyThread.java @@ -61,6 +61,7 @@ import org.joni.Matcher; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.exceptions.MainExitException; import org.jruby.exceptions.RaiseException; import org.jruby.exceptions.ThreadKill; @@ -93,6 +94,8 @@ import org.jruby.util.log.LoggerFactory; import org.jruby.common.IRubyWarnings.ID; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.*; import static org.jruby.util.RubyStringBuilder.ids; @@ -492,7 +495,7 @@ public Backtrace(Ruby runtime, RubyClass metaClass) { @JRubyMethod(module = true) public static IRubyObject limit(ThreadContext context, IRubyObject self) { - return context.runtime.newFixnum(context.runtime.getInstanceConfig().getBacktraceLimit()); + return asFixnum(context, context.runtime.getInstanceConfig().getBacktraceLimit()); } } @@ -538,12 +541,12 @@ public IRubyObject label(ThreadContext context) { @JRubyMethod public IRubyObject lineno(ThreadContext context) { - return context.runtime.newFixnum(element.getLineNumber()); + return asFixnum(context, element.getLineNumber()); } @JRubyMethod public IRubyObject path(ThreadContext context) { - return context.runtime.newString(element.getFileName()); + return Convert.asString(context, element.getFileName()); } @JRubyMethod @@ -890,7 +893,7 @@ public static IRubyObject pending_interrupt_p_s(ThreadContext context, IRubyObje @JRubyMethod(name = "pending_interrupt?") public IRubyObject pending_interrupt_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, !pendingInterruptQueue.isEmpty()); + return asBoolean(context, !pendingInterruptQueue.isEmpty()); } @JRubyMethod(name = "pending_interrupt?") @@ -1099,7 +1102,7 @@ public RubyBoolean key_p(ThreadContext context, IRubyObject key) { key = getSymbolKey(key); final Map locals = getFiberLocals(); synchronized (locals) { - return RubyBoolean.newBoolean(context, locals.containsKey(key)); + return asBoolean(context, locals.containsKey(key)); } } @@ -1122,7 +1125,7 @@ public IRubyObject thread_variable_p(ThreadContext context, IRubyObject key) { key = getSymbolKey(key); final Map locals = getThreadLocals(); synchronized (locals) { - return RubyBoolean.newBoolean(context, locals.containsKey(key)); + return asBoolean(context, locals.containsKey(key)); } } @@ -1199,7 +1202,7 @@ public static IRubyObject abort_on_exception_set(ThreadContext context, IRubyObj @JRubyMethod(name = "alive?") public RubyBoolean alive_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isAlive()); + return asBoolean(context, isAlive()); } @JRubyMethod @@ -1380,9 +1383,14 @@ public static IRubyObject exit(IRubyObject receiver, Block block) { } @JRubyMethod(name = "stop?") - public RubyBoolean stop_p() { + public RubyBoolean stop_p(ThreadContext context) { // not valid for "dead" state - return getRuntime().newBoolean(getStatus() == Status.SLEEP || getStatus() == Status.DEAD); + return asBoolean(context, getStatus() == Status.SLEEP || getStatus() == Status.DEAD); + } + + @Deprecated + public RubyBoolean stop_p() { + return stop_p(getRuntime().getCurrentContext()); } @JRubyMethod @@ -1485,7 +1493,7 @@ public IRubyObject native_thread_id(ThreadContext context) { try { int id = Integer.parseInt(encodedString.substring(0, atIndex)); - return context.runtime.newFixnum(id); + return asFixnum(context, id); } catch (NumberFormatException e) { // if we fail to parse this we will just act like we don't support it } @@ -2010,20 +2018,13 @@ public IRubyObject report_on_exception(ThreadContext context) { @JRubyMethod(name = "report_on_exception=", meta = true) public static IRubyObject report_on_exception_set(ThreadContext context, IRubyObject self, IRubyObject state) { - Ruby runtime = context.runtime; - - if (state.isNil()) { - runtime.setReportOnException(state); - } else { - runtime.setReportOnException(runtime.newBoolean(state.isTrue())); - } - + context.runtime.setReportOnException(state.isNil() ? context.nil : asBoolean(context, state.isTrue())); return state; } @JRubyMethod(name = "report_on_exception", meta = true) public static IRubyObject report_on_exception(ThreadContext context, IRubyObject self) { - return context.runtime.isReportOnException() ? context.tru : context.fals; + return asBoolean(context, context.runtime.isReportOnException()); } public StackTraceElement[] javaBacktrace() { diff --git a/core/src/main/java/org/jruby/RubyThreadGroup.java b/core/src/main/java/org/jruby/RubyThreadGroup.java index 41013109ee1..a523db2d8d4 100644 --- a/core/src/main/java/org/jruby/RubyThreadGroup.java +++ b/core/src/main/java/org/jruby/RubyThreadGroup.java @@ -41,6 +41,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.collections.WeakHashSet; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; /** @@ -71,23 +72,22 @@ public static RubyClass createThreadGroupClass(Ruby runtime) { @JRubyMethod(name = "add") public IRubyObject add(ThreadContext context, IRubyObject rubyThread, Block block) { - if (!(rubyThread instanceof RubyThread)) throw typeError(context, rubyThread, "Thread"); + if (!(rubyThread instanceof RubyThread thread)) throw typeError(context, rubyThread, "Thread"); // synchronize on the RubyThread for threadgroup updates if (isFrozen()) throw typeError(context, "can't add to the frozen thread group"); if (enclosed) throw typeError(context, "can't move to the enclosed thread group"); - RubyThread thread = (RubyThread) rubyThread; RubyThreadGroup threadGroup = thread.getThreadGroup(); // edit by headius: ThreadGroup may be null, perhaps if this is an adopted thread etc - if(threadGroup != null) { + if (threadGroup != null) { if (threadGroup.isFrozen()) throw typeError(context, "can't move from the frozen thread group"); if (threadGroup.enclosed_p(block).isTrue()) throw typeError(context, "can't move from the enclosed thread group"); } // we only add live threads - if (thread.alive_p(context).isTrue()) addDirectly(thread); + if (thread.isAlive()) addDirectly(thread); return this; } @@ -120,8 +120,13 @@ public IRubyObject enclose(Block block) { } @JRubyMethod(name = "enclosed?") + public IRubyObject enclosed_p(ThreadContext context, Block block) { + return asBoolean(context, enclosed); + } + + @Deprecated public IRubyObject enclosed_p(Block block) { - return getRuntime().newBoolean(enclosed); + return enclosed_p(getRuntime().getCurrentContext(), block); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/RubyTime.java b/core/src/main/java/org/jruby/RubyTime.java index 1523fad2477..a37379d22af 100644 --- a/core/src/main/java/org/jruby/RubyTime.java +++ b/core/src/main/java/org/jruby/RubyTime.java @@ -47,6 +47,7 @@ import org.joda.time.tz.FixedDateTimeZone; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.ast.util.ArgsUtil; import org.jruby.exceptions.RaiseException; import org.jruby.exceptions.TypeError; @@ -81,8 +82,7 @@ import java.util.regex.Pattern; import static org.jruby.RubyComparable.invcmp; -import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.invokedynamic; import static org.jruby.runtime.ThreadContext.hasKeywords; @@ -609,8 +609,13 @@ private RubyTime adjustTimeZone(Ruby runtime, final DateTimeZone zone, boolean i } @JRubyMethod(name = {"gmt?", "utc?", "gmtime?"}) + public RubyBoolean gmt(ThreadContext context) { + return asBoolean(context, isUTC()); + } + + @Deprecated public RubyBoolean gmt() { - return metaClass.runtime.newBoolean(isUTC()); + return gmt(getRuntime().getCurrentContext()); } public boolean isUTC() { @@ -673,12 +678,8 @@ public RubyString strftime(ThreadContext context, IRubyObject format) { @JRubyMethod(name = "==") @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, safeCmp(context, this, other) == 0); - } - if (other == context.nil) { - return context.fals; - } + if (other instanceof RubyTime) return asBoolean(context, safeCmp(context, this, other) == 0); + if (other == context.nil) return context.fals; return RubyComparable.op_equal(context, this, other); } @@ -696,36 +697,28 @@ private static int safeCmp(ThreadContext context, RubyTime self, IRubyObject oth @JRubyMethod(name = ">=") public IRubyObject op_ge(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, safeCmp(context, this, other) >= 0); - } + if (other instanceof RubyTime) return asBoolean(context, safeCmp(context, this, other) >= 0); return RubyComparable.op_ge(context, this, other); } @JRubyMethod(name = ">") public IRubyObject op_gt(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, safeCmp(context, this, other) > 0); - } + if (other instanceof RubyTime) return asBoolean(context, safeCmp(context, this, other) > 0); return RubyComparable.op_gt(context, this, other); } @JRubyMethod(name = "<=") public IRubyObject op_le(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, safeCmp(context, this, other) <= 0); - } + if (other instanceof RubyTime) return asBoolean(context, safeCmp(context, this, other) <= 0); return RubyComparable.op_le(context, this, other); } @JRubyMethod(name = "<") public IRubyObject op_lt(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, safeCmp(context, this, other) < 0); - } + if (other instanceof RubyTime) return asBoolean(context, safeCmp(context, this, other) < 0); return RubyComparable.op_lt(context, this, other); } @@ -826,7 +819,7 @@ private RubyTime opMinus(Ruby runtime, double other) { @Override public IRubyObject op_eqq(ThreadContext context, IRubyObject other) { if (other instanceof RubyTime) { - return RubyBoolean.newBoolean(context, RubyNumeric.fix2int(invokedynamic(context, this, OP_CMP, other)) == 0); + return asBoolean(context, RubyNumeric.fix2int(invokedynamic(context, this, OP_CMP, other)) == 0); } return context.fals; @@ -835,11 +828,9 @@ public IRubyObject op_eqq(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "<=>") @Override public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { - if (other instanceof RubyTime) { - return context.runtime.newFixnum(cmp((RubyTime) other)); - } - - return invcmp(context, sites(context).recursive_cmp, this, other); + return other instanceof RubyTime ? + asFixnum(context, cmp((RubyTime) other)) : + invcmp(context, sites(context).recursive_cmp, this, other); } @JRubyMethod(name = "eql?") @@ -939,8 +930,13 @@ public RubyFloat to_f() { } @JRubyMethod(name = {"to_i", "tv_sec"}) + public RubyInteger to_i(ThreadContext context) { + return asFixnum(context, Math.floorDiv(getTimeInMillis(), (long) 1000)); + } + + @Deprecated public RubyInteger to_i() { - return getRuntime().newFixnum(Math.floorDiv(getTimeInMillis(), (long) 1000)); + return to_i(getCurrentContext()); } /** @@ -952,8 +948,13 @@ public RubyInteger to_i() { * @return the (sec) fractional part of time (in nanos) */ @JRubyMethod(name = {"nsec", "tv_nsec"}) + public RubyInteger nsec(ThreadContext context) { + return asFixnum(context, getNanos()); + } + + @Deprecated public RubyInteger nsec() { - return getRuntime().newFixnum(getNanos()); + return nsec(getCurrentContext()); } @JRubyMethod @@ -967,8 +968,13 @@ public IRubyObject to_r(ThreadContext context) { * @return the (whole) microsecond part of time */ @JRubyMethod(name = {"usec", "tv_usec"}) + public RubyInteger usec(ThreadContext context) { + return asFixnum(context, dt.getMillisOfSecond() * 1000 + getUSec()); + } + + @Deprecated public RubyInteger usec() { - return getRuntime().newFixnum(dt.getMillisOfSecond() * 1000 + getUSec()); + return usec(getCurrentContext()); } /** @@ -1033,78 +1039,115 @@ public void setNanos(int nanos) { } @JRubyMethod + public RubyInteger sec(ThreadContext context) { + return asFixnum(context, dt.getSecondOfMinute()); + } + public RubyInteger sec() { - return getRuntime().newFixnum(dt.getSecondOfMinute()); + return sec(getCurrentContext()); } @JRubyMethod + public RubyInteger min(ThreadContext context) { + return asFixnum(context, dt.getMinuteOfHour()); + } + + @Deprecated public RubyInteger min() { - return getRuntime().newFixnum(dt.getMinuteOfHour()); + return min(getCurrentContext()); } @JRubyMethod + public RubyInteger hour(ThreadContext context) { + return asFixnum(context, dt.getHourOfDay()); + } + + @Deprecated public RubyInteger hour() { - return getRuntime().newFixnum(dt.getHourOfDay()); + return hour(getCurrentContext()); } @JRubyMethod(name = {"mday", "day"}) + public RubyInteger mday(ThreadContext context) { + return asFixnum(context, dt.getDayOfMonth()); + } + + @Deprecated public RubyInteger mday() { - return getRuntime().newFixnum(dt.getDayOfMonth()); + return mday(getCurrentContext()); } @JRubyMethod(name = {"month", "mon"}) + public RubyInteger month(ThreadContext context) { + return asFixnum(context, dt.getMonthOfYear()); + } + public RubyInteger month() { - return getRuntime().newFixnum(dt.getMonthOfYear()); + return month(getCurrentContext()); } @JRubyMethod + public RubyInteger year(ThreadContext context) { + return asFixnum(context, dt.getYear()); + } + + @Deprecated public RubyInteger year() { - return getRuntime().newFixnum(dt.getYear()); + return year(getCurrentContext()); } @JRubyMethod + public RubyInteger wday(ThreadContext context) { + return asFixnum(context, (dt.getDayOfWeek() % 7)); + } + + @Deprecated public RubyInteger wday() { - return getRuntime().newFixnum((dt.getDayOfWeek() % 7)); + return wday(getCurrentContext()); } @JRubyMethod + public RubyInteger yday(ThreadContext context) { + return asFixnum(context, dt.getDayOfYear()); + } + public RubyInteger yday() { - return getRuntime().newFixnum(dt.getDayOfYear()); + return yday(getCurrentContext()); } @JRubyMethod(name = "sunday?") public RubyBoolean sunday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 0); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 0); } @JRubyMethod(name = "monday?") public RubyBoolean monday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 1); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 1); } @JRubyMethod(name = "tuesday?") public RubyBoolean tuesday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 2); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 2); } @JRubyMethod(name = "wednesday?") public RubyBoolean wednesday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 3); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 3); } @JRubyMethod(name = "thursday?") public RubyBoolean thursday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 4); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 4); } @JRubyMethod(name = "friday?") public RubyBoolean friday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 5); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 5); } @JRubyMethod(name = "saturday?") public RubyBoolean saturday_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, (dt.getDayOfWeek() % 7) == 6); + return asBoolean(context, (dt.getDayOfWeek() % 7) == 6); } @Deprecated @@ -1121,15 +1164,24 @@ public RubyNumeric subsec(final ThreadContext context) { } @JRubyMethod(name = {"gmt_offset", "gmtoff", "utc_offset"}) - public RubyInteger gmt_offset() { + public RubyInteger gmt_offset(ThreadContext context) { int offset = dt.getZone().getOffset(dt.getMillis()); - return getRuntime().newFixnum(offset / 1000); + return asFixnum(context, offset / 1000); + } + + public RubyInteger gmt_offset() { + return gmt_offset(getCurrentContext()); } @JRubyMethod(name = {"isdst", "dst?"}) + public RubyBoolean isdst(ThreadContext context) { + return asBoolean(context, !dt.getZone().isStandardOffset(dt.getMillis())); + } + + @Deprecated public RubyBoolean isdst() { - return getRuntime().newBoolean(!dt.getZone().isStandardOffset(dt.getMillis())); + return isdst(getRuntime().getCurrentContext()); } @JRubyMethod @@ -1213,7 +1265,7 @@ public int hashCode() { @JRubyMethod(name = "_dump") public RubyString dump(final ThreadContext context) { - RubyString str = mdump(context.runtime); + RubyString str = mdump(context); str.syncVariables(this); return str; } @@ -1232,10 +1284,10 @@ public RubyString dump(IRubyObject[] args, Block unusedBlock) { @Deprecated public RubyObject mdump() { - return mdump(getRuntime()); + return mdump(getCurrentContext()); } - private RubyString mdump(final Ruby runtime) { + private RubyString mdump(ThreadContext context) { DateTime dateTime = dt.toDateTime(DateTimeZone.UTC); byte dumpValue[] = new byte[8]; long usec = this.nsec / 1000; @@ -1262,15 +1314,15 @@ private RubyString mdump(final Ruby runtime) { se >>>= 8; } - RubyString string = RubyString.newString(runtime, new ByteList(dumpValue, false)); + RubyString string = RubyString.newString(context.runtime, new ByteList(dumpValue, false)); // 1.9 includes more nsecs copyInstanceVariablesInto(string); // nanos in numerator/denominator form if (nanosec != 0) { - string.setInternalVariable("nano_num", runtime.newFixnum(nanosec)); - string.setInternalVariable("nano_den", runtime.newFixnum(1)); + string.setInternalVariable("nano_num", asFixnum(context, nanosec)); + string.setInternalVariable("nano_den", asFixnum(context, 1)); } // submicro for 1.9.1 compat @@ -1282,17 +1334,17 @@ private RubyString mdump(final Ruby runtime) { nanosec /= 10; submicro[0] |= (byte)((nanosec % 10) << 4); if (submicro[1] == 0) len = 1; - string.setInternalVariable("submicro", RubyString.newString(runtime, submicro, 0, len)); + string.setInternalVariable("submicro", RubyString.newString(context.runtime, submicro, 0, len)); // time zone final DateTimeZone zone = dt.getZone(); if (zone != DateTimeZone.UTC) { long offset = zone.getOffset(dt.getMillis()); - string.setInternalVariable("offset", runtime.newFixnum(offset / 1000)); + string.setInternalVariable("offset", asFixnum(context, offset / 1000)); String zoneName = zone.getShortName(dt.getMillis()); if (!TIME_OFFSET_PATTERN.matcher(zoneName).matches()) { - string.setInternalVariable("zone", runtime.newString(zoneName)); + string.setInternalVariable("zone", Convert.asString(context, zoneName)); } } diff --git a/core/src/main/java/org/jruby/RubyUnboundMethod.java b/core/src/main/java/org/jruby/RubyUnboundMethod.java index a2c7a8c0931..223d4e26987 100644 --- a/core/src/main/java/org/jruby/RubyUnboundMethod.java +++ b/core/src/main/java/org/jruby/RubyUnboundMethod.java @@ -42,6 +42,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.callsite.CacheEntry; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.RubyStringBuilder.str; /** @@ -93,7 +95,7 @@ public static RubyClass defineUnboundMethodClass(Ruby runtime) { @Override @JRubyMethod(name = "==") public RubyBoolean op_equal(ThreadContext context, IRubyObject other) { - return RubyBoolean.newBoolean(context, equals(other) ); + return asBoolean(context, equals(other) ); } @Override @@ -109,7 +111,7 @@ public boolean equals(Object other) { @JRubyMethod public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCode()); + return asFixnum(context, hashCode()); } @Override diff --git a/core/src/main/java/org/jruby/api/Convert.java b/core/src/main/java/org/jruby/api/Convert.java index adca97f4d70..2f92ec92d25 100644 --- a/core/src/main/java/org/jruby/api/Convert.java +++ b/core/src/main/java/org/jruby/api/Convert.java @@ -2,6 +2,7 @@ import org.jruby.RubyArray; import org.jruby.RubyBignum; +import org.jruby.RubyBoolean; import org.jruby.RubyClass; import org.jruby.RubyFile; import org.jruby.RubyFixnum; @@ -31,6 +32,9 @@ * be that thing. For example, `integerAsInt` implies the value is already an int and will error if * it is not. `checkToInteger` implies the value might not be an integer and that it may try and convert * it to one. + * + * Methods where the parameter to `As` methods will omit the type from in front of as. For example, + * `longAsInteger` will be `asInteger(context, long)`. */ public class Convert { /** @@ -330,13 +334,53 @@ public static int checkInt(ThreadContext context, long num) { return (int) num; } + /** + * Create a Ruby Boolean from a java boolean. + * @param context the current thread context + * @param value the boolean value + * @return the Ruby Boolean + */ + public static RubyBoolean asBoolean(ThreadContext context, boolean value) { + return value ? context.tru : context.fals; + } + + /** + * Create a Ruby Fixnum from a java long. + * @param context the current thread context + * @param value the long value + * @return the Ruby Fixnum + */ + public static RubyFixnum asFixnum(ThreadContext context, long value) { + return RubyFixnum.newFixnum(context.runtime, value); + } + + /** + * Create a Ruby Fixnum from a java int. + * @param context the current thread context + * @param value the int value + * @return the Ruby Fixnum + */ + public static RubyFixnum asFixnum(ThreadContext context, int value) { + return RubyFixnum.newFixnum(context.runtime, value); + } + + /** + * Create a Ruby String from a java String. + * @param context the current thread context + * @param value the String value + * @return the Ruby String + */ + public static RubyString asString(ThreadContext context, String value) { + return context.runtime.newString(value); + } + /** * Safely convert a Ruby Integer into a java int value. Raising if the value will not fit. * @param context the current thread context * @param value the RubyInteger to convert * @return the int value */ - public static int integerAsInt(ThreadContext context, RubyInteger value) { + public static int asInt(ThreadContext context, RubyInteger value) { long num = value.getLongValue(); return checkInt(context, num); @@ -348,7 +392,7 @@ public static int integerAsInt(ThreadContext context, RubyInteger value) { * @param value the RubyInteger to convert * @return the int value */ - public static long integerAsLong(ThreadContext context, RubyInteger value) { + public static long asLong(ThreadContext context, RubyInteger value) { if (value instanceof RubyBignum) { return big2long((RubyBignum) value); } diff --git a/core/src/main/java/org/jruby/common/RubyWarnings.java b/core/src/main/java/org/jruby/common/RubyWarnings.java index e910c2b933e..8c428d6fd47 100644 --- a/core/src/main/java/org/jruby/common/RubyWarnings.java +++ b/core/src/main/java/org/jruby/common/RubyWarnings.java @@ -47,6 +47,7 @@ import org.jruby.util.TypeConverter; import org.jruby.util.func.TriFunction; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.util.RubyStringBuilder.str; /** @@ -258,7 +259,7 @@ public static IRubyObject op_aref(ThreadContext context, IRubyObject self, IRuby if (category == null) throw runtime.newArgumentError(str(runtime, "unknown category: ", arg)); - return runtime.newBoolean(runtime.getWarningCategories().contains(category)); + return asBoolean(context, runtime.getWarningCategories().contains(category)); } @JRubyMethod(name = "[]=") diff --git a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java index 855c359c543..5654d9c1126 100644 --- a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java +++ b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java @@ -59,8 +59,7 @@ import org.jruby.util.SafeDoubleParser; import org.jruby.util.StringSupport; -import static org.jruby.api.Convert.castAsFixnum; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; /** @@ -312,7 +311,7 @@ public static RubyBigDecimal _load(ThreadContext context, IRubyObject recv, IRub @JRubyMethod(meta = true) public static IRubyObject double_fig(ThreadContext context, IRubyObject recv) { - return context.runtime.newFixnum(VP_DOUBLE_FIG); + return asFixnum(context, VP_DOUBLE_FIG); } /** @@ -1644,7 +1643,7 @@ private IRubyObject cmp(ThreadContext context, final IRubyObject arg, final char case '=': { if (falsyEqlCheck(context, arg)) return context.fals; IRubyObject res = callCoerced(context, sites(context).op_eql, arg, false); - return RubyBoolean.newBoolean(context, res != context.nil && res != context.fals); + return asBoolean(context, res != context.nil && res != context.fals); } case '!': if (falsyEqlCheck(context, arg)) return context.tru; @@ -1666,13 +1665,13 @@ private IRubyObject cmp(ThreadContext context, final IRubyObject arg, final char e = infinitySign != 0 || rb.infinitySign != 0 ? infinitySign - rb.infinitySign : value.compareTo(rb.value); switch (op) { - case '*': return context.runtime.newFixnum(e); - case '=': return RubyBoolean.newBoolean(context, e == 0); - case '!': return RubyBoolean.newBoolean(context, e != 0); - case 'G': return RubyBoolean.newBoolean(context, e >= 0); - case '>': return RubyBoolean.newBoolean(context, e > 0); - case 'L': return RubyBoolean.newBoolean(context, e <= 0); - case '<': return RubyBoolean.newBoolean(context, e < 0); + case '*': return asFixnum(context, e); + case '=': return asBoolean(context, e == 0); + case '!': return asBoolean(context, e != 0); + case 'G': return asBoolean(context, e >= 0); + case '>': return asBoolean(context, e > 0); + case 'L': return asBoolean(context, e <= 0); + case '<': return asBoolean(context, e < 0); } return context.nil; } @@ -1834,8 +1833,13 @@ public IRubyObject exponent() { } @JRubyMethod(name = "finite?") + public IRubyObject finite_p(ThreadContext context) { + return asBoolean(context, !isNaN() && !isInfinity()); + } + + @Deprecated public IRubyObject finite_p() { - return getRuntime().newBoolean(!isNaN() && !isInfinity()); + return finite_p(getRuntime().getCurrentContext()); } private RubyBigDecimal floorNaNInfinityCheck(ThreadContext context) { @@ -1874,7 +1878,7 @@ public IRubyObject frac(ThreadContext context) { @JRubyMethod(name = "infinite?") public IRubyObject infinite_p(ThreadContext context) { - return infinitySign == 0 ? context.nil : context.runtime.newFixnum(infinitySign); + return infinitySign == 0 ? context.nil : asFixnum(context, infinitySign); } @JRubyMethod @@ -1884,7 +1888,7 @@ public IRubyObject inspect(ThreadContext context) { @JRubyMethod(name = "nan?") public IRubyObject nan_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isNaN()); + return asBoolean(context, isNaN()); } @Override @@ -1900,10 +1904,9 @@ public IRubyObject nonzero_p() { @JRubyMethod public IRubyObject n_significant_digits(ThreadContext context) { - if (value.equals(BigDecimal.ZERO)) { - return RubyFixnum.zero(context.runtime); - } - return context.runtime.newFixnum(value.stripTrailingZeros().precision()); + return value.equals(BigDecimal.ZERO) ? + RubyFixnum.zero(context.runtime) : + asFixnum(context, value.stripTrailingZeros().precision()); } @JRubyMethod @@ -1974,8 +1977,8 @@ private int getPrec() { public IRubyObject precs(ThreadContext context) { context.runtime.getWarnings().warn(IRubyWarnings.ID.DEPRECATED_METHOD, "BigDecimal#precs is deprecated and will be removed in the future; use BigDecimal#precision instead."); return RubyArray.newArray(context.runtime, - context.runtime.newFixnum(getSignificantDigits().length()), - context.runtime.newFixnum(((getAllDigits().length() / 4) + 1) * 4)); + asFixnum(context, getSignificantDigits().length()), + asFixnum(context, ((getAllDigits().length() / 4) + 1) * 4)); } @JRubyMethod(name = "round", optional = 2, checkArity = false) @@ -2148,20 +2151,20 @@ public IRubyObject sign() { return getMetaClass().getConstant(value.signum() < 0 ? "SIGN_NEGATIVE_FINITE" : "SIGN_POSITIVE_FINITE"); } - private RubyFixnum signValue(Ruby runtime) { - if (isNaN()) return RubyFixnum.zero(runtime); - if (isInfinity()) return runtime.newFixnum(infinitySign); - if (isZero()) return runtime.newFixnum(zeroSign); + private RubyFixnum signValue(ThreadContext context) { + if (isNaN()) return RubyFixnum.zero(context.runtime); + if (isInfinity()) return asFixnum(context, infinitySign); + if (isZero()) return asFixnum(context, zeroSign); - return runtime.newFixnum(value.signum()); + return asFixnum(context, value.signum()); } @JRubyMethod public RubyArray split(ThreadContext context) { return RubyArray.newArray(context.runtime, - signValue(context.runtime), - context.runtime.newString(splitDigits()), - context.runtime.newFixnum(10), + signValue(context), + Convert.asString(context, splitDigits()), + asFixnum(context, 10), exponent()); } @@ -2485,12 +2488,12 @@ public IRubyObject truncate(ThreadContext context, IRubyObject arg) { @Override @JRubyMethod(name = "zero?") public IRubyObject zero_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isZero()); + return asBoolean(context, isZero()); } @Deprecated public IRubyObject zero_p() { - return getRuntime().newBoolean(isZero()); + return zero_p(getRuntime().getCurrentContext()); } @Override diff --git a/core/src/main/java/org/jruby/ext/date/RubyDate.java b/core/src/main/java/org/jruby/ext/date/RubyDate.java index 2605817b5c6..ea73ae72907 100644 --- a/core/src/main/java/org/jruby/ext/date/RubyDate.java +++ b/core/src/main/java/org/jruby/ext/date/RubyDate.java @@ -59,8 +59,7 @@ import java.util.GregorianCalendar; import static org.jruby.RubyRegexp.*; -import static org.jruby.api.Convert.castAsArray; -import static org.jruby.api.Convert.castAsNumeric; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.ext.date.DateUtils.*; import static org.jruby.util.Numeric.*; @@ -278,7 +277,7 @@ private long initMillis(final ThreadContext context, IRubyObject ajd) { private RubyFixnum DAY_MS(final ThreadContext context) { RubyFixnum v = DAY_MS_CACHE; - if (v == null) v = DAY_MS_CACHE = context.runtime.newFixnum(DAY_MS); + if (v == null) v = DAY_MS_CACHE = asFixnum(context, DAY_MS); return v; } @@ -827,7 +826,7 @@ private IRubyObject fallback_eqq(ThreadContext context, IRubyObject other) { @Override @JRubyMethod(name = "<=>") public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { - if (other instanceof RubyDate date) return context.runtime.newFixnum(cmp(context, date)); + if (other instanceof RubyDate date) return asFixnum(context, cmp(context, date)); // other (Numeric) - interpreted as an Astronomical Julian Day Number. @@ -908,12 +907,12 @@ public final long getJulianDayNumber() { @JRubyMethod(name = "julian?") public RubyBoolean julian_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isJulian()); + return asBoolean(context, isJulian()); } @JRubyMethod(name = "gregorian?") public RubyBoolean gregorian_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, ! isJulian()); + return asBoolean(context, ! isJulian()); } public final boolean isJulian() { @@ -1178,13 +1177,13 @@ private RubyDate newStart(ThreadContext context, final long start) { @JRubyMethod(name = "julian_leap?", meta = true) public static IRubyObject julian_leap_p(ThreadContext context, IRubyObject self, IRubyObject year) { final RubyInteger y = year.convertToInteger(); - return RubyBoolean.newBoolean(context, isJulianLeap(y.getLongValue())); + return asBoolean(context, isJulianLeap(y.getLongValue())); } @JRubyMethod(name = "gregorian_leap?", alias = "leap?", meta = true) public static IRubyObject gregorian_leap_p(ThreadContext context, IRubyObject self, IRubyObject year) { final RubyInteger y = year.convertToInteger(); - return RubyBoolean.newBoolean(context, isGregorianLeap(y.getLongValue())); + return asBoolean(context, isGregorianLeap(y.getLongValue())); } // All years divisible by 4 are leap years in the Julian calendar. @@ -1208,7 +1207,7 @@ private static boolean isGregorianLeap(final long year) { @JRubyMethod(name = "leap?") public IRubyObject leap_p(ThreadContext context) { final long year = dt.getYear(); - return RubyBoolean.newBoolean(context, isJulian() ? isJulianLeap(year) : isGregorianLeap(year) ); + return asBoolean(context, isJulian() ? isJulianLeap(year) : isGregorianLeap(year) ); } // @@ -1467,7 +1466,7 @@ private static IRubyObject valMinusOneHalf(ThreadContext context, IRubyObject va } static RubyRational newRationalConvert(ThreadContext context, IRubyObject num, long den) { - return (RubyRational) RubyRational.newRationalConvert(context, num, context.runtime.newFixnum(den)); + return (RubyRational) RubyRational.newRationalConvert(context, num, asFixnum(context, den)); } // def jd_to_ajd(jd, fr, of=0) jd + fr - of - Rational(1, 2) end @@ -2440,7 +2439,7 @@ private static IRubyObject s3e(ThreadContext context, final RubyHash hash, set_hash(context, hash, "mday", cstr2num(context.runtime, d, bp, ep)); } - if (comp != null) set_hash(context, hash, "_comp", RubyBoolean.newBoolean(context, comp)); + if (comp != null) set_hash(context, hash, "_comp", asBoolean(context, comp)); return hash; } diff --git a/core/src/main/java/org/jruby/ext/etc/RubyEtc.java b/core/src/main/java/org/jruby/ext/etc/RubyEtc.java index 3d57d41a3b4..c7c318d7fc7 100644 --- a/core/src/main/java/org/jruby/ext/etc/RubyEtc.java +++ b/core/src/main/java/org/jruby/ext/etc/RubyEtc.java @@ -37,6 +37,7 @@ import org.jruby.util.io.OpenFile; import java.nio.ByteBuffer; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; @JRubyModule(name="Etc") @@ -58,7 +59,7 @@ public static synchronized IRubyObject pathconf(ThreadContext context, IRubyObje throw context.runtime.newErrnoFromLastPOSIXErrno(); } } - return context.runtime.newFixnum(ret); + return asFixnum(context, ret); } } diff --git a/core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java b/core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java index 17f730fbba9..dd6cbd92b5a 100644 --- a/core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java +++ b/core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java @@ -49,7 +49,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; /** @@ -164,7 +164,7 @@ public final long getSize() { */ @JRubyMethod(name = "hash") public RubyFixnum hash(ThreadContext context) { - return context.runtime.newFixnum(hashCode()); + return asFixnum(context, hashCode()); } @JRubyMethod(name = "[]") @@ -196,7 +196,7 @@ public boolean equals(Object obj) { @JRubyMethod(name = "==") @Override public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this.equals(obj)); + return asBoolean(context, this.equals(obj)); } /** @@ -236,7 +236,7 @@ public IRubyObject total(ThreadContext context) { */ @JRubyMethod(name = "type_size") public final IRubyObject type_size(ThreadContext context) { - return context.runtime.newFixnum(typeSize); + return asFixnum(context, typeSize); } /** diff --git a/core/src/main/java/org/jruby/ext/ffi/MemoryPointer.java b/core/src/main/java/org/jruby/ext/ffi/MemoryPointer.java index 910f7aed76a..1e791f1c8ce 100644 --- a/core/src/main/java/org/jruby/ext/ffi/MemoryPointer.java +++ b/core/src/main/java/org/jruby/ext/ffi/MemoryPointer.java @@ -12,6 +12,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.cli.Options; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.runtime.Visibility.PRIVATE; @JRubyClass(name = "FFI::MemoryPointer", parent = "FFI::Pointer") @@ -82,7 +84,7 @@ static MemoryPointer allocate(Ruby runtime, int typeSize, int count, boolean cle @JRubyMethod(name = "from_string", meta = true) public static IRubyObject from_string(ThreadContext context, IRubyObject klass, IRubyObject s) { org.jruby.util.ByteList bl = s.convertToString().getByteList(); - MemoryPointer ptr = (MemoryPointer) ((RubyClass) klass).newInstance(context, context.runtime.newFixnum(bl.length() + 1), Block.NULL_BLOCK); + MemoryPointer ptr = (MemoryPointer) ((RubyClass) klass).newInstance(context, asFixnum(context, bl.length() + 1), Block.NULL_BLOCK); ptr.getMemoryIO().putZeroTerminatedByteArray(0, bl.unsafeBytes(), bl.begin(), bl.length()); return ptr; @@ -114,11 +116,10 @@ public final String toString() { @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this == obj + return asBoolean(context, this == obj || getAddress() == 0L && obj.isNil() - || (obj instanceof MemoryPointer - && ((MemoryPointer) obj).getAddress() == getAddress()) - && ((MemoryPointer) obj).getSize() == getSize()); + || (obj instanceof MemoryPointer mem && mem.getAddress() == getAddress()) + && mem.getSize() == getSize()); } @JRubyMethod(name = "free") @@ -137,6 +138,6 @@ public final IRubyObject autorelease(ThreadContext context, IRubyObject release) @JRubyMethod(name = "autorelease?") public final IRubyObject autorelease_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, ((AllocatedDirectMemoryIO) getMemoryIO()).isAutoRelease()); + return asBoolean(context, ((AllocatedDirectMemoryIO) getMemoryIO()).isAutoRelease()); } } diff --git a/core/src/main/java/org/jruby/ext/ffi/Platform.java b/core/src/main/java/org/jruby/ext/ffi/Platform.java index 7a2ab25fa48..61f3e07a78d 100644 --- a/core/src/main/java/org/jruby/ext/ffi/Platform.java +++ b/core/src/main/java/org/jruby/ext/ffi/Platform.java @@ -38,6 +38,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.SafePropertyAccessor; +import static org.jruby.api.Convert.asBoolean; + /** * */ @@ -308,27 +310,27 @@ public static void createPlatformModule(Ruby runtime, RubyModule ffi) { @JRubyMethod(name = "windows?", module=true) public static IRubyObject windows_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, OS == OS.WINDOWS); + return asBoolean(context, OS == OS.WINDOWS); } @JRubyMethod(name = "mac?", module=true) public static IRubyObject mac_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, OS == OS.DARWIN); + return asBoolean(context, OS == OS.DARWIN); } @JRubyMethod(name = "unix?", module=true) public static IRubyObject unix_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, Platform.getPlatform().isUnix()); + return asBoolean(context, Platform.getPlatform().isUnix()); } @JRubyMethod(name = "bsd?", module=true) public static IRubyObject bsd_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, Platform.getPlatform().isBSD()); + return asBoolean(context, Platform.getPlatform().isBSD()); } @JRubyMethod(name = "linux?", module=true) public static IRubyObject linux_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, OS == OS.LINUX); + return asBoolean(context, OS == OS.LINUX); } @JRubyMethod(name = "solaris?", module=true) public static IRubyObject solaris_p(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, OS == OS.SOLARIS); + return asBoolean(context, OS == OS.SOLARIS); } /** diff --git a/core/src/main/java/org/jruby/ext/ffi/Pointer.java b/core/src/main/java/org/jruby/ext/ffi/Pointer.java index 7a04e186335..26557f4044c 100644 --- a/core/src/main/java/org/jruby/ext/ffi/Pointer.java +++ b/core/src/main/java/org/jruby/ext/ffi/Pointer.java @@ -11,7 +11,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.cli.Options; -import static org.jruby.api.Convert.numericToLong; +import static org.jruby.api.Convert.*; import static org.jruby.runtime.Visibility.*; /** @@ -135,7 +135,7 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) { */ @JRubyMethod(name = "null?") public IRubyObject null_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getMemoryIO().isNull()); + return asBoolean(context, getMemoryIO().isNull()); } @@ -150,7 +150,7 @@ public IRubyObject to_s(ThreadContext context) { @JRubyMethod(name = { "address", "to_i" }) public IRubyObject address(ThreadContext context) { - return context.runtime.newFixnum(getAddress()); + return asFixnum(context, getAddress()); } /** @@ -164,7 +164,7 @@ public final long getAddress() { @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this == obj + return asBoolean(context, this == obj || getAddress() == 0L && obj.isNil() || (obj instanceof Pointer && ((Pointer) obj).getAddress() == getAddress())); } diff --git a/core/src/main/java/org/jruby/ext/ffi/Struct.java b/core/src/main/java/org/jruby/ext/ffi/Struct.java index 0ffc8bd9a04..246c454c8c4 100644 --- a/core/src/main/java/org/jruby/ext/ffi/Struct.java +++ b/core/src/main/java/org/jruby/ext/ffi/Struct.java @@ -11,6 +11,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.cli.Options; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Convert.castAsClass; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.*; @@ -330,7 +331,7 @@ public IRubyObject alignment(ThreadContext context) { @JRubyMethod(name="null?") public IRubyObject null_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getMemory().getMemoryIO().isNull()); + return asBoolean(context, getMemory().getMemoryIO().isNull()); } @JRubyMethod(name = "order") diff --git a/core/src/main/java/org/jruby/ext/ffi/StructLayout.java b/core/src/main/java/org/jruby/ext/ffi/StructLayout.java index be7a96a4a92..448025b4b06 100644 --- a/core/src/main/java/org/jruby/ext/ffi/StructLayout.java +++ b/core/src/main/java/org/jruby/ext/ffi/StructLayout.java @@ -60,6 +60,7 @@ import org.jruby.runtime.callsite.FunctionalCachingCallSite; import org.jruby.util.ByteList; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.castAsArray; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Visibility.*; @@ -726,17 +727,17 @@ static ByteOrder getByteOrderOption(ThreadContext context, IRubyObject[] args) { @JRubyMethod public final IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum(type.getNativeSize()); + return asFixnum(context, type.getNativeSize()); } @JRubyMethod public final IRubyObject alignment(ThreadContext context) { - return context.runtime.newFixnum(type.getNativeAlignment()); + return asFixnum(context, type.getNativeAlignment()); } @JRubyMethod public final IRubyObject offset(ThreadContext context) { - return context.runtime.newFixnum(offset); + return asFixnum(context, offset); } @JRubyMethod(name = { "type", "ffi_type" }) diff --git a/core/src/main/java/org/jruby/ext/ffi/Type.java b/core/src/main/java/org/jruby/ext/ffi/Type.java index b53860be35d..55e15f6ff47 100644 --- a/core/src/main/java/org/jruby/ext/ffi/Type.java +++ b/core/src/main/java/org/jruby/ext/ffi/Type.java @@ -16,6 +16,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; /** @@ -161,7 +163,7 @@ public final int getNativeAlignment() { */ @JRubyMethod(name = "size") public IRubyObject size(ThreadContext context) { - return context.runtime.newFixnum(getNativeSize()); + return asFixnum(context, getNativeSize()); } /** @@ -172,7 +174,7 @@ public IRubyObject size(ThreadContext context) { */ @JRubyMethod(name = "alignment") public IRubyObject alignment(ThreadContext context) { - return context.runtime.newFixnum(getNativeAlignment()); + return asFixnum(context, getNativeAlignment()); } @JRubyClass(name = "FFI::Type::Builtin", parent = "FFI::Type") @@ -219,18 +221,18 @@ public final IRubyObject to_sym(ThreadContext context) { @Override @JRubyMethod(name = "==") public IRubyObject op_equal(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this.equals(obj)); + return asBoolean(context, this.equals(obj)); } @Override @JRubyMethod(name = "equal?") public IRubyObject equal_p(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this.equals(obj)); + return asBoolean(context, this.equals(obj)); } @JRubyMethod(name = "eql?") public IRubyObject eql_p(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, this.equals(obj)); + return asBoolean(context, this.equals(obj)); } } @@ -274,7 +276,7 @@ public static final IRubyObject newInstance(ThreadContext context, IRubyObject k @JRubyMethod public final IRubyObject length(ThreadContext context) { - return context.runtime.newFixnum(length); + return asFixnum(context, length); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/ext/ffi/jffi/Factory.java b/core/src/main/java/org/jruby/ext/ffi/jffi/Factory.java index afda33b9892..32742eb4065 100644 --- a/core/src/main/java/org/jruby/ext/ffi/jffi/Factory.java +++ b/core/src/main/java/org/jruby/ext/ffi/jffi/Factory.java @@ -12,6 +12,8 @@ import org.jruby.platform.Platform; import org.jruby.util.WindowsFFI; +import static org.jruby.api.Convert.asFixnum; + public class Factory extends org.jruby.ext.ffi.Factory { public Factory() { @@ -117,7 +119,7 @@ public int alignmentOf(NativeType type) { public static final class LastError { @JRubyMethod(name = { "error" }, module = true) public static final IRubyObject error(ThreadContext context, IRubyObject recv) { - return context.runtime.newFixnum(com.kenai.jffi.LastError.getInstance().get()); + return asFixnum(context, com.kenai.jffi.LastError.getInstance().get()); } @JRubyMethod(name = { "error=" }, module = true) diff --git a/core/src/main/java/org/jruby/ext/ffi/jffi/Function.java b/core/src/main/java/org/jruby/ext/ffi/jffi/Function.java index 2ba11beb437..dd0cb6c8142 100644 --- a/core/src/main/java/org/jruby/ext/ffi/jffi/Function.java +++ b/core/src/main/java/org/jruby/ext/ffi/jffi/Function.java @@ -19,6 +19,7 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; @JRubyClass(name="FFI::Function", parent="FFI::Pointer") @@ -161,7 +162,7 @@ public final IRubyObject autorelease(ThreadContext context, IRubyObject release) @JRubyMethod(name = { "autorelease?", "autorelease" }) public final IRubyObject autorelease_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, autorelease); + return asBoolean(context, autorelease); } @Override diff --git a/core/src/main/java/org/jruby/ext/ffi/jffi/JITRuntime.java b/core/src/main/java/org/jruby/ext/ffi/jffi/JITRuntime.java index 3f381bff959..5811f4244ad 100644 --- a/core/src/main/java/org/jruby/ext/ffi/jffi/JITRuntime.java +++ b/core/src/main/java/org/jruby/ext/ffi/jffi/JITRuntime.java @@ -10,6 +10,7 @@ import java.math.BigInteger; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; /** @@ -383,7 +384,7 @@ public static IRubyObject newString(Ruby runtime, long address) { } public static IRubyObject newBoolean(ThreadContext context, int value) { - return RubyBoolean.newBoolean(context, (value & 0x1) != 0); + return asBoolean(context, (value & 0x1) != 0); } public static IRubyObject newBoolean(Ruby runtime, int value) { @@ -391,7 +392,7 @@ public static IRubyObject newBoolean(Ruby runtime, int value) { } public static IRubyObject newBoolean(ThreadContext context, long value) { - return RubyBoolean.newBoolean(context, (value & 0x1L) != 0); + return asBoolean(context, (value & 0x1L) != 0); } public static IRubyObject newBoolean(Ruby runtime, long value) { diff --git a/core/src/main/java/org/jruby/ext/ffi/jffi/NativeClosureProxy.java b/core/src/main/java/org/jruby/ext/ffi/jffi/NativeClosureProxy.java index e79ac07cf96..96a1583cc96 100644 --- a/core/src/main/java/org/jruby/ext/ffi/jffi/NativeClosureProxy.java +++ b/core/src/main/java/org/jruby/ext/ffi/jffi/NativeClosureProxy.java @@ -14,6 +14,7 @@ import java.lang.ref.WeakReference; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; /** @@ -268,7 +269,7 @@ private static final IRubyObject fromNative(ThreadContext context, Type type, return getStringParameter(runtime, buffer, index); case BOOL: - return runtime.newBoolean(buffer.getByte(index) != 0); + return asBoolean(context, buffer.getByte(index) != 0); default: throw typeError(context, "invalid callback parameter type " + type); diff --git a/core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java b/core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java index 95af0ba4d47..96d03cd346b 100644 --- a/core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java +++ b/core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java @@ -38,6 +38,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Convert.castAsHash; import static org.jruby.api.Error.typeError; @@ -483,7 +484,7 @@ private static FiberData verifyCurrentFiber(ThreadContext context, Ruby runtime) @JRubyMethod(name = "alive?") public IRubyObject alive_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, alive()); + return asBoolean(context, alive()); } @JRubyMethod(meta = true) @@ -642,7 +643,7 @@ protected void finalize() throws Throwable { @JRubyMethod(name = "blocking?") public IRubyObject blocking_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isBlocking()); + return asBoolean(context, isBlocking()); } @JRubyMethod(name = "blocking?", meta = true) diff --git a/core/src/main/java/org/jruby/ext/io/nonblock/IONonBlock.java b/core/src/main/java/org/jruby/ext/io/nonblock/IONonBlock.java index 30561eb66af..84961803035 100644 --- a/core/src/main/java/org/jruby/ext/io/nonblock/IONonBlock.java +++ b/core/src/main/java/org/jruby/ext/io/nonblock/IONonBlock.java @@ -8,6 +8,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.io.OpenFile; +import static org.jruby.api.Convert.asBoolean; + public class IONonBlock { public static void load(Ruby runtime) { @@ -16,14 +18,14 @@ public static void load(Ruby runtime) { @JRubyMethod(name = "nonblock?") public static IRubyObject nonblock_p(ThreadContext context, IRubyObject io) { - return context.runtime.newBoolean( !getIO(io).getBlocking() ); + return asBoolean(context, !getIO(io).getBlocking()); } @JRubyMethod(name = "nonblock=") public static IRubyObject nonblock_set(ThreadContext context, IRubyObject io, IRubyObject nonblocking) { final boolean nonblock = nonblocking.isTrue(); getIO(io).setBlocking(!nonblock); - return context.runtime.newBoolean(nonblock); // NOTE: MRI seems to return io + return asBoolean(context, nonblock); // NOTE: MRI seems to return io } @JRubyMethod(name = "nonblock") @@ -43,7 +45,7 @@ public static IRubyObject nonblock(ThreadContext context, IRubyObject io, IRubyO ioObj.setBlocking(oldBlocking); } } - return context.runtime.newBoolean(oldBlocking); + return asBoolean(context, oldBlocking); } private static RubyIO getIO(IRubyObject io) { diff --git a/core/src/main/java/org/jruby/ext/jruby/CoreExt.java b/core/src/main/java/org/jruby/ext/jruby/CoreExt.java index 8661d33f79e..47be0d5773d 100644 --- a/core/src/main/java/org/jruby/ext/jruby/CoreExt.java +++ b/core/src/main/java/org/jruby/ext/jruby/CoreExt.java @@ -37,6 +37,7 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.castAsString; /** @@ -54,7 +55,7 @@ public static class String { @JRubyMethod public static RubyFixnum unseeded_hash(ThreadContext context, IRubyObject recv) { - return context.runtime.newFixnum(castAsString(context, recv).unseededStrHashCode(context.runtime)); + return asFixnum(context, castAsString(context, recv).unseededStrHashCode(context.runtime)); } @JRubyMethod(name = "alloc", meta = true) diff --git a/core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java b/core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java index c1e3f2a2d81..738daf320b5 100644 --- a/core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java +++ b/core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java @@ -52,6 +52,8 @@ import java.io.ByteArrayInputStream; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.parser.ParserType.INLINE; @@ -89,12 +91,12 @@ public void load(Ruby runtime, boolean wrap) { public static class JRubyConfig { @JRubyMethod(name = "rubygems_disabled?") public static IRubyObject rubygems_disabled_p(ThreadContext context, IRubyObject self) { - return RubyBoolean.newBoolean(context, context.runtime.getInstanceConfig().isDisableGems()); + return asBoolean(context, context.runtime.getInstanceConfig().isDisableGems()); } @JRubyMethod(name = "did_you_mean_disabled?") public static IRubyObject did_you_mean_disabled_p(ThreadContext context, IRubyObject self) { - return RubyBoolean.newBoolean(context, context.runtime.getInstanceConfig().isDisableDidYouMean()); + return asBoolean(context, context.runtime.getInstanceConfig().isDisableDidYouMean()); } } @@ -169,9 +171,13 @@ public static IRubyObject set_context_class_loader(ThreadContext context, IRubyO } @JRubyMethod(name = "security_restricted?", module = true) + public static RubyBoolean is_security_restricted(ThreadContext context, IRubyObject recv) { + return asBoolean(context, Ruby.isSecurityRestricted()); + } + + @Deprecated public static RubyBoolean is_security_restricted(IRubyObject recv) { - final Ruby runtime = recv.getRuntime(); - return RubyBoolean.newBoolean(runtime, Ruby.isSecurityRestricted()); + return is_security_restricted(recv.getRuntime().getCurrentContext(), recv); } // NOTE: its probably too late to set this when jruby library is booted (due the java library) ? @@ -190,7 +196,7 @@ public static IRubyObject set_security_restricted(IRubyObject recv, IRubyObject */ @JRubyMethod(module = true) public static IRubyObject identity_hash(ThreadContext context, IRubyObject recv, IRubyObject obj) { - return context.runtime.newFixnum(System.identityHashCode(obj)); + return asFixnum(context, System.identityHashCode(obj)); } @JRubyMethod(module = true, name = "parse", alias = "ast_for", required = 1, optional = 3, checkArity = false) diff --git a/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java b/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java index 614e08525bd..87241a6745f 100644 --- a/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java +++ b/core/src/main/java/org/jruby/ext/jruby/JRubyUtilLibrary.java @@ -48,6 +48,8 @@ import org.jruby.runtime.load.Library; import org.jruby.util.ClasspathLauncher; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.URLUtil.getPath; /** @@ -74,9 +76,13 @@ public static IRubyObject gc(ThreadContext context, IRubyObject recv) { } @JRubyMethod(name = { "objectspace", "object_space?" }, alias = { "objectspace?" }, module = true) + public static IRubyObject getObjectSpaceEnabled(ThreadContext context, IRubyObject recv) { + return asBoolean(context, context.runtime.isObjectSpaceEnabled()); + } + + @Deprecated public static IRubyObject getObjectSpaceEnabled(IRubyObject recv) { - final Ruby runtime = recv.getRuntime(); - return RubyBoolean.newBoolean(runtime, runtime.isObjectSpaceEnabled()); + return getObjectSpaceEnabled(recv.getRuntime().getCurrentContext(), recv); } @JRubyMethod(name = { "objectspace=", "object_space=" }, module = true) @@ -92,7 +98,7 @@ public static IRubyObject setObjectSpaceEnabled(IRubyObject recv, IRubyObject ar @JRubyMethod(meta = true, name = "native_posix?") public static IRubyObject native_posix_p(ThreadContext context, IRubyObject self) { - return RubyBoolean.newBoolean(context, context.runtime.getPosix().isNative()); + return asBoolean(context, context.runtime.getPosix().isNative()); } @Deprecated @@ -409,8 +415,8 @@ public static IRubyObject cache_stats(ThreadContext context, IRubyObject self) { Ruby runtime = context.runtime; RubyHash stat = RubyHash.newHash(runtime); - stat.op_aset(context, runtime.newSymbol("method_invalidation_count"), runtime.newFixnum(runtime.getCaches().getMethodInvalidationCount())); - stat.op_aset(context, runtime.newSymbol("constant_invalidation_count"), runtime.newFixnum(runtime.getCaches().getConstantInvalidationCount())); + stat.op_aset(context, runtime.newSymbol("method_invalidation_count"), asFixnum(context, runtime.getCaches().getMethodInvalidationCount())); + stat.op_aset(context, runtime.newSymbol("constant_invalidation_count"), asFixnum(context, runtime.getCaches().getConstantInvalidationCount())); return stat; } diff --git a/core/src/main/java/org/jruby/ext/monitor/Monitor.java b/core/src/main/java/org/jruby/ext/monitor/Monitor.java index f6d729cb434..ec59ab6307a 100644 --- a/core/src/main/java/org/jruby/ext/monitor/Monitor.java +++ b/core/src/main/java/org/jruby/ext/monitor/Monitor.java @@ -13,6 +13,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; + @JRubyClass(name = "Monitor") public class Monitor extends RubyObject { private final Mutex mutex; @@ -99,7 +101,7 @@ public IRubyObject mon_check_owner(ThreadContext context) { @JRubyMethod(name = "mon_owned?") public RubyBoolean mon_owned_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, ownedByCurrentThread(context)); + return asBoolean(context, ownedByCurrentThread(context)); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/ext/pathname/RubyPathname.java b/core/src/main/java/org/jruby/ext/pathname/RubyPathname.java index b570f55d4ee..6e531cc16a1 100644 --- a/core/src/main/java/org/jruby/ext/pathname/RubyPathname.java +++ b/core/src/main/java/org/jruby/ext/pathname/RubyPathname.java @@ -30,6 +30,7 @@ package org.jruby.ext.pathname; import static org.jruby.anno.FrameField.BACKREF; +import static org.jruby.api.Convert.asFixnum; import org.jruby.*; import org.jruby.anno.JRubyClass; @@ -229,11 +230,9 @@ private int cmp(RubyPathname other) { @Override @JRubyMethod(name = "<=>") public IRubyObject op_cmp(ThreadContext context, IRubyObject other) { - if (other instanceof RubyPathname) { - return context.runtime.newFixnum(cmp((RubyPathname) other)); - } else { - return context.nil; - } + return other instanceof RubyPathname path ? + asFixnum(context, cmp(path)) : + context.nil; } @JRubyMethod(name = "hash") diff --git a/core/src/main/java/org/jruby/ext/ripper/RubyRipper.java b/core/src/main/java/org/jruby/ext/ripper/RubyRipper.java index 18f4d10f86d..1b25821db9f 100644 --- a/core/src/main/java/org/jruby/ext/ripper/RubyRipper.java +++ b/core/src/main/java/org/jruby/ext/ripper/RubyRipper.java @@ -47,6 +47,8 @@ import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.lexer.LexingCommon.*; public class RubyRipper extends RubyObject { @@ -299,7 +301,7 @@ public IRubyObject column(ThreadContext context) { if (!parseStarted) return context.nil; - return context.runtime.newFixnum(parser.getColumn()); + return asFixnum(context, parser.getColumn()); } @JRubyMethod @@ -309,12 +311,12 @@ public IRubyObject encoding(ThreadContext context) { @JRubyMethod(name = "end_seen?") public IRubyObject end_seen_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, parser.isEndSeen()); + return asBoolean(context, parser.isEndSeen()); } @JRubyMethod(name = "error?") public IRubyObject error_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, parser.isError()); + return asBoolean(context, parser.isError()); } @JRubyMethod public IRubyObject filename(ThreadContext context) { @@ -327,14 +329,14 @@ public IRubyObject lineno(ThreadContext context) { if (!parseStarted) return context.nil; - return context.runtime.newFixnum(parser.getLineno()); + return asFixnum(context, parser.getLineno()); } @JRubyMethod public IRubyObject state(ThreadContext context) { int state = parser.getState(); - return state == 0 ? context.nil : context.runtime.newFixnum(parser.getState()); + return state == 0 ? context.nil : asFixnum(context, state); } @JRubyMethod @@ -358,7 +360,7 @@ public IRubyObject parse(ThreadContext context) { @JRubyMethod public IRubyObject yydebug(ThreadContext context) { - return RubyBoolean.newBoolean(context, parser.getYYDebug()); + return asBoolean(context, parser.getYYDebug()); } @JRubyMethod(name = "yydebug=") @@ -373,7 +375,7 @@ public static IRubyObject dedent_string(ThreadContext context, IRubyObject self, int wid = _width.convertToInteger().getIntValue(); input.modifyAndClearCodeRange(); int col = LexingCommon.dedent_string(input.getByteList(), wid); - return context.runtime.newFixnum(col); + return asFixnum(context, col); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/ext/set/RubySet.java b/core/src/main/java/org/jruby/ext/set/RubySet.java index acf343336f2..b53601d819c 100644 --- a/core/src/main/java/org/jruby/ext/set/RubySet.java +++ b/core/src/main/java/org/jruby/ext/set/RubySet.java @@ -50,6 +50,8 @@ import java.util.Set; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; /** * Native implementation of Ruby's Set (set.rb replacement). @@ -335,12 +337,12 @@ public IRubyObject freeze(ThreadContext context) { @JRubyMethod(name = "size", alias = "length") public IRubyObject length(ThreadContext context) { - return context.runtime.newFixnum( size() ); + return asFixnum(context, size()); } @JRubyMethod(name = "empty?") public IRubyObject empty_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isEmpty() ); + return asBoolean(context, isEmpty()); } @JRubyMethod(name = "clear") @@ -496,7 +498,7 @@ public IRubyObject flatten_bang(final ThreadContext context) { */ @JRubyMethod(name = "include?", alias = { "member?", "===" }) public RubyBoolean include_p(final ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, containsImpl(obj) ); + return asBoolean(context, containsImpl(obj) ); } final boolean containsImpl(IRubyObject obj) { @@ -518,7 +520,7 @@ public IRubyObject superset_p(final ThreadContext context, IRubyObject set) { return this.hash.op_ge(context, ((RubySet) set).hash); } // size >= set.size && set.all? { |o| include?(o) } - return RubyBoolean.newBoolean(context, + return asBoolean(context, size() >= ((RubySet) set).size() && allElementsIncluded((RubySet) set) ); } @@ -533,7 +535,7 @@ public IRubyObject proper_superset_p(final ThreadContext context, IRubyObject se return this.hash.op_gt(context, ((RubySet) set).hash); } // size >= set.size && set.all? { |o| include?(o) } - return RubyBoolean.newBoolean(context, + return asBoolean(context, size() > ((RubySet) set).size() && allElementsIncluded((RubySet) set) ); } @@ -547,7 +549,7 @@ public IRubyObject subset_p(final ThreadContext context, IRubyObject set) { return this.hash.op_le(context, ((RubySet) set).hash); } // size >= set.size && set.all? { |o| include?(o) } - return RubyBoolean.newBoolean(context, + return asBoolean(context, size() <= ((RubySet) set).size() && allElementsIncluded((RubySet) set) ); } @@ -561,7 +563,7 @@ public IRubyObject proper_subset_p(final ThreadContext context, IRubyObject set) return this.hash.op_lt(context, ((RubySet) set).hash); } // size >= set.size && set.all? { |o| include?(o) } - return RubyBoolean.newBoolean(context, + return asBoolean(context, size() < ((RubySet) set).size() && allElementsIncluded((RubySet) set) ); } @@ -574,7 +576,7 @@ public IRubyObject proper_subset_p(final ThreadContext context, IRubyObject set) @JRubyMethod(name = "intersect?") public IRubyObject intersect_p(final ThreadContext context, IRubyObject set) { if ( set instanceof RubySet ) { - return RubyBoolean.newBoolean(context, intersect((RubySet) set) ); + return asBoolean(context, intersect((RubySet) set) ); } throw context.runtime.newArgumentError("value must be a set"); } @@ -602,7 +604,7 @@ public boolean intersect(final RubySet set) { @JRubyMethod(name = "disjoint?") public IRubyObject disjoint_p(final ThreadContext context, IRubyObject set) { if ( set instanceof RubySet ) { - return RubyBoolean.newBoolean(context, ! intersect((RubySet) set) ); + return asBoolean(context, ! intersect((RubySet) set) ); } throw context.runtime.newArgumentError("value must be a set"); } @@ -623,7 +625,7 @@ public IRubyObject each(final ThreadContext context, Block block) { * @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[]) */ private static IRubyObject size(ThreadContext context, RubySet recv, IRubyObject[] args) { - return context.runtime.newFixnum(recv.size()); + return asFixnum(context, recv.size()); } /** diff --git a/core/src/main/java/org/jruby/ext/socket/Addrinfo.java b/core/src/main/java/org/jruby/ext/socket/Addrinfo.java index a22ffe384b8..f1f4578faf5 100644 --- a/core/src/main/java/org/jruby/ext/socket/Addrinfo.java +++ b/core/src/main/java/org/jruby/ext/socket/Addrinfo.java @@ -48,6 +48,8 @@ import static jnr.constants.platform.ProtocolFamily.PF_UNIX; import static jnr.constants.platform.ProtocolFamily.PF_UNSPEC; import static jnr.constants.platform.Sock.*; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.ext.socket.SocketUtils.sockerr; public class Addrinfo extends RubyObject { @@ -434,25 +436,23 @@ public static IRubyObject unix(ThreadContext context, IRubyObject recv, IRubyObj @JRubyMethod public IRubyObject afamily(ThreadContext context) { - return context.runtime.newFixnum(getAddressFamily().intValue()); + return asFixnum(context, getAddressFamily().intValue()); } @JRubyMethod public IRubyObject pfamily(ThreadContext context) { - return context.runtime.newFixnum(pfamily.intValue()); + return asFixnum(context, pfamily.intValue()); } @JRubyMethod public IRubyObject socktype(ThreadContext context) { - return context.runtime.newFixnum(sock == null ? 0 : sock.intValue()); + return asFixnum(context, sock == null ? 0 : sock.intValue()); } @JRubyMethod public IRubyObject protocol(ThreadContext context) { // Any unknown protocol will end up null but the one case we see is IPPROTO_RAW is not listed in /etc/protocols - int proto = protocol == null ? 255 : protocol.getProto(); - - return context.runtime.newFixnum(proto); + return asFixnum(context, protocol == null ? 255 : protocol.getProto()); } @JRubyMethod @@ -469,22 +469,22 @@ public IRubyObject canonname(ThreadContext context) { @JRubyMethod(name = "ipv4?") public IRubyObject ipv4_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getAddressFamily() == AF_INET); + return asBoolean(context, getAddressFamily() == AF_INET); } @JRubyMethod(name = "ipv6?") public IRubyObject ipv6_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getAddressFamily() == AF_INET6); + return asBoolean(context, getAddressFamily() == AF_INET6); } @JRubyMethod(name = "unix?") public IRubyObject unix_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getAddressFamily() == AF_UNIX); + return asBoolean(context, getAddressFamily() == AF_UNIX); } @JRubyMethod(name = "ip?") public IRubyObject ip_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getAddressFamily() == AF_INET || getAddressFamily() == AF_INET6); + return asBoolean(context, getAddressFamily() == AF_INET || getAddressFamily() == AF_INET6); } @JRubyMethod @@ -512,29 +512,29 @@ public IRubyObject ip_port(ThreadContext context) { if (getAddressFamily() != AF_INET && getAddressFamily() != AF_INET6) { throw sockerr(context.runtime, "need IPv4 or IPv6 address"); } - return context.runtime.newFixnum(((InetSocketAddress) socketAddress).getPort()); + return asFixnum(context, ((InetSocketAddress) socketAddress).getPort()); } @JRubyMethod(name = "ipv4_private?") public IRubyObject ipv4_private_p(ThreadContext context) { if (getAddressFamily() == AF_INET) { - return RubyBoolean.newBoolean(context, getInet4Address().isSiteLocalAddress()); + return asBoolean(context, getInet4Address().isSiteLocalAddress()); } - return RubyBoolean.newBoolean(context, false); + return asBoolean(context, false); } @JRubyMethod(name = "ipv4_loopback?") public IRubyObject ipv4_loopback_p(ThreadContext context) { if (getAddressFamily() == AF_INET) { - return RubyBoolean.newBoolean(context, ((InetSocketAddress) socketAddress).getAddress().isLoopbackAddress()); + return asBoolean(context, ((InetSocketAddress) socketAddress).getAddress().isLoopbackAddress()); } - return RubyBoolean.newBoolean(context, false); + return asBoolean(context, false); } @JRubyMethod(name = "ipv4_multicast?") public IRubyObject ipv4_multicast_p(ThreadContext context) { if (getAddressFamily() == AF_INET) { - return RubyBoolean.newBoolean(context, getInet4Address().isMulticastAddress()); + return asBoolean(context, getInet4Address().isMulticastAddress()); } return context.fals; } @@ -542,7 +542,7 @@ public IRubyObject ipv4_multicast_p(ThreadContext context) { @JRubyMethod(name = "ipv6_unspecified?") public IRubyObject ipv6_unspecified_p(ThreadContext context) { if (getAddressFamily() == AF_INET6) { - return RubyBoolean.newBoolean(context, ipv6_ip().equals("::")); + return asBoolean(context, ipv6_ip().equals("::")); } return context.fals; } @@ -550,27 +550,27 @@ public IRubyObject ipv6_unspecified_p(ThreadContext context) { @JRubyMethod(name = "ipv6_loopback?") public IRubyObject ipv6_loopback_p(ThreadContext context) { if (getAddressFamily() == AF_INET6) { - return RubyBoolean.newBoolean(context, getInetSocketAddress().getAddress().isLoopbackAddress()); + return asBoolean(context, getInetSocketAddress().getAddress().isLoopbackAddress()); } - return RubyBoolean.newBoolean(context, false); + return asBoolean(context, false); } @JRubyMethod(name = "ipv6_multicast?") public IRubyObject ipv6_multicast_p(ThreadContext context) { if (getAddressFamily() == AF_INET6) { - return RubyBoolean.newBoolean(context, getInet6Address().isMulticastAddress()); + return asBoolean(context, getInet6Address().isMulticastAddress()); } return context.fals; } @JRubyMethod(name = "ipv6_linklocal?") public IRubyObject ipv6_linklocal_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, getInetSocketAddress().getAddress().isLinkLocalAddress()); + return asBoolean(context, getInetSocketAddress().getAddress().isLinkLocalAddress()); } @JRubyMethod(name = "ipv6_sitelocal?") public IRubyObject ipv6_sitelocal_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, ((InetSocketAddress) socketAddress).getAddress().isSiteLocalAddress()); + return asBoolean(context, ((InetSocketAddress) socketAddress).getAddress().isSiteLocalAddress()); } @JRubyMethod(name = "ipv6_unique_local?") @@ -583,47 +583,47 @@ public IRubyObject ipv6_unique_local_p(ThreadContext context) { return context.fals; } int firstAddrByte = address.getAddress()[0] & 0xff; - return RubyBoolean.newBoolean(context, firstAddrByte == 0xfc || firstAddrByte == 0xfd); + return asBoolean(context, firstAddrByte == 0xfc || firstAddrByte == 0xfd); } @JRubyMethod(name = "ipv6_v4mapped?") public IRubyObject ipv6_v4mapped_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, looksLikeV4ButIsV6); + return asBoolean(context, looksLikeV4ButIsV6); } @JRubyMethod(name = "ipv6_v4compat?") public IRubyObject ipv6_v4compat_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isIPV6() && !looksLikeV4ButIsV6); + return asBoolean(context, isIPV6() && !looksLikeV4ButIsV6); } @JRubyMethod(name = "ipv6_mc_nodelocal?") public IRubyObject ipv6_mc_nodelocal_p(ThreadContext context) { Inet6Address in6 = getInet6Address(); - return RubyBoolean.newBoolean(context, in6 != null && in6.isMCNodeLocal()); + return asBoolean(context, in6 != null && in6.isMCNodeLocal()); } @JRubyMethod(name = "ipv6_mc_linklocal?") public IRubyObject ipv6_mc_linklocal_p(ThreadContext context) { Inet6Address in6 = getInet6Address(); - return RubyBoolean.newBoolean(context, in6 != null && in6.isMCLinkLocal()); + return asBoolean(context, in6 != null && in6.isMCLinkLocal()); } @JRubyMethod(name = "ipv6_mc_sitelocal?") public IRubyObject ipv6_mc_sitelocal_p(ThreadContext context) { Inet6Address in6 = getInet6Address(); - return RubyBoolean.newBoolean(context, in6 != null && in6.isMCSiteLocal()); + return asBoolean(context, in6 != null && in6.isMCSiteLocal()); } @JRubyMethod(name = "ipv6_mc_orglocal?") public IRubyObject ipv6_mc_orglocal_p(ThreadContext context) { Inet6Address in6 = getInet6Address(); - return RubyBoolean.newBoolean(context, in6 != null && in6.isMCOrgLocal()); + return asBoolean(context, in6 != null && in6.isMCOrgLocal()); } @JRubyMethod(name = "ipv6_mc_global?") public IRubyObject ipv6_mc_global_p(ThreadContext context) { Inet6Address in6 = getInet6Address(); - return RubyBoolean.newBoolean(context, in6 != null && in6.isMCGlobal()); + return asBoolean(context, in6 != null && in6.isMCGlobal()); } // FIXME: What is the actual name for ::0.0.0.1 diff --git a/core/src/main/java/org/jruby/ext/socket/Ifaddr.java b/core/src/main/java/org/jruby/ext/socket/Ifaddr.java index 3d6a077b046..1ea597a33ca 100644 --- a/core/src/main/java/org/jruby/ext/socket/Ifaddr.java +++ b/core/src/main/java/org/jruby/ext/socket/Ifaddr.java @@ -17,6 +17,8 @@ import jnr.constants.platform.InterfaceInfo; +import static org.jruby.api.Convert.asFixnum; + /** * * @author Lucas Allan Amorim @@ -115,12 +117,12 @@ public IRubyObject broadaddr(ThreadContext context) { @JRubyMethod public IRubyObject ifindex(ThreadContext context) { - return context.runtime.newFixnum(index); + return asFixnum(context, index); } @JRubyMethod public IRubyObject flags(ThreadContext context) { - return context.runtime.newFixnum(flags); + return asFixnum(context, flags); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/ext/socket/Option.java b/core/src/main/java/org/jruby/ext/socket/Option.java index 4f9f2f27c65..3448bc9361f 100644 --- a/core/src/main/java/org/jruby/ext/socket/Option.java +++ b/core/src/main/java/org/jruby/ext/socket/Option.java @@ -12,19 +12,19 @@ import org.jruby.RubyObject; import org.jruby.RubyString; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; import org.jruby.util.Pack; import org.jruby.util.Sprintf; -import org.jruby.util.TypeConverter; import java.nio.ByteBuffer; import java.util.Locale; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.integerAsInt; import static org.jruby.api.Error.typeError; public class Option extends RubyObject { @@ -66,17 +66,17 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) { @JRubyMethod public IRubyObject family(ThreadContext context) { - return context.runtime.newFixnum(family.longValue()); + return asFixnum(context, family.longValue()); } @JRubyMethod public IRubyObject level(ThreadContext context) { - return context.runtime.newFixnum(level.longValue()); + return asFixnum(context, level.longValue()); } @JRubyMethod public IRubyObject optname(ThreadContext context) { - return context.runtime.newFixnum(option.longValue()); + return asFixnum(context, option.longValue()); } @JRubyMethod @@ -203,7 +203,7 @@ public static IRubyObject rb_int(ThreadContext context, IRubyObject self, IRubyO public IRubyObject asInt(ThreadContext context) { validateDataSize(context, data, 4); - return context.runtime.newFixnum(unpackInt(data)); + return asFixnum(context, unpackInt(data)); } @JRubyMethod(required = 4, meta = true) @@ -226,7 +226,7 @@ public IRubyObject bool(ThreadContext context) { @JRubyMethod(meta = true) public static IRubyObject linger(ThreadContext context, IRubyObject self, IRubyObject vonoffArg, IRubyObject vsecs) { IRubyObject vonoff = checkToInteger(context, vonoffArg); - int coercedVonoff = !vonoff.isNil() ? integerAsInt(context, (RubyInteger) vonoff) : (vonoffArg.isTrue() ? 1 : 0); + int coercedVonoff = !vonoff.isNil() ? Convert.asInt(context, (RubyInteger) vonoff) : (vonoffArg.isTrue() ? 1 : 0); ByteList data = packLinger(coercedVonoff, vsecs.convertToInteger().getIntValue()); return new Option(context.runtime, ProtocolFamily.PF_UNSPEC, SocketLevel.SOL_SOCKET, SocketOption.SO_LINGER, data); diff --git a/core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java b/core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java index e572d4f81e0..7e2a735cf3d 100644 --- a/core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java +++ b/core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java @@ -52,6 +52,7 @@ import org.jruby.RubySymbol; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; +import org.jruby.api.Convert; import org.jruby.ast.util.ArgsUtil; import org.jruby.ext.fcntl.FcntlLibrary; import org.jruby.platform.Platform; @@ -81,6 +82,7 @@ import static jnr.constants.platform.TCP.TCP_KEEPIDLE; import static jnr.constants.platform.TCP.TCP_KEEPINTVL; import static jnr.constants.platform.TCP.TCP_NODELAY; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.runtime.Helpers.extractExceptionOnlyArg; import static org.jruby.runtime.Helpers.throwErrorFromException; import static com.headius.backport9.buffer.Buffers.flipBuffer; @@ -118,7 +120,7 @@ public static IRubyObject for_fd(ThreadContext context, IRubyObject _klass, IRub @JRubyMethod(name = "do_not_reverse_lookup") public IRubyObject do_not_reverse_lookup(ThreadContext context) { - return RubyBoolean.newBoolean(context, doNotReverseLookup); + return Convert.asBoolean(context, doNotReverseLookup); } @JRubyMethod(name = "do_not_reverse_lookup=") @@ -129,7 +131,7 @@ public IRubyObject set_do_not_reverse_lookup(ThreadContext context, IRubyObject @JRubyMethod(meta = true) public static IRubyObject do_not_reverse_lookup(ThreadContext context, IRubyObject recv) { - return RubyBoolean.newBoolean(context, context.runtime.isDoNotReverseLookupEnabled()); + return Convert.asBoolean(context, context.runtime.isDoNotReverseLookupEnabled()); } @JRubyMethod(name = "do_not_reverse_lookup=", meta = true) @@ -174,7 +176,7 @@ private IRubyObject doSend(ThreadContext context, IRubyObject _mesg, IRubyObject if (channel instanceof DatagramChannel && sockaddr != null) { written = ((DatagramChannel) channel).send(mesgBytes, sockaddr); - return context.runtime.newFixnum(written); + return asFixnum(context, written); } else { return syswrite(context, _mesg); } diff --git a/core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java b/core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java index 2ad4a113090..f0249c71786 100644 --- a/core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java +++ b/core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java @@ -51,6 +51,7 @@ import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; /** @@ -74,7 +75,7 @@ public IRubyObject listen(ThreadContext context, IRubyObject backlog) { IRubyWarnings.ID.LISTEN_SERVER_SOCKET, "pass backlog to #bind instead of #listen (https://github.com/jruby/jruby/wiki/ServerSocket)"); - return context.runtime.newFixnum(0); + return asFixnum(context, 0); } @JRubyMethod(notImplemented = true) diff --git a/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java b/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java index a936898a4d3..7e73bf62337 100644 --- a/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java +++ b/core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java @@ -46,6 +46,8 @@ import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; +import static org.jruby.api.Convert.asFixnum; + @JRubyClass(name="UNIXServer", parent="UNIXSocket") public class RubyUNIXServer extends RubyUNIXSocket { static void createUNIXServer(Ruby runtime) { @@ -147,13 +149,13 @@ public IRubyObject accept_nonblock(ThreadContext context, Ruby runtime, boolean @JRubyMethod public IRubyObject listen(ThreadContext context, IRubyObject log) { // TODO listen backlog - return context.runtime.newFixnum(0); + return asFixnum(context, 0); } @JRubyMethod public IRubyObject sysaccept(ThreadContext context) { RubyUNIXSocket socket = (RubyUNIXSocket) accept(context); - return context.runtime.newFixnum(((UnixSocketChannel) socket.getChannel()).getFD()); + return asFixnum(context, ((UnixSocketChannel) socket.getChannel()).getFD()); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java b/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java index 28139d1baef..9087ad11090 100644 --- a/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java +++ b/core/src/main/java/org/jruby/ext/socket/RubyUNIXSocket.java @@ -67,6 +67,7 @@ import java.nio.channels.Channel; import static com.headius.backport9.buffer.Buffers.flipBuffer; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; @@ -311,7 +312,7 @@ public IRubyObject setsockopt(ThreadContext context, IRubyObject _level, IRubyOb throw context.runtime.newErrnoENOPROTOOPTError(); } - return context.runtime.newFixnum(0); + return asFixnum(context, 0); } protected static void rb_sys_fail(Ruby runtime, String message) { diff --git a/core/src/main/java/org/jruby/ext/thread/Mutex.java b/core/src/main/java/org/jruby/ext/thread/Mutex.java index 1d6fe9a3a39..cbaf8c59c83 100644 --- a/core/src/main/java/org/jruby/ext/thread/Mutex.java +++ b/core/src/main/java/org/jruby/ext/thread/Mutex.java @@ -44,6 +44,9 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.marshal.DataType; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; + /** * The "Mutex" class from the 'thread' library. */ @@ -80,7 +83,7 @@ public static RubyClass setup(RubyClass threadClass, RubyClass objectClass) { @JRubyMethod(name = "locked?") public RubyBoolean locked_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isLocked()); + return asBoolean(context, isLocked()); } public boolean isLocked() { @@ -89,7 +92,7 @@ public boolean isLocked() { @JRubyMethod public RubyBoolean try_lock(ThreadContext context) { - return RubyBoolean.newBoolean(context, tryLock(context)); + return asBoolean(context, tryLock(context)); } public boolean tryLock(ThreadContext context) { @@ -154,9 +157,8 @@ public IRubyObject sleep(ThreadContext context) { @JRubyMethod public IRubyObject sleep(ThreadContext context, IRubyObject timeout) { - Ruby runtime = context.runtime; - final long beg = System.currentTimeMillis(); + try { RubyThread thread = context.getThread(); @@ -173,12 +175,12 @@ public IRubyObject sleep(ThreadContext context, IRubyObject timeout) { } } } catch (IllegalMonitorStateException imse) { - throw runtime.newThreadError("Attempt to unlock a mutex which is not locked"); + throw context.runtime.newThreadError("Attempt to unlock a mutex which is not locked"); } catch (InterruptedException ex) { context.pollThreadEvents(); } - return runtime.newFixnum((System.currentTimeMillis() - beg) / 1000); + return asFixnum(context, (System.currentTimeMillis() - beg) / 1000); } @JRubyMethod @@ -193,7 +195,7 @@ public IRubyObject synchronize(ThreadContext context, Block block) { @JRubyMethod(name = "owned?") public IRubyObject owned_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, lock.isHeldByCurrentThread()); + return asBoolean(context, lock.isHeldByCurrentThread()); } private void checkRelocking(ThreadContext context) { diff --git a/core/src/main/java/org/jruby/ext/thread/Queue.java b/core/src/main/java/org/jruby/ext/thread/Queue.java index 8481a5649db..74d3b4c08e6 100644 --- a/core/src/main/java/org/jruby/ext/thread/Queue.java +++ b/core/src/main/java/org/jruby/ext/thread/Queue.java @@ -55,6 +55,8 @@ import org.jruby.runtime.marshal.DataType; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; /* @@ -342,7 +344,7 @@ public final IRubyObject freeze(ThreadContext context) { @JRubyMethod(name = "empty?") public RubyBoolean empty_p(ThreadContext context) { initializedCheck(); - return RubyBoolean.newBoolean(context, count.get() == 0); + return asBoolean(context, count.get() == 0); } @JRubyMethod(name = {"length", "size"}) @@ -358,7 +360,7 @@ public RubyNumeric num_waiting(ThreadContext context) { try { takeLock.lockInterruptibly(); try { - return context.runtime.newFixnum(takeLock.getWaitQueueLength(notEmpty)); + return asFixnum(context, takeLock.getWaitQueueLength(notEmpty)); } finally { takeLock.unlock(); } @@ -591,7 +593,7 @@ protected void closeInternal() throws InterruptedException { @JRubyMethod(name = "closed?") public IRubyObject closed_p(ThreadContext context) { initializedCheck(); - return RubyBoolean.newBoolean(context, closed); + return asBoolean(context, closed); } public synchronized void shutdown() throws InterruptedException { diff --git a/core/src/main/java/org/jruby/ext/thread/SizedQueue.java b/core/src/main/java/org/jruby/ext/thread/SizedQueue.java index 9d15c719bc7..d886272d188 100644 --- a/core/src/main/java/org/jruby/ext/thread/SizedQueue.java +++ b/core/src/main/java/org/jruby/ext/thread/SizedQueue.java @@ -44,6 +44,8 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; +import static org.jruby.api.Convert.asFixnum; + /** * The "SizedQueue" class from the 'thread' library. */ @@ -117,7 +119,7 @@ public RubyNumeric num_waiting(ThreadContext context) { try { putLock.lockInterruptibly(); try { - return context.runtime.newFixnum(takeLock.getWaitQueueLength(notEmpty) + putLock.getWaitQueueLength(notFull)); + return asFixnum(context, takeLock.getWaitQueueLength(notEmpty) + putLock.getWaitQueueLength(notFull)); } finally { putLock.unlock(); } diff --git a/core/src/main/java/org/jruby/ext/tracepoint/TracePoint.java b/core/src/main/java/org/jruby/ext/tracepoint/TracePoint.java index 60b5f2a34ce..9268bb029c5 100644 --- a/core/src/main/java/org/jruby/ext/tracepoint/TracePoint.java +++ b/core/src/main/java/org/jruby/ext/tracepoint/TracePoint.java @@ -13,13 +13,14 @@ import org.jruby.runtime.Block; import org.jruby.runtime.EventHook; import org.jruby.runtime.JavaSites; -import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.RubyEvent; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.RubyStringBuilder.str; public class TracePoint extends RubyObject { @@ -159,7 +160,7 @@ public IRubyObject enable(ThreadContext context, Block block) { @JRubyMethod(name = "enabled?") public IRubyObject enabled_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, enabled); + return asBoolean(context, enabled); } @JRubyMethod @@ -183,7 +184,7 @@ public IRubyObject inspect(ThreadContext context) { public IRubyObject lineno(ThreadContext context) { checkInside(context); - return context.runtime.newFixnum(line); + return asFixnum(context, line); } @JRubyMethod @@ -260,7 +261,7 @@ private synchronized IRubyObject doToggle(ThreadContext context, Block block, bo } } - IRubyObject old = RubyBoolean.newBoolean(context, enabled); + IRubyObject old = asBoolean(context, enabled); updateEnabled(context, toggle); return old; diff --git a/core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipReader.java b/core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipReader.java index 93b361a4aa5..fa6434c4f0a 100644 --- a/core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipReader.java +++ b/core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipReader.java @@ -61,6 +61,7 @@ import java.util.List; import static org.jruby.RubyIO.PARAGRAPH_SEPARATOR; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.castAsString; import static org.jruby.runtime.Visibility.PRIVATE; @@ -176,21 +177,24 @@ private long internalPosition() { @JRubyMethod public IRubyObject rewind(ThreadContext context) { - Ruby runtime = context.runtime; - // should invoke seek on realIo... realIo.callMethod(context, "seek", - new IRubyObject[]{runtime.newFixnum(-internalPosition()), runtime.newFixnum(PosixShim.SEEK_CUR)}); + new IRubyObject[]{asFixnum(context, -internalPosition()), asFixnum(context, PosixShim.SEEK_CUR)}); // ... and then reinitialize initialize(context, realIo); - return getRuntime().getNil(); + return context.nil; } @JRubyMethod(name = "lineno") + public IRubyObject lineno(ThreadContext context) { + return asFixnum(context, line); + } + + @Deprecated public IRubyObject lineno() { - return getRuntime().newFixnum(line); + return lineno(getCurrentContext()); } @JRubyMethod(name = "readline", writes = FrameField.LASTLINE) diff --git a/core/src/main/java/org/jruby/ext/zlib/RubyZlib.java b/core/src/main/java/org/jruby/ext/zlib/RubyZlib.java index 24887c90b16..951937c6aa7 100644 --- a/core/src/main/java/org/jruby/ext/zlib/RubyZlib.java +++ b/core/src/main/java/org/jruby/ext/zlib/RubyZlib.java @@ -54,6 +54,7 @@ import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.ThreadContext; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Convert.numericToLong; import static org.jruby.runtime.Visibility.*; import org.jruby.runtime.builtin.IRubyObject; @@ -186,8 +187,7 @@ public static IRubyObject zlib_version(IRubyObject recv) { } @JRubyMethod(name = "crc32", optional = 2, checkArity = false, module = true, visibility = PRIVATE) - public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) { - ThreadContext context = recv.getRuntime().getCurrentContext(); + public static IRubyObject crc32(ThreadContext context, IRubyObject recv, IRubyObject[] args) { args = Arity.scanArgs(context.runtime, args, 0, 2); long start = 0; ByteList bytes = null; @@ -206,12 +206,16 @@ public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) { if (slowPath) { result = JZlib.crc32_combine(start, result, bytesLength); } - return recv.getRuntime().newFixnum(result); + return asFixnum(context, result); + } + + @Deprecated + public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) { + return crc32(((RubyBasicObject) recv).getCurrentContext(), recv, args); } @JRubyMethod(name = "adler32", optional = 2, checkArity = false, module = true, visibility = PRIVATE) - public static IRubyObject adler32(IRubyObject recv, IRubyObject[] args) { - ThreadContext context = recv.getRuntime().getCurrentContext(); + public static IRubyObject adler32(ThreadContext context, IRubyObject recv, IRubyObject[] args) { args = Arity.scanArgs(context.runtime, args, 0, 2); int start = 1; ByteList bytes = null; @@ -226,7 +230,12 @@ public static IRubyObject adler32(IRubyObject recv, IRubyObject[] args) { if (start != 1) { result = JZlib.adler32_combine(start, result, bytes.length()); } - return recv.getRuntime().newFixnum(result); + return asFixnum(context, result); + } + + @Deprecated + public static IRubyObject adler32(IRubyObject recv, IRubyObject[] args) { + return adler32(((RubyBasicObject) recv).getCurrentContext(), recv, args); } @JRubyMethod(module = true) @@ -240,42 +249,48 @@ public static IRubyObject deflate(IRubyObject recv, IRubyObject[] args) { } @JRubyMethod(name = "crc_table", module = true, visibility = PRIVATE) - public static IRubyObject crc_table(IRubyObject recv) { - Ruby runtime = recv.getRuntime(); + public static IRubyObject crc_table(ThreadContext context, IRubyObject recv) { int[] table = com.jcraft.jzlib.CRC32.getCRC32Table(); - RubyArray array = runtime.newArray(table.length); + RubyArray array = context.runtime.newArray(table.length); for (int i = 0; i < table.length; i++) { - array.append(runtime.newFixnum(table[i] & 0xffffffffL)); + array.append(asFixnum(context, table[i] & 0xffffffffL)); } return array; } + @Deprecated + public static IRubyObject crc_table(IRubyObject recv) { + return crc_table(((RubyBasicObject) recv).getCurrentContext(), recv); + } + @JRubyMethod(name = "crc32_combine", module = true, visibility = PRIVATE) - public static IRubyObject crc32_combine(IRubyObject recv, - IRubyObject arg0, - IRubyObject arg1, - IRubyObject arg2) { - ThreadContext context = recv.getRuntime().getCurrentContext(); + public static IRubyObject crc32_combine(ThreadContext context, IRubyObject recv, + IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { long crc1 = numericToLong(context, arg0); long crc2 = numericToLong(context, arg1); long len2 = numericToLong(context, arg2); - long crc3 = com.jcraft.jzlib.JZlib.crc32_combine(crc1, crc2, len2); - return recv.getRuntime().newFixnum(crc3); + return asFixnum(context, com.jcraft.jzlib.JZlib.crc32_combine(crc1, crc2, len2)); + } + + @Deprecated + public static IRubyObject crc32_combine(IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { + return crc32_combine(((RubyBasicObject) recv).getCurrentContext(), recv, arg0, arg1, arg2); } @JRubyMethod(name = "adler32_combine", module = true, visibility = PRIVATE) - public static IRubyObject adler32_combine(IRubyObject recv, - IRubyObject arg0, - IRubyObject arg1, - IRubyObject arg2) { - ThreadContext context = recv.getRuntime().getCurrentContext(); + public static IRubyObject adler32_combine(ThreadContext context, IRubyObject recv, + IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { long adler1 = numericToLong(context, arg0); long adler2 = numericToLong(context, arg1); long len2 = numericToLong(context, arg2); - long adler3 = com.jcraft.jzlib.JZlib.adler32_combine(adler1, adler2, len2); - return recv.getRuntime().newFixnum(adler3); + return asFixnum(context, com.jcraft.jzlib.JZlib.adler32_combine(adler1, adler2, len2)); + } + + @Deprecated + public static IRubyObject adler32_combine(IRubyObject recv, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { + return adler32_combine(((RubyBasicObject) recv).getCurrentContext(), recv, arg0, arg1, arg2); } static RaiseException newZlibError(Ruby runtime, String message) { diff --git a/core/src/main/java/org/jruby/ext/zlib/ZStream.java b/core/src/main/java/org/jruby/ext/zlib/ZStream.java index 77d6d9271fc..ce6b10e6c75 100644 --- a/core/src/main/java/org/jruby/ext/zlib/ZStream.java +++ b/core/src/main/java/org/jruby/ext/zlib/ZStream.java @@ -38,6 +38,9 @@ import org.jruby.anno.JRubyMethod; import org.jruby.runtime.Block; import org.jruby.runtime.ThreadContext; + +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.runtime.Visibility.PRIVATE; import org.jruby.runtime.builtin.IRubyObject; @@ -123,10 +126,15 @@ public IRubyObject set_avail_out(IRubyObject p1) { } @JRubyMethod(name = "adler") - public IRubyObject adler() { + public IRubyObject adler(ThreadContext context) { checkClosed(); - - return getRuntime().newFixnum(internalAdler()); + + return asFixnum(context, internalAdler()); + } + + @Deprecated + public IRubyObject adler() { + return adler(getCurrentContext()); } @JRubyMethod(name = "finish") @@ -158,7 +166,7 @@ public IRubyObject total_in() { @JRubyMethod(name = "finished?") public IRubyObject finished_p(ThreadContext context) { checkClosed(); - return RubyBoolean.newBoolean(context, internalFinished()); + return asBoolean(context, internalFinished()); } @JRubyMethod(name = {"close", "end"}) diff --git a/core/src/main/java/org/jruby/ir/instructions/AsFixnumInstr.java b/core/src/main/java/org/jruby/ir/instructions/AsFixnumInstr.java index 7759083a474..14a1e524e80 100644 --- a/core/src/main/java/org/jruby/ir/instructions/AsFixnumInstr.java +++ b/core/src/main/java/org/jruby/ir/instructions/AsFixnumInstr.java @@ -11,6 +11,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asFixnum; + public class AsFixnumInstr extends OneOperandResultBaseInstr { public AsFixnumInstr(Variable result, Operand operand) { super(Operation.AS_FIXNUM, result, operand); @@ -32,6 +34,6 @@ public void visit(IRVisitor visitor) { @Override public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) { - return context.runtime.newFixnum((int) getOperand1().retrieve(context, self, currScope, currDynScope, temp)); + return asFixnum(context, (int) getOperand1().retrieve(context, self, currScope, currDynScope, temp)); } } diff --git a/core/src/main/java/org/jruby/ir/interpreter/InterpreterEngine.java b/core/src/main/java/org/jruby/ir/interpreter/InterpreterEngine.java index 1dafa55a463..7f9a50eb3c1 100644 --- a/core/src/main/java/org/jruby/ir/interpreter/InterpreterEngine.java +++ b/core/src/main/java/org/jruby/ir/interpreter/InterpreterEngine.java @@ -65,6 +65,9 @@ import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; + /** * Base full interpreter. Subclasses can use utility methods here and override what they want. This method requires * that it has fully built and has had a CFG made, etc... @@ -502,13 +505,13 @@ protected static void processOtherOp(ThreadContext context, Block block, Instr i } case BOX_FIXNUM: { - RubyFixnum f = context.runtime.newFixnum(getFixnumArg(fixnums, ((BoxFixnumInstr) instr).getValue())); + RubyFixnum f = asFixnum(context, getFixnumArg(fixnums, ((BoxFixnumInstr) instr).getValue())); setResult(temp, currDynScope, ((BoxInstr)instr).getResult(), f); break; } case BOX_BOOLEAN: { - RubyBoolean f = RubyBoolean.newBoolean(context, getBooleanArg(booleans, ((BoxBooleanInstr) instr).getValue())); + RubyBoolean f = asBoolean(context, getBooleanArg(booleans, ((BoxBooleanInstr) instr).getValue())); setResult(temp, currDynScope, ((BoxInstr)instr).getResult(), f); break; } diff --git a/core/src/main/java/org/jruby/ir/operands/Boolean.java b/core/src/main/java/org/jruby/ir/operands/Boolean.java index 7a8bb072875..f969f8a6927 100644 --- a/core/src/main/java/org/jruby/ir/operands/Boolean.java +++ b/core/src/main/java/org/jruby/ir/operands/Boolean.java @@ -6,6 +6,8 @@ import org.jruby.ir.persistence.IRWriterEncoder; import org.jruby.runtime.ThreadContext; +import static org.jruby.api.Convert.asBoolean; + public class Boolean extends ImmutableLiteral { private final boolean truthy; @@ -22,7 +24,7 @@ public OperandType getOperandType() { @Override public Object createCacheObject(ThreadContext context) { - return RubyBoolean.newBoolean(context, isTrue()); + return asBoolean(context, isTrue()); } public boolean isTrue() { diff --git a/core/src/main/java/org/jruby/ir/operands/Fixnum.java b/core/src/main/java/org/jruby/ir/operands/Fixnum.java index 4d257be7295..b604d692ce2 100644 --- a/core/src/main/java/org/jruby/ir/operands/Fixnum.java +++ b/core/src/main/java/org/jruby/ir/operands/Fixnum.java @@ -7,6 +7,8 @@ import java.math.BigInteger; +import static org.jruby.api.Convert.asFixnum; + /* * Represents a literal fixnum. * @@ -46,7 +48,7 @@ public Fixnum(BigInteger val) { @Override public Object createCacheObject(ThreadContext context) { - return context.runtime.newFixnum(value); + return asFixnum(context, value); } @Override diff --git a/core/src/main/java/org/jruby/ir/operands/UnboxedBoolean.java b/core/src/main/java/org/jruby/ir/operands/UnboxedBoolean.java index 8f035942c1b..ace67ef0d7a 100644 --- a/core/src/main/java/org/jruby/ir/operands/UnboxedBoolean.java +++ b/core/src/main/java/org/jruby/ir/operands/UnboxedBoolean.java @@ -1,11 +1,12 @@ package org.jruby.ir.operands; -import org.jruby.RubyBoolean; import org.jruby.ir.IRVisitor; import org.jruby.ir.persistence.IRReaderDecoder; import org.jruby.ir.persistence.IRWriterEncoder; import org.jruby.runtime.ThreadContext; +import static org.jruby.api.Convert.asBoolean; + public class UnboxedBoolean extends ImmutableLiteral { private final boolean truthy; @@ -25,7 +26,7 @@ public OperandType getOperandType() { @Override public Object createCacheObject(ThreadContext context) { - return RubyBoolean.newBoolean(context, isTrue()); + return asBoolean(context, isTrue()); } public boolean isTrue() { diff --git a/core/src/main/java/org/jruby/ir/operands/UnboxedFixnum.java b/core/src/main/java/org/jruby/ir/operands/UnboxedFixnum.java index a48c20116b4..1c5ba4eee3a 100644 --- a/core/src/main/java/org/jruby/ir/operands/UnboxedFixnum.java +++ b/core/src/main/java/org/jruby/ir/operands/UnboxedFixnum.java @@ -3,6 +3,8 @@ import org.jruby.ir.IRVisitor; import org.jruby.runtime.ThreadContext; +import static org.jruby.api.Convert.asFixnum; + /* * Represents a literal fixnum. * @@ -38,7 +40,7 @@ public OperandType getOperandType() { @Override public Object createCacheObject(ThreadContext context) { - return context.runtime.newFixnum(value); + return asFixnum(context, value); } @Override diff --git a/core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java b/core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java index ec705196413..6d3cadb4264 100644 --- a/core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java +++ b/core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java @@ -89,7 +89,7 @@ import org.jruby.util.log.LoggerFactory; import org.objectweb.asm.Type; -import static org.jruby.api.Convert.castAsModule; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.ir.operands.UndefinedValue.UNDEFINED; import static org.jruby.runtime.Block.Type.LAMBDA; @@ -451,7 +451,7 @@ public static IRubyObject isExceptionHandled(ThreadContext context, IRubyObject boolean ret = IRRuntimeHelpers.isRubyExceptionHandled(context, excType, excObj) || IRRuntimeHelpers.isJavaExceptionHandled(context, excType, excObj, false); - return RubyBoolean.newBoolean(context, ret); + return asBoolean(context, ret); } // partially: vm_insnhelper.c - vm_check_match + check_match @@ -1257,7 +1257,7 @@ public static RubyModule findInstanceMethodContainer(ThreadContext context, Dyna public static RubyBoolean isBlockGiven(ThreadContext context, Object blk) { if (blk instanceof RubyProc) blk = ((RubyProc) blk).getBlock(); if (blk instanceof RubyNil) blk = Block.NULL_BLOCK; - return RubyBoolean.newBoolean(context, ((Block) blk).isGiven() ); + return asBoolean(context, ((Block) blk).isGiven() ); } @JIT @Interp @@ -2111,7 +2111,7 @@ public static IRubyObject irReqdArgMultipleAsgn(ThreadContext context, RubyArray } public static IRubyObject irNot(ThreadContext context, IRubyObject obj) { - return RubyBoolean.newBoolean(context, !(obj.isTrue())); + return asBoolean(context, !(obj.isTrue())); } @JIT @@ -2179,7 +2179,7 @@ public static RubyFixnum getArgScopeDepth(ThreadContext context, StaticScope cur currScope = currScope.getEnclosingScope(); i++; } - return context.runtime.newFixnum(i); + return asFixnum(context, i); } public static IRubyObject[] toAry(ThreadContext context, IRubyObject[] args) { diff --git a/core/src/main/java/org/jruby/ir/targets/indy/FixnumObjectSite.java b/core/src/main/java/org/jruby/ir/targets/indy/FixnumObjectSite.java index 8cd151aa8cd..349f172d802 100644 --- a/core/src/main/java/org/jruby/ir/targets/indy/FixnumObjectSite.java +++ b/core/src/main/java/org/jruby/ir/targets/indy/FixnumObjectSite.java @@ -9,6 +9,7 @@ import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.CodegenUtils.p; import static org.jruby.util.CodegenUtils.sig; @@ -35,6 +36,6 @@ public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, Metho } public IRubyObject construct(ThreadContext context) { - return context.runtime.newFixnum(value); + return asFixnum(context, value); } } diff --git a/core/src/main/java/org/jruby/ir/targets/indy/JavaBootstrap.java b/core/src/main/java/org/jruby/ir/targets/indy/JavaBootstrap.java index 0b4c937ae51..49ae9e30b63 100644 --- a/core/src/main/java/org/jruby/ir/targets/indy/JavaBootstrap.java +++ b/core/src/main/java/org/jruby/ir/targets/indy/JavaBootstrap.java @@ -275,7 +275,7 @@ public static IRubyObject floatOrNil(Ruby runtime, Double d) { } public static IRubyObject booleanOrNil(Ruby runtime, Boolean b) { - return b == null ? runtime.getNil() : RubyBoolean.newBoolean(runtime, b); + return b == null ? runtime.getNil() : runtime.newBoolean(b); } public static IRubyObject stringOrNil(Ruby runtime, CharSequence cs) { diff --git a/core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java b/core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java index 82b4ad4081f..0ccc36b5e2b 100644 --- a/core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java +++ b/core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java @@ -18,8 +18,7 @@ import org.jruby.util.ConvertBytes; import org.jruby.util.RubyStringBuilder; -import static org.jruby.api.Convert.castAsFixnum; -import static org.jruby.api.Convert.castAsRange; +import static org.jruby.api.Convert.*; import static org.jruby.api.Error.typeError; import static org.jruby.javasupport.ext.JavaLang.Character.inspectCharValue; import static org.jruby.RubyEnumerator.enumeratorizeWithSize; @@ -92,12 +91,12 @@ public IRubyObject setValue(final Ruby runtime, final int index, final IRubyObje @JRubyMethod(name = {"length", "size"}) public RubyFixnum length(ThreadContext context) { - return context.runtime.newFixnum( length() ); + return asFixnum(context, length()); } @JRubyMethod(name = "empty?") public RubyBoolean empty_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, length() == 0 ); + return asBoolean(context, length() == 0 ); } @JRubyMethod(name = "[]") @@ -129,30 +128,30 @@ public IRubyObject include_p(ThreadContext context, IRubyObject obj) { if ( componentClass.isPrimitive() ) { switch (componentClass.getName().charAt(0)) { case 'b': - if (componentClass == byte.class) return RubyBoolean.newBoolean(context, includes(context, (byte[]) array, obj) ); - else /* if (componentClass == boolean.class) */ return RubyBoolean.newBoolean(context, includes(context, (boolean[]) array, obj) ); + if (componentClass == byte.class) return asBoolean(context, includes(context, (byte[]) array, obj) ); + else /* if (componentClass == boolean.class) */ return asBoolean(context, includes(context, (boolean[]) array, obj) ); // break; case 's': - /* if (componentClass == short.class) */ return RubyBoolean.newBoolean(context, includes(context, (short[]) array, obj) ); + /* if (componentClass == short.class) */ return asBoolean(context, includes(context, (short[]) array, obj) ); // break; case 'c': - /* if (componentClass == char.class) */ return RubyBoolean.newBoolean(context, includes(context, (char[]) array, obj) ); + /* if (componentClass == char.class) */ return asBoolean(context, includes(context, (char[]) array, obj) ); // break; case 'i': - /* if (componentClass == int.class) */ return RubyBoolean.newBoolean(context, includes(context, (int[]) array, obj) ); + /* if (componentClass == int.class) */ return asBoolean(context, includes(context, (int[]) array, obj) ); // break; case 'l': - /* if (componentClass == long.class) */ return RubyBoolean.newBoolean(context, includes(context, (long[]) array, obj) ); + /* if (componentClass == long.class) */ return asBoolean(context, includes(context, (long[]) array, obj) ); // break; case 'f': - /* if (componentClass == float.class) */ return RubyBoolean.newBoolean(context, includes(context, (float[]) array, obj) ); + /* if (componentClass == float.class) */ return asBoolean(context, includes(context, (float[]) array, obj) ); // break; case 'd': - /* if (componentClass == double.class) */ return RubyBoolean.newBoolean(context, includes(context, (double[]) array, obj) ); + /* if (componentClass == double.class) */ return asBoolean(context, includes(context, (double[]) array, obj) ); // break; } } - return RubyBoolean.newBoolean(context, includes(context, (Object[]) array, obj) ); + return asBoolean(context, includes(context, (Object[]) array, obj) ); } private boolean includes(final ThreadContext context, final Object[] array, final IRubyObject obj) { @@ -268,9 +267,8 @@ private boolean includes(final ThreadContext context, final char[] array, final private boolean includes(final ThreadContext context, final boolean[] array, final IRubyObject obj) { final int len = array.length; if ( len == 0 ) return false; - final Ruby runtime = context.runtime; - if ( obj instanceof RubyBoolean ) { - final boolean objVal = ((RubyBoolean) obj).isTrue(); + if ( obj instanceof RubyBoolean bool) { + final boolean objVal = bool.isTrue(); for ( int i = 0; i < len; i++ ) { if ( objVal == array[i] ) return true; @@ -278,7 +276,7 @@ private boolean includes(final ThreadContext context, final boolean[] array, fin return false; } for ( int i = 0; i < len; i++ ) { - IRubyObject value = RubyBoolean.newBoolean(runtime, array[i]); + IRubyObject value = asBoolean(context, array[i]); if ( equalInternal(context, value, obj) ) return true; } return false; @@ -651,7 +649,7 @@ public String toString() { public RubyBoolean op_equal(ThreadContext context, IRubyObject other) { if ( other instanceof RubyArray ) { // we respond_to? to_ary thus shall handle [1].to_java == [1] - return RubyBoolean.newBoolean(context, equalsRubyArray((RubyArray) other) ); + return asBoolean(context, equalsRubyArray((RubyArray) other) ); } return eql_p(context, other); } @@ -680,7 +678,7 @@ public RubyBoolean eql_p(ThreadContext context, IRubyObject obj) { else if ( obj.getClass().isArray() ) { equals = arraysEquals(getObject(), obj); } - return RubyBoolean.newBoolean(context, equals); + return asBoolean(context, equals); } @Override diff --git a/core/src/main/java/org/jruby/java/proxies/JavaProxy.java b/core/src/main/java/org/jruby/java/proxies/JavaProxy.java index 50cf1b6f8b8..d5ef785e687 100644 --- a/core/src/main/java/org/jruby/java/proxies/JavaProxy.java +++ b/core/src/main/java/org/jruby/java/proxies/JavaProxy.java @@ -57,6 +57,7 @@ import org.jruby.util.CodegenUtils; import org.jruby.util.JRubyObjectInputStream; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Convert.castAsModule; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.Helpers.arrayOf; @@ -210,7 +211,7 @@ public static IRubyObject persistent(final IRubyObject clazz, final IRubyObject @JRubyMethod(name = "__persistent__", meta = true) public static IRubyObject persistent(final ThreadContext context, final IRubyObject clazz) { - return RubyBoolean.newBoolean(context, ((RubyClass) clazz).getRealClass().getCacheProxy()); + return asBoolean(context, ((RubyClass) clazz).getRealClass().getCacheProxy()); } @Override @@ -364,11 +365,7 @@ public static IRubyObject field_writer(ThreadContext context, IRubyObject self, @Override @JRubyMethod(name = "equal?") public IRubyObject equal_p(ThreadContext context, IRubyObject other) { - if ( other instanceof JavaProxy ) { - boolean equal = getObject() == ((JavaProxy) other).getObject(); - return RubyBoolean.newBoolean(context, equal); - } - return context.fals; + return other instanceof JavaProxy proxy ? asBoolean(context, getObject() == proxy.getObject()) : context.fals; } @JRubyMethod diff --git a/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java b/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java index 028261ed5ce..398b81bae37 100644 --- a/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java +++ b/core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java @@ -54,6 +54,8 @@ import org.jruby.util.RubyStringBuilder; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.Inspector.*; /** @@ -112,7 +114,7 @@ public int size() { @Override public RubyFixnum rb_size(ThreadContext context) { - return context.runtime.newFixnum( mapDelegate().size() ); + return asFixnum(context, mapDelegate().size()); } @Override @@ -288,7 +290,7 @@ public RubyHash rehash(ThreadContext context) { @Override public RubyBoolean compare_by_identity_p(ThreadContext context) { // NOTE: obviously little we can do to detect - but at least report Java built-in one : - return RubyBoolean.newBoolean(context, mapDelegate() instanceof java.util.IdentityHashMap ); + return asBoolean(context, mapDelegate() instanceof java.util.IdentityHashMap ); } @Override diff --git a/core/src/main/java/org/jruby/javasupport/JavaField.java b/core/src/main/java/org/jruby/javasupport/JavaField.java index 014b494a62e..cb055513571 100644 --- a/core/src/main/java/org/jruby/javasupport/JavaField.java +++ b/core/src/main/java/org/jruby/javasupport/JavaField.java @@ -46,6 +46,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asString; import static org.jruby.api.Error.typeError; @Deprecated @@ -75,22 +77,22 @@ public RubyString value_type(ThreadContext context) { @JRubyMethod(name = "public?") public RubyBoolean public_p(ThreadContext context) { - return context.runtime.newBoolean(Modifier.isPublic(field.getModifiers())); + return asBoolean(context, Modifier.isPublic(field.getModifiers())); } @JRubyMethod(name = "static?") public RubyBoolean static_p(ThreadContext context) { - return context.runtime.newBoolean(Modifier.isStatic(field.getModifiers())); + return asBoolean(context, Modifier.isStatic(field.getModifiers())); } @JRubyMethod(name = "enum_constant?") public RubyBoolean enum_constant_p(ThreadContext context) { - return context.runtime.newBoolean(field.isEnumConstant()); + return asBoolean(context, field.isEnumConstant()); } @JRubyMethod public RubyString to_generic_string(ThreadContext context) { - return context.runtime.newString(field.toGenericString()); + return asString(context, field.toGenericString()); } @JRubyMethod(name = "type") @@ -128,7 +130,7 @@ public IRubyObject set_value(ThreadContext context, IRubyObject object, IRubyObj @JRubyMethod(name = "final?") public RubyBoolean final_p(ThreadContext context) { - return context.runtime.newBoolean(Modifier.isFinal(field.getModifiers())); + return asBoolean(context, Modifier.isFinal(field.getModifiers())); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/javasupport/JavaObject.java b/core/src/main/java/org/jruby/javasupport/JavaObject.java index 2019e0ab352..4e0ced2075d 100644 --- a/core/src/main/java/org/jruby/javasupport/JavaObject.java +++ b/core/src/main/java/org/jruby/javasupport/JavaObject.java @@ -56,6 +56,7 @@ import org.jruby.util.ByteList; import org.jruby.util.JRubyObjectInputStream; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.javasupport.JavaUtil.unwrapJava; @@ -192,18 +193,19 @@ public static RubyBoolean op_equal(JavaProxy self, IRubyObject other) { } @JRubyMethod(name = "equal?") - public IRubyObject same(final IRubyObject other) { - final Ruby runtime = getRuntime(); + public IRubyObject same(ThreadContext context, final IRubyObject other) { final Object thisValue = getValue(); final Object otherValue = unwrapJava(other, NEVER); - if ( otherValue == NEVER ) { // not a wrapped object - return runtime.getFalse(); - } + if (otherValue == NEVER) return context.fals; // not a wrapped object + if (!(other instanceof JavaObject)) return context.fals; - if ( ! (other instanceof JavaObject) ) return runtime.getFalse(); + return asBoolean(context, thisValue == otherValue); + } - return runtime.newBoolean(thisValue == otherValue); + @Deprecated + public IRubyObject same(final IRubyObject other) { + return same(getCurrentContext(), other); } @JRubyMethod diff --git a/core/src/main/java/org/jruby/javasupport/JavaPackage.java b/core/src/main/java/org/jruby/javasupport/JavaPackage.java index a68bd9de706..6931191fdf8 100644 --- a/core/src/main/java/org/jruby/javasupport/JavaPackage.java +++ b/core/src/main/java/org/jruby/javasupport/JavaPackage.java @@ -46,6 +46,7 @@ import org.jruby.util.ClassProvider; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.runtime.Visibility.PRIVATE; /** @@ -118,7 +119,7 @@ public IRubyObject inspect() { @JRubyMethod(name = "===") public RubyBoolean op_eqq(ThreadContext context, IRubyObject obj) { // maybe we could handle java.lang === java.lang.reflect as well ? - return RubyBoolean.newBoolean(context, obj == this || isInstance(obj)); + return asBoolean(context, obj == this || isInstance(obj)); } @JRubyMethod(name = "const_missing") @@ -215,8 +216,8 @@ private IRubyObject respond_to(final ThreadContext context, IRubyObject mname, f */ //if ( ! (mname instanceof RubySymbol) ) mname = context.runtime.newSymbol(name); - //IRubyObject respond = Helpers.invoke(context, this, "respond_to_missing?", mname, RubyBoolean.newBoolean(context, includePrivate)); - //return RubyBoolean.newBoolean(context, respond.isTrue()); + //IRubyObject respond = Helpers.invoke(context, this, "respond_to_missing?", mname, asBoolean(context, includePrivate)); + //return asBoolean(context, respond.isTrue()); return context.nil; // NOTE: this is wrong - should be true but compatibility first, for now } @@ -244,7 +245,7 @@ public IRubyObject respond_to_missing_p(final ThreadContext context, IRubyObject } private RubyBoolean respond_to_missing(final ThreadContext context, IRubyObject mname, final boolean includePrivate) { - return RubyBoolean.newBoolean(context, BlankSlateWrapper.handlesMethod(TypeConverter.checkID(mname).idString()) == null); + return asBoolean(context, BlankSlateWrapper.handlesMethod(TypeConverter.checkID(mname).idString()) == null); } @JRubyMethod(name = "method_missing") @@ -283,14 +284,14 @@ private Package getPackage() { @JRubyMethod(name = "available?") public IRubyObject available_p(ThreadContext context) { - return RubyBoolean.newBoolean(context, isAvailable()); + return asBoolean(context, isAvailable()); } @JRubyMethod(name = "sealed?") public IRubyObject sealed_p(ThreadContext context) { final Package pkg = getPackage(); if ( pkg == null ) return context.nil; - return RubyBoolean.newBoolean(context, pkg.isSealed()); + return asBoolean(context, pkg.isSealed()); } @Override diff --git a/core/src/main/java/org/jruby/javasupport/JavaUtil.java b/core/src/main/java/org/jruby/javasupport/JavaUtil.java index 4d9f09a31aa..dc7dbe83177 100644 --- a/core/src/main/java/org/jruby/javasupport/JavaUtil.java +++ b/core/src/main/java/org/jruby/javasupport/JavaUtil.java @@ -672,13 +672,13 @@ public void set(Ruby runtime, Object array, int i, IRubyObject value) { public static final JavaConverter JAVA_BOOLEAN_CONVERTER = new JavaConverter(Boolean.class) { public IRubyObject convert(Ruby runtime, Object object) { if (object == null) return runtime.getNil(); - return RubyBoolean.newBoolean(runtime, ((Boolean)object).booleanValue()); + return runtime.newBoolean(((Boolean)object).booleanValue()); } public IRubyObject get(Ruby runtime, Object array, int i) { return convert(runtime, ((Boolean[]) array)[i]); } public void set(Ruby runtime, Object array, int i, IRubyObject value) { - ((Boolean[])array)[i] = (Boolean)value.toJava(Boolean.class); + ((Boolean[])array)[i] = value.toJava(Boolean.class); } }; @@ -776,13 +776,13 @@ public void set(Ruby runtime, Object array, int i, IRubyObject value) { public static final JavaConverter JAVA_BOOLEANPRIM_CONVERTER = new JavaConverter(boolean.class) { public IRubyObject convert(Ruby runtime, Object object) { if (object == null) return runtime.getNil(); - return RubyBoolean.newBoolean(runtime, ((Boolean)object).booleanValue()); + return runtime.newBoolean(((Boolean)object).booleanValue()); } public IRubyObject get(Ruby runtime, Object array, int i) { - return RubyBoolean.newBoolean(runtime, ((boolean[])array)[i]); + return runtime.newBoolean(((boolean[])array)[i]); } public void set(Ruby runtime, Object array, int i, IRubyObject value) { - ((boolean[])array)[i] = (Boolean)value.toJava(boolean.class); + ((boolean[])array)[i] = value.toJava(boolean.class); } }; diff --git a/core/src/main/java/org/jruby/javasupport/JavaUtilities.java b/core/src/main/java/org/jruby/javasupport/JavaUtilities.java index 593e39229b5..3493bcce667 100644 --- a/core/src/main/java/org/jruby/javasupport/JavaUtilities.java +++ b/core/src/main/java/org/jruby/javasupport/JavaUtilities.java @@ -11,6 +11,7 @@ import org.jruby.util.StringSupport; import static org.jruby.anno.FrameField.*; +import static org.jruby.api.Convert.asBoolean; @JRubyModule(name = "JavaUtilities") public class JavaUtilities { @@ -66,8 +67,7 @@ public static IRubyObject get_proxy_or_package_under_package(ThreadContext conte @JRubyMethod(name = "valid_java_identifier?", meta = true) public static IRubyObject valid_java_identifier_p(ThreadContext context, IRubyObject recv, IRubyObject name) { - final String javaName = name.convertToString().decodeString(); - return RubyBoolean.newBoolean(context, validJavaIdentifier(javaName)); + return asBoolean(context, validJavaIdentifier(name.convertToString().decodeString())); } public static boolean validJavaIdentifier(final String javaName) { diff --git a/core/src/main/java/org/jruby/javasupport/ext/JavaLang.java b/core/src/main/java/org/jruby/javasupport/ext/JavaLang.java index aa42c52462f..18623038b6a 100644 --- a/core/src/main/java/org/jruby/javasupport/ext/JavaLang.java +++ b/core/src/main/java/org/jruby/javasupport/ext/JavaLang.java @@ -56,7 +56,8 @@ import static org.jruby.RubyEnumerator.enumeratorize; import static org.jruby.RubyModule.undefinedMethodMessage; -import static org.jruby.api.Convert.castAsInteger; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.api.Error.typeError; import static org.jruby.javasupport.JavaUtil.convertJavaToUsableRubyObject; import static org.jruby.javasupport.JavaUtil.isJavaObject; @@ -128,7 +129,7 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu java.util.Iterator iterator = iterable.iterator(); final boolean twoArguments = block.getSignature().isTwoArguments(); int i = 0; while ( iterator.hasNext() ) { - final RubyInteger index = RubyFixnum.newFixnum(runtime, i++); + final RubyInteger index = asFixnum(context, i++); final Object value = iterator.next(); final IRubyObject rValue = convertJavaToUsableRubyObject(runtime, value); @@ -156,38 +157,36 @@ public static IRubyObject to_a(final ThreadContext context, final IRubyObject se @JRubyMethod(name = "count") // @override Enumerable#count public static IRubyObject count(final ThreadContext context, final IRubyObject self, final Block block) { - final Ruby runtime = context.runtime; java.lang.Iterable iterable = unwrapIfJavaObject(self); - if ( block.isGiven() ) { - return countBlock(context, iterable.iterator(), block); - } + if (block.isGiven()) return countBlock(context, iterable.iterator(), block); + if ( iterable instanceof java.util.Collection ) { - return RubyFixnum.newFixnum(runtime, ((java.util.Collection) iterable).size()); + return asFixnum(context, ((java.util.Collection) iterable).size()); } int count = 0; for( java.util.Iterator it = iterable.iterator(); it.hasNext(); ) { it.next(); count++; } - return RubyFixnum.newFixnum(runtime, count); + return asFixnum(context, count); } static RubyFixnum countBlock(final ThreadContext context, final java.util.Iterator it, final Block block) { - final Ruby runtime = context.runtime; - int count = 0; while ( it.hasNext() ) { - IRubyObject next = convertJavaToUsableRubyObject( runtime, it.next() ); + int count = 0; + while ( it.hasNext() ) { + IRubyObject next = convertJavaToUsableRubyObject(context.runtime, it.next()); if ( block.yield( context, next ).isTrue() ) count++; } - return RubyFixnum.newFixnum(runtime, count); + return asFixnum(context, count); } @JRubyMethod(name = "count") // @override Enumerable#count public static IRubyObject count(final ThreadContext context, final IRubyObject self, final IRubyObject obj, final Block unused) { // unused block due DescriptorInfo not (yet) supporting if a method receives block and an override doesn't - final Ruby runtime = context.runtime; java.lang.Iterable iterable = unwrapIfJavaObject(self); - int count = 0; for ( java.util.Iterator it = iterable.iterator(); it.hasNext(); ) { - IRubyObject next = convertJavaToUsableRubyObject( runtime, it.next() ); + int count = 0; + for ( java.util.Iterator it = iterable.iterator(); it.hasNext(); ) { + IRubyObject next = convertJavaToUsableRubyObject(context.runtime, it.next() ); if ( RubyObject.equalInternal(context, next, obj) ) count++; } - return RubyFixnum.newFixnum(runtime, count); + return asFixnum(context, count); } } @@ -209,7 +208,7 @@ public static IRubyObject cmp(final ThreadContext context, final IRubyObject sel final java.lang.Object otherComp = unwrapIfJavaObject(other); try { - return RubyFixnum.newFixnum(context.runtime, comparable.compareTo(otherComp)); + return asFixnum(context, comparable.compareTo(otherComp)); } catch (ClassCastException ex) { throw typeError(context, ex.getMessage()); } @@ -382,39 +381,37 @@ static RubyClass define(final Ruby runtime, final RubyClass proxy) { @JRubyMethod(name = "to_f") public static IRubyObject to_f(final ThreadContext context, final IRubyObject self) { - java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class); + java.lang.Number val = self.toJava(java.lang.Number.class); return context.runtime.newFloat(val.doubleValue()); } @JRubyMethod(name = "real?") public static IRubyObject real_p(final ThreadContext context, final IRubyObject self) { - java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class); - return RubyBoolean.newBoolean(context, val instanceof Integer || val instanceof Long || - val instanceof Short || val instanceof Byte || - val instanceof Float || val instanceof Double || - val instanceof java.math.BigInteger || val instanceof java.math.BigDecimal); + java.lang.Number val = self.toJava(java.lang.Number.class); + return asBoolean(context, val instanceof Integer || val instanceof Long || val instanceof Short || + val instanceof Byte || val instanceof Float || val instanceof Double || + val instanceof java.math.BigInteger || val instanceof java.math.BigDecimal); } @JRubyMethod(name = { "to_i", "to_int" }) public static IRubyObject to_i(final ThreadContext context, final IRubyObject self) { - java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class); - if (val instanceof java.math.BigInteger) { // NOTE: should be moved into its own? - return RubyBignum.newBignum(context.runtime, (java.math.BigInteger) val); + java.lang.Number val = self.toJava(java.lang.Number.class); + if (val instanceof java.math.BigInteger bigint) { // NOTE: should be moved into its own? + return RubyBignum.newBignum(context.runtime, bigint); } - return context.runtime.newFixnum(val.longValue()); + return asFixnum(context, val.longValue()); } @JRubyMethod(name = "integer?") public static IRubyObject integer_p(final ThreadContext context, final IRubyObject self) { - java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class); - return RubyBoolean.newBoolean(context, val instanceof Integer || val instanceof Long || - val instanceof Short || val instanceof Byte || - val instanceof java.math.BigInteger); + java.lang.Number val = self.toJava(java.lang.Number.class); + return asBoolean(context, val instanceof Integer || val instanceof Long || + val instanceof Short || val instanceof Byte || val instanceof java.math.BigInteger); } @JRubyMethod(name = "zero?") public static IRubyObject zero_p(final ThreadContext context, final IRubyObject self) { - return RubyBoolean.newBoolean(context, isZero(self)); + return asBoolean(context, isZero(self)); } private static boolean isZero(final IRubyObject self) { @@ -456,13 +453,13 @@ static RubyClass define(final Ruby runtime, final RubyClass proxy) { @JRubyMethod(name = "java_identifier_start?", meta = true) public static IRubyObject java_identifier_start_p(final ThreadContext context, final IRubyObject self, final IRubyObject num) { - return RubyBoolean.newBoolean(context, java.lang.Character.isJavaIdentifierStart(int_char(num)) ); + return asBoolean(context, java.lang.Character.isJavaIdentifierStart(int_char(num))); } @JRubyMethod(name = "java_identifier_part?", meta = true) public static IRubyObject java_identifier_part_p(final ThreadContext context, final IRubyObject self, final IRubyObject num) { - return RubyBoolean.newBoolean(context, java.lang.Character.isJavaIdentifierPart(int_char(num)) ); + return asBoolean(context, java.lang.Character.isJavaIdentifierPart(int_char(num))); } private static int int_char(IRubyObject num) { // str.ord -> Fixnum @@ -471,13 +468,13 @@ private static int int_char(IRubyObject num) { // str.ord -> Fixnum @JRubyMethod(name = "to_i") public static IRubyObject to_i(final ThreadContext context, final IRubyObject self) { - java.lang.Character c = (java.lang.Character) self.toJava(java.lang.Character.class); - return context.runtime.newFixnum(c); + java.lang.Character c = self.toJava(java.lang.Character.class); + return asFixnum(context, c); } @JRubyMethod(name = "inspect") public static IRubyObject inspect(final ThreadContext context, final IRubyObject self) { - java.lang.Character c = (java.lang.Character) self.toJava(java.lang.Character.class); + java.lang.Character c = self.toJava(java.lang.Character.class); return RubyString.newString(context.runtime, inspectCharValue(new java.lang.StringBuilder(3), c)); } @@ -545,13 +542,13 @@ private static java.lang.String getClassName(final IRubyObject self) { @JRubyMethod(name = "annotations?") public static IRubyObject annotations_p(final ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return RubyBoolean.newBoolean(context, klass.getAnnotations().length > 0); + return asBoolean(context, klass.getAnnotations().length > 0); } @JRubyMethod(name = "declared_annotations?") public static IRubyObject declared_annotations_p(final ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return RubyBoolean.newBoolean(context, klass.getDeclaredAnnotations().length > 0); + return asBoolean(context, klass.getDeclaredAnnotations().length > 0); } @JRubyMethod @@ -596,65 +593,94 @@ public static IRubyObject declared_class_methods(final ThreadContext context, fi @JRubyMethod(name = "<=>") // Ruby Comparable public static IRubyObject cmp(final ThreadContext context, final IRubyObject self, final IRubyObject other) { - final java.lang.Class that; - if ( isJavaObject(other) ) { - that = unwrapJavaObject(other); - } - else { - return context.nil; - } - + if (!isJavaObject(other)) return context.nil; + final java.lang.Class that = unwrapJavaObject(other); final java.lang.Class thiz = unwrapJavaObject(self); - if ( thiz == that ) return context.runtime.newFixnum(0); - if ( thiz.isAssignableFrom(that) ) return context.runtime.newFixnum(+1); - if ( that.isAssignableFrom(thiz) ) return context.runtime.newFixnum(-1); + if (thiz == that) return asFixnum(context, 0); + if (thiz.isAssignableFrom(that)) asFixnum(context, +1); + if (that.isAssignableFrom(thiz)) asFixnum(context, -1); return context.nil; } @JRubyMethod(name = "anonymous?") - public static IRubyObject anonymous_p(final IRubyObject self) { + public static IRubyObject anonymous_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return self.getRuntime().newBoolean( klass.isAnonymousClass() ); + return asBoolean(context, klass.isAnonymousClass()); + } + + @Deprecated + public static IRubyObject anonymous_p(final IRubyObject self) { + return anonymous_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "abstract?") - public static IRubyObject abstract_p(final IRubyObject self) { + public static IRubyObject abstract_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isAbstract( self, klass.getModifiers() ); + return JavaLangReflect.isAbstract(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject abstract_p(final IRubyObject self) { + return abstract_p(((RubyBasicObject) self).getCurrentContext(), self); } // JavaUtilities::ModifiedShortcuts : @JRubyMethod(name = "public?") - public static IRubyObject public_p(final IRubyObject self) { + public static IRubyObject public_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isPublic( self, klass.getModifiers() ); + return JavaLangReflect.isPublic(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject public_p(final IRubyObject self) { + return public_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "protected?") - public static IRubyObject protected_p(final IRubyObject self) { + public static IRubyObject protected_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isProtected(self, klass.getModifiers()); + return JavaLangReflect.isProtected(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject protected_p(final IRubyObject self) { + return protected_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "private?") - public static IRubyObject private_p(final IRubyObject self) { + public static IRubyObject private_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isPrivate(self, klass.getModifiers()); + return JavaLangReflect.isPrivate(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject private_p(final IRubyObject self) { + return private_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "final?") - public static IRubyObject final_p(final IRubyObject self) { + public static IRubyObject final_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isFinal(self, klass.getModifiers()); + return JavaLangReflect.isFinal(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject final_p(final IRubyObject self) { + return final_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "static?") - public static IRubyObject static_p(final IRubyObject self) { + public static IRubyObject static_p(ThreadContext context, final IRubyObject self) { final java.lang.Class klass = unwrapJavaObject(self); - return JavaLangReflect.isStatic(self, klass.getModifiers()); + return JavaLangReflect.isStatic(context, self, klass.getModifiers()); + } + + @Deprecated + public static IRubyObject static_p(final IRubyObject self) { + return static_p(((RubyBasicObject) self).getCurrentContext(), self); } // JavaClass facade (compatibility) : @@ -942,7 +968,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz final RubyInteger val = (RubyInteger) self.callMethod(context, "[]", idx); int byte_val = val.getIntValue(); if ( byte_val >= 0 ) return val; - return RubyFixnum.newFixnum(context.runtime, byte_val + 256); // byte += 256 if byte < 0 + return asFixnum(context, byte_val + 256); // byte += 256 if byte < 0 } } @@ -956,7 +982,7 @@ private static final class UByteSet extends JavaMethod.JavaMethodTwo { public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, java.lang.String name, IRubyObject idx, IRubyObject val) { int byte_val = ((RubyInteger) val).getIntValue(); if ( byte_val > 127 ) { - val = RubyFixnum.newFixnum(context.runtime, byte_val - 256); // value -= 256 if value > 127 + val = asFixnum(context, byte_val - 256); // value -= 256 if value > 127 } return self.callMethod(context, "[]=", new IRubyObject[] { idx, val }); } diff --git a/core/src/main/java/org/jruby/javasupport/ext/JavaLangReflect.java b/core/src/main/java/org/jruby/javasupport/ext/JavaLangReflect.java index 7673c3d9a3d..cfe5bb4344d 100644 --- a/core/src/main/java/org/jruby/javasupport/ext/JavaLangReflect.java +++ b/core/src/main/java/org/jruby/javasupport/ext/JavaLangReflect.java @@ -40,6 +40,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.javasupport.JavaUtil.convertArguments; import static org.jruby.javasupport.JavaUtil.convertJavaToUsableRubyObject; import static org.jruby.util.Inspector.GT; @@ -120,33 +121,58 @@ public static IRubyObject new_instance(final ThreadContext context, final IRubyO // JavaUtilities::ModifiedShortcuts : @JRubyMethod(name = "public?") - public static IRubyObject public_p(final IRubyObject self) { + public static IRubyObject public_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Constructor thiz = JavaUtil.unwrapJavaObject(self); - return isPublic(self, thiz.getModifiers()); + return isPublic(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject public_p(final IRubyObject self) { + return public_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "protected?") - public static IRubyObject protected_p(final IRubyObject self) { + public static IRubyObject protected_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Constructor thiz = JavaUtil.unwrapJavaObject(self); - return isProtected(self, thiz.getModifiers()); + return isProtected(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject protected_p(final IRubyObject self) { + return protected_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "private?") - public static IRubyObject private_p(final IRubyObject self) { + public static IRubyObject private_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Constructor thiz = JavaUtil.unwrapJavaObject(self); - return isPrivate(self, thiz.getModifiers()); + return isPrivate(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject private_p(final IRubyObject self) { + return private_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "final?") - public static IRubyObject final_p(final IRubyObject self) { + public static IRubyObject final_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Constructor thiz = JavaUtil.unwrapJavaObject(self); - return isFinal(self, thiz.getModifiers()); + return isFinal(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject final_p(final IRubyObject self) { + return final_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "static?") - public static IRubyObject static_p(final IRubyObject self) { + public static IRubyObject static_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Constructor thiz = JavaUtil.unwrapJavaObject(self); - return isStatic(self, thiz.getModifiers()); + return isStatic(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject static_p(final IRubyObject self) { + return static_p(((RubyBasicObject) self).getCurrentContext(), self); } } @@ -209,41 +235,70 @@ public static IRubyObject invoke_static(final ThreadContext context, final IRuby // @JRubyMethod(name = "abstract?") - public static IRubyObject abstract_p(final IRubyObject self) { + public static IRubyObject abstract_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isAbstract(self, thiz.getModifiers()); + return isAbstract(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject abstract_p(final IRubyObject self) { + return abstract_p(((RubyBasicObject) self).getCurrentContext(), self); } // JavaUtilities::ModifiedShortcuts : @JRubyMethod(name = "public?") - public static IRubyObject public_p(final IRubyObject self) { + public static IRubyObject public_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Method thiz = JavaUtil.unwrapJavaObject(self); - return isPublic(self, thiz.getModifiers()); + return isPublic(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject public_p(final IRubyObject self) { + return public_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "protected?") - public static IRubyObject protected_p(final IRubyObject self) { + public static IRubyObject protected_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Method thiz = JavaUtil.unwrapJavaObject(self); - return isProtected(self, thiz.getModifiers()); + return isProtected(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject protected_p(final IRubyObject self) { + return protected_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "private?") - public static IRubyObject private_p(final IRubyObject self) { + public static IRubyObject private_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Method thiz = JavaUtil.unwrapJavaObject(self); - return isPrivate(self, thiz.getModifiers()); + return isPrivate(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject private_p(final IRubyObject self) { + return private_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "final?") - public static IRubyObject final_p(final IRubyObject self) { + public static IRubyObject final_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Method thiz = JavaUtil.unwrapJavaObject(self); - return isFinal(self, thiz.getModifiers()); + return isFinal(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject final_p(final IRubyObject self) { + return final_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "static?") - public static IRubyObject static_p(final IRubyObject self) { + public static IRubyObject static_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Method thiz = JavaUtil.unwrapJavaObject(self); - return isStatic(self, thiz.getModifiers()); + return isStatic(context, self, thiz.getModifiers()); + } + + public static IRubyObject static_p(final IRubyObject self) { + return static_p(((RubyBasicObject) self).getCurrentContext(), self); } } @@ -318,33 +373,59 @@ public static IRubyObject set_static_value(final ThreadContext context, final IR // JavaUtilities::ModifiedShortcuts : @JRubyMethod(name = "public?") - public static IRubyObject public_p(final IRubyObject self) { + public static IRubyObject public_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isPublic(self, thiz.getModifiers()); + return isPublic(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject public_p(final IRubyObject self) { + return public_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "protected?") - public static IRubyObject protected_p(final IRubyObject self) { + public static IRubyObject protected_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isProtected(self, thiz.getModifiers()); + return isProtected(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject protected_p(final IRubyObject self) { + return protected_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "private?") - public static IRubyObject private_p(final IRubyObject self) { + public static IRubyObject private_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isPrivate(self, thiz.getModifiers()); + return isPrivate(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject private_p(final IRubyObject self) { + return private_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "final?") - public static IRubyObject final_p(final IRubyObject self) { + public static IRubyObject final_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isFinal(self, thiz.getModifiers()); + return isFinal(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject final_p(final IRubyObject self) { + + return final_p(((RubyBasicObject) self).getCurrentContext(), self); } @JRubyMethod(name = "static?") - public static IRubyObject static_p(final IRubyObject self) { + public static IRubyObject static_p(ThreadContext context, final IRubyObject self) { final java.lang.reflect.Field thiz = JavaUtil.unwrapJavaObject(self); - return isStatic(self, thiz.getModifiers()); + return isStatic(context, self, thiz.getModifiers()); + } + + @Deprecated + public static IRubyObject static_p(final IRubyObject self) { + return static_p(((RubyBasicObject) self).getCurrentContext(), self); } } @@ -360,28 +441,28 @@ private static Object convertValueToJava(final java.lang.reflect.Field field, IR return value.toJava(field.getType()); } - static RubyBoolean isAbstract(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isAbstract(mod)); + static RubyBoolean isAbstract(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isAbstract(mod)); } - static RubyBoolean isPublic(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isPublic(mod)); + static RubyBoolean isPublic(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isPublic(mod)); } - static RubyBoolean isProtected(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isProtected(mod)); + static RubyBoolean isProtected(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isProtected(mod)); } - static RubyBoolean isPrivate(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isPrivate(mod)); + static RubyBoolean isPrivate(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isPrivate(mod)); } - static RubyBoolean isFinal(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isFinal(mod)); + static RubyBoolean isFinal(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isFinal(mod)); } - static RubyBoolean isStatic(final IRubyObject self, final int mod) { - return self.getRuntime().newBoolean(java.lang.reflect.Modifier.isStatic(mod)); + static RubyBoolean isStatic(ThreadContext context, final IRubyObject self, final int mod) { + return asBoolean(context, java.lang.reflect.Modifier.isStatic(mod)); } } diff --git a/core/src/main/java/org/jruby/javasupport/ext/JavaNio.java b/core/src/main/java/org/jruby/javasupport/ext/JavaNio.java index 9ee2c9f86a5..93d1ee398ef 100644 --- a/core/src/main/java/org/jruby/javasupport/ext/JavaNio.java +++ b/core/src/main/java/org/jruby/javasupport/ext/JavaNio.java @@ -39,6 +39,7 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.RubyStringBuilder; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.javasupport.JavaUtil.unwrapIfJavaObject; import static org.jruby.runtime.Visibility.PUBLIC; import static org.jruby.util.Inspector.GT; @@ -70,7 +71,7 @@ static RubyModule define(final Ruby runtime, final RubyClass proxy) { @JRubyMethod(name = {"length", "size"}) public static IRubyObject length(final ThreadContext context, final IRubyObject self) { java.nio.Buffer obj = self.toJava(java.nio.Buffer.class); - return context.runtime.newFixnum(obj.remaining()); // limit - position + return asFixnum(context, obj.remaining()); // limit - position } } diff --git a/core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java b/core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java index 255967f6931..1c9b4ab94e3 100644 --- a/core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java +++ b/core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java @@ -45,6 +45,7 @@ import java.lang.reflect.InvocationTargetException; import static org.jruby.RubyEnumerator.enumeratorize; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.javasupport.JavaUtil.convertJavaArrayToRuby; import static org.jruby.javasupport.JavaUtil.convertJavaToUsableRubyObject; import static org.jruby.javasupport.JavaUtil.inspectObject; @@ -146,7 +147,7 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu @JRubyMethod(name = { "include?", "member?" }) // @override Enumerable#include? public static RubyBoolean include_p(final ThreadContext context, final IRubyObject self, final IRubyObject obj) { final java.util.Collection coll = unwrapIfJavaObject(self); - return RubyBoolean.newBoolean(context, coll.contains( obj.toJava(java.lang.Object.class) ) ); + return asBoolean(context, coll.contains(obj.toJava(java.lang.Object.class))); } // NOTE: first might conflict with some Java types (e.g. java.util.Deque) thus providing a ruby_ alias diff --git a/core/src/main/java/org/jruby/javasupport/ext/JavaUtilRegex.java b/core/src/main/java/org/jruby/javasupport/ext/JavaUtilRegex.java index 2aafc1681c6..4773b2608e4 100644 --- a/core/src/main/java/org/jruby/javasupport/ext/JavaUtilRegex.java +++ b/core/src/main/java/org/jruby/javasupport/ext/JavaUtilRegex.java @@ -35,6 +35,8 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.RubyStringBuilder; +import static org.jruby.api.Convert.asBoolean; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.javasupport.JavaUtil.convertJavaToUsableRubyObject; import static org.jruby.javasupport.JavaUtil.unwrapJavaObject; import static org.jruby.util.Inspector.*; @@ -62,7 +64,7 @@ static RubyClass define(final Ruby runtime, final RubyClass proxy) { @JRubyMethod(name = "=~") public static IRubyObject op_match(final ThreadContext context, final IRubyObject self, IRubyObject str) { final java.util.regex.Matcher matcher = matcher(self, str); - return matcher.find() ? context.runtime.newFixnum(matcher.start()) : context.nil; + return matcher.find() ? asFixnum(context, matcher.start()) : context.nil; } @JRubyMethod(name = "match") @@ -76,14 +78,13 @@ public static IRubyObject match(final ThreadContext context, final IRubyObject s @JRubyMethod(name = "===") public static IRubyObject eqq(final ThreadContext context, final IRubyObject self, IRubyObject str) { - return RubyBoolean.newBoolean(context, matcher(self, str).find() ); + return asBoolean(context, matcher(self, str).find() ); } @JRubyMethod(name = "casefold?") public static IRubyObject casefold_p(final ThreadContext context, final IRubyObject self) { final java.util.regex.Pattern regex = unwrapJavaObject(self); - boolean i = ( regex.flags() & java.util.regex.Pattern.CASE_INSENSITIVE ) != 0; - return RubyBoolean.newBoolean(context, i); + return asBoolean(context, (regex.flags() & java.util.regex.Pattern.CASE_INSENSITIVE) != 0); } @JRubyMethod(name = "inspect") @@ -123,35 +124,34 @@ public static IRubyObject regexp(final ThreadContext context, final IRubyObject @JRubyMethod public static IRubyObject begin(final ThreadContext context, final IRubyObject self, final IRubyObject idx) { final java.util.regex.Matcher matcher = unwrapJavaObject(self); - if ( idx instanceof RubySymbol ) { - return context.runtime.newFixnum( matcher.start(idx.toString()) ); - } + if (idx instanceof RubySymbol) return asFixnum(context, matcher.start(idx.toString())); + final int group = idx.convertToInteger().getIntValue(); - return context.runtime.newFixnum( matcher.start(group) ); + return asFixnum(context, matcher.start(group)); } @JRubyMethod public static IRubyObject end(final ThreadContext context, final IRubyObject self, final IRubyObject idx) { final java.util.regex.Matcher matcher = unwrapJavaObject(self); - if ( idx instanceof RubySymbol ) { - return context.runtime.newFixnum( matcher.end(idx.toString()) ); - } + if (idx instanceof RubySymbol) return asFixnum(context, matcher.end(idx.toString())); + final int group = idx.convertToInteger().getIntValue(); - return context.runtime.newFixnum(matcher.end(group)); + return asFixnum(context, matcher.end(group)); } @JRubyMethod public static IRubyObject offset(final ThreadContext context, final IRubyObject self, final IRubyObject idx) { final java.util.regex.Matcher matcher = unwrapJavaObject(self); - final IRubyObject beg; final IRubyObject end; - if ( idx instanceof RubySymbol ) { - beg = context.runtime.newFixnum( matcher.start(idx.toString()) ); - end = context.runtime.newFixnum( matcher.end(idx.toString()) ); - } - else { + final IRubyObject beg; + final IRubyObject end; + + if (idx instanceof RubySymbol) { + beg = asFixnum(context, matcher.start(idx.toString())); + end = asFixnum(context, matcher.end(idx.toString())); + } else { final int group = idx.convertToInteger().getIntValue(); - beg = context.runtime.newFixnum( matcher.start(group) ); - end = context.runtime.newFixnum( matcher.end(group) ); + beg = asFixnum(context, matcher.start(group)); + end = asFixnum(context, matcher.end(group)); } return RubyArray.newArray(context.runtime, beg, end); } @@ -159,7 +159,7 @@ public static IRubyObject offset(final ThreadContext context, final IRubyObject @JRubyMethod(name = { "length", "size" }) public static RubyFixnum size(final ThreadContext context, final IRubyObject self) { final java.util.regex.Matcher matcher = unwrapJavaObject(self); - return context.runtime.newFixnum(matcher.groupCount() + 1); // the Ruby way! + return asFixnum(context, matcher.groupCount() + 1); // the Ruby way! } @JRubyMethod diff --git a/core/src/main/java/org/jruby/javasupport/proxy/JavaProxyReflectionObject.java b/core/src/main/java/org/jruby/javasupport/proxy/JavaProxyReflectionObject.java index 0649c7ad5ba..0d9965e69ea 100644 --- a/core/src/main/java/org/jruby/javasupport/proxy/JavaProxyReflectionObject.java +++ b/core/src/main/java/org/jruby/javasupport/proxy/JavaProxyReflectionObject.java @@ -40,6 +40,7 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; public class JavaProxyReflectionObject extends RubyObject { @@ -68,7 +69,7 @@ public RubyBoolean op_eqq(final ThreadContext context, IRubyObject obj) { } obj = (IRubyObject) wrappedObj; } - return RubyBoolean.newBoolean(context, this.equals(obj) ); + return asBoolean(context, this.equals(obj)); } @Deprecated @@ -88,7 +89,7 @@ public RubyBoolean op_equal(final ThreadContext context, IRubyObject obj) { } obj = (IRubyObject) wrappedObj; } - return RubyBoolean.newBoolean(context, this == obj); + return asBoolean(context, this == obj); } @Override diff --git a/core/src/main/java/org/jruby/runtime/Helpers.java b/core/src/main/java/org/jruby/runtime/Helpers.java index acbfc164759..d95055d2e1b 100644 --- a/core/src/main/java/org/jruby/runtime/Helpers.java +++ b/core/src/main/java/org/jruby/runtime/Helpers.java @@ -81,6 +81,7 @@ import org.jcodings.unicode.UnicodeEncoding; import static org.jruby.RubyBasicObject.getMetaClass; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; import static org.jruby.runtime.ThreadContext.CALL_KEYWORD_EMPTY; import static org.jruby.runtime.Visibility.*; @@ -2394,8 +2395,7 @@ public static Block getBlock(Ruby runtime, ThreadContext context, IRubyObject se */ public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObject b) { if (a == b) return context.tru; - IRubyObject res = sites(context).op_equal.call(context, a, a, b); - return RubyBoolean.newBoolean(context, res.isTrue()); + return asBoolean(context, sites(context).op_equal.call(context, a, a, b).isTrue()); } /** @@ -2408,8 +2408,7 @@ public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObj */ public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObject b, CallSite equal) { if (a == b) return context.tru; - IRubyObject res = equal.call(context, a, a, b); - return RubyBoolean.newBoolean(context, res.isTrue()); + return asBoolean(context, equal.call(context, a, a, b).isTrue()); } /** @@ -2422,8 +2421,7 @@ public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObj */ public static RubyBoolean rbEql(ThreadContext context, IRubyObject a, IRubyObject b) { if (a == b) return context.tru; - IRubyObject res = invokedynamic(context, a, EQL, b); - return RubyBoolean.newBoolean(context, res.isTrue()); + return asBoolean(context, invokedynamic(context, a, EQL, b).isTrue()); } /** diff --git a/core/src/main/java/org/jruby/runtime/backtrace/TraceType.java b/core/src/main/java/org/jruby/runtime/backtrace/TraceType.java index fc5819a0810..383702eb7a0 100644 --- a/core/src/main/java/org/jruby/runtime/backtrace/TraceType.java +++ b/core/src/main/java/org/jruby/runtime/backtrace/TraceType.java @@ -24,6 +24,7 @@ import org.jruby.util.log.LoggerFactory; import org.jruby.util.TypeConverter; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.util.RubyStringBuilder.str; public class TraceType { @@ -397,7 +398,7 @@ private static IRubyObject checkHighlightKeyword(ThreadContext context, IRubyObj } if (highlightArg.isNil()) { - highlightArg = RubyBoolean.newBoolean(context, autoTTYDetect && RubyException.to_tty_p(context, runtime.getException()).isTrue()); + highlightArg = asBoolean(context, autoTTYDetect && RubyException.to_tty_p(context, runtime.getException()).isTrue()); } return highlightArg; diff --git a/core/src/main/java/org/jruby/runtime/callsite/RespondToCallSite.java b/core/src/main/java/org/jruby/runtime/callsite/RespondToCallSite.java index 81eca06c75b..d64527c7c56 100644 --- a/core/src/main/java/org/jruby/runtime/callsite/RespondToCallSite.java +++ b/core/src/main/java/org/jruby/runtime/callsite/RespondToCallSite.java @@ -10,6 +10,7 @@ import org.jruby.util.TypeConverter; import static org.jruby.RubyBasicObject.getMetaClass; +import static org.jruby.api.Convert.asBoolean; public class RespondToCallSite extends MonomorphicCallSite { private volatile RespondToTuple respondToTuple = RespondToTuple.NULL_CACHE; @@ -101,7 +102,7 @@ public boolean respondsTo(ThreadContext context, IRubyObject caller, IRubyObject if (strName.equals(tuple.name) && !includePrivate == tuple.checkVisibility) return tuple.respondsToBoolean; } // go through normal call logic, which will hit overridden cacheAndCall - return super.call(context, caller, self, getRespondToNameSym(context), RubyBoolean.newBoolean(context, includePrivate)).isTrue(); + return super.call(context, caller, self, getRespondToNameSym(context), asBoolean(context, includePrivate)).isTrue(); } private RubySymbol getRespondToNameSym(ThreadContext context) { @@ -171,6 +172,6 @@ private static RespondToTuple recacheRespondsTo(CacheEntry respondToMethod, Stri CacheEntry respondToLookupResult = klass.searchWithCache(newString); boolean respondsTo = Helpers.respondsToMethod(respondToLookupResult.method, checkVisibility); - return new RespondToTuple(newString, checkVisibility, respondToMethod, respondToLookupResult, RubyBoolean.newBoolean(context, respondsTo)); + return new RespondToTuple(newString, checkVisibility, respondToMethod, respondToLookupResult, asBoolean(context, respondsTo)); } } \ No newline at end of file diff --git a/core/src/main/java/org/jruby/runtime/invokedynamic/MathLinker.java b/core/src/main/java/org/jruby/runtime/invokedynamic/MathLinker.java index a96660fe23c..33210a660cd 100644 --- a/core/src/main/java/org/jruby/runtime/invokedynamic/MathLinker.java +++ b/core/src/main/java/org/jruby/runtime/invokedynamic/MathLinker.java @@ -34,6 +34,7 @@ import static java.lang.invoke.MethodHandles.*; import static java.lang.invoke.MethodType.*; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.CodegenUtils.p; import com.headius.invokebinder.Binder; @@ -136,7 +137,7 @@ public static IRubyObject fixnumOperator(ThreadContext context, IRubyObject call SwitchPoint switchPoint = (SwitchPoint) classFixnum.getInvalidator().getData(); MethodHandle fallback = Binder.from(site.type()) - .append(IRubyObject.class, runtime.newFixnum(value)) + .append(IRubyObject.class, asFixnum(context, value)) .invoke(normalSite.dynamicInvoker()); CacheEntry entry = searchWithCache(operator, caller, self.getMetaClass(), site); diff --git a/core/src/main/java/org/jruby/util/Numeric.java b/core/src/main/java/org/jruby/util/Numeric.java index a137badd9fa..235c34de90b 100644 --- a/core/src/main/java/org/jruby/util/Numeric.java +++ b/core/src/main/java/org/jruby/util/Numeric.java @@ -47,6 +47,7 @@ import java.math.BigInteger; +import static org.jruby.api.Convert.asBoolean; import static org.jruby.api.Error.typeError; public class Numeric { @@ -430,12 +431,12 @@ public static IRubyObject f_truncate(ThreadContext context, IRubyObject x) { * return non-boolean (which unless it is nil it will be isTrue()). * */ - public static IRubyObject f_equal(ThreadContext context, IRubyObject x, IRubyObject y) { - if (x instanceof RubyFixnum && y instanceof RubyFixnum) { - return RubyBoolean.newBoolean(context, ((RubyFixnum) x).getLongValue() == ((RubyFixnum) y).getLongValue()); + public static IRubyObject f_equal(ThreadContext context, IRubyObject a, IRubyObject b) { + if (a instanceof RubyFixnum x && b instanceof RubyFixnum y) { + return asBoolean(context, x.getLongValue() == y.getLongValue()); } - return sites(context).op_equals.call(context, x, x, y); + return sites(context).op_equals.call(context, a, a, b); } public static IRubyObject f_equal(ThreadContext context, RubyInteger x, RubyInteger y) { diff --git a/core/src/main/java/org/jruby/util/collections/StringArraySet.java b/core/src/main/java/org/jruby/util/collections/StringArraySet.java index b40e08164fe..a0639aed0d2 100644 --- a/core/src/main/java/org/jruby/util/collections/StringArraySet.java +++ b/core/src/main/java/org/jruby/util/collections/StringArraySet.java @@ -39,6 +39,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; +import static org.jruby.api.Convert.asBoolean; + /** * An RubyArray that maintains an O(1) Set for fast include? operations. */ @@ -97,7 +99,7 @@ public synchronized IRubyObject delete_if(ThreadContext context, Block block) { @Override public final RubyBoolean include_p(ThreadContext context, IRubyObject item) { - return RubyBoolean.newBoolean(context, containsString(convertToString(item))); + return asBoolean(context, containsString(convertToString(item))); } @Override diff --git a/core/src/main/java/org/jruby/util/io/EncodingUtils.java b/core/src/main/java/org/jruby/util/io/EncodingUtils.java index ee5b1f23946..ddbc1fb6d5a 100644 --- a/core/src/main/java/org/jruby/util/io/EncodingUtils.java +++ b/core/src/main/java/org/jruby/util/io/EncodingUtils.java @@ -58,7 +58,7 @@ import static org.jruby.RubyString.*; import static org.jruby.api.Convert.checkToInteger; -import static org.jruby.api.Convert.integerAsInt; +import static org.jruby.api.Convert.asInt; import static org.jruby.api.Error.typeError; import static org.jruby.util.StringSupport.CR_UNKNOWN; import static org.jruby.util.StringSupport.searchNonAscii; @@ -210,7 +210,7 @@ public static void extractModeEncoding(ThreadContext context, if (!intmode.isNil()) { vmode(vmodeAndVperm_p, intmode); - oflags_p[0] = integerAsInt(context, (RubyInteger) intmode); + oflags_p[0] = asInt(context, (RubyInteger) intmode); fmode_p[0] = ModeFlags.getOpenFileFlagsFor(oflags_p[0]); } else { String p = vmode(vmodeAndVperm_p).convertToString().asJavaString(); diff --git a/core/src/main/java/org/jruby/util/io/OpenFile.java b/core/src/main/java/org/jruby/util/io/OpenFile.java index a2ef5703f0f..93b90cdba6b 100644 --- a/core/src/main/java/org/jruby/util/io/OpenFile.java +++ b/core/src/main/java/org/jruby/util/io/OpenFile.java @@ -49,6 +49,7 @@ import org.jruby.util.StringSupport; import org.jruby.util.cli.Options; +import static org.jruby.api.Convert.asFixnum; import static org.jruby.util.StringSupport.*; public class OpenFile implements Finalizable { @@ -2608,7 +2609,7 @@ IRubyObject finishWriteconv(ThreadContext context, boolean noalloc) { while (res == EConvResult.DestinationBufferFull) { if (wbuf.len == wbuf.capa) { if (io_fflush(context) < 0) - return noalloc ? context.tru : runtime.newFixnum(posix.getErrno() == null ? 0 : posix.getErrno().longValue()); + return noalloc ? context.tru : asFixnum(context, posix.getErrno() == null ? 0 : posix.getErrno().longValue()); } dsBytes = wbuf.ptr; diff --git a/core/src/main/java/org/jruby/util/io/PopenExecutor.java b/core/src/main/java/org/jruby/util/io/PopenExecutor.java index 44fe091b9c0..5fc4bd7d4b1 100644 --- a/core/src/main/java/org/jruby/util/io/PopenExecutor.java +++ b/core/src/main/java/org/jruby/util/io/PopenExecutor.java @@ -44,6 +44,7 @@ import java.util.Set; import static org.jruby.RubyString.newString; +import static org.jruby.api.Convert.asFixnum; /** * Port of MRI's popen+exec logic. @@ -100,7 +101,7 @@ public static RubyFixnum spawn(ThreadContext context, IRubyObject[] argv) { } throw runtime.newErrnoFromErrno(executor.errno, errmsg[0]); } - return runtime.newFixnum(pid); + return asFixnum(context, pid); } // MRI: rb_f_system diff --git a/core/src/test/java/org/jruby/test/TestRubyRational.java b/core/src/test/java/org/jruby/test/TestRubyRational.java index 89716231ef3..fc81c7824eb 100644 --- a/core/src/test/java/org/jruby/test/TestRubyRational.java +++ b/core/src/test/java/org/jruby/test/TestRubyRational.java @@ -31,8 +31,10 @@ import org.jruby.RubyFixnum; import org.jruby.RubyFloat; import org.jruby.RubyRational; +import org.jruby.runtime.ThreadContext; import org.junit.Test; +import static org.jruby.api.Convert.asFixnum; import static org.junit.Assert.*; public class TestRubyRational extends junit.framework.TestCase { @@ -74,7 +76,8 @@ public void testConvertToInteger() { } private RubyRational newRational(final long num, final long den) { - return (RubyRational) RubyRational.newInstance(runtime.getCurrentContext(), runtime.newFixnum(num), runtime.newFixnum(den)); + ThreadContext context = runtime.getCurrentContext(); + return (RubyRational) RubyRational.newInstance(context, asFixnum(context, num), asFixnum(context, den)); } }