Skip to content

Commit

Permalink
Also follow IR kwarg logic in these methods
Browse files Browse the repository at this point in the history
* File.read
* IO.open

See jruby#8398.
  • Loading branch information
headius committed Jan 9, 2025
1 parent 4c00e56 commit e2e1ba8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.NotImplementedError;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.runtime.*;
import org.jruby.runtime.JavaSites.FileSites;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -344,16 +345,20 @@ public IRubyObject flock(ThreadContext context, IRubyObject operation) {
// rb_file_initialize
@JRubyMethod(name = "initialize", required = 1, optional = 3, checkArity = false, visibility = PRIVATE, keywords = true)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block) {
boolean keywords = hasKeywords(ThreadContext.resetCallInfo(context));
// capture callInfo for delegating to IO#initialize
int callInfo = context.callInfo;
IRubyObject keywords = IRRuntimeHelpers.receiveKeywords(context, args, false, true, false);
// Mild hack. We want to arity-mismatch if extra arg is not really a kwarg but not if it is one.
int maxArgs = keywords ? 4 : 3;
int maxArgs = keywords instanceof RubyHash ? 4 : 3;
int argc = Arity.checkArgumentCount(context, args, 1, maxArgs);

if (openFile != null) throw context.runtime.newRuntimeError("reinitializing File");

if (argc > 0 && argc <= 3) {
IRubyObject fd = TypeConverter.convertToTypeWithCheck(context, args[0], context.runtime.getFixnum(), sites(context).to_int_checked);
if (!fd.isNil()) {
// restore callInfo for delegated call to IO#initialize
IRRuntimeHelpers.setCallInfo(context, callInfo);
if (argc == 1) {
return super.initialize(context, fd, block);
} else if (argc == 2) {
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4229,7 +4229,7 @@ public static IRubyObject binread(ThreadContext context, IRubyObject recv, IRuby
// Enebo: annotation processing forced me to do pangea method here...
@JRubyMethod(name = "read", meta = true, required = 1, optional = 3, checkArity = false, keywords = true)
public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
boolean keywords = hasKeywords(ThreadContext.resetCallInfo(context));
IRubyObject keywords = IRRuntimeHelpers.receiveKeywords(context, args, false, true, false);
int argc = Arity.checkArgumentCount(context, args, 1, 4);

Ruby runtime = context.runtime;
Expand All @@ -4239,20 +4239,20 @@ public static IRubyObject read(ThreadContext context, IRubyObject recv, IRubyObj

{ // rb_scan_args logic, basically
if (argc > 3) {
if (!keywords) throw runtime.newArgumentError(args.length, 1, 4);
options = (RubyHash) args[3];
if (!(keywords instanceof RubyHash)) throw runtime.newArgumentError(args.length, 1, 4);
options = args[3];
offset = args[2];
length = args[1];
} else if (argc > 2) {
if (args[2] instanceof RubyHash) {
options = (RubyHash) args[2];
if (keywords instanceof RubyHash) {
options = keywords;
} else {
offset = args[2];
}
length = args[1];
} else if (argc > 1) {
if (args[1] instanceof RubyHash) {
options = (RubyHash) args[1];
if (keywords instanceof RubyHash) {
options = keywords;
} else {
length = args[1];
}
Expand Down

0 comments on commit e2e1ba8

Please sign in to comment.