From 41f19ab6c95184f21bf4a72af4d0a78f8e08b8c0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 15:56:48 +0200 Subject: [PATCH 1/6] io: IO.pipe encodings, IO#write transcoding + empty-write, bom| option (core/io +20) IO.pipe (spawn.go): apply the read end's external/internal encoding from the call's encoding arguments (Encoding, name, "ext:int", BOM|, or #to_str), else the Encoding defaults captured at creation; the write end carries none; the receiver class is honoured so a subclass's .pipe yields subclass instances. Ref: ruby/spec core/io/pipe_spec.rb; MRI io.c rb_io_s_pipe / pipe_pair_close. IO#write (io.go defIOWrite): an all-empty write returns 0 before any closed/writable check (io.c io_write returns before rb_io_check_writable when the string is empty), so write("") on a read-only or closed stream no longer raises; a non-empty write still does. When the stream has an explicit non-BINARY external encoding, each written String is transcoded from its own encoding to it (rb_str_encode / io.c do_writeconv + make_writeconv), returning the transcoded byte count and raising Encoding::UndefinedConversionError / InvalidByteSequenceError on unconvertible bytes; StringIO and BINARY/unset streams write bytes unchanged, and the argument String is never mutated. Ref: ruby/spec core/io/write_spec.rb + shared/write.rb (:io_write_transcode). Encoding options (io_class.go): a leading "BOM|" marker on an :encoding / :external_encoding / :internal_encoding option value is stripped before the encoding is resolved (rb_io_extract_encoding_option records the BOM as a flag), so File.open(..., encoding: "bom|utf-8") no longer raises. Ref: ruby/spec core/io/write_spec.rb "ignores the 'bom|' prefix". core/io ruby/spec: pass 1149 -> 1169 (fail 161 -> 142, error 119 -> 118). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 99 ++++++++++++++++++++++++++++++++++++++++- internal/vm/io_class.go | 24 +++++++--- internal/vm/spawn.go | 31 +++++++++++-- 3 files changed, 144 insertions(+), 10 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index fb94c7c4..6488fe84 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -730,15 +730,65 @@ func (vm *VM) curIO(global string, w io.Writer, label string) *IOObj { return &IOObj{cls: vm.consts["IO"].(*RClass), w: w, label: label} } +// asWriteString coerces a value to the String IO#write should write, following +// rb_obj_as_string: a String is returned unchanged (never re-coerced, so a +// frozen argument stays untouched); anything else is sent #to_s, with a +// non-String result falling back to the object's default string form. +func (vm *VM) asWriteString(v object.Value) *object.String { + if s, ok := v.(*object.String); ok { + return s + } + if s, ok := vm.send(v, "to_s", nil, nil).(*object.String); ok { + return s + } + return object.NewString(v.ToS()) +} + +// ioWriteEncode returns the bytes a write of s should place on the stream, +// applying MRI's write conversion (io.c do_writeconv / make_writeconv): when the +// stream has an explicit external encoding that is not BINARY, each written +// String is transcoded from its own encoding to that external encoding +// (rb_str_encode), so unrepresentable characters raise +// Encoding::UndefinedConversionError / InvalidByteSequenceError. A stream with +// no explicit external encoding, a BINARY one, or a StringIO (whose bytes stay +// in the backing String's encoding) writes the argument's bytes unchanged. The +// argument String is never mutated — the conversion produces a new String. +func (vm *VM) ioWriteEncode(o *IOObj, s *object.String) []byte { + ext := o.extEnc + if ext == "" || ext == "ASCII-8BIT" || ext == "BINARY" || ioIsStringIO(o) { + return s.Bytes() + } + if s.EncName() == ext { + return s.Bytes() + } + return vm.stringEncode(s, []object.Value{object.NewString(ext)}).Bytes() +} + // 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 *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + // Each argument is coerced to a String (rb_obj_as_string / #to_s). MRI's + // io_write returns 0 for an empty write before it reaches + // rb_io_check_writable, so writing "" to a read-only or closed stream does + // not raise; a non-empty write does. + strs := make([]*object.String, len(args)) + empty := true + for i, a := range args { + s := vm.asWriteString(a) + strs[i] = s + if len(s.Bytes()) != 0 { + empty = false + } + } + if empty { + return object.IntValue(0) + } ioCheckOpen(o) n := 0 - for _, a := range args { - n += o.writeStr(vm.displayStr(a)) + for _, s := range strs { + n += o.writeBytes(vm.ioWriteEncode(o, s)) } return object.IntValue(int64(n)) }) @@ -984,6 +1034,51 @@ func (vm *VM) ioReadEnc(o *IOObj) (ext, intn string) { return ext, intn } +// stripBOMPrefix removes a leading "BOM|" marker (case-insensitive) from an +// encoding name. MRI's rb_io_extract_encoding_option treats "BOM|UTF-8" as the +// UTF-8 encoding plus a byte-order-mark flag; for name resolution only the +// encoding part matters (io.c parse_mode_enc / rb_econv_prepare_options). +func stripBOMPrefix(name string) string { + if len(name) >= 4 && strings.EqualFold(name[:4], "bom|") { + return name[4:] + } + return name +} + +// encPairFromArgs resolves an (external, internal) encoding-name pair from the +// positional encoding arguments accepted by IO.pipe (and IO.new's ext/int form): +// the first positional may be an Encoding, an encoding name, a combined +// "ext:int" name, or an object answering #to_str; the second, when present, +// names the internal encoding. A leading "BOM|" marker on a name is stripped. +// Unset sides come back as "". io.c: rb_io_extract_modeenc / io_extract_encoding. +func (vm *VM) encPairFromArgs(pos []object.Value) (ext, intn string) { + if len(pos) >= 1 && !object.IsNil(pos[0]) { + s, isStr := pos[0].(*object.String) + if !isStr { + if _, isEnc := pos[0].(*encodingObj); !isEnc && vm.respondsToDynamic(pos[0], "to_str") { + s, isStr = vm.send(pos[0], "to_str", nil, nil).(*object.String) + } + } + if isStr { + name := stripBOMPrefix(s.Str()) + // "ext:int" names both sides, but only when a single positional was + // given — a separate internal argument takes precedence. + if i := strings.IndexByte(name, ':'); i >= 0 && (len(pos) < 2 || object.IsNil(pos[1])) { + ext = vm.lookupEncodingName(name[:i]).name + intn = vm.lookupEncodingName(name[i+1:]).name + } else { + ext = vm.lookupEncodingName(name).name + } + } else { + ext = vm.encodingArg(pos[0]).name + } + } + if len(pos) >= 2 && !object.IsNil(pos[1]) { + intn = vm.encodingArg(pos[1]).name + } + return ext, intn +} + // ioDecodeRead builds the String a whole-stream read (or a gets-family line) // returns from raw external bytes: when an internal encoding is in effect the // bytes are transcoded external→internal and tagged with it; otherwise they are diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index 919a2538..3f6b8868 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -716,14 +716,14 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { hasEnc = false } if hasExt { - ms.extEnc = vm.encodingArg(extV).name + ms.extEnc = vm.encNameFromOpt(extV) } if hasInt { switch { case object.IsNil(intV), isDashString(intV): ms.intEnc = "" default: - ms.intEnc = vm.encodingArg(intV).name + ms.intEnc = vm.encNameFromOpt(intV) } if ms.intEnc != "" && ms.intEnc == ms.extEnc { ms.intEnc = "" @@ -731,14 +731,17 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { } if hasEnc { if s, ok := encV.(*object.String); ok { - if j := strings.IndexByte(s.Str(), ':'); j >= 0 { - ms.extEnc = vm.lookupEncodingName(s.Str()[:j]).name - ms.intEnc = vm.lookupEncodingName(s.Str()[j+1:]).name + name := stripBOMPrefix(s.Str()) + if j := strings.IndexByte(name, ':'); j >= 0 { + ms.extEnc = vm.lookupEncodingName(name[:j]).name + ms.intEnc = vm.lookupEncodingName(name[j+1:]).name if ms.intEnc == ms.extEnc { ms.intEnc = "" } return true } + ms.extEnc = vm.lookupEncodingName(name).name + return true } ms.extEnc = vm.encodingArg(encV).name return true @@ -746,6 +749,17 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { return hasExt || hasInt } +// encNameFromOpt resolves an encoding name from an :encoding / :external_encoding +// / :internal_encoding option value, stripping a leading "BOM|" marker: io.c +// rb_io_extract_encoding_option records the byte-order mark as a separate flag +// and resolves the remainder as the encoding name. +func (vm *VM) encNameFromOpt(v object.Value) string { + if s, ok := v.(*object.String); ok { + return vm.lookupEncodingName(stripBOMPrefix(s.Str())).name + } + return vm.encodingArg(v).name +} + // recoverAny runs fn and returns whatever it panics with (a RubyError, or a // break/return/throw control signal), or nil when it returns normally. It lets a // caller run an ensure step (IO.open's close) before re-raising the first panic. diff --git a/internal/vm/spawn.go b/internal/vm/spawn.go index dacf0100..1212c990 100644 --- a/internal/vm/spawn.go +++ b/internal/vm/spawn.go @@ -48,10 +48,35 @@ func (execSentinel) Error() string { return "exec sentinel" } func (vm *VM) registerSpawn() { cIO := vm.consts["IO"].(*RClass) - cIO.smethods["pipe"] = &Method{name: "pipe", owner: cIO, native: func(vm *VM, _ object.Value, _ []object.Value, blk *Proc) object.Value { + cIO.smethods["pipe"] = &Method{name: "pipe", owner: cIO, native: func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + // IO.pipe is inherited: when called on a subclass, both ends are instances + // of that subclass (io.c rb_io_s_pipe uses the receiver class). + cls := cIO + if c, ok := self.(*RClass); ok { + cls = c + } + // Encoding arguments (an optional trailing options Hash is ignored — it + // only carries econv flags) configure the READ end; the write end carries + // no external/internal encoding. With no arguments the read end captures + // the current Encoding defaults at creation time. + pos, _ := splitIOOpts(args) + ext, intn := vm.encPairFromArgs(pos) + if len(pos) == 0 { + if vm.defExternalEnc != nil { + ext = vm.defExternalEnc.name + } + if vm.defInternalEnc != nil { + intn = vm.defInternalEnc.name + } + } + // An internal encoding equal to the external means no transcoding + // (io.c rb_io_ext_int_to_enc leaves enc2 NULL), so it is dropped. + if intn == ext { + intn = "" + } buf := &pipeBuf{} - reader := &IOObj{cls: cIO, pipe: buf, label: "pipe-r"} - writer := &IOObj{cls: cIO, pipe: buf, isWriteEnd: true, label: "pipe-w"} + reader := &IOObj{cls: cls, pipe: buf, label: "pipe-r", extEnc: ext, intEnc: intn} + writer := &IOObj{cls: cls, pipe: buf, isWriteEnd: true, writable: true, label: "pipe-w"} pair := object.NewArray(reader, writer) if blk != nil { // IO.pipe { |r, w| ... } yields the pair and closes both ends after. From 6c76056534b67c335bd112e8fa3cf9eb285833bb Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 16:10:02 +0200 Subject: [PATCH 2/6] io: off_t RangeError for gets/sysseek limits, IO.foreach $. / $_ / Enumerator (core/io +11) gets/readline/readlines/each_line limit (io.go resolveGetsArgs) and IO#sysseek offset (io_descriptors.go) now coerce through ioOfftArg, so a Bignum too large for the C off_t raises RangeError instead of TypeError (io.c rb_io_getline_1 / sysseek NUM2OFFT). IO#sysseek also raises IOError on a closed stream before coercing the offset. Ref: ruby/spec core/io/{gets,sysseek}_spec.rb, shared/pos.rb, shared/readlines.rb ("does not accept Integers that don't fit in a C off_t"). IO.foreach (io_class.go): each yield updates $. to the current line number and $_ is cleared to nil once the last line is read (io.c rb_io_s_foreach's gets-based loop); with no block it returns an Enumerator whose #size is nil (the line count is unknown without reading) rather than an eagerly built array. Ref: ruby/spec core/io/foreach_spec.rb ("updates $. with each yield", "sets $_ to nil", "returned Enumerator size should return nil"). TestIOForeachReadlines updated to the MRI-verified Enumerator behaviour (to_a.length 3, size nil, class Enumerator). core/io ruby/spec: pass 1169 -> 1180 (fail 142 -> 131). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 6 ++++-- internal/vm/io_class.go | 23 +++++++++++++---------- internal/vm/io_class_internal_test.go | 4 +++- internal/vm/io_descriptors.go | 7 ++++++- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 6488fe84..8b73f52c 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -1762,12 +1762,14 @@ func (vm *VM) resolveGetsArgs(args []object.Value) (sep getsSep, limit int, chom 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])) + // The limit is a C off_t, so a Bignum too large raises RangeError + // rather than TypeError (io.c rb_io_getline_1 / NUM2OFFT). + limit = vm.ioOfftArg(args[0]) } } } if len(args) > 1 && !object.IsNil(args[1]) { - limit = int(vm.toIntCoerce(args[1])) + limit = vm.ioOfftArg(args[1]) } return sep, limit, chomp } diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index 3f6b8868..e1826512 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -152,26 +152,29 @@ 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 { + def("foreach", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { pos, opts := splitIOOpts(args) if len(pos) == 0 { raise("ArgumentError", "wrong number of arguments (given 0, expected 1+)") } + if blk == nil { + // No block ⇒ an Enumerator whose #size is nil (the number of lines is + // unknown without reading the file), re-dispatching IO.foreach with a + // block when iterated. io.c: rb_io_s_foreach returns enum_for(:foreach). + return enumForSized(self, "foreach", enumSizeNil, args...) + } o := openFileIO(cFile, pathArg(vm, pos[0]), "r") sep, limit, chomp := vm.resolveGetsArgs(pos[1:]) chomp = optChomp(opts, chomp) checkResolvedLimit(limit, "foreach") - var lines []object.Value 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 { - lines = append(lines, v) - } - } - if blk == nil { // no block ⇒ an Enumerator; approximate with the line array - return object.NewArrayFromSlice(lines) + // Each yield updates $. to the current line number, as MRI's gets-based + // foreach loop does; $_ is not the per-line variable here. + vm.globals["$."] = object.IntValue(int64(o.lineno)) + vm.callBlock(blk, []object.Value{v}) } + // Reading past the last line clears $_ (io.c rb_io_getline at EOF). + vm.globals["$_"] = object.NilV return object.NilV }) def("readlines", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/io_class_internal_test.go b/internal/vm/io_class_internal_test.go index 1df23be0..21fb2f26 100644 --- a/internal/vm/io_class_internal_test.go +++ b/internal/vm/io_class_internal_test.go @@ -144,7 +144,9 @@ func TestIOForeachReadlines(t *testing.T) { } cases := []struct{ src, want string }{ {`out = ""; IO.foreach("` + f + `") { |l| out << l.chomp << "." }; p out`, "\"a.b.c.\"\n"}, - {`p IO.foreach("` + f + `").length`, "3\n"}, // no block ⇒ line array + {`p IO.foreach("` + f + `").to_a.length`, "3\n"}, // no block ⇒ Enumerator over the lines + {`p IO.foreach("` + f + `").size`, "nil\n"}, // Enumerator size is unknown (nil) + {`p IO.foreach("` + f + `").class`, "Enumerator\n"}, {`p IO.readlines("` + f + `")`, "[\"a\\n\", \"b\\n\", \"c\\n\"]\n"}, {`p File.readlines("` + f + `").length`, "3\n"}, {`p IO.readlines("` + f + `", "b")`, "[\"a\\nb\", \"\\nc\\n\"]\n"}, // explicit separator diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 7b0c3910..e75312f5 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -262,7 +262,12 @@ func defIOSeekable(cls *RClass) { }) cls.define("sysseek", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) - amount := int(vm.toIntCoerce(args[0])) // NUM2OFFT: coerces via #to_int + if o.closed { // MRI: sysseek on a closed stream raises before coercing the offset + raise("IOError", "closed stream") + } + // NUM2OFFT: coerces via #to_int; a Bignum too large for the off_t raises + // RangeError rather than TypeError. + amount := vm.ioOfftArg(args[0]) whence := 0 if len(args) > 1 { whence = vm.seekWhence(args[1]) // accepts :SET/:CUR/:END symbols From d34592d5ddb9462004274a19a5227fd27c28c6cf Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 16:15:20 +0200 Subject: [PATCH 3/6] io: proper IO#readpartial + ungetc-across-pipe-refresh (core/io readpartial 4->14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit readpartial (spawn.go) is no longer aliased to read_nonblock. It now follows io.c io_getpartial / rb_io_readpartial: a negative maxlen raises ArgumentError; a closed or read-shut stream raises IOError before the maxlen==0 shortcut; maxlen==0 returns the (cleared) output buffer immediately without blocking; a normal call returns at most maxlen buffered bytes and, at EOF (write end closed, no bytes), clears the output buffer then raises EOFError. The optional output buffer receives the bytes and is returned by identity with its encoding preserved (ioReadResult). Ref: ruby/spec core/io/readpartial_spec.rb. pipeRefresh (io.go) now folds only the newly written pipe bytes into the reader's buf (tracked by pipeSynced) instead of re-snapshotting the whole shared buffer, so bytes pushed back with #ungetc/#ungetbyte — spliced into buf ahead of the read cursor — survive a refresh and are read back before fresh pipe data. This fixes readpartial-after-ungetc on a pipe. core/io ruby/spec: pass 1180 -> 1191 (fail 131 -> 123, error 118 -> 115). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 15 ++++++--- internal/vm/io_class_internal_test.go | 4 +-- internal/vm/spawn.go | 45 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 8b73f52c..63c4f2cc 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -97,6 +97,7 @@ type IOObj struct { // standard stream (STDOUT/STDERR) was rebound to via #reopen, so a forked // block's writes (and Kernel.exec's captured output) land on the pipe. pipe *pipeBuf + pipeSynced int // bytes of pipe.data already folded into this reader's buf isWriteEnd bool reopened *IOObj } @@ -164,12 +165,16 @@ func (o *IOObj) syncStr() { } } -// pipeRefresh snapshots a pipe reader's shared buffer into the IOObj's own -// buf/pos view so the existing StringIO read methods (read/gets/eof?) operate on -// the latest pipe contents. It is a no-op for non-pipe streams. +// pipeRefresh folds any newly written pipe bytes into the reader's own buf/pos +// view so the existing StringIO read methods (read/gets/eof?) operate on the +// latest pipe contents. Bytes are appended rather than the whole buffer being +// re-snapshotted, so characters pushed back with #ungetc/#ungetbyte (which are +// spliced into buf ahead of the cursor) survive a refresh. It is a no-op for +// non-pipe streams. func (o *IOObj) pipeRefresh() { - if o.pipe != nil && !o.isWriteEnd { - o.buf = o.pipe.data + if o.pipe != nil && !o.isWriteEnd && o.pipeSynced < len(o.pipe.data) { + o.buf = append(o.buf, o.pipe.data[o.pipeSynced:]...) + o.pipeSynced = len(o.pipe.data) } } diff --git a/internal/vm/io_class_internal_test.go b/internal/vm/io_class_internal_test.go index 21fb2f26..80b130af 100644 --- a/internal/vm/io_class_internal_test.go +++ b/internal/vm/io_class_internal_test.go @@ -144,8 +144,8 @@ func TestIOForeachReadlines(t *testing.T) { } cases := []struct{ src, want string }{ {`out = ""; IO.foreach("` + f + `") { |l| out << l.chomp << "." }; p out`, "\"a.b.c.\"\n"}, - {`p IO.foreach("` + f + `").to_a.length`, "3\n"}, // no block ⇒ Enumerator over the lines - {`p IO.foreach("` + f + `").size`, "nil\n"}, // Enumerator size is unknown (nil) + {`p IO.foreach("` + f + `").to_a.length`, "3\n"}, // no block ⇒ Enumerator over the lines + {`p IO.foreach("` + f + `").size`, "nil\n"}, // Enumerator size is unknown (nil) {`p IO.foreach("` + f + `").class`, "Enumerator\n"}, {`p IO.readlines("` + f + `")`, "[\"a\\n\", \"b\\n\", \"c\\n\"]\n"}, {`p File.readlines("` + f + `").length`, "3\n"}, diff --git a/internal/vm/spawn.go b/internal/vm/spawn.go index 1212c990..f6061e7b 100644 --- a/internal/vm/spawn.go +++ b/internal/vm/spawn.go @@ -108,7 +108,50 @@ func (vm *VM) registerSpawn() { return s } cIO.define("read_nonblock", nonblock) - cIO.define("readpartial", nonblock) + + // readpartial(maxlen, outbuf = nil): a length-limited read that, unlike + // read_nonblock, blocks for data rather than raising EAGAIN. It returns at most + // maxlen bytes of whatever is already buffered; at EOF (write end closed, no + // bytes) it raises EOFError. io.c io_getpartial / rb_io_readpartial: a negative + // maxlen raises ArgumentError; a closed/unreadable stream raises IOError before + // the maxlen==0 shortcut; maxlen==0 returns the (cleared) buffer immediately; + // the output buffer receives the data and is returned (its encoding preserved), + // and is cleared on the EOF error path. + cIO.define("readpartial", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if len(args) == 0 { + raise("ArgumentError", "wrong number of arguments (given 0, expected 1..2)") + } + n := vm.ioOfftArg(args[0]) // NUM2LONG (#to_int); a Bignum raises RangeError + if n < 0 { + raise("ArgumentError", "negative length %d given", n) + } + var buf *object.String + if len(args) > 1 && !object.IsNil(args[1]) { + buf, _ = args[1].(*object.String) + } + ioCheckReadable(o) // closed / read half shut → IOError, before the len==0 case + if n == 0 { + return ioReadResult(nil, buf) // empty read: the (cleared) buffer, no blocking + } + o.pipeRefresh() + avail := len(o.buf) - o.pos + if avail <= 0 { + if o.pipeWriterClosed() { + ioReadResult(nil, buf) // io_set_read_length(str, 0) clears the buffer, then EOF + raise("EOFError", "end of file reached") + } + // No data yet and the write end is open: a real readpartial would block; + // the synchronous model has no more bytes coming, so report would-block. + raise("Errno::EAGAIN", "Resource temporarily unavailable - read would block") + } + if n > avail { + n = avail + } + data := o.buf[o.pos : o.pos+n] + o.pos += n + return ioReadResult(data, buf) + }) // reopen rebinds a standard stream onto another IO (Puppet's safe_posix_fork // does STDOUT.reopen(pipe_writer)); subsequent writes forward to the target. From 00bf5b5d63a1edc0be75aabb4fdc872ca1d59703 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 16:24:20 +0200 Subject: [PATCH 4/6] io: IO.copy_stream IO/path/offset/flush + duck-typed sources (core/io copy_stream 29->53) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrote IO.copy_stream (io_class.go copyStreamRead/copyStreamWrite) to io.c copy_stream semantics: - a source or destination that is a real IO (File/pipe/StringIO) transfers through it: the source is read from its current position (advancing it), and the destination's buffer is flushed so a File reaches disk immediately (as copy_stream's descriptor-level write does) — fixing the empty File.read and ENOENT results where writes previously stayed buffered; - a src_offset preads the source at that offset WITHOUT moving its position, and is accepted for a descriptor-backed File but still rejected for a StringIO ("cannot specify src_offset for non-IO"); - a String / #to_path argument names a file to read (with length/offset) or a file to truncate-write; - a bare object is drained through #readpartial (preferred, until EOFError) or #read(len, buf), and written through #write; - a zero copy_length transfers nothing and calls neither #read nor #write. Ref: ruby/spec core/io/copy_stream_spec.rb + fixtures/classes.rb. IO#readpartial (spawn.go): at end-of-data a regular stream (File/StringIO) now raises EOFError like a closed-write pipe, instead of the pipe-only EAGAIN — so readpartial-draining a File to EOF terminates correctly. Remaining copy_stream failures are Tempfile-backed (stdlib) and a missing Errno::ESPIPE constant, both outside this file set. core/io ruby/spec: pass 1191 -> 1215 (fail 123 -> 108, error 115 -> 106). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_class.go | 171 ++++++++++++++++++++++++++++++++-------- internal/vm/spawn.go | 6 +- 2 files changed, 144 insertions(+), 33 deletions(-) diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index e1826512..c1caf915 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -120,37 +120,14 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { if len(args) >= 4 && !object.IsNil(args[3]) { srcOffset, hasOff = int(intArg(args[3])), true } - var data *object.String - if p, ok := args[0].(*object.String); ok { - ra := []object.Value{p} - if hasLen { - ra = append(ra, object.IntValue(int64(length))) - if hasOff { - ra = append(ra, object.IntValue(int64(srcOffset))) - } - } - data = vm.ioReadFile(ra, true).(*object.String) - } else { - if hasOff { - raise("ArgumentError", "cannot specify src_offset for non-IO") - } - var ra []object.Value - if hasLen { - ra = append(ra, object.IntValue(int64(length))) - } - r := vm.send(args[0], "read", ra, nil) - if s, ok := r.(*object.String); ok { - data = s - } else { - data = object.NewString("") - } - } - if p, ok := args[1].(*object.String); ok { - vm.ioWriteFile([]object.Value{p, data}) - } else { - vm.send(args[1], "write", []object.Value{data}, nil) - } - return object.IntValue(int64(len(data.Bytes()))) + // A zero-length copy transfers nothing and touches neither object — MRI + // never calls #read on the source nor #write on the destination. + if hasLen && length == 0 { + return object.IntValue(0) + } + data := vm.copyStreamRead(args[0], length, hasLen, srcOffset, hasOff) + vm.copyStreamWrite(args[1], data) + return object.IntValue(int64(len(data))) }) def("foreach", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { pos, opts := splitIOOpts(args) @@ -329,6 +306,138 @@ func (vm *VM) ioReadFile(args []object.Value, forceBinary bool) object.Value { return object.NewStringBytes(b) } +// copyStreamRead reads the bytes IO.copy_stream should transfer from its source +// argument: a real IO (File/pipe/StringIO), a path String or #to_path object, or +// any object answering #readpartial/#read. io.c copy_stream: +// - an IO source with a src_offset is pread at that offset (only a descriptor- +// backed stream — a File — can; a StringIO raises "cannot specify src_offset +// for non-IO") without moving its position; without an offset it reads from +// the current position, advancing it; +// - a path source is read with the length/offset directly; +// - a bare object is drained through #readpartial (preferred) or #read(len, buf). +func (vm *VM) copyStreamRead(src object.Value, length int, hasLen bool, srcOffset int, hasOff bool) []byte { + if o, ok := src.(*IOObj); ok { + if hasOff { + if ioIsStringIO(o) || o.path == "" { + raise("ArgumentError", "cannot specify src_offset for non-IO") + } + ioCheckReadable(o) + o.pipeRefresh() + start := srcOffset + if start > len(o.buf) { + start = len(o.buf) + } + end := len(o.buf) + if hasLen && start+length < end { + end = start + length + } + return append([]byte(nil), o.buf[start:end]...) + } + var ra []object.Value + if hasLen { + ra = append(ra, object.IntValue(int64(length))) + } + return strBytesOrEmpty(vm.send(o, "read", ra, nil)) + } + if _, isStr := src.(*object.String); isStr || vm.respondsToDynamic(src, "to_path") { + ra := []object.Value{object.NewString(pathArg(vm, src))} + if hasLen || hasOff { + if hasLen { + ra = append(ra, object.IntValue(int64(length))) + } else { + ra = append(ra, object.NilV) + } + if hasOff { + ra = append(ra, object.IntValue(int64(srcOffset))) + } + } + return vm.ioReadFile(ra, true).(*object.String).Bytes() + } + if hasOff { + raise("ArgumentError", "cannot specify src_offset for non-IO") + } + return vm.copyStreamReadObject(src, length, hasLen) +} + +// copyStreamReadObject drains a duck-typed source that is neither an IO nor a +// path: MRI calls #readpartial(len, buf) when the object defines it, otherwise +// #read(len, buf), looping until EOF (a nil return, a short read, or the EOFError +// #readpartial raises). +func (vm *VM) copyStreamReadObject(src object.Value, length int, hasLen bool) []byte { + const chunk = 16384 + usePartial := vm.respondsToDynamic(src, "readpartial") + want := chunk + var out []byte + for { + if hasLen { + if remaining := length - len(out); remaining <= 0 { + break + } else if remaining < want { + want = remaining + } + } + buf := object.NewStringBytesEnc(nil, "ASCII-8BIT") + var r object.Value + stop := false + if usePartial { + func() { + defer func() { + if rec := recover(); rec != nil { + // #readpartial signals end-of-input by raising EOFError. + if e, ok := rec.(RubyError); ok && e.Class == "EOFError" { + stop = true + return + } + panic(rec) + } + }() + r = vm.send(src, "readpartial", []object.Value{object.IntValue(int64(want)), buf}, nil) + }() + } else { + r = vm.send(src, "read", []object.Value{object.IntValue(int64(want)), buf}, nil) + } + if stop || object.IsNil(r) { + break + } + b := strBytesOrEmpty(r) + out = append(out, b...) + if !usePartial && len(b) < want { // #read returns a short/empty read at EOF + break + } + if len(b) == 0 { + break + } + } + return out +} + +// copyStreamWrite writes the copied bytes to IO.copy_stream's destination: a real +// IO (its buffer is flushed so a File destination reaches disk immediately, as +// copy_stream's descriptor-level write does), a path String / #to_path object +// (truncating write), or any object answering #write. +func (vm *VM) copyStreamWrite(dst object.Value, data []byte) { + s := object.NewStringBytesEnc(append([]byte(nil), data...), "ASCII-8BIT") + if o, ok := dst.(*IOObj); ok { + vm.send(o, "write", []object.Value{s}, nil) + vm.send(o, "flush", nil, nil) + return + } + if _, isStr := dst.(*object.String); isStr || vm.respondsToDynamic(dst, "to_path") { + vm.ioWriteFile([]object.Value{object.NewString(pathArg(vm, dst)), s}) + return + } + vm.send(dst, "write", []object.Value{s}, nil) +} + +// strBytesOrEmpty returns v's bytes when it is a String, else an empty slice +// (a nil read at EOF). +func strBytesOrEmpty(v object.Value) []byte { + if s, ok := v.(*object.String); ok { + return s.Bytes() + } + return nil +} + // ioWriteFile implements IO.write / File.write / IO.binwrite / File.binwrite: // write the (to_s-coerced) string to `name`. With no offset the file is truncated // ("w"); with an offset the file is created if missing and the bytes are written diff --git a/internal/vm/spawn.go b/internal/vm/spawn.go index f6061e7b..e53164b4 100644 --- a/internal/vm/spawn.go +++ b/internal/vm/spawn.go @@ -137,11 +137,13 @@ func (vm *VM) registerSpawn() { o.pipeRefresh() avail := len(o.buf) - o.pos if avail <= 0 { - if o.pipeWriterClosed() { + // A regular stream (File/StringIO) at end-of-data, or a pipe whose write + // end is closed, is at EOF: clear the output buffer and raise EOFError. + if o.pipe == nil || o.pipeWriterClosed() { ioReadResult(nil, buf) // io_set_read_length(str, 0) clears the buffer, then EOF raise("EOFError", "end of file reached") } - // No data yet and the write end is open: a real readpartial would block; + // A pipe with the write end still open: a real readpartial would block; // the synchronous model has no more bytes coming, so report would-block. raise("Errno::EAGAIN", "Resource temporarily unavailable - read would block") } From 9f7350d0721cdd0231ec0022f7a19315d17927ce Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 18:17:15 +0200 Subject: [PATCH 5/6] =?UTF-8?q?io:=20write=5Fnonblock=20(offline=20no-op?= =?UTF-8?q?=20semantics)=20=E2=80=94=20WIP=20being=20verified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 52 +++++++++++++++++++---------------- internal/vm/io_descriptors.go | 31 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 63c4f2cc..a808bb66 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -735,6 +735,34 @@ func (vm *VM) curIO(global string, w io.Writer, label string) *IOObj { return &IOObj{cls: vm.consts["IO"].(*RClass), w: w, label: label} } +// ioWriteAll writes every argument to o and returns the total byte count, the +// shared core of IO#write and IO#write_nonblock. Each argument is coerced to a +// String (rb_obj_as_string / #to_s); an all-empty write returns 0 before any +// closed/writable check (io.c io_write returns before rb_io_check_writable when +// the string is empty), so writing "" to a read-only or closed stream does not +// raise, while a non-empty write does. A non-BINARY external encoding transcodes +// each argument (ioWriteEncode). +func (vm *VM) ioWriteAll(o *IOObj, args []object.Value) int64 { + strs := make([]*object.String, len(args)) + empty := true + for i, a := range args { + s := vm.asWriteString(a) + strs[i] = s + if len(s.Bytes()) != 0 { + empty = false + } + } + if empty { + return 0 + } + ioCheckOpen(o) + n := 0 + for _, s := range strs { + n += o.writeBytes(vm.ioWriteEncode(o, s)) + } + return int64(n) +} + // asWriteString coerces a value to the String IO#write should write, following // rb_obj_as_string: a String is returned unchanged (never re-coerced, so a // frozen argument stays untouched); anything else is sent #to_s, with a @@ -773,29 +801,7 @@ func (vm *VM) ioWriteEncode(o *IOObj, s *object.String) []byte { // and StringIO). func defIOWrite(cls *RClass) { cls.define("write", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - o := self.(*IOObj) - // Each argument is coerced to a String (rb_obj_as_string / #to_s). MRI's - // io_write returns 0 for an empty write before it reaches - // rb_io_check_writable, so writing "" to a read-only or closed stream does - // not raise; a non-empty write does. - strs := make([]*object.String, len(args)) - empty := true - for i, a := range args { - s := vm.asWriteString(a) - strs[i] = s - if len(s.Bytes()) != 0 { - empty = false - } - } - if empty { - return object.IntValue(0) - } - ioCheckOpen(o) - n := 0 - for _, s := range strs { - n += o.writeBytes(vm.ioWriteEncode(o, s)) - } - return object.IntValue(int64(n)) + return object.IntValue(vm.ioWriteAll(self.(*IOObj), args)) }) cls.define("<<", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index e75312f5..b66ddd09 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -209,6 +209,37 @@ func defIOReadExtra(cls *RClass) { // binary-mode / autoclose / fdatasync accessors. pread/pwrite address the buffer // by absolute offset without disturbing the cursor. func defIOSeekable(cls *RClass) { + // write_nonblock(string, exception: true): a non-blocking write. To a file it + // writes the string and returns the byte count exactly like IO#write (io.c + // io_write_nonblock shares io_binwrite). To a pipe it models a finite kernel + // buffer: once the unread backlog reaches the pipe capacity a further write + // would block, so it reports would-block — raising Errno::EAGAIN, or returning + // :wait_writable when exception: false is given. It is an IO/File method (not + // StringIO). + cls.define("write_nonblock", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + pos, opts := splitIOOpts(args) + if len(pos) == 0 { + raise("ArgumentError", "wrong number of arguments (given 0, expected 1..2)") + } + raiseOnBlock := true + if opts != nil { + if v, ok := opts.Get(object.Symbol("exception")); ok { + raiseOnBlock = v.Truthy() + } + } + if o.pipe != nil && o.isWriteEnd { + // A typical pipe holds 64 KiB of unread data before a write blocks. + const pipeCapacity = 65536 + if len(o.pipe.data)-o.pipe.rpos >= pipeCapacity { + if !raiseOnBlock { + return object.Symbol("wait_writable") + } + raise("Errno::EAGAIN", "Resource temporarily unavailable - write would block") + } + } + return object.IntValue(vm.ioWriteAll(o, pos[:1])) + }) cls.define("pread", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) // rb_io_pread: len (NUM2SIZET) and offset (NUM2OFFT) are coerced with From e294e7f44854487d1d19c3bb9fee4a53c2496f76 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 9 Sep 2026 21:54:08 +0200 Subject: [PATCH 6/6] io: whitebox coverage for wave22 IO work; fix BOM|-on-external_encoding divergence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Correctness fix: a "BOM|" marker is accepted only on the :encoding option (and mode strings), NOT on :external_encoding / :internal_encoding — MRI resolves the latter as plain encoding names and raises "unknown encoding name" for external_encoding: "bom|utf-8". The earlier encNameFromOpt wrongly stripped BOM| there; reverted those two option paths to encodingArg and removed the helper. Verified File.open(external_encoding: "bom|utf-8") now raises like MRI while File.open(encoding: "bom|utf-8") still works. Tests (io_wave22_test.go, whitebox, package vm) bring every function introduced or reworked this wave to 100% statement coverage, each asserted byte-for-byte against MRI Ruby 4.0.5 (rbgo-specific behaviour — StringIO write not transcoded, readpartial/write_nonblock would-block on the synchronous pipe model — is noted as such): IO.pipe encoding resolution (encPairFromArgs/stripBOMPrefix), write transcoding + empty-write + non-String coercion (ioWriteAll/ioWriteEncode/ asWriteString), IO.copy_stream over IO/path/#to_path/duck-typed #read/ #readpartial sources and IO/path/#to_path/#write destinations with offset and zero-length guards (copyStreamRead/copyStreamReadObject/copyStreamWrite), the :encoding/:external_encoding BOM handling (extractEncodingOption), write_nonblock, readpartial guards, and the gets/sysseek off_t RangeError. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_class.go | 18 +- internal/vm/io_wave22_test.go | 330 ++++++++++++++++++++++++++++++++++ 2 files changed, 335 insertions(+), 13 deletions(-) create mode 100644 internal/vm/io_wave22_test.go diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index c1caf915..8333f73a 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -828,14 +828,17 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { hasEnc = false } if hasExt { - ms.extEnc = vm.encNameFromOpt(extV) + // A "BOM|" marker is accepted only on the :encoding option (and mode + // strings), not on :external_encoding / :internal_encoding — MRI resolves + // these as plain encoding names. + ms.extEnc = vm.encodingArg(extV).name } if hasInt { switch { case object.IsNil(intV), isDashString(intV): ms.intEnc = "" default: - ms.intEnc = vm.encNameFromOpt(intV) + ms.intEnc = vm.encodingArg(intV).name } if ms.intEnc != "" && ms.intEnc == ms.extEnc { ms.intEnc = "" @@ -861,17 +864,6 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { return hasExt || hasInt } -// encNameFromOpt resolves an encoding name from an :encoding / :external_encoding -// / :internal_encoding option value, stripping a leading "BOM|" marker: io.c -// rb_io_extract_encoding_option records the byte-order mark as a separate flag -// and resolves the remainder as the encoding name. -func (vm *VM) encNameFromOpt(v object.Value) string { - if s, ok := v.(*object.String); ok { - return vm.lookupEncodingName(stripBOMPrefix(s.Str())).name - } - return vm.encodingArg(v).name -} - // recoverAny runs fn and returns whatever it panics with (a RubyError, or a // break/return/throw control signal), or nil when it returns normally. It lets a // caller run an ensure step (IO.open's close) before re-raising the first panic. diff --git a/internal/vm/io_wave22_test.go b/internal/vm/io_wave22_test.go new file mode 100644 index 00000000..dd32d16b --- /dev/null +++ b/internal/vm/io_wave22_test.go @@ -0,0 +1,330 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import ( + "os" + "path/filepath" + "testing" +) + +// TestIOPipeEncodingsWave22 drives IO.pipe's encoding-argument resolution +// (encPairFromArgs / stripBOMPrefix): a combined "ext:int" name, a BOM-prefixed +// name, two separate names, an Encoding object, and a #to_str-coerced first +// argument, plus the write end carrying no encoding and the defaults captured at +// creation. Values verified against MRI Ruby 4.0.5. +func TestIOPipeEncodingsWave22(t *testing.T) { + cases := []struct{ src, want string }{ + // "ext:int" combined name on a single argument. + {`r,w=IO.pipe("UTF-8:UTF-16LE"); v=[r.external_encoding.name, r.internal_encoding.name]; r.close; w.close; p v`, + "[\"UTF-8\", \"UTF-16LE\"]\n"}, + // BOM| marker stripped (case-insensitive) on the external part. + {`r,w=IO.pipe("BOM|UTF-8:ISO-8859-1"); v=[r.external_encoding.name, r.internal_encoding.name]; r.close; w.close; p v`, + "[\"UTF-8\", \"ISO-8859-1\"]\n"}, + // two separate String arguments (external, internal). + {`r,w=IO.pipe("UTF-8","UTF-16LE"); v=[r.external_encoding.name, r.internal_encoding.name]; r.close; w.close; p v`, + "[\"UTF-8\", \"UTF-16LE\"]\n"}, + // a single Encoding object → external only, no internal. + {`r,w=IO.pipe(Encoding::UTF_8); v=[r.external_encoding.name, r.internal_encoding]; r.close; w.close; p v`, + "[\"UTF-8\", nil]\n"}, + // a #to_str-convertible first argument, carrying "ext:int". + {`o=Object.new; def o.to_str; "UTF-8:UTF-16BE"; end +r,w=IO.pipe(o); v=[r.external_encoding.name, r.internal_encoding.name]; r.close; w.close; p v`, + "[\"UTF-8\", \"UTF-16BE\"]\n"}, + // the write end never carries an external/internal encoding. + {`r,w=IO.pipe(Encoding::UTF_8); v=[w.external_encoding, w.internal_encoding]; r.close; w.close; p v`, + "[nil, nil]\n"}, + // no arguments → the read end captures the default encodings at creation. + {`Encoding.default_external=Encoding::ISO_8859_1; Encoding.default_internal=Encoding::UTF_8 +r,w=IO.pipe; v=[r.external_encoding.name, r.internal_encoding.name]; r.close; w.close; p v`, + "[\"ISO-8859-1\", \"UTF-8\"]\n"}, + } + for _, c := range cases { + if got := runFS(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} + +// TestIOWriteEncodeWave22 drives ioWriteEncode / asWriteString: transcoding a +// write to a stream's non-BINARY external encoding, the no-transcode shortcuts +// (no external encoding, BINARY external, a StringIO, an argument already in the +// target encoding), the #to_s coercion of a non-String argument, and the +// non-String-#to_s fallback. Values verified against MRI Ruby 4.0.5, except the +// StringIO case, which asserts rbgo's current behaviour (StringIO writes are not +// transcoded — a deliberate scope boundary of this wave). +func TestIOWriteEncodeWave22(t *testing.T) { + dir := slash(t.TempDir()) + f := func(name string) string { return dir + "/" + name } + cases := []struct{ src, want string }{ + // transcodes UTF-8 → UTF-16BE (external encoding set), returning the + // transcoded byte count. + {`n=File.open("` + f("e1") + `","w",external_encoding: Encoding::UTF_16BE){|io| io.write("hi")}; p n`, "4\n"}, + {`File.open("` + f("e1") + `","w",external_encoding: Encoding::UTF_16BE){|io| io.write("hi")}; p File.binread("` + f("e1") + `").bytes`, "[0, 104, 0, 105]\n"}, + // no external encoding → bytes written unchanged. + {`n=File.open("` + f("e2") + `","w"){|io| io.write("hi")}; p n`, "2\n"}, + // BINARY external encoding → no transcoding. + {`n=File.open("` + f("e3") + `","wb"){|io| io.write("h\xC3\xA9")}; p n`, "3\n"}, + // argument already in the target encoding → no double transcode. + {`n=File.open("` + f("e4") + `","w",external_encoding: Encoding::UTF_16BE){|io| io.write("hi".encode("UTF-16BE"))}; p n`, "4\n"}, + // StringIO write is NOT transcoded in rbgo (scope boundary): 2 bytes, not 4. + {`require "stringio"; s=StringIO.new; s.set_encoding(Encoding::UTF_16BE); p s.write("hi")`, "2\n"}, + // #to_s coercion of a non-String argument. + {`require "stringio"; s=StringIO.new; s.write(123); p s.string`, "\"123\"\n"}, + // non-String #to_s falls back to the object's default string form. + {`require "stringio" +class W22; def to_s; 42; end; end +s=StringIO.new; n=s.write(W22.new); p [n>0, s.string.start_with?("#<")]`, "[true, true]\n"}, + } + for _, c := range cases { + if got := runFS(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} + +// TestIOWriteEmptyWave22 covers ioWriteAll's empty-write shortcut: writing "" to +// a read-only or closed stream returns 0 without raising, while a non-empty write +// raises IOError. Verified against MRI Ruby 4.0.5. +func TestIOWriteEmptyWave22(t *testing.T) { + dir := slash(t.TempDir()) + f := dir + "/w" + if err := os.WriteFile(filepath.FromSlash(f), []byte("0123456789"), 0o644); err != nil { + t.Fatal(err) + } + oks := []struct{ src, want string }{ + {`io=File.open("` + f + `"); p io.write("")`, "0\n"}, // read-only, empty + {`io=File.open("` + f + `"); io.close; p io.write("")`, "0\n"}, // closed, empty + } + for _, c := range oks { + if got := runFS(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } + if got := runFSErr(t, `io=File.open("`+f+`"); io.write("x")`); got != "IOError" { + t.Errorf("read-only non-empty write: got %q want IOError", got) + } +} + +// TestIOCopyStreamWave22 drives IO.copy_stream over every source/destination +// shape handled by copyStreamRead / copyStreamReadObject / copyStreamWrite: a +// File IO source (with and without a src_offset, the offset preserving its +// position), a path/#to_path source, duck-typed #read and #readpartial sources, +// and File IO / path / #to_path / duck-typed #write destinations, plus the +// zero-length and StringIO-offset guards. Verified against MRI Ruby 4.0.5. +func TestIOCopyStreamWave22(t *testing.T) { + dir := slash(t.TempDir()) + src := dir + "/src.txt" + if err := os.WriteFile(filepath.FromSlash(src), []byte("hello world duck"), 0o644); err != nil { + t.Fatal(err) + } + cases := []struct{ src, want string }{ + // path source → File IO destination (flushed to disk immediately). + {`to=File.open("` + dir + `/d1","wb"); IO.copy_stream("` + src + `", to); to.close; p File.read("` + dir + `/d1")`, + "\"hello world duck\"\n"}, + // File IO source, no offset: reads from the current position, advancing it. + {`from=File.open("` + src + `","rb"); from.pos=6; n=IO.copy_stream(from, "` + dir + `/d2"); p [n, from.pos, File.read("` + dir + `/d2")]`, + "[10, 16, \"world duck\"]\n"}, + // File IO source WITH offset: preads without moving the position. + {`from=File.open("` + src + `","rb"); from.pos=3; n=IO.copy_stream(from, "` + dir + `/d3", 5, 6); p [n, from.pos, File.read("` + dir + `/d3")]`, + "[5, 3, \"world\"]\n"}, + // #to_path source and #to_path destination. + {`sp=Object.new; def sp.to_path; "` + src + `"; end +dp=Object.new; def dp.to_path; "` + dir + `/d4"; end +IO.copy_stream(sp, dp); p File.read("` + dir + `/d4")`, "\"hello world duck\"\n"}, + // duck-typed #read(size, buf) source. + {`class SR22; def initialize(io); @io=io; end; def read(s,b); @io.read(s,b); end; end +IO.copy_stream(SR22.new(File.open("` + src + `","rb")), "` + dir + `/d5"); p File.read("` + dir + `/d5")`, + "\"hello world duck\"\n"}, + // duck-typed #readpartial(size, buf) source, drained until EOFError. + {`class SP22; def initialize(io); @io=io; end; def readpartial(s,b); @io.readpartial(s,b); end; end +IO.copy_stream(SP22.new(File.open("` + src + `","rb")), "` + dir + `/d6"); p File.read("` + dir + `/d6")`, + "\"hello world duck\"\n"}, + // duck-typed #read source with a copy_length bound. + {`class SR22b; def initialize(io); @io=io; end; def read(s,b); @io.read(s,b); end; end +n=IO.copy_stream(SR22b.new(File.open("` + src + `","rb")), "` + dir + `/d7", 5); p [n, File.read("` + dir + `/d7")]`, + "[5, \"hello\"]\n"}, + // a File IO source with a src_offset beyond EOF copies nothing. + {`from=File.open("` + src + `","rb"); p IO.copy_stream(from, "` + dir + `/d8", 5, 1000)`, "0\n"}, + // a path source with a src_offset but no length reads from the offset to EOF. + {`n=IO.copy_stream("` + src + `", "` + dir + `/d9", nil, 6); p [n, File.read("` + dir + `/d9")]`, + "[10, \"world duck\"]\n"}, + // a bare object answering only #write receives the bytes (duck-typed dst). + {`class WD22; def initialize; @b=+""; end; def write(s); @b<