diff --git a/internal/vm/io.go b/internal/vm/io.go index fb94c7c4..a808bb66 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) } } @@ -730,17 +735,73 @@ 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 +// 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) - ioCheckOpen(o) - n := 0 - for _, a := range args { - n += o.writeStr(vm.displayStr(a)) - } - 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) @@ -984,6 +1045,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 @@ -1667,12 +1773,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 919a2538..8333f73a 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -120,58 +120,38 @@ 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, _ 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 { @@ -326,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 @@ -716,6 +828,9 @@ func (vm *VM) extractEncodingOption(opts *object.Hash, ms *ioModeSpec) bool { hasEnc = false } if hasExt { + // 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 { @@ -731,14 +846,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 diff --git a/internal/vm/io_class_internal_test.go b/internal/vm/io_class_internal_test.go index 1df23be0..80b130af 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..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 @@ -262,7 +293,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 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< 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 { + // 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") + } + // 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") + } + 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.