Skip to content

Commit 0215ef9

Browse files
committed
[fix] proper call-info reset/restoration
1 parent df5d33b commit 0215ef9

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

core/src/main/java/org/jruby/RubyArgsFile.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,7 @@ public static IRubyObject external_encoding(ThreadContext context, IRubyObject r
388388
}
389389

390390
// MRI: argf_getline
391-
private static IRubyObject argf_getline(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
392-
final int callInfo = ThreadContext.resetCallInfo(context);
391+
private static IRubyObject argf_getline(ThreadContext context, final int callInfo, IRubyObject[] args) {
393392
IRubyObject line;
394393
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
395394

@@ -399,8 +398,10 @@ private static IRubyObject argf_getline(ThreadContext context, IRubyObject recv,
399398
RubyIO currentFile = (RubyIO) data.currentFile;
400399

401400
if (isGenericInput(context, data)) {
401+
final int prevCallInfo = context.callInfo;
402402
context.callInfo = callInfo; // restore callInfo for kwargs
403403
line = data.currentFile.callMethod(context, "gets", args);
404+
context.callInfo = prevCallInfo;
404405
} else {
405406
if (args.length == 0 && context.runtime.getRecordSeparatorVar().get() == globalVariables(context).getDefaultSeparator()) {
406407
line = (currentFile).gets(context);
@@ -433,9 +434,10 @@ private static boolean isGenericInput(ThreadContext context, ArgsFileData data)
433434
*/
434435
@JRubyMethod(name = "gets", optional = 1, keywords = true, checkArity = false, writes = LASTLINE)
435436
public static IRubyObject gets(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
437+
final int callInfo = ThreadContext.resetCallInfo(context);
436438
Arity.checkArgumentCount(context, args, 0, 1);
437439

438-
return context.setLastLine(argf_getline(context, recv, args));
440+
return context.setLastLine(argf_getline(context, callInfo, args));
439441
}
440442

441443
/** Read a line.
@@ -452,36 +454,26 @@ public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRub
452454

453455
@JRubyMethod(optional = 1, keywords = true, checkArity = false)
454456
public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
457+
final int callInfo = ThreadContext.resetCallInfo(context);
455458
Arity.checkArgumentCount(context, args, 0, 1);
456459

457-
int callInfo = context.callInfo;
458460
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
459461

460462
if (!data.next_argv(context)) return newEmptyArray(context);
461463

462-
if (!(data.currentFile instanceof RubyIO)) return data.currentFile.callMethod(context, "readlines", args);
464+
if (!(data.currentFile instanceof RubyIO)) {
465+
// TODO do we need to restore callInfo here?
466+
return data.currentFile.callMethod(context, "readlines", args);
467+
}
463468

464469
var ary = newArray(context);
465470
IRubyObject line;
466-
while(!(line = argfGetlineLoopWithKeywords(context, recv, args, callInfo)).isNil()) {
471+
while(!(line = argf_getline(context, callInfo, args)).isNil()) {
467472
ary.append(context, line);
468473
}
469474
return ary;
470475
}
471476

472-
/**
473-
* Call argf_getline as in a loop, providing the given keywords state between calls.
474-
*
475-
* @param context
476-
* @param recv
477-
* @param args
478-
* @return
479-
*/
480-
private static IRubyObject argfGetlineLoopWithKeywords(ThreadContext context, IRubyObject recv, IRubyObject[] args, int callInfo) {
481-
context.callInfo = callInfo;
482-
return argf_getline(context, recv, args);
483-
}
484-
485477
@JRubyMethod(optional = 1, checkArity = false)
486478
public static IRubyObject to_a(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
487479
Arity.checkArgumentCount(context, args, 0, 1);
@@ -493,7 +485,7 @@ public static IRubyObject to_a(ThreadContext context, IRubyObject recv, IRubyObj
493485

494486
var ary = newArray(context);
495487
IRubyObject line;
496-
while ((line = argf_getline(context, recv, args)) != context.nil) {
488+
while ((line = argf_getline(context, 0, args)) != context.nil) {
497489
ary.append(context, line);
498490
}
499491
return ary;
@@ -598,9 +590,9 @@ public static IRubyObject codepoints(ThreadContext context, IRubyObject recv, Bl
598590
public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
599591
if (!block.isGiven()) return enumeratorize(context.runtime, recv, "each_line", args);
600592

593+
final int callInfo = ThreadContext.resetCallInfo(context);
601594
Arity.checkArgumentCount(context, args, 0, 1);
602595

603-
int callInfo = context.callInfo;
604596
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);
605597

606598
if (!data.next_argv(context)) return context.nil;
@@ -613,7 +605,7 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
613605
}
614606

615607
IRubyObject str;
616-
while ((str = argfGetlineLoopWithKeywords(context, recv, args, callInfo)) != context.nil) {
608+
while ((str = argf_getline(context, callInfo, args)) != context.nil) {
617609
block.yield(context, str);
618610
}
619611

0 commit comments

Comments
 (0)