diff --git a/internal/vm/io.go b/internal/vm/io.go index d56d0696..86090889 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -759,10 +759,32 @@ func defStringIORead(cls *RClass) { o.pos += sz return s }) - gets := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + // setLineGlobals records a successful line read the way MRI's IO#gets family + // does. $. always becomes the reading IO's line number (a StringIO updates it + // too). $_ (the "last read line") is set only by the single-line readers — + // gets/readline (lastLine true) — not by the bulk readlines/each_line, which + // leave $_ alone, matching MRI. + setLineGlobals := func(vm *VM, o *IOObj, v object.Value, lastLine bool) { + if v == object.NilV { + return + } + vm.globals["$."] = object.IntValue(int64(o.lineno)) + if lastLine { + vm.globals["$_"] = v + } + } + gets := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - return ioGets(o, args) + v := ioGets(o, args) + if v == object.NilV { + // Reading past the last line clears $_ (but leaves $. reporting the + // final line number), matching MRI. + vm.globals["$_"] = object.NilV + } else { + setLineGlobals(vm, o, v, true) + } + return v } cls.define("gets", gets) cls.define("readline", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -772,7 +794,7 @@ func defStringIORead(cls *RClass) { } return v }) - cls.define("readlines", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("readlines", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) checkGetsLimit(args, "readlines") @@ -782,6 +804,7 @@ func defStringIORead(cls *RClass) { if v == object.NilV { break } + setLineGlobals(vm, o, v, false) lines = append(lines, v) } return object.NewArrayFromSlice(lines) @@ -795,6 +818,7 @@ func defStringIORead(cls *RClass) { if v == object.NilV { break } + setLineGlobals(vm, o, v, false) vm.callBlock(blk, []object.Value{v}) } return self diff --git a/internal/vm/io_gets_globals_test.go b/internal/vm/io_gets_globals_test.go new file mode 100644 index 00000000..fb45b999 --- /dev/null +++ b/internal/vm/io_gets_globals_test.go @@ -0,0 +1,27 @@ +package vm_test + +import "testing" + +// TestGetsLineGlobals covers the $. (line number) and $_ (last read line) globals +// that IO#gets / #readline / #each_line / #readlines maintain, verified against +// MRI Ruby 4.0.6: gets and readline set both $. and $_; the bulk readers set $. +// but leave $_ untouched; and reading past the end clears $_. +func TestGetsLineGlobals(t *testing.T) { + cases := []struct{ src, want string }{ + // gets sets $. to the line number and $_ to the line (a StringIO too). + {`require "stringio"; io = StringIO.new("a\nb\nc\n"); io.gets; p [$., $_]`, "[1, \"a\\n\"]\n"}, + {`require "stringio"; io = StringIO.new("a\nb\n"); io.gets; io.gets; p $.`, "2\n"}, + {`require "stringio"; io = StringIO.new("x\n"); p io.readline; p [$., $_]`, "\"x\\n\"\n[1, \"x\\n\"]\n"}, + // Reading past the last line clears $_ but keeps $. at the final number. + {`require "stringio"; io = StringIO.new("a\n"); io.gets; io.gets; p [$., $_]`, "[1, nil]\n"}, + // readlines advances $. to the count but does not touch $_. + {`require "stringio"; io = StringIO.new("a\nb\nc\n"); $_ = "pre"; io.readlines; p [$., $_]`, "[3, \"pre\"]\n"}, + // each_line advances $. but leaves the outer $_ alone. + {`require "stringio"; io = StringIO.new("a\nb\n"); $_ = "pre"; io.each_line {}; p [$., $_]`, "[2, \"pre\"]\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 7d75b0ab..dd895343 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -17300 +17310