From 07de97382984a4cc03cf96c3b0cbf72ed7192fef Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 17:29:46 +0200 Subject: [PATCH 1/6] io: StringIO/IO reader & writer conformance to MRI 4.0.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StringIO.new now honours its mode argument (r/r+/w/w+/a/a+, integer flags, and mode:) — read-only rejects writes, write-only rejects reads, "w"/"w+" truncate, "a"/"a+" append every write at end-of-buffer, and a frozen backing string opens read-only. File.open enforces the same access half. gets/readline/readlines/each_line coerce a non-String separator via #to_str and a non-Integer limit via #to_int exactly once per call, default the separator to $/, and treat a lone #to_int-only argument as the byte limit. Paragraph mode ("") honours the limit and keeps the whole newline run for StringIO but only two for a real IO/File, matching MRI's divergence. read(length) coerces via #to_int, returns an ASCII-8BIT String, and accepts a #to_str-convertible output buffer. write/<< stringify via #to_s. print writes $_ with no args and honours $, / $\. puts expands a #to_ary object. #each is a true alias of #each_line (shared method entry). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 288 +++++++++++++++++++++++++++++----- internal/vm/io_descriptors.go | 19 +-- 2 files changed, 258 insertions(+), 49 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 86090889..5f8f1273 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -25,8 +25,9 @@ type IOObj struct { path string // backing file path for a File stream (else "") writable bool // a File opened for writing — flush the buffer on flush/close lineno int // #lineno — advanced by each successful line read (gets/readline) - rdClosed bool // #close_read was called — reads raise "not opened for reading" - wrClosed bool // #close_write was called — writes raise "not opened for writing" + rdClosed bool // #close_read (or a write-only mode) — reads raise "not opened for reading" + wrClosed bool // #close_write (or a read-only mode) — writes raise "not opened for writing" + appendMode bool // opened in append mode ("a"/"a+") — every write lands at end-of-buffer extEnc string // external encoding name, "" ⇒ Encoding.default_external intEnc string // internal encoding name, "" ⇒ none (nil) fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) @@ -77,6 +78,9 @@ func (o *IOObj) writeBytes(p []byte) int { return len(p) } if o.isStr { + if o.appendMode { // append mode: every write lands at end, ignoring position + o.pos = len(o.buf) + } if end := o.pos + len(p); end > len(o.buf) { o.buf = append(o.buf, make([]byte, end-len(o.buf))...) } @@ -170,14 +174,40 @@ func (vm *VM) registerIO() { defIOWrite(cStringIO) defStringIORead(cStringIO) defIOReadExtra(cStringIO) - cStringIO.smethods["new"] = &Method{name: "new", owner: cStringIO, native: func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + cStringIO.smethods["new"] = &Method{name: "new", owner: cStringIO, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { o := &IOObj{cls: cStringIO, isStr: true} - if len(args) > 0 { - if s, ok := args[0].(*object.String); ok { - o.buf = append([]byte(nil), s.Bytes()...) - } else { + // A trailing options Hash may carry mode: (StringIO.new(str, mode: "r")). + mode, modeGiven := "", false + if h, ok := lastHash(args); ok { + if v, ok := h.Get(object.Symbol("mode")); ok { + mode, modeGiven = stringIOModeString(v), true + } + args = args[:len(args)-1] + } + frozenSrc := false + if len(args) > 0 && !object.IsNil(args[0]) { + s, ok := args[0].(*object.String) + if !ok { raise("TypeError", "no implicit conversion of %s into String", classNameOf(args[0])) } + o.buf = append([]byte(nil), s.Bytes()...) + frozenSrc = s.Frozen + } + if len(args) > 1 && !object.IsNil(args[1]) { + mode, modeGiven = stringIOModeString(args[1]), true + } + if !modeGiven { + // With no explicit mode, a frozen backing string opens read-only; a + // mutable one (or none) is read/write, matching MRI. + mode = "r+" + if frozenSrc { + mode = "r" + } + } + read, write, trunc, appnd := stringIOModeFlags(mode) + o.rdClosed, o.wrClosed, o.appendMode = !read, !write, appnd + if trunc { + o.buf = nil } return o }} @@ -377,6 +407,39 @@ func fileMode(args []object.Value) string { return "r" } +// stringIOModeString normalises a StringIO mode argument (a String like "r+" or +// an Integer bit-OR of File::Constants flags) to an access-mode string. +func stringIOModeString(v object.Value) string { + if i, ok := v.(object.Integer); ok { + return flagsToMode(int64(i)) + } + return strArg(v) +} + +// stringIOModeFlags decodes a StringIO access mode ("r", "r+", "w", "w+", "a", +// "a+") into its capabilities: whether reads and writes are permitted, whether +// the buffer is truncated on open ("w"/"w+"), and whether writes append ("a"/"a+"). +// The mode may carry trailing flags (":BINARY", "b", "t"), which do not affect +// these bits. An unrecognised mode raises ArgumentError, as MRI does. +func stringIOModeFlags(mode string) (read, write, trunc, appnd bool) { + base := mode + if i := strings.IndexByte(base, ':'); i >= 0 { + base = base[:i] + } + base = strings.TrimRight(base, "bt") + plus := strings.Contains(base, "+") + switch { + case strings.HasPrefix(base, "r"): + return true, plus, false, false + case strings.HasPrefix(base, "w"): + return plus, true, true, false + case strings.HasPrefix(base, "a"): + return plus, true, false, true + } + raise("ArgumentError", "invalid access mode %s", mode) + return false, false, false, false +} + // openFileIO opens path into a buffered, file-backed IOObj per mode (r/w/a, with // an optional "+" making a read mode writable). The file's bytes are read into // the buffer; writes accumulate there and are flushed back on flush/close. @@ -425,6 +488,17 @@ func openFileIO(cls *RClass, p, mode string) *IOObj { default: raise("ArgumentError", "invalid access mode %s", mode) } + // Enforce the mode's access half: a write-only ("w"/"a") stream raises on + // read, a read-only ("r") stream raises on write, matching MRI. A "+" mode + // permits both. Append modes ("a"/"a+") force every write to end-of-buffer. + plus := strings.Contains(mode, "+") + switch mode[0] { + case 'w', 'a': + o.rdClosed = !plus + o.appendMode = mode[0] == 'a' + case 'r': + o.wrClosed = !plus + } return o } @@ -466,26 +540,42 @@ func (vm *VM) curIO(global string, w io.Writer, label string) *IOObj { // defIOWrite defines the writing half of the IO protocol on cls (shared by IO // and StringIO). func defIOWrite(cls *RClass) { - cls.define("write", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("write", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckOpen(o) n := 0 for _, a := range args { - n += o.writeStr(a.ToS()) + n += o.writeStr(vm.displayStr(a)) } return object.IntValue(int64(n)) }) - cls.define("<<", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("<<", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckOpen(o) - o.writeStr(args[0].ToS()) + o.writeStr(vm.displayStr(args[0])) return self }) cls.define("print", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckOpen(o) - for _, a := range args { - o.writeStr(vm.displayStr(a)) + // With no arguments, #print writes $_ (the last line read). Multiple + // arguments are separated by the output field separator $, (when set) and + // the whole is terminated by the output record separator $\ (when set). + if len(args) == 0 { + if lastLine := vm.gvar("$_"); !object.IsNil(lastLine) { + o.writeStr(vm.displayStr(lastLine)) + } + } else { + ofs, ofsSet := vm.optStrGlobal("$,") + for i, a := range args { + if i > 0 && ofsSet { + o.writeStr(ofs) + } + o.writeStr(vm.displayStr(a)) + } + } + if ors, ok := vm.optStrGlobal("$\\"); ok { + o.writeStr(ors) } return object.NilV }) @@ -662,17 +752,17 @@ func defStringIORead(cls *RClass) { } return object.IntValue(0) }) - cls.define("read", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("read", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) o.pipeRefresh() - // An optional second argument is an output-buffer String: the read fills it - // (and is returned in its place), and it is cleared when the read yields nil. + lengthGiven := len(args) > 0 && !object.IsNil(args[0]) + // An optional second argument is an output-buffer String (coerced via + // #to_str): the read fills it (and is returned in its place), and it is + // cleared when the read yields nil. var buf *object.String if len(args) > 1 { - if b, ok := args[1].(*object.String); ok { - buf = b - } + buf = vm.ioBufferArg(args[1]) } result := func(data []byte, isNil bool) object.Value { if buf != nil { @@ -689,10 +779,15 @@ func defStringIORead(cls *RClass) { if isNil { return object.NilV } + // read(length) returns a binary (ASCII-8BIT) String; read with no + // length returns the remainder in the stream's default encoding. + if lengthGiven { + return object.NewStringBytesEnc(append([]byte(nil), data...), "ASCII-8BIT") + } return object.NewStringBytes(append([]byte(nil), data...)) } - if len(args) > 0 && args[0] != object.NilV { - n := int(toInt(args[0])) + if lengthGiven { + n := int(vm.toIntCoerce(args[0])) if n < 0 { raise("ArgumentError", "negative length %d given", n) } @@ -776,7 +871,8 @@ func defStringIORead(cls *RClass) { gets := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - v := ioGets(o, args) + sep, limit, chomp := vm.resolveGetsArgs(args) + v := vm.ioGetsResolved(o, sep, limit, chomp) if v == object.NilV { // Reading past the last line clears $_ (but leaves $. reporting the // final line number), matching MRI. @@ -797,10 +893,11 @@ func defStringIORead(cls *RClass) { cls.define("readlines", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - checkGetsLimit(args, "readlines") + sep, limit, chomp := vm.resolveGetsArgs(args) + checkResolvedLimit(limit, "readlines") var lines []object.Value for { - v := ioGets(o, args) + v := vm.ioGetsResolved(o, sep, limit, chomp) if v == object.NilV { break } @@ -812,9 +909,10 @@ func defStringIORead(cls *RClass) { cls.define("each_line", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - checkGetsLimit(args, "each_line") + sep, limit, chomp := vm.resolveGetsArgs(args) + checkResolvedLimit(limit, "each_line") for { - v := ioGets(o, args) + v := vm.ioGetsResolved(o, sep, limit, chomp) if v == object.NilV { break } @@ -832,6 +930,9 @@ func defStringIORead(cls *RClass) { } return self }) + // #each is a true alias of #each_line — it must share the method entry so + // IO.instance_method(:each) == IO.instance_method(:each_line), as in MRI. + cls.methods["each"] = cls.methods["each_line"] } // ioGets reads one line (up to and including the separator, default "\n") from a @@ -881,6 +982,77 @@ func parseGetsArgs(args []object.Value) (sep getsSep, limit int, chomp bool) { return sep, limit, chomp } +// ioGetsResolved reads one line from o with already-resolved (sep, limit, chomp) +// — the coercion of the separator/limit arguments has been done once by the +// caller so a bulk reader (readlines/each_line) never re-runs a #to_str/#to_int +// side effect per line. A successful (non-nil) read advances #lineno. +func (vm *VM) ioGetsResolved(o *IOObj, sep getsSep, limit int, chomp bool) object.Value { + o.pipeRefresh() + v := ioGetsLine(o, sep, limit, chomp) + if v != object.NilV { + o.lineno++ + } + return v +} + +// resolveGetsArgs decodes the (sep, limit, chomp:) arguments of the StringIO/IO +// gets family, coercing a non-String separator via #to_str and a non-Integer +// limit via #to_int exactly once, and defaulting the separator to $/ (the input +// record separator) when none is given. A nil separator selects whole-remainder +// mode; an Integer first positional is the byte limit. +func (vm *VM) resolveGetsArgs(args []object.Value) (sep getsSep, limit int, chomp bool) { + sep, limit = vm.defaultGetsSep(), -1 + if h, ok := lastHash(args); ok { + if v, ok := h.Get(object.Symbol("chomp")); ok { + chomp = v.Truthy() + } + args = args[:len(args)-1] + } + if len(args) > 0 { + switch a := args[0].(type) { + case object.Integer: + limit = int(a) + case *object.String: + sep = getsSep{s: a.Str(), set: true} + default: + switch { + case object.IsNil(args[0]): + sep = getsSep{s: "", set: true, nilSep: true} + case vm.respondsToDynamic(args[0], "to_str"): + // A #to_str-convertible first argument is the separator (MRI checks + // the String conversion before the Integer one). + sep = getsSep{s: string(vm.strArgConv(args, 0)), set: true} + default: + // Otherwise a single non-String positional is the byte limit, + // coerced via #to_int (gets(obj) where obj defines only #to_int). + limit = int(vm.toIntCoerce(args[0])) + } + } + } + if len(args) > 1 && !object.IsNil(args[1]) { + limit = int(vm.toIntCoerce(args[1])) + } + return sep, limit, chomp +} + +// defaultGetsSep returns the separator used when gets is called with no explicit +// one: the input record separator $/ when it holds a String, else "\n". +func (vm *VM) defaultGetsSep() getsSep { + if s, ok := vm.gvar("$/").(*object.String); ok { + return getsSep{s: s.Str(), set: true} + } + return getsSep{s: "\n"} +} + +// checkResolvedLimit raises ArgumentError for an explicit limit of 0 on a +// line-iterating read (readlines/each_line), matching MRI (a 0-byte line would +// never advance the cursor). +func checkResolvedLimit(limit int, meth string) { + if limit == 0 { + raise("ArgumentError", "invalid limit: 0 for %s", meth) + } +} + // checkGetsLimit raises ArgumentError for an explicit limit of 0 on a // line-iterating read (readlines/each_line/foreach/each): a 0-byte line never // advances the cursor, so MRI rejects it rather than looping. A single #gets(0) @@ -905,7 +1077,7 @@ func ioGetsLine(o *IOObj, sep getsSep, limit int, chomp bool) object.Value { return object.NilV } if sep.set && sep.s == "" && !sep.nilSep { // an empty separator selects paragraph mode - return ioGetsParagraph(o) + return ioGetsParagraph(o, limit) } rest := o.buf[o.pos:] end := len(o.buf) // default: read to end (nil separator, or separator not found) @@ -941,9 +1113,13 @@ func getsChomp(line []byte, sep string) []byte { } // ioGetsParagraph implements gets/each_line paragraph mode (an empty separator): -// leading blank lines are skipped, then the paragraph up to and including the run -// of two or more newlines that terminates it (or end of input) is returned. -func ioGetsParagraph(o *IOObj) object.Value { +// leading blank lines are skipped, then the paragraph up to the run of two or +// more newlines that terminates it (or end of input) is returned. A non-negative +// limit caps the number of bytes returned. A StringIO keeps the whole terminating +// newline run in the result, whereas a real IO/File keeps only the two newlines +// that close the paragraph (the rest are skipped as leading blanks on the next +// read) — matching MRI, whose StringIO and IO differ here. +func ioGetsParagraph(o *IOObj, limit int) object.Value { for o.pos < len(o.buf) && o.buf[o.pos] == '\n' { // skip leading blank lines o.pos++ } @@ -951,16 +1127,23 @@ func ioGetsParagraph(o *IOObj) object.Value { return object.NilV } start := o.pos + end := len(o.buf) if idx := strings.Index(string(o.buf[start:]), "\n\n"); idx >= 0 { - end := start + idx + end = start + idx for end < len(o.buf) && o.buf[end] == '\n' { // consume the whole newline run end++ } - o.pos = end - return object.NewString(string(o.buf[start:end])) + if !(o.cls != nil && o.cls.name == "StringIO") { // real IO keeps only two + if two := start + idx + 2; two < end { + end = two + } + } + } + if limit >= 0 && start+limit < end { + end = start + limit } - o.pos = len(o.buf) - return object.NewString(string(o.buf[start:])) + o.pos = end + return object.NewString(string(o.buf[start:end])) } // ioPuts writes args to o with Kernel#puts semantics (arrays flattened, a @@ -1003,6 +1186,15 @@ func (vm *VM) ioPutsValueRec(o *IOObj, v object.Value, seen map[*object.Array]st } return } + // A non-Array object that implements #to_ary is expanded like an Array (each + // element on its own line), matching MRI's puts — which tries #to_ary before + // falling back to #to_s. + if vm.respondsToDynamic(v, "to_ary") { + if arr, ok := vm.send(v, "to_ary", nil, nil).(*object.Array); ok { + vm.ioPutsValueRec(o, arr, seen) + return + } + } if s := vm.displayStr(v); strings.HasSuffix(s, "\n") { o.writeStr(s) } else { @@ -1010,6 +1202,32 @@ func (vm *VM) ioPutsValueRec(o *IOObj, v object.Value, seen map[*object.Array]st } } +// optStrGlobal reads a global that is meaningful only when set to a String — the +// output field/record separators $, and $\ that #print honours. It reports the +// separator and whether one is in effect (a nil or unset global ⇒ false). +func (vm *VM) optStrGlobal(name string) (string, bool) { + if s, ok := vm.gvar(name).(*object.String); ok { + return s.Str(), true + } + return "", false +} + +// ioBufferArg resolves the output-buffer argument of #read/#readpartial to a +// mutable String, coercing a non-String via #to_str (as MRI does). Anything that +// is neither a String nor #to_str-convertible raises TypeError. +func (vm *VM) ioBufferArg(v object.Value) *object.String { + if s, ok := v.(*object.String); ok { + return s + } + if vm.respondsToDynamic(v, "to_str") { + if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok { + return s + } + } + raise("TypeError", "no implicit conversion of %s into String", classNameOf(v)) + return nil +} + // displayStr renders v the way Kernel#print / #puts / String() do: a user object // (RObject) goes through its (possibly user-defined) #to_s, so an overridden to_s // is honoured; built-in value types use their authoritative native ToS directly. diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 92a12c11..d745372d 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -90,20 +90,11 @@ func defIOReadExtra(cls *RClass) { } return self }) - each := func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { - o := self.(*IOObj) - ioCheckReadable(o) - checkGetsLimit(args, "each_line") - for { - v := ioGets(o, args) - if v == object.NilV { - break - } - vm.callBlock(blk, []object.Value{v}) - } - return self - } - cls.define("each", each) + // #each is a true alias of #each_line (defined in defStringIORead, which runs + // before defIOReadExtra): they must share the method entry so + // IO.instance_method(:each) == IO.instance_method(:each_line), as in MRI, and + // #each inherits each_line's separator/limit/$/ handling. + cls.methods["each"] = cls.methods["each_line"] cls.define("sysread", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) From e7971883dfdf74e649d47ad4362f5ee15a4404d4 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 17:42:04 +0200 Subject: [PATCH 2/6] =?UTF-8?q?io:=20more=20IO=20reader=20conformance=20?= =?UTF-8?q?=E2=80=94=20enumerators,=20class=20readlines,=20puts=20via=20#w?= =?UTF-8?q?rite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit each_line/each_char/each_byte with no block now return an Enumerator (whose #size is nil) instead of crashing, and defer the readable check to iteration so the enumerator is buildable on a closed stream. #each is a real alias of #each_line on IO as well. Paragraph mode honours chomp: (stripping the terminating newline run but not a lone EOF "\n"). IO.readlines/foreach route their separator/limit/chomp/$/ through the same coercion path as the instance methods, so a #to_str separator, a #to_int (or Integer) limit, and the $/ default all work. Kernel#puts and IO#puts emit through the stream's #write method, so a stream that overrides #write (as the puts specs do) captures the output. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 120 +++++++++++++++++++------------ internal/vm/io_class.go | 18 ++--- internal/vm/io_descriptors.go | 5 +- internal/vm/io_residuals_test.go | 120 +++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+), 54 deletions(-) create mode 100644 internal/vm/io_residuals_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index 5f8f1273..6ac3c4b0 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -14,23 +14,23 @@ import ( // in-memory byte buffer with a read/write cursor. The two share the write path // so puts/print/printf/<< work uniformly, and StringIO adds the read methods. type IOObj struct { - cls *RClass // IO, StringIO or File — so classOf/is_a? are exact - w io.Writer - buf []byte // StringIO / File content (buffered in memory) - pos int // read/write cursor - isStr bool // buffer-backed (StringIO / File) vs writer-backed (real IO) - sync bool - closed bool - label string // "STDOUT"/"STDERR"/"STDIN" for inspect - path string // backing file path for a File stream (else "") - writable bool // a File opened for writing — flush the buffer on flush/close - lineno int // #lineno — advanced by each successful line read (gets/readline) - rdClosed bool // #close_read (or a write-only mode) — reads raise "not opened for reading" - wrClosed bool // #close_write (or a read-only mode) — writes raise "not opened for writing" - appendMode bool // opened in append mode ("a"/"a+") — every write lands at end-of-buffer - extEnc string // external encoding name, "" ⇒ Encoding.default_external - intEnc string // internal encoding name, "" ⇒ none (nil) - fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) + cls *RClass // IO, StringIO or File — so classOf/is_a? are exact + w io.Writer + buf []byte // StringIO / File content (buffered in memory) + pos int // read/write cursor + isStr bool // buffer-backed (StringIO / File) vs writer-backed (real IO) + sync bool + closed bool + label string // "STDOUT"/"STDERR"/"STDIN" for inspect + path string // backing file path for a File stream (else "") + writable bool // a File opened for writing — flush the buffer on flush/close + lineno int // #lineno — advanced by each successful line read (gets/readline) + rdClosed bool // #close_read (or a write-only mode) — reads raise "not opened for reading" + wrClosed bool // #close_write (or a read-only mode) — writes raise "not opened for writing" + appendMode bool // opened in append mode ("a"/"a+") — every write lands at end-of-buffer + extEnc string // external encoding name, "" ⇒ Encoding.default_external + intEnc string // internal encoding name, "" ⇒ none (nil) + fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) // Pipe ends (IO.pipe) share a single byte buffer in *pipe. The write end // appends; the read end drains from pipe.rpos. Because subprocess execution @@ -580,9 +580,8 @@ func defIOWrite(cls *RClass) { return object.NilV }) cls.define("puts", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - o := self.(*IOObj) - ioCheckOpen(o) - vm.ioPuts(o, args) + ioCheckOpen(self.(*IOObj)) + vm.ioPuts(self, args) return object.NilV }) cls.define("printf", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -908,7 +907,10 @@ func defStringIORead(cls *RClass) { }) cls.define("each_line", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { o := self.(*IOObj) - ioCheckReadable(o) + if blk == nil { // no block ⇒ an Enumerator (buildable even on a closed stream) + return enumForSized(self, "each_line", enumSizeNil, args...) + } + ioCheckReadable(o) // iterating a closed/unreadable stream raises, as in MRI sep, limit, chomp := vm.resolveGetsArgs(args) checkResolvedLimit(limit, "each_line") for { @@ -923,6 +925,10 @@ func defStringIORead(cls *RClass) { }) cls.define("each_char", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { o := self.(*IOObj) + if blk == nil { // no block ⇒ an Enumerator (buildable even on a closed stream) + return enumForSized(self, "each_char", enumSizeNil) + } + ioCheckReadable(o) // iterating a closed/unreadable stream raises, as in MRI for o.pos < len(o.buf) { r, sz := utf8.DecodeRune(o.buf[o.pos:]) o.pos += sz @@ -982,6 +988,11 @@ func parseGetsArgs(args []object.Value) (sep getsSep, limit int, chomp bool) { return sep, limit, chomp } +// enumSizeNil is the #size block for a reader Enumerator (each_line/each_char/ +// each_byte with no block): the number of elements a buffered read will yield is +// not known in advance, so #size is nil, matching MRI. +func enumSizeNil(*VM) object.Value { return object.NilV } + // ioGetsResolved reads one line from o with already-resolved (sep, limit, chomp) // — the coercion of the separator/limit arguments has been done once by the // caller so a bulk reader (readlines/each_line) never re-runs a #to_str/#to_int @@ -1035,6 +1046,18 @@ func (vm *VM) resolveGetsArgs(args []object.Value) (sep getsSep, limit int, chom return sep, limit, chomp } +// optChomp returns whether chomp is in effect given a separated options Hash +// (IO.readlines/foreach carry their keyword arguments there rather than as a +// trailing positional Hash): a chomp: entry in opts overrides the incoming value. +func optChomp(opts *object.Hash, cur bool) bool { + if opts != nil { + if v, ok := opts.Get(object.Symbol("chomp")); ok { + return v.Truthy() + } + } + return cur +} + // defaultGetsSep returns the separator used when gets is called with no explicit // one: the input record separator $/ when it holds a String, else "\n". func (vm *VM) defaultGetsSep() getsSep { @@ -1077,7 +1100,7 @@ func ioGetsLine(o *IOObj, sep getsSep, limit int, chomp bool) object.Value { return object.NilV } if sep.set && sep.s == "" && !sep.nilSep { // an empty separator selects paragraph mode - return ioGetsParagraph(o, limit) + return ioGetsParagraph(o, limit, chomp) } rest := o.buf[o.pos:] end := len(o.buf) // default: read to end (nil separator, or separator not found) @@ -1119,7 +1142,7 @@ func getsChomp(line []byte, sep string) []byte { // newline run in the result, whereas a real IO/File keeps only the two newlines // that close the paragraph (the rest are skipped as leading blanks on the next // read) — matching MRI, whose StringIO and IO differ here. -func ioGetsParagraph(o *IOObj, limit int) object.Value { +func ioGetsParagraph(o *IOObj, limit int, chomp bool) object.Value { for o.pos < len(o.buf) && o.buf[o.pos] == '\n' { // skip leading blank lines o.pos++ } @@ -1128,7 +1151,9 @@ func ioGetsParagraph(o *IOObj, limit int) object.Value { } start := o.pos end := len(o.buf) + sepFound := false if idx := strings.Index(string(o.buf[start:]), "\n\n"); idx >= 0 { + sepFound = true end = start + idx for end < len(o.buf) && o.buf[end] == '\n' { // consume the whole newline run end++ @@ -1143,35 +1168,40 @@ func ioGetsParagraph(o *IOObj, limit int) object.Value { end = start + limit } o.pos = end - return object.NewString(string(o.buf[start:end])) + line := o.buf[start:end] + if chomp && sepFound { // chomp strips the terminating newline run (the paragraph + for len(line) > 0 && line[len(line)-1] == '\n' { // separator), not a lone EOF "\n" + line = line[:len(line)-1] + } + } + return object.NewString(string(line)) } -// ioPuts writes args to o with Kernel#puts semantics (arrays flattened, a -// trailing newline added unless already present; no args ⇒ a lone newline). It -// is a VM method so each value is stringified through its (possibly -// user-defined) #to_s, matching MRI. -func (vm *VM) ioPuts(o *IOObj, args []object.Value) { +// ioPuts writes args to self with Kernel#puts semantics (arrays flattened, a +// trailing newline added unless already present; no args ⇒ a lone newline). +// Every piece is emitted through self's #write method (via vm.send) rather than +// the buffer directly, so a stream that overrides #write sees puts's output, as +// in MRI; each value is also stringified through its (possibly user-defined) +// #to_s. +func (vm *VM) ioPuts(self object.Value, args []object.Value) { + write := func(s string) { vm.send(self, "write", []object.Value{object.NewString(s)}, nil) } if len(args) == 0 { - o.writeStr("\n") + write("\n") return } for _, a := range args { - vm.ioPutsValue(o, a) + vm.ioPutsValueRec(write, a, nil) } } -func (vm *VM) ioPutsValue(o *IOObj, v object.Value) { - vm.ioPutsValueRec(o, v, nil) -} - -// ioPutsValueRec is ioPutsValue with a guard against a self-referential array: -// puts recurses into nested arrays (each element on its own line), so a member -// that is its own container is written as "[...]" (as MRI does) rather than -// looping forever. seen tracks the arrays currently being expanded. -func (vm *VM) ioPutsValueRec(o *IOObj, v object.Value, seen map[*object.Array]struct{}) { +// ioPutsValueRec emits one puts value through write, guarding against a +// self-referential array: puts recurses into nested arrays (each element on its +// own line), so a member that is its own container is written as "[...]" (as MRI +// does) rather than looping forever. seen tracks the arrays currently expanding. +func (vm *VM) ioPutsValueRec(write func(string), v object.Value, seen map[*object.Array]struct{}) { if arr, ok := v.(*object.Array); ok { if _, rec := seen[arr]; rec { - o.writeStr("[...]\n") + write("[...]\n") return } if seen == nil { @@ -1182,7 +1212,7 @@ func (vm *VM) ioPutsValueRec(o *IOObj, v object.Value, seen map[*object.Array]st // An empty array writes nothing (MRI), unlike a no-arg puts which writes a // lone newline. for _, e := range arr.Elems { - vm.ioPutsValueRec(o, e, seen) + vm.ioPutsValueRec(write, e, seen) } return } @@ -1191,14 +1221,14 @@ func (vm *VM) ioPutsValueRec(o *IOObj, v object.Value, seen map[*object.Array]st // falling back to #to_s. if vm.respondsToDynamic(v, "to_ary") { if arr, ok := vm.send(v, "to_ary", nil, nil).(*object.Array); ok { - vm.ioPutsValueRec(o, arr, seen) + vm.ioPutsValueRec(write, arr, seen) return } } if s := vm.displayStr(v); strings.HasSuffix(s, "\n") { - o.writeStr(s) + write(s) } else { - o.writeStr(s + "\n") + write(s + "\n") } } diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index be3c9606..d1971ffc 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -110,15 +110,16 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { return object.IntValue(int64(len(data.Bytes()))) }) def("foreach", func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value { - pos, _ := splitIOOpts(args) + pos, opts := splitIOOpts(args) if len(pos) == 0 { raise("ArgumentError", "wrong number of arguments (given 0, expected 1+)") } o := openFileIO(cFile, pathArg(vm, pos[0]), "r") - sep := pos[1:] - checkGetsLimit(sep, "foreach") + sep, limit, chomp := vm.resolveGetsArgs(pos[1:]) + chomp = optChomp(opts, chomp) + checkResolvedLimit(limit, "foreach") var lines []object.Value - for v := ioGets(o, sep); v != object.NilV; v = ioGets(o, sep) { + for v := vm.ioGetsResolved(o, sep, limit, chomp); v != object.NilV; v = vm.ioGetsResolved(o, sep, limit, chomp) { if blk != nil { vm.callBlock(blk, []object.Value{v}) } else { @@ -131,15 +132,16 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { return object.NilV }) def("readlines", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - pos, _ := splitIOOpts(args) + pos, opts := splitIOOpts(args) if len(pos) == 0 { raise("ArgumentError", "wrong number of arguments (given 0, expected 1+)") } o := openFileIO(cFile, pathArg(vm, pos[0]), "r") - sep := pos[1:] - checkGetsLimit(sep, "readlines") + sep, limit, chomp := vm.resolveGetsArgs(pos[1:]) + chomp = optChomp(opts, chomp) + checkResolvedLimit(limit, "readlines") var lines []object.Value - for v := ioGets(o, sep); v != object.NilV; v = ioGets(o, sep) { + for v := vm.ioGetsResolved(o, sep, limit, chomp); v != object.NilV; v = vm.ioGetsResolved(o, sep, limit, chomp) { lines = append(lines, v) } return object.NewArrayFromSlice(lines) diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index d745372d..a52dccb7 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -81,7 +81,10 @@ func defIOReadExtra(cls *RClass) { }) cls.define("each_byte", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { o := self.(*IOObj) - ioCheckReadable(o) + if blk == nil { // no block ⇒ an Enumerator (buildable even on a closed stream) + return enumForSized(self, "each_byte", enumSizeNil) + } + ioCheckReadable(o) // iterating a closed/unreadable stream raises, as in MRI o.pipeRefresh() for o.pos < len(o.buf) { b := o.buf[o.pos] diff --git a/internal/vm/io_residuals_test.go b/internal/vm/io_residuals_test.go new file mode 100644 index 00000000..54398eef --- /dev/null +++ b/internal/vm/io_residuals_test.go @@ -0,0 +1,120 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestIOResiduals covers the StringIO/IO reader and writer conformance details +// brought in line with MRI Ruby 4.0.6: mode-aware StringIO/File construction, +// the gets separator/limit coercion and $/ default, paragraph mode, read's +// binary encoding and #to_int/#to_str coercions, print's $_ / $, / $\ handling, +// puts's #to_ary expansion, and the #each alias. Every want was checked against +// MRI 4.0.6. +func TestIOResiduals(t *testing.T) { + const req = `require "stringio"; ` + cases := []struct{ src, want string }{ + // --- StringIO.new modes ------------------------------------------------- + // Append mode writes at end, ignoring the cursor, and updates pos to end. + {req + `s = StringIO.new(+"example", "a"); s.print(", x"); p [s.string, s.pos]`, + "[\"example, x\", 10]\n"}, + {req + `s = StringIO.new(+"example", "a"); s.pos = 3; s << ", y"; p s.string`, + "\"example, y\"\n"}, + // "w"/"w+" truncate the backing string; "r+" preserves it. + {req + `p [StringIO.new(+"data", "w").string, StringIO.new(+"data", "w+").string, StringIO.new(+"data", "r+").string]`, + "[\"\", \"\", \"data\"]\n"}, + // r+ is readable and writable; a mid-buffer write overwrites in place. + {req + `s = StringIO.new(+"abcd", "r+"); a = s.read(2); s.write("XY"); p [a, s.string]`, + "[\"ab\", \"abXY\"]\n"}, + // mode: keyword and an integer mode are both honoured. + {req + `p [StringIO.new(+"x", mode: "r").class, StringIO.new(+"x", File::WRONLY).class]`, + "[StringIO, StringIO]\n"}, + // A frozen backing string opens read-only. + {req + `s = StringIO.new("frozen".freeze); r = (s.write("y") rescue $!.class); p r`, + "IOError\n"}, + + // --- read --------------------------------------------------------------- + // read(length) is ASCII-8BIT; read (no length) is the default encoding. + {req + `s = StringIO.new("abc"); p [s.read(2).encoding.name, StringIO.new("abc").read.encoding.name]`, + "[\"ASCII-8BIT\", \"UTF-8\"]\n"}, + // #to_int coercion of the length argument. + {req + `o = Object.new; def o.to_int; 3; end; p StringIO.new("hello").read(o)`, + "\"hel\"\n"}, + // A #to_str-convertible output buffer is filled and returned. + {req + `o = Object.new; def o.to_str; +""; end; b = StringIO.new("hello").read(3, o); p b`, + "\"hel\"\n"}, + // nil vs "" at EOF: a length read past EOF is nil, a zero-length read is "". + {req + `s = StringIO.new("ab"); s.pos = 2; p [s.read(3), s.read(0)]`, + "[nil, \"\"]\n"}, + + // --- gets separator / limit / $/ --------------------------------------- + // A #to_str separator and a #to_int limit are each coerced. + {req + `o = Object.new; def o.to_str; ">"; end; p StringIO.new("a>b>c").gets(o)`, + "\"a>\"\n"}, + {req + `o = Object.new; def o.to_int; 2; end; p StringIO.new("hello").gets(o)`, + "\"he\"\n"}, + // Separator and limit together; the limit truncates before the separator. + {req + `s = StringIO.new("this>is"); p [s.gets(">", 8), s.gets(">", 2)]`, + "[\"this>\", \"is\"]\n"}, + // A nil separator with a limit reads the limit's worth of the remainder. + {req + `p StringIO.new("this>is").gets(nil, 5)`, "\"this>\"\n"}, + // The default separator follows $/. + {req + `$/ = " "; r = StringIO.new("an example").gets; $/ = "\n"; p r`, "\"an \"\n"}, + // chomp strips the separator (and a preceding \r for the "\n" default). + {req + `p StringIO.new("a\r\nb").gets(chomp: true)`, "\"a\"\n"}, + + // --- paragraph mode ----------------------------------------------------- + // StringIO keeps the whole newline run; a limit still caps the bytes. + {req + `p [StringIO.new("a\n\n\nb").gets(""), StringIO.new("one\n\ntwo").gets("", 4)]`, + "[\"a\\n\\n\\n\", \"one\\n\"]\n"}, + + // --- readlines / each_line --------------------------------------------- + {req + `p StringIO.new("a>b>c").readlines(">")`, "[\"a>\", \"b>\", \"c\"]\n"}, + {req + `a = []; StringIO.new("x>y").each_line(">") { |l| a << l }; p a`, + "[\"x>\", \"y\"]\n"}, + + // --- print / puts / write ---------------------------------------------- + // print with no args writes $_; multiple args use $, and terminate with $\. + {req + `$_ = "LL"; s = StringIO.new; s.print; p s.string`, "\"LL\"\n"}, + {req + `$, = "-"; $\ = "!"; s = StringIO.new; s.print("a", "b"); $, = nil; $\ = nil; p s.string`, + "\"a-b!\"\n"}, + // write/<< stringify via #to_s. + {req + `o = Object.new; def o.to_s; "TS"; end; s = StringIO.new; s.write(o); s << o; p s.string`, + "\"TSTS\"\n"}, + // puts expands a #to_ary object, one element per line. + {req + `o = Object.new; def o.to_ary; ["p", "q"]; end; s = StringIO.new; s.puts(o); p s.string`, + "\"p\\nq\\n\"\n"}, + + // --- #each alias -------------------------------------------------------- + {req + `p [StringIO.instance_method(:each) == StringIO.instance_method(:each_line), IO.instance_method(:each) == IO.instance_method(:each_line)]`, + "[true, true]\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) + } + } + + errs := []struct{ src, want string }{ + // A read-only StringIO rejects writes; a write-only one rejects reads. + {req + `StringIO.new(+"x", "r").write("y")`, "not opened for writing"}, + {req + `StringIO.new(+"x", "w").read`, "not opened for reading"}, + {req + `StringIO.new(+"x", "w").gets`, "not opened for reading"}, + // A negative read length is an ArgumentError. + {req + `StringIO.new("x").read(-1)`, "negative length"}, + // A read length that is neither Integer nor #to_int-convertible. + {req + `StringIO.new("x").read(Object.new)`, "into Integer"}, + // A read buffer that cannot become a String. + {req + `StringIO.new("hello").read(3, Object.new)`, "into String"}, + // A zero limit for a line-iterating read is rejected. + {req + `StringIO.new("x\ny").readlines(0)`, "invalid limit: 0 for readlines"}, + {req + `StringIO.new("x\ny").each_line(0) { |l| l }`, "invalid limit: 0 for each_line"}, + // An invalid StringIO mode string. + {req + `StringIO.new(+"x", "z")`, "invalid access mode"}, + } + for _, c := range errs { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q got=%v want error containing %q", c.src, err, c.want) + } + } +} From d149b6417291c60dbed44b57eb7e9eac9fdfc96b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:00:32 +0200 Subject: [PATCH 3/6] io: consolidate gets-arg parsing and cover the new reader/writer branches Fold the single-shot ioGets (ARGF#gets, IRB reader) onto the shared resolveGetsArgs/ioGetsResolved path and delete the now-duplicate parseGetsArgs and the dead checkGetsLimit, so there is one separator/limit/$/-aware parser. Extend io_residuals_test.go to exercise the new branches: StringIO/File modes, read coercions and binary encoding, gets/readlines/each_line separator+limit+$/, paragraph mode (StringIO whole-run vs File two-newline, chomp, leading blanks, EOF), the no-block Enumerator forms, puts via #write with array flattening and recursion, print's $_/$,/$\, IO.readlines/foreach, and File read/write gating. Co-Authored-By: Claude Opus 4.8 --- internal/vm/argf.go | 2 +- internal/vm/io.go | 62 ++---------- internal/vm/io_residuals_test.go | 158 +++++++++++++++++++++++++++++++ internal/vm/irb.go | 2 +- 4 files changed, 167 insertions(+), 57 deletions(-) diff --git a/internal/vm/argf.go b/internal/vm/argf.go index 51e73bfd..5faede56 100644 --- a/internal/vm/argf.go +++ b/internal/vm/argf.go @@ -164,7 +164,7 @@ func (vm *VM) registerARGF() { return object.NilV } // ensure guarantees o has bytes, so ioGets yields a line here. - line := ioGets(o, args) + line := vm.ioGets(o, args) a.bumpLine() return line }) diff --git a/internal/vm/io.go b/internal/vm/io.go index 6ac3c4b0..e81e9214 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -941,51 +941,13 @@ func defStringIORead(cls *RClass) { cls.methods["each"] = cls.methods["each_line"] } -// ioGets reads one line (up to and including the separator, default "\n") from a -// StringIO, returning nil at end of input. It accepts MRI's (sep, limit, chomp:) -// argument shapes: a leading Integer positional is the byte limit (separator -// defaults to "\n"); a String or nil is the separator, optionally followed by an -// Integer limit; a trailing chomp: true strips the separator from the result. A -// successful (non-nil) read advances #lineno. -func ioGets(o *IOObj, args []object.Value) object.Value { - o.pipeRefresh() - sep, limit, chomp := parseGetsArgs(args) - v := ioGetsLine(o, sep, limit, chomp) - if v != object.NilV { - o.lineno++ - } - return v -} - -// parseGetsArgs decodes the (sep, limit, chomp:) arguments of gets/readline/ -// each_line. sepSet is false when the separator defaults to "\n"; a nil separator -// (read the whole remainder) is reported as sepSet with sep == "" and nilSep. -func parseGetsArgs(args []object.Value) (sep getsSep, limit int, chomp bool) { - sep, limit = getsSep{s: "\n"}, -1 - if h, ok := lastHash(args); ok { - if v, ok := h.Get(object.Symbol("chomp")); ok { - chomp = v.Truthy() - } - args = args[:len(args)-1] - } - if len(args) > 0 { - switch a := args[0].(type) { - case object.Integer: - limit = int(a) - case *object.String: - sep = getsSep{s: a.Str(), set: true} - default: - if args[0] == object.NilV { - sep = getsSep{s: "", set: true, nilSep: true} - } - } - } - if len(args) > 1 { - if n, ok := args[1].(object.Integer); ok { - limit = int(n) - } - } - return sep, limit, chomp +// ioGets reads one line from o using MRI's (sep, limit, chomp:) argument shapes, +// returning nil at end of input and advancing #lineno on a successful read. It is +// the single-shot entry used by ARGF#gets and the IRB reader; the coercion and +// $/ default live in resolveGetsArgs, shared with the StringIO/IO gets family. +func (vm *VM) ioGets(o *IOObj, args []object.Value) object.Value { + sep, limit, chomp := vm.resolveGetsArgs(args) + return vm.ioGetsResolved(o, sep, limit, chomp) } // enumSizeNil is the #size block for a reader Enumerator (each_line/each_char/ @@ -1076,16 +1038,6 @@ func checkResolvedLimit(limit int, meth string) { } } -// checkGetsLimit raises ArgumentError for an explicit limit of 0 on a -// line-iterating read (readlines/each_line/foreach/each): a 0-byte line never -// advances the cursor, so MRI rejects it rather than looping. A single #gets(0) -// is allowed (it just returns "") and does not go through this guard. -func checkGetsLimit(args []object.Value, meth string) { - if _, limit, _ := parseGetsArgs(args); limit == 0 { - raise("ArgumentError", "invalid limit: 0 for %s", meth) - } -} - // getsSep is a resolved line separator for ioGetsLine. type getsSep struct { s string diff --git a/internal/vm/io_residuals_test.go b/internal/vm/io_residuals_test.go index 54398eef..881b5269 100644 --- a/internal/vm/io_residuals_test.go +++ b/internal/vm/io_residuals_test.go @@ -1,6 +1,8 @@ package vm_test import ( + "os" + "path/filepath" "strings" "testing" ) @@ -118,3 +120,159 @@ func TestIOResiduals(t *testing.T) { } } } + +// TestIOClassReaders covers the IO.readlines / IO.foreach class methods' +// separator/limit/$/ and chomp handling, and the mode-flag decoding, against +// MRI Ruby 4.0.6. The reader class methods open a real file, so a temp file is +// written first. +func TestIOClassReaders(t *testing.T) { + dir := filepath.ToSlash(t.TempDir()) + write := func(name, content string) string { + p := dir + "/" + name + if err := os.WriteFile(filepath.FromSlash(p), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + return `"` + p + `"` + } + cases := []struct{ src, want string }{ + // A #to_str-convertible separator splits the lines. + func() (c struct{ src, want string }) { + c.src = `o = Object.new; def o.to_str; ">"; end; p IO.readlines(` + write("r1", "a>b>c") + `, o)` + c.want = "[\"a>\", \"b>\", \"c\"]\n" + return + }(), + // An Integer limit caps each line's byte length. + func() (c struct{ src, want string }) { + c.src = `p IO.readlines(` + write("r2", "hello world") + `, 4)` + c.want = "[\"hell\", \"o wo\", \"rld\"]\n" + return + }(), + // The default separator follows $/. + func() (c struct{ src, want string }) { + c.src = `$/ = " "; r = IO.readlines(` + write("r3", "an example here") + `); $/ = "\n"; p r` + c.want = "[\"an \", \"example \", \"here\"]\n" + return + }(), + // chomp: as a keyword option strips the separator. + func() (c struct{ src, want string }) { + c.src = `p IO.readlines(` + write("r4", "x\ny\n") + `, chomp: true)` + c.want = "[\"x\", \"y\"]\n" + return + }(), + // IO.foreach with a block yields each line; the separator is honoured. + func() (c struct{ src, want string }) { + c.src = `a = []; IO.foreach(` + write("r5", "p>q>r") + `, ">") { |l| a << l }; p a` + c.want = "[\"p>\", \"q>\", \"r\"]\n" + return + }(), + } + 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) + } + } + + // A zero limit for the line-iterating class readers is rejected. + if err := runErr(t, `IO.readlines(`+write("e1", "x\ny")+`, 0)`); err == nil || !strings.Contains(err.Error(), "invalid limit: 0 for readlines") { + t.Errorf("IO.readlines(path, 0) got %v", err) + } +} + +// TestStringIOModeFlags covers the remaining StringIO mode variants — a+, w+, +// binary/text suffixes, and encoding-tagged modes — against MRI Ruby 4.0.6. +func TestStringIOModeFlags(t *testing.T) { + const req = `require "stringio"; ` + cases := []struct{ src, want string }{ + // "a+" appends on write but is readable from the start. + {req + `s = StringIO.new(+"data", "a+"); a = s.read; s.write("X"); p [a, s.string]`, + "[\"data\", \"dataX\"]\n"}, + // "w+" truncates yet stays readable and writable. + {req + `s = StringIO.new(+"data", "w+"); s.write("hi"); s.rewind; p [s.read, s.string]`, + "[\"hi\", \"hi\"]\n"}, + // A "rb" (binary) suffix keeps read access; a trailing :enc is tolerated. + {req + `p [StringIO.new(+"x", "rb").read, StringIO.new(+"x", "r:utf-8").read]`, + "[\"x\", \"x\"]\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) + } + } +} + +// TestIOResidualBranches exercises the less-travelled branches of the reader/ +// writer paths so they are covered: enumerator forms, recursive-array puts, +// paragraph mode on a real IO (two-newline rule, chomp with and without a +// terminating run, leading blanks, EOF), a real-String read buffer, and File +// modes that gate reads/writes. Values verified against MRI Ruby 4.0.6. +func TestIOResidualBranches(t *testing.T) { + const req = `require "stringio"; ` + cases := []struct{ src, want string }{ + // Enumerator forms (no block) iterate correctly and report #size nil. + {req + `p StringIO.new("ab").each_char.to_a`, "[\"a\", \"b\"]\n"}, + {req + `p StringIO.new("ab").each_byte.to_a`, "[97, 98]\n"}, + {req + `p StringIO.new("x\ny\n").each_line.to_a`, "[\"x\\n\", \"y\\n\"]\n"}, + {req + `p StringIO.new("ab").each_char.size`, "nil\n"}, + // puts writes [...] for a self-referential array and appends a newline only + // when the string does not already end in one. + {req + `a = [1]; a << a; s = StringIO.new; s.puts(a); p s.string`, "\"1\\n[...]\\n\"\n"}, + {req + `s = StringIO.new; s.puts("x\n", "y"); p s.string`, "\"x\\ny\\n\"\n"}, + {req + `s = StringIO.new; s.puts([]); p s.string`, "\"\"\n"}, + // Paragraph mode edges: leading blank lines skipped; chomp strips a + // terminating run but keeps a lone EOF newline; nil at true EOF. + {req + `p StringIO.new("\n\nabc").gets("")`, "\"abc\"\n"}, + {req + `p StringIO.new("a\n\nb\n").each_line("", chomp: true).to_a`, "[\"a\", \"b\\n\"]\n"}, + {req + `s = StringIO.new("x"); s.read; p s.gets("")`, "nil\n"}, + // A real String read buffer is filled in place and returned. + {req + `s = StringIO.new("hello"); b = +"z"; r = s.read(3, b); p [r, b, r.equal?(b)]`, + "[\"hel\", \"hel\", true]\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) + } + } +} + +// TestFileModeAccess covers openFileIO's mode-driven read/write gating: a File +// opened write-only rejects reads, append-only rejects reads and appends every +// write at end, and read-only rejects writes. Verified against MRI Ruby 4.0.6. +func TestFileModeAccess(t *testing.T) { + dir := filepath.ToSlash(t.TempDir()) + path := func(name, content string) string { + p := dir + "/" + name + if err := os.WriteFile(filepath.FromSlash(p), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + return `"` + p + `"` + } + // Append mode writes at end and reports the appended content on reopen. + a1 := path("a1", "base") + if got := eval(t, `File.open(`+a1+`, "a") { |f| f.write("+more") }; p File.read(`+a1+`)`); got != "\"base+more\"\n" { + t.Errorf("append-mode File = %q", got) + } + oks := []struct{ src, want string }{ + // Paragraph mode on a real File keeps only the two terminating newlines. + {`p(File.open(` + path("pg", "a\n\n\nb") + `, "r") { |f| f.gets("") })`, "\"a\\n\\n\"\n"}, + // A character device (non-regular) opens with an empty buffer, so a read + // sees end-of-file rather than an unbounded stream. + {`p(File.open("/dev/null", "r") { |f| f.read })`, "\"\"\n"}, + // A write-only File with no explicit encoding reports a nil external encoding. + {`p(File.open(` + path("we", "") + `, "w") { |f| f.external_encoding })`, "nil\n"}, + } + for _, c := range oks { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } + errs := []struct{ src, want string }{ + {`File.open(` + path("w1", "x") + `, "w") { |f| f.read }`, "not opened for reading"}, + {`File.open(` + path("a2", "x") + `, "a") { |f| f.gets }`, "not opened for reading"}, + {`File.open(` + path("r1", "x") + `, "r") { |f| f.write("y") }`, "not opened for writing"}, + } + for _, c := range errs { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q got=%v want error containing %q", c.src, err, c.want) + } + } +} diff --git a/internal/vm/irb.go b/internal/vm/irb.go index 119d330b..4ed66212 100644 --- a/internal/vm/irb.go +++ b/internal/vm/irb.go @@ -106,7 +106,7 @@ func (vm *VM) irbRun(b *Binding, in, out *IOObj, mode irb.PromptMode, mainStr st verdict, opens := irb.CheckCode(buf) out.writeStr(irb.GeneratePrompt(mode, ctx, opens, verdict == irb.More, lineNo, true, mode.AutoIndent)) - line := ioGets(in, nil) + line := vm.ioGets(in, nil) s, ok := line.(*object.String) if !ok { out.writeStr("\n") From 299c0d74555623d0b2e00e2967cc45a68a96b9ad Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:43:53 +0200 Subject: [PATCH 4/6] io: cover the all-blank paragraph-to-EOF branch Add a StringIO.gets("") case over a buffer of only blank lines, which the leading-blank skip consumes to end-of-input, exercising ioGetsParagraph's end-of-buffer nil return. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_residuals_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/vm/io_residuals_test.go b/internal/vm/io_residuals_test.go index 881b5269..08a58f71 100644 --- a/internal/vm/io_residuals_test.go +++ b/internal/vm/io_residuals_test.go @@ -221,6 +221,8 @@ func TestIOResidualBranches(t *testing.T) { // Paragraph mode edges: leading blank lines skipped; chomp strips a // terminating run but keeps a lone EOF newline; nil at true EOF. {req + `p StringIO.new("\n\nabc").gets("")`, "\"abc\"\n"}, + // A buffer of only blank lines is consumed to EOF and yields nil. + {req + `p StringIO.new("\n\n").gets("")`, "nil\n"}, {req + `p StringIO.new("a\n\nb\n").each_line("", chomp: true).to_a`, "[\"a\", \"b\\n\"]\n"}, {req + `s = StringIO.new("x"); s.read; p s.gets("")`, "nil\n"}, // A real String read buffer is filled in place and returned. From 58d38368ba088e537a8a85220eac5100a1768033 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:50:39 +0200 Subject: [PATCH 5/6] io: cover the append-to-device branch of openFileIO Add a File.open("/dev/null", "a") case: a non-regular file opens with an empty buffer and its write reports the byte count, exercising openFileIO's append-mode notRegular branch. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_residuals_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/vm/io_residuals_test.go b/internal/vm/io_residuals_test.go index 08a58f71..0a980ed4 100644 --- a/internal/vm/io_residuals_test.go +++ b/internal/vm/io_residuals_test.go @@ -257,8 +257,10 @@ func TestFileModeAccess(t *testing.T) { // Paragraph mode on a real File keeps only the two terminating newlines. {`p(File.open(` + path("pg", "a\n\n\nb") + `, "r") { |f| f.gets("") })`, "\"a\\n\\n\"\n"}, // A character device (non-regular) opens with an empty buffer, so a read - // sees end-of-file rather than an unbounded stream. + // sees end-of-file rather than an unbounded stream; appending to one just + // reports the byte count without buffering readable content. {`p(File.open("/dev/null", "r") { |f| f.read })`, "\"\"\n"}, + {`p(File.open("/dev/null", "a") { |f| f.write("x") })`, "1\n"}, // A write-only File with no explicit encoding reports a nil external encoding. {`p(File.open(` + path("we", "") + `, "w") { |f| f.external_encoding })`, "nil\n"}, } From a9a635eb1f0e09c2d43bc42117cececbc1fea2ca Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 20:53:52 +0200 Subject: [PATCH 6/6] test: gate /dev/null File cases in TestFileModeAccess to non-Windows The character-device subcases open "/dev/null", which does not exist on Windows (its null device is NUL), failing test (windows-latest). Guard the two device cases behind runtime.GOOS != "windows"; the rest of the mode gating still runs on every platform. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_residuals_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/vm/io_residuals_test.go b/internal/vm/io_residuals_test.go index 0a980ed4..0937dac5 100644 --- a/internal/vm/io_residuals_test.go +++ b/internal/vm/io_residuals_test.go @@ -3,6 +3,7 @@ package vm_test import ( "os" "path/filepath" + "runtime" "strings" "testing" ) @@ -256,14 +257,19 @@ func TestFileModeAccess(t *testing.T) { oks := []struct{ src, want string }{ // Paragraph mode on a real File keeps only the two terminating newlines. {`p(File.open(` + path("pg", "a\n\n\nb") + `, "r") { |f| f.gets("") })`, "\"a\\n\\n\"\n"}, - // A character device (non-regular) opens with an empty buffer, so a read - // sees end-of-file rather than an unbounded stream; appending to one just - // reports the byte count without buffering readable content. - {`p(File.open("/dev/null", "r") { |f| f.read })`, "\"\"\n"}, - {`p(File.open("/dev/null", "a") { |f| f.write("x") })`, "1\n"}, // A write-only File with no explicit encoding reports a nil external encoding. {`p(File.open(` + path("we", "") + `, "w") { |f| f.external_encoding })`, "nil\n"}, } + if runtime.GOOS != "windows" { + // A character device (non-regular) opens with an empty buffer, so a read + // sees end-of-file rather than an unbounded stream; appending to one just + // reports the byte count without buffering readable content. /dev/null is + // POSIX-only (Windows spells the null device NUL), so gate these two. + oks = append(oks, + struct{ src, want string }{`p(File.open("/dev/null", "r") { |f| f.read })`, "\"\"\n"}, + struct{ src, want string }{`p(File.open("/dev/null", "a") { |f| f.write("x") })`, "1\n"}, + ) + } for _, c := range oks { if got := eval(t, c.src); got != c.want { t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)