Skip to content

Commit

Permalink
New asFixnum, asBoolean, asString in Convert
Browse files Browse the repository at this point in the history
Note: asString overlaps with a method on IRubyObject so this may be changing
its name.

These are the first methods to replace Ruby.newFixnum sorts of methods.
Decoupling this is good for reasons:

  1. Ruby is some mega method holder and it has long lost favor as an
     object vs ThreadContext.
  2. Passing ThreadContext to all JRuby methods (long term goal) means
     we increasingly do not need Ruby in Ruby objects at all (very long
     term goal).
  3. Using static methods decouples our implementation from our API

This round did not completely eliminate all uses of the three methods but
it converted hundreds of them.  The biggest disruption in this PR is that
a lot of @JRubyMethods which did not have ThreadContext now has them.  This
meant keeping the old method around but marking it deprecated.  The fact we
need to do this is a prime reason why the API initiave will pay off since
there will come a time when we do not need to worry about people consuming
our methods.  The other side of this is our API will also be exposed properly
once we fully modularize (which will mean we long convert people to these
methods before we get to point where we cut off access to our other public
methods -- which is out impl).
  • Loading branch information
enebo committed Jul 16, 2024
1 parent 8de5b35 commit 9725480
Show file tree
Hide file tree
Showing 127 changed files with 2,077 additions and 1,423 deletions.
18 changes: 13 additions & 5 deletions core/src/main/java/org/jruby/AbstractRubyMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/BasicObjectStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 9 additions & 8 deletions core/src/main/java/org/jruby/FiberScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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=")
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
16 changes: 6 additions & 10 deletions core/src/main/java/org/jruby/RubyArithmeticSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down
Loading

0 comments on commit 9725480

Please sign in to comment.