Skip to content

Commit

Permalink
Merge pull request jruby#8562 from headius/r2k_kwargs_io_write
Browse files Browse the repository at this point in the history
Use same logic as IR for kwarg handling in IO#write
  • Loading branch information
headius authored Jan 11, 2025
2 parents 42197f4 + e2e1ba8 commit 01d88c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 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
28 changes: 16 additions & 12 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.fcntl.FcntlLibrary;
import org.jruby.internal.runtime.ThreadedRunnable;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.platform.Platform;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
Expand Down Expand Up @@ -4228,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 @@ -4238,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 Expand Up @@ -4292,21 +4293,24 @@ public static IRubyObject write(ThreadContext context, IRubyObject recv, IRubyOb

// MRI: io_s_write
public static IRubyObject ioStaticWrite(ThreadContext context, IRubyObject recv, IRubyObject[] argv, boolean binary) {
boolean keywords = hasKeywords(ThreadContext.resetCallInfo(context));
IRubyObject keywords = IRRuntimeHelpers.receiveKeywords(context, argv, false, true, false);
final Ruby runtime = context.runtime;
IRubyObject string, offset, opt;
string = offset = opt = context.nil;

switch (argv.length) {
case 4:
if (!keywords) throw runtime.newArgumentError(argv.length, 2, 3);
opt = argv[3].convertToHash();
if (!(keywords instanceof RubyHash)) throw runtime.newArgumentError(argv.length, 2, 3);
opt = keywords;
offset = argv[2];
string = argv[1];
break;
case 3:
opt = TypeConverter.checkHashType(runtime, argv[2]);
if (opt.isNil()) offset = argv[2];
if (keywords instanceof RubyHash) {
opt = keywords;
} else {
offset = argv[2];
}
string = argv[1];
break;
case 2:
Expand Down

0 comments on commit 01d88c3

Please sign in to comment.