7878import static org .jruby .api .Define .defineClass ;
7979import static org .jruby .api .Error .argumentError ;
8080import static org .jruby .api .Warn .warn ;
81- import static org .jruby .runtime .ThreadContext .CALL_KEYWORD ;
82- import static org .jruby .runtime .ThreadContext .resetCallInfo ;
81+ import static org .jruby .runtime .ThreadContext .hasKeywords ;
8382import static org .jruby .runtime .Visibility .PRIVATE ;
8483
8584public class RubyArgsFile extends RubyObject {
@@ -389,9 +388,7 @@ public static IRubyObject external_encoding(ThreadContext context, IRubyObject r
389388 }
390389
391390 // MRI: argf_getline
392- private static IRubyObject argf_getline (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
393- int callInfo = resetCallInfo (context );
394- boolean keywords = (callInfo & CALL_KEYWORD ) != 0 ;
391+ private static IRubyObject argf_getline (ThreadContext context , final int callInfo , IRubyObject [] args ) {
395392 IRubyObject line ;
396393 ArgsFileData data = ArgsFileData .getArgsFileData (context .runtime );
397394
@@ -401,14 +398,13 @@ private static IRubyObject argf_getline(ThreadContext context, IRubyObject recv,
401398 RubyIO currentFile = (RubyIO ) data .currentFile ;
402399
403400 if (isGenericInput (context , data )) {
404- // restore callInfo for kwargs
405- context .callInfo = callInfo ;
401+ context .callInfo = callInfo ; // restore callInfo for kwargs
406402 line = data .currentFile .callMethod (context , "gets" , args );
407403 } else {
408404 if (args .length == 0 && context .runtime .getRecordSeparatorVar ().get () == globalVariables (context ).getDefaultSeparator ()) {
409405 line = (currentFile ).gets (context );
410406 } else {
411- line = Getline .getlineCall (context , GETLINE , currentFile , currentFile .getReadEncoding (), keywords , args );
407+ line = Getline .getlineCall (context , GETLINE , currentFile , currentFile .getReadEncoding (), hasKeywords ( callInfo ) , args );
412408 }
413409
414410 if (line .isNil () && data .next_p != Stream ) {
@@ -436,9 +432,10 @@ private static boolean isGenericInput(ThreadContext context, ArgsFileData data)
436432 */
437433 @ JRubyMethod (name = "gets" , optional = 1 , keywords = true , checkArity = false , writes = LASTLINE )
438434 public static IRubyObject gets (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
435+ final int callInfo = ThreadContext .resetCallInfo (context );
439436 Arity .checkArgumentCount (context , args , 0 , 1 );
440437
441- return context .setLastLine (argf_getline (context , recv , args ));
438+ return context .setLastLine (argf_getline (context , callInfo , args ));
442439 }
443440
444441 /** Read a line.
@@ -455,36 +452,26 @@ public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRub
455452
456453 @ JRubyMethod (optional = 1 , keywords = true , checkArity = false )
457454 public static IRubyObject readlines (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
455+ final int callInfo = ThreadContext .resetCallInfo (context );
458456 Arity .checkArgumentCount (context , args , 0 , 1 );
459457
460- int callInfo = context .callInfo ;
461458 ArgsFileData data = ArgsFileData .getArgsFileData (context .runtime );
462459
463460 if (!data .next_argv (context )) return newEmptyArray (context );
464461
465- if (!(data .currentFile instanceof RubyIO )) return data .currentFile .callMethod (context , "readlines" , args );
462+ if (!(data .currentFile instanceof RubyIO )) {
463+ // TODO do we need to restore callInfo here?
464+ return data .currentFile .callMethod (context , "readlines" , args );
465+ }
466466
467467 var ary = newArray (context );
468468 IRubyObject line ;
469- while (!(line = argfGetlineLoopWithKeywords (context , recv , args , callInfo )).isNil ()) {
469+ while (!(line = argf_getline (context , callInfo , args )).isNil ()) {
470470 ary .append (context , line );
471471 }
472472 return ary ;
473473 }
474474
475- /**
476- * Call argf_getline as in a loop, providing the given keywords state between calls.
477- *
478- * @param context
479- * @param recv
480- * @param args
481- * @return
482- */
483- private static IRubyObject argfGetlineLoopWithKeywords (ThreadContext context , IRubyObject recv , IRubyObject [] args , int callInfo ) {
484- context .callInfo = callInfo ;
485- return argf_getline (context , recv , args );
486- }
487-
488475 @ JRubyMethod (optional = 1 , checkArity = false )
489476 public static IRubyObject to_a (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
490477 Arity .checkArgumentCount (context , args , 0 , 1 );
@@ -496,7 +483,7 @@ public static IRubyObject to_a(ThreadContext context, IRubyObject recv, IRubyObj
496483
497484 var ary = newArray (context );
498485 IRubyObject line ;
499- while ((line = argf_getline (context , recv , args )) != context .nil ) {
486+ while ((line = argf_getline (context , 0 , args )) != context .nil ) {
500487 ary .append (context , line );
501488 }
502489 return ary ;
@@ -601,9 +588,9 @@ public static IRubyObject codepoints(ThreadContext context, IRubyObject recv, Bl
601588 public static IRubyObject each_line (ThreadContext context , IRubyObject recv , IRubyObject [] args , Block block ) {
602589 if (!block .isGiven ()) return enumeratorize (context .runtime , recv , "each_line" , args );
603590
591+ final int callInfo = ThreadContext .resetCallInfo (context );
604592 Arity .checkArgumentCount (context , args , 0 , 1 );
605593
606- int callInfo = context .callInfo ;
607594 ArgsFileData data = ArgsFileData .getArgsFileData (context .runtime );
608595
609596 if (!data .next_argv (context )) return context .nil ;
@@ -616,7 +603,7 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
616603 }
617604
618605 IRubyObject str ;
619- while ((str = argfGetlineLoopWithKeywords (context , recv , args , callInfo )) != context .nil ) {
606+ while ((str = argf_getline (context , callInfo , args )) != context .nil ) {
620607 block .yield (context , str );
621608 }
622609
0 commit comments