From e893a2ccb0d73d2895673ac1f282942c6da32843 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 7 Sep 2026 23:33:44 +0200 Subject: [PATCH 1/4] io: pread/pwrite coercion, sysopen, dup, initialize, path/to_io/inspect (core/io) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Advance MRI-Ruby-4.0.x core/io conformance, reading MRI io.c (v3_4_0) and the ruby/spec expectations for each method: - IO#pread / IO#pwrite (io.c rb_io_pread/rb_io_pwrite): coerce maxlen/offset with #to_int (NUM2SIZET/NUM2OFFT) and the buffer with #to_str (io_setstrbuf → StringValue), pwrite coerces the object with #to_s (rb_obj_as_string); the maxlen==0 early return leaves a given buffer's bytes untouched. pread 14->22, pwrite 5->9. - IO.sysopen (io.c rb_io_s_sysopen): open the path into a buffered stream, register a synthetic descriptor (so IO.for_fd can wrap it) and return it; #to_path coerces the path. sysopen 0->5. - IO#dup (io.c rb_io_dup): an independent IO on a fresh descriptor with its own open/close state and the autoclose + close-on-exec flags always set, plus IO#close_on_exec? / #close_on_exec=. dup 1->7. - IO#initialize (io.c rb_io_initialize): reassociate the receiver with a descriptor, coercing the fd with #to_int and raising EBADF/IOError/TypeError as MRI does; the fd-adoption logic is shared with IO.for_fd/new via a new ioAdoptDescriptor helper. initialize 1->7. - IO#to_io (returns self), IO#path / IO#to_path (rb_io_path, incl. the "" pseudo-paths and IO.new's path: option) and IO#inspect (rb_io_inspect, "#" / path / "(closed)"). to_io 0->2, path 0->1, to_path 0->1, inspect 0->3. All changes verified byte-for-byte against MRI Ruby 4.0.5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 169 ++++++++++++++++++++++++++++++---- internal/vm/io_class.go | 91 +++++++++++++----- internal/vm/io_descriptors.go | 43 +++++---- internal/vm/io_wave19_test.go | 150 ++++++++++++++++++++++++++++++ 4 files changed, 394 insertions(+), 59 deletions(-) create mode 100644 internal/vm/io_wave19_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index 79df49cd..334e1746 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -3,12 +3,52 @@ package vm import ( "io" "os" + "strconv" "strings" "unicode/utf8" "github.com/go-embedded-ruby/ruby/internal/object" ) +// ioFd returns o's descriptor for #fileno/#to_i/#inspect: the fixed 0/1/2 for +// the standard streams (registering them in the fd table), or a distinct +// synthetic descriptor assigned on first request for any other stream. rbgo has +// no OS fds, so this is an identity, not a real descriptor. +func (vm *VM) ioFd(o *IOObj) int { + switch o.label { + case "STDIN": + vm.fdTable[0] = o + return 0 + case "STDOUT": + vm.fdTable[1] = o + return 1 + case "STDERR": + vm.fdTable[2] = o + return 2 + } + if o.fd == 0 { + vm.nextFd++ + o.fd = vm.nextFd + vm.fdTable[o.fd] = o + } + return o.fd +} + +// ioPathString reports o's backing path (rb_io_path / rb_io_inspect's pathv): the +// explicit File/IO path, or a standard stream's "" pseudo-path. The bool is +// false when no path is known (a plain fd wrapper or a pipe end), which #path +// renders as nil and #inspect as "fd N". +func ioPathString(o *IOObj) (string, bool) { + if o.path != "" { + return o.path, true + } + switch o.label { + case "STDIN", "STDOUT", "STDERR": + return "<" + o.label + ">", true + } + return "", false +} + // IOObj backs a Ruby IO and StringIO. A real IO (the $stdout/$stderr/$stdin // streams) writes to / reads from an os-level writer/reader; a StringIO is an // in-memory byte buffer with a read/write cursor. The two share the write path @@ -33,6 +73,9 @@ type IOObj struct { fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) binmode bool // opened in binary mode ("b"/binmode:) — #binmode? is true noAutoclose bool // autoclose: false was requested — #autoclose? is false + // close-on-exec defaults to set (#close_on_exec? is true), so the flag records + // only its clearing — a zero-value IOObj reports close-on-exec, as MRI does. + closeOnExecOff bool // strObj is the live String object backing a StringIO — MRI's StringIO holds // (and mutates in place) the very String passed to it, so #string returns that @@ -175,27 +218,119 @@ func (vm *VM) registerIO() { if o.closed { raise("IOError", "closed stream") } - switch o.label { - case "STDIN": - vm.fdTable[0] = o - return object.IntValue(0) - case "STDOUT": - vm.fdTable[1] = o - return object.IntValue(1) - case "STDERR": - vm.fdTable[2] = o - return object.IntValue(2) - } - if o.fd == 0 { - vm.nextFd++ - o.fd = vm.nextFd - vm.fdTable[o.fd] = o - } - return object.IntValue(int64(o.fd)) + return object.IntValue(int64(vm.ioFd(o))) } cIO.define("fileno", fileno) cIO.define("to_i", fileno) + // IO#to_io returns the IO itself (rb_io_to_io), for open or closed streams. + cIO.define("to_io", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self + }) + // IO#path (rb_io_path) returns the stream's backing path: the path given at + // creation (File.open, or IO.new's path: option), the ""/""/ + // "" pseudo-path of a standard stream, or nil when none is known (a + // plain fd wrapper or a pipe end). IO#to_path is a true alias. + cIO.define("path", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + if p, ok := ioPathString(self.(*IOObj)); ok { + return object.NewString(p) + } + return object.NilV + }) + cIO.methods["to_path"] = cIO.methods["path"] + // IO#inspect (rb_io_inspect): "#" when a path is known — a closed + // path stream appends " (closed)" — else "#" for an open plain + // descriptor and "#" once it is closed. Defining it on IO (not + // inheriting Object#inspect) makes IO its Method object's owner, as in MRI. + cIO.define("inspect", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + var b strings.Builder + b.WriteString("#<") + b.WriteString(vm.classOf(self).name) + b.WriteByte(':') + if p, ok := ioPathString(o); ok { + b.WriteString(p) + if o.closed { + b.WriteString(" (closed)") + } + } else if o.closed { + b.WriteString("(closed)") + } else { + b.WriteString("fd ") + b.WriteString(strconv.Itoa(vm.ioFd(o))) + } + b.WriteByte('>') + return object.NewString(b.String()) + }) + // IO#close_on_exec? / #close_on_exec= (rb_io_close_on_exec_p / _set): the flag + // defaults to set; assigning a false/nil value clears it, any other value sets + // it. Both raise IOError on a closed stream. + cIO.define("close_on_exec?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + return object.Bool(!o.closeOnExecOff) + }) + cIO.define("close_on_exec=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + o.closeOnExecOff = !args[0].Truthy() + return args[0] + }) + // IO#dup (rb_io_dup) returns an independent IO on a duplicated descriptor: a + // fresh synthetic fd, its own open/close state, and — as MRI documents — the + // autoclose and close-on-exec flags always set on the new object regardless of + // the receiver's. A closed receiver raises IOError. + cIO.define("dup", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + cp := *o + cp.fd = 0 // a distinct descriptor is assigned on first #fileno + cp.noAutoclose = false // dup always sets autoclose on the new IO + cp.closeOnExecOff = false // dup always sets close-on-exec on the new IO + vm.ioFd(&cp) // assign it now so #fileno already differs from the receiver's + return &cp + }) + // IO#initialize (rb_io_initialize) reassociates the receiver with an existing + // descriptor: the fd is coerced with #to_int (an IO/nil/String is a TypeError), + // looked up (an unknown one raises Errno::EBADF, a closed one IOError), and the + // receiver adopts its buffer, path and access mode. + cIO.define("initialize", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + pos, opts := splitIOOpts(args) + if len(pos) < 1 || len(pos) > 2 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1..2)", len(pos)) + } + var fd int + switch fv := pos[0].(type) { + case object.Integer: + fd = int(fv) + default: + if vm.respondsToDynamic(pos[0], "to_int") { + fd = int(vm.repeatLong(pos[0])) + } else { + raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(pos[0])) + } + } + src, ok := vm.fdTable[fd] + if !ok { + raise("Errno::EBADF", "Bad file descriptor - fd %d", fd) + } + if src.closed { + raise("IOError", "closed stream") + } + o.fd, o.closed = fd, false + vm.ioAdoptDescriptor(o, src, pos, opts) + vm.fdTable[fd] = o // #fileno now returns this descriptor for the receiver + return self + }) + vm.setInstanceVisibility(cIO, "initialize", visPrivate) // MRI keeps #initialize private + cStringIO := newClass("StringIO", vm.cObject) vm.consts["StringIO"] = cStringIO defIOWrite(cStringIO) diff --git a/internal/vm/io_class.go b/internal/vm/io_class.go index cbc3da09..58c2ee29 100644 --- a/internal/vm/io_class.go +++ b/internal/vm/io_class.go @@ -39,31 +39,8 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { if src.closed { raise("IOError", "closed stream") } - ms := vm.ioResolveModeEnc(pos, opts) - // Wrap the underlying descriptor's buffer/path so writes reach the same - // backing store the fd names (MRI shares the OS fd). The mode governs the - // read/write intent and encoding of the fresh wrapper. - res := &IOObj{cls: cIO, isStr: true, buf: src.buf, path: src.path, - binmode: ms.binmode, noAutoclose: ms.noAutoclose, - extEnc: ms.extEnc, intEnc: ms.intEnc} - if ms.explicit { - // An explicit mode must be compatible with the descriptor's current mode - // (io.c io_reopen / rb_update_max_fd path: EINVAL when e.g. a write-only fd - // is opened for reading). - if (ms.readable && src.rdClosed) || (ms.writable && src.wrClosed) { - raise("Errno::EINVAL", "Invalid argument") - } - res.writable, res.appendMode = ms.writable, ms.appendMode - res.rdClosed, res.wrClosed = !ms.readable, !ms.writable - } else { - // No explicit mode: the wrapper inherits the descriptor's actual access - // half (MRI derives it from the fd via fcntl(F_GETFL)). - res.writable, res.appendMode = src.writable, src.appendMode - res.rdClosed, res.wrClosed = src.rdClosed, src.wrClosed - } - if res.appendMode { - res.pos = len(res.buf) - } + res := &IOObj{cls: cIO} + vm.ioAdoptDescriptor(res, src, pos, opts) return res } cIO.smethods["for_fd"] = &Method{name: "for_fd", owner: cIO, native: forFd} @@ -94,6 +71,27 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { } return ret }} + // IO.sysopen(path, mode = "r", perm = 0666) opens path and returns its raw + // descriptor (io.c rb_io_s_sysopen → rb_sysopen). rbgo has no OS fds, so the + // file is opened into a buffered stream, registered in the synthetic fd table + // (so IO.for_fd(fd) can wrap it), and its synthetic descriptor returned. The + // path is coerced with #to_path; the permission argument is accepted but has + // no effect on the in-memory model. + cIO.smethods["sysopen"] = &Method{name: "sysopen", owner: cIO, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) < 1 || len(args) > 3 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1..3)", len(args)) + } + path := pathArg(vm, args[0]) + mode := "r" + if len(args) >= 2 && !object.IsNil(args[1]) { + mode = fileMode(args[:2]) + } + o := openFileIO(cFile, path, mode) + vm.nextFd++ + o.fd = vm.nextFd + vm.fdTable[o.fd] = o + return object.IntValue(int64(o.fd)) + }} def("read", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { return vm.ioReadFile(args, false) }) @@ -193,6 +191,49 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) { }) } +// ioAdoptDescriptor makes o wrap the descriptor src under the mode/encoding +// decoded from (pos, opts): the shared buffer/path, the read/write access half +// (an explicit mode incompatible with the descriptor is EINVAL, no mode inherits +// the descriptor's), the append position, and IO.new's optional path: override +// (an explicit nil clears the inherited path). Shared by IO.for_fd/new and +// IO#initialize so the two decode a descriptor identically (io.c io_initialize). +func (vm *VM) ioAdoptDescriptor(o, src *IOObj, pos []object.Value, opts *object.Hash) { + ms := vm.ioResolveModeEnc(pos, opts) + o.isStr, o.buf, o.path = true, src.buf, src.path + o.binmode, o.noAutoclose = ms.binmode, ms.noAutoclose + o.extEnc, o.intEnc = ms.extEnc, ms.intEnc + if ms.explicit { + // An explicit mode must be compatible with the descriptor's current mode + // (io.c io_reopen / rb_update_max_fd path: EINVAL when e.g. a write-only fd + // is opened for reading). + if (ms.readable && src.rdClosed) || (ms.writable && src.wrClosed) { + raise("Errno::EINVAL", "Invalid argument") + } + o.writable, o.appendMode = ms.writable, ms.appendMode + o.rdClosed, o.wrClosed = !ms.readable, !ms.writable + } else { + // No explicit mode: inherit the descriptor's actual access half (MRI + // derives it from the fd via fcntl(F_GETFL)). + o.writable, o.appendMode = src.writable, src.appendMode + o.rdClosed, o.wrClosed = src.rdClosed, src.wrClosed + } + if o.appendMode { + o.pos = len(o.buf) + } + // IO.new(fd, path:) records an explicit path for #path/#inspect (io.c + // rb_io_extract_modeenc stores the :path option in fptr->pathv). An explicit + // path: — even nil — overrides the path inherited from the descriptor. + if opts != nil { + if pv, ok := opts.Get(object.Symbol("path")); ok { + if object.IsNil(pv) { + o.path = "" + } else { + o.path = pathArg(vm, pv) + } + } + } +} + // splitIOOpts separates a trailing options Hash (IO.read/write keyword arguments) // from the positional arguments. func splitIOOpts(args []object.Value) ([]object.Value, *object.Hash) { diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index d23d9ae3..3f787029 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -178,26 +178,31 @@ 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) { - cls.define("pread", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("pread", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) - ioCheckReadable(o) - o.pipeRefresh() - n := int(intArg(args[0])) + // rb_io_pread: len (NUM2SIZET) and offset (NUM2OFFT) are coerced with + // #to_int, then io_setstrbuf coerces the optional buffer with #to_str — + // all before the maxlen==0 early return, so a non-String buffer still + // raises and a zero-length read leaves the buffer's bytes untouched. + n := int(vm.toIntCoerce(args[0])) + off := int(vm.toIntCoerce(args[1])) + var buf *object.String + if len(args) > 2 { + buf = vm.ioBufferArg(args[2]) + } if n < 0 { raise("ArgumentError", "negative string size (or size too big)") } - off := int(intArg(args[1])) - if off < 0 { - raise("Errno::EINVAL", "Invalid argument - pread") - } - var buf *object.String - if len(args) > 2 { - if b, ok := args[2].(*object.String); ok { - buf = b + if n == 0 { // MRI returns the (coerced) buffer unshrunk, or a fresh "" + if buf != nil { + return buf } + return ioReadResult(nil, nil) } - if n == 0 { - return ioReadResult(nil, buf) + ioCheckReadable(o) + o.pipeRefresh() + if off < 0 { + raise("Errno::EINVAL", "Invalid argument - pread") } if off >= len(o.buf) { raise("EOFError", "end of file reached") @@ -205,14 +210,18 @@ func defIOSeekable(cls *RClass) { data := o.buf[off:min(off+n, len(o.buf))] return ioReadResult(data, buf) }) - cls.define("pwrite", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("pwrite", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + // rb_io_pwrite: a non-String object is coerced with #to_s + // (rb_obj_as_string) and the offset with #to_int (NUM2OFFT) before the + // stream is checked, so a missing #to_s surfaces as NoMethodError and a + // non-Integer offset as the "into Integer" TypeError. + data := []byte(vm.objAsString(args[0])) + off := int(vm.toIntCoerce(args[1])) ioCheckOpen(o) - off := int(intArg(args[1])) if off < 0 { raise("Errno::EINVAL", "Invalid argument - pwrite") } - data := []byte(args[0].ToS()) if end := off + len(data); end > len(o.buf) { o.buf = append(o.buf, make([]byte, end-len(o.buf))...) } diff --git a/internal/vm/io_wave19_test.go b/internal/vm/io_wave19_test.go new file mode 100644 index 00000000..c7fbb810 --- /dev/null +++ b/internal/vm/io_wave19_test.go @@ -0,0 +1,150 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "fmt" + "path/filepath" + "testing" +) + +// TestIOWave19Pread covers IO#pread coercion and boundary branches (io.c +// rb_io_pread): #to_int on maxlen/offset, #to_str on the buffer, the maxlen==0 +// early return (with and without a buffer), and the negative-size / negative- +// offset / EOF error paths. Asserted against MRI Ruby 4.0.5. +func TestIOWave19Pread(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "pread.txt")) + setup := fmt.Sprintf("File.write(%q, \"1234567890\"); f = File.open(%q, \"r+\"); ", p, p) + cases := []struct{ src, want string }{ + {setup + `p f.pread(4, 0)`, "\"1234\"\n"}, + {setup + `p f.pread(3, 4)`, "\"567\"\n"}, + {setup + `b = +"foo"; r = f.pread(3, 4, b); p [r, b, r.equal?(b)]`, "[\"567\", \"567\", true]\n"}, + // maxlen==0: no buffer yields "", a buffer is returned untouched. + {setup + `p f.pread(0, 4)`, "\"\"\n"}, + {setup + `b = +"foo"; r = f.pread(0, 4, b); p [r, b]`, "[\"foo\", \"foo\"]\n"}, + {setup + `b = +"foo"; f.pread(0, 400, b); p b`, "\"foo\"\n"}, + // #to_int coercion of maxlen and offset. + {setup + `o = Object.new; def o.to_int; 4; end; p f.pread(o, 0)`, "\"1234\"\n"}, + {setup + `o = Object.new; def o.to_int; 0; end; p f.pread(4, o)`, "\"1234\"\n"}, + // #to_str coercion of the buffer. + {setup + `o = Object.new; def o.to_str; @s ||= +"x"; end; f.pread(4, 0, o); p o.to_str`, "\"1234\"\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) + } + } + errCases := []struct{ src, class, msg string }{ + {setup + `f.pread(-4, 0)`, "ArgumentError", "negative string size (or size too big)"}, + {setup + `f.pread(4, -1)`, "Errno::EINVAL", "Invalid argument - pread"}, + {setup + `f.pread(1, 10)`, "EOFError", "end of file reached"}, + {setup + `f.pread(Object.new, 0)`, "TypeError", "no implicit conversion of Object into Integer"}, + {setup + `f.pread(4, Object.new)`, "TypeError", "no implicit conversion of Object into Integer"}, + {setup + `f.pread(4, 0, Object.new)`, "TypeError", "no implicit conversion of Object into String"}, + {setup + `f.close; f.pread(1, 0)`, "IOError", "closed stream"}, + } + for _, c := range errCases { + cls, msg := evalErr(t, c.src) + if cls != c.class || msg != c.msg { + t.Errorf("src=%q\n got=%s: %q\nwant=%s: %q", c.src, cls, msg, c.class, c.msg) + } + } + // A write-only stream cannot be pread. + cls, msg := evalErr(t, fmt.Sprintf("f = File.open(%q, \"w\"); f.pread(1, 0)", p)) + if cls != "IOError" || msg != "not opened for reading" { + t.Errorf("pread on write-only: got %s: %q", cls, msg) + } +} + +// TestIOWave19Pwrite covers IO#pwrite (io.c rb_io_pwrite): #to_s on the object, +// #to_int on the offset, and the closed / not-writable / negative-offset error +// paths. Asserted against MRI Ruby 4.0.5. +func TestIOWave19Pwrite(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "pwrite.txt")) + setup := fmt.Sprintf("f = File.open(%q, \"w+\"); ", p) + cases := []struct{ src, want string }{ + {setup + `p f.pwrite("foo", 0)`, "3\n"}, + {setup + `f.pwrite("bar", 3); f.write("foo"); p f.pread(6, 0)`, "\"foobar\"\n"}, + {setup + `o = Object.new; def o.to_s; "foo"; end; f.pwrite(o, 0); p f.pread(3, 0)`, "\"foo\"\n"}, + {setup + `o = Object.new; def o.to_int; 2; end; f.pwrite("foo", o); p f.pread(3, 2)`, "\"foo\"\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) + } + } + errCases := []struct{ src, class, msg string }{ + {setup + `f.pwrite("foo", Object.new)`, "TypeError", "no implicit conversion of Object into Integer"}, + {setup + `f.pwrite(BasicObject.new, 0)`, "NoMethodError", "undefined method 'to_s' for an instance of BasicObject"}, + {setup + `f.pwrite("foo", -1)`, "Errno::EINVAL", "Invalid argument - pwrite"}, + {setup + `f.close; f.pwrite("foo", 1)`, "IOError", "closed stream"}, + } + for _, c := range errCases { + cls, msg := evalErr(t, c.src) + if cls != c.class || msg != c.msg { + t.Errorf("src=%q\n got=%s: %q\nwant=%s: %q", c.src, cls, msg, c.class, c.msg) + } + } + // A read-only stream cannot be pwritten. + cls, msg := evalErr(t, fmt.Sprintf("File.write(%q, \"x\"); f = File.open(%q, \"r\"); f.pwrite(\"y\", 0)", p, p)) + if cls != "IOError" || msg != "not opened for writing" { + t.Errorf("pwrite on read-only: got %s: %q", cls, msg) + } +} + +// TestIOWave19PathAndToIO covers IO#to_io (returns self, open or closed), IO#path +// (a File.open path, the standard-stream "" pseudo-paths, a pipe's nil, and +// IO.new's path: option) and the IO#to_path alias. Asserted against MRI 4.0.5. +func TestIOWave19PathAndToIO(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "path.txt")) + cases := []struct{ src, want string }{ + {`$stdout.to_io.equal?($stdout) ? (p true) : (p false)`, "true\n"}, + {fmt.Sprintf("f = File.open(%q, \"w\"); io = f.to_io; f.close; p io.equal?(f)", p), "true\n"}, + {`p $stdout.path`, "\"\"\n"}, + {`p $stderr.path`, "\"\"\n"}, + {`p STDIN.path`, "\"\"\n"}, + {`r, w = IO.pipe; p r.path; r.close; w.close`, "nil\n"}, + {fmt.Sprintf("File.open(%q, \"w\") { |f| p IO.new(f.fileno, path: f.path, autoclose: false).path }", p), fmt.Sprintf("%q\n", p)}, + // path: nil falls through, so #path reports the descriptor's own (empty) path as nil. + {fmt.Sprintf("File.open(%q, \"w\") { |f| p IO.new(f.fileno, path: nil, autoclose: false).path }", p), "nil\n"}, + {`p IO.instance_method(:to_path) == IO.instance_method(:path)`, "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) + } + } +} + +// TestIOWave19Sysopen covers IO.sysopen (io.c rb_io_s_sysopen): the returned +// descriptor is a non-zero Integer wrapable by IO.for_fd, #to_path coerces the +// path, an explicit or nil mode is accepted, and a bad arity raises. Asserted +// against MRI Ruby 4.0.5. +func TestIOWave19Sysopen(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "sysopen.txt")) + cases := []struct{ src, want string }{ + {fmt.Sprintf("fd = IO.sysopen(%q, \"w\"); p [fd.is_a?(Integer), fd != 0]", p), "[true, true]\n"}, + {fmt.Sprintf("fd = IO.sysopen(%q, \"w\"); io = IO.for_fd(fd); io.write(\"hi\"); io.close; p File.read(%q)", p, p), "\"hi\"\n"}, + // #to_path coercion of the path object. + {fmt.Sprintf("o = Object.new; def o.to_path; %q; end; fd = IO.sysopen(o, \"w\"); p fd != 0", p), "true\n"}, + // nil mode and nil permission default to read. + {fmt.Sprintf("File.write(%q, \"z\"); fd = IO.sysopen(%q, nil, nil); p fd != 0", p, p), "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) + } + } + if cls, _ := evalErr(t, `IO.sysopen`); cls != "ArgumentError" { + t.Errorf("sysopen with no args: got %s", cls) + } + if cls, _ := evalErr(t, fmt.Sprintf("IO.sysopen(%q, \"r\", 0, 0)", p)); cls != "ArgumentError" { + t.Errorf("sysopen with 4 args: got %s", cls) + } +} From 99130847e0e727cdd56a3af989f6a390bf1e5102 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 8 Sep 2026 00:10:54 +0200 Subject: [PATCH 2/4] io: set_encoding coercion/arity, ungetbyte, standard-stream modes (core/io) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Continue MRI-Ruby-4.0.x core/io conformance, reading MRI io.c (v3_4_0) and the ruby/spec expectations: - IO#set_encoding (io.c io_encoding_set): enforce the 1..2 arity over the positional args (a trailing econv-options Hash is separated first), coerce a non-String/non-Encoding first argument with #to_str before the "ext:int" split, and drop an internal encoding equal to the external one (enc == enc2 => enc2 = NULL). set_encoding 21->26. - IO#ungetbyte (io.c rb_io_ungetbyte): reduce an Integer/Bignum modulo 256 to a single byte (never RangeError) and coerce any other value with #to_str (StringValue), so a non-coercible argument is an "into String" TypeError — the pinned StringIO#ungetbyte([]) test is updated to the MRI-verified message. ungetbyte 3->6. - Standard streams carry their real access half: $stdout/$stderr are write-only and $stdin read-only, so a read of $stdout / a write to $stdin raises IOError ("not opened for reading"/"...writing"), matching MRI's fd modes. This also gives IO#ungetbyte its "not opened for reading" IOError. - IO#to_i is a true alias of IO#fileno (equal UnboundMethods), and IO mixes in Enumerable. to_i 0->1, io 1->2. 100% coverage on internal/vm; all changes verified byte-for-byte against MRI Ruby 4.0.5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 67 ++++++---- internal/vm/io_descriptors.go | 24 ++-- internal/vm/io_descriptors_test.go | 5 +- internal/vm/io_wave19_test.go | 196 +++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+), 33 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 334e1746..5f41901c 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -221,7 +221,7 @@ func (vm *VM) registerIO() { return object.IntValue(int64(vm.ioFd(o))) } cIO.define("fileno", fileno) - cIO.define("to_i", fileno) + cIO.methods["to_i"] = cIO.methods["fileno"] // #to_i is a true alias of #fileno // IO#to_io returns the IO itself (rb_io_to_io), for open or closed streams. cIO.define("to_io", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { @@ -338,10 +338,13 @@ func (vm *VM) registerIO() { defIOReadExtra(cStringIO) defStringIOExtra(vm, cStringIO) - stdout := &IOObj{cls: cIO, w: vm.out, label: "STDOUT"} + // The standard streams carry their real access half: $stdout/$stderr are + // write-only (a read raises "not opened for reading") and $stdin read-only (a + // write raises "not opened for writing"), as MRI's fd modes dictate. + stdout := &IOObj{cls: cIO, w: vm.out, label: "STDOUT", rdClosed: true} // STDERR is synchronous by default (MRI: STDERR.sync == true), unlike STDOUT. - stderr := &IOObj{cls: cIO, w: vm.errOut, label: "STDERR", sync: true} - stdin := &IOObj{cls: cIO, isStr: true, label: "STDIN"} // empty input by default + stderr := &IOObj{cls: cIO, w: vm.errOut, label: "STDERR", sync: true, rdClosed: true} + stdin := &IOObj{cls: cIO, isStr: true, label: "STDIN", wrClosed: true} // empty input by default vm.consts["STDOUT"], vm.consts["STDERR"], vm.consts["STDIN"] = stdout, stderr, stdin vm.globals["$stdout"], vm.globals["$stderr"], vm.globals["$stdin"] = stdout, stderr, stdin @@ -853,23 +856,44 @@ func defIOWrite(cls *RClass) { // "external:internal" string; a nil argument clears that side. Returns self. cls.define("set_encoding", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + // A trailing Hash carries econv options (invalid:/replace:/…), not an + // encoding, so it is separated before the 1..2 arity is checked + // (rb_scan_args "11:"). + pos, _ := splitIOOpts(args) + if len(pos) < 1 || len(pos) > 2 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1..2)", len(pos)) + } o.extEnc, o.intEnc = "", "" - if len(args) > 0 && !object.IsNil(args[0]) { - if s, ok := args[0].(*object.String); ok { - if i := strings.IndexByte(s.Str(), ':'); i >= 0 && len(args) == 1 { + if !object.IsNil(pos[0]) { + // A single String — or a non-Encoding coerced via #to_str + // (rb_check_string_type) — of the form "ext:int" names both encodings; + // otherwise the first argument is one external encoding. + 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 { + if i := strings.IndexByte(s.Str(), ':'); i >= 0 && len(pos) == 1 { o.extEnc = vm.lookupEncodingName(s.Str()[:i]).name o.intEnc = vm.lookupEncodingName(s.Str()[i+1:]).name } else { - o.extEnc = vm.encodingArg(args[0]).name + o.extEnc = vm.lookupEncodingName(s.Str()).name } } else { - o.extEnc = vm.encodingArg(args[0]).name + o.extEnc = vm.encodingArg(pos[0]).name } } - if len(args) > 1 && !object.IsNil(args[1]) { - o.intEnc = vm.encodingArg(args[1]).name + if len(pos) > 1 && !object.IsNil(pos[1]) { + o.intEnc = vm.encodingArg(pos[1]).name } - return o + // io_encoding_set: an internal encoding equal to the external one means no + // transcoding, so the internal encoding is dropped (enc2 = NULL). + if o.intEnc == o.extEnc { + o.intEnc = "" + } + return self }) } @@ -1087,11 +1111,9 @@ func defStringIORead(cls *RClass) { // does. $. always becomes the reading IO's line number (a StringIO updates it // too). $_ (the "last read line") is set only by the single-line readers — // gets/readline (lastLine true) — not by the bulk readlines/each_line, which - // leave $_ alone, matching MRI. + // leave $_ alone, matching MRI. Every caller guards end-of-stream (a nil line) + // itself, so v is always a real line here. setLineGlobals := func(vm *VM, o *IOObj, v object.Value, lastLine bool) { - if v == object.NilV { - return - } vm.globals["$."] = object.IntValue(int64(o.lineno)) if lastLine { vm.globals["$_"] = v @@ -1232,12 +1254,15 @@ func ioIsStringIO(o *IOObj) bool { // in stringIOSetup), #string=, and the StringIO-specific overrides of // #sync/#binmode/#close_read/#close_write/#fcntl whose behaviour differs from a // real IO's. -// includeStringIOEnumerable mixes Enumerable into StringIO (whose #each yields -// lines), as in MRI. It runs after the prelude, where Enumerable is defined. +// includeStringIOEnumerable mixes Enumerable into IO and StringIO (whose #each +// yields lines), as in MRI. It runs after the prelude, where Enumerable is +// defined. func (vm *VM) includeStringIOEnumerable() { - if c, ok := vm.consts["StringIO"].(*RClass); ok { - if en, ok := vm.consts["Enumerable"].(*RClass); ok { - c.includes = append(c.includes, en) + if en, ok := vm.consts["Enumerable"].(*RClass); ok { + for _, name := range []string{"IO", "StringIO"} { + if c, ok := vm.consts[name].(*RClass); ok { + c.includes = append(c.includes, en) + } } } } diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 3f787029..27393d00 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -5,6 +5,7 @@ package vm import ( + "math/big" "unicode/utf8" "github.com/go-embedded-ruby/ruby/internal/object" @@ -49,19 +50,22 @@ func defIOReadExtra(cls *RClass) { o.pos += sz return object.NewString(string(r)) }) - cls.define("ungetbyte", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("ungetbyte", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - switch a := args[0].(type) { - case object.Integer: - ioUnget(o, []byte{byte(a)}) - case *object.String: - ioUnget(o, a.Bytes()) - default: - if args[0] != object.NilV { // a nil argument is a no-op, as in MRI - raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(args[0])) - } + if object.IsNil(args[0]) { // a nil argument is a no-op, as in MRI + return object.NilV + } + if bi, ok := object.BigOf(args[0]); ok { + // rb_io_ungetbyte: an Integer/Bignum is reduced modulo 256 to a single + // byte (rb_int_modulo(b, 256) & 0xFF), so it never raises RangeError. + m := new(big.Int).Mod(bi, big.NewInt(256)) + ioUnget(o, []byte{byte(m.Int64())}) + return object.NilV } + // Any other value is coerced with #to_str (StringValue), raising + // "no implicit conversion of into String" when it cannot be. + ioUnget(o, vm.strToStr(args[0])) return object.NilV }) cls.define("ungetc", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/io_descriptors_test.go b/internal/vm/io_descriptors_test.go index fb4e283f..14483dc2 100644 --- a/internal/vm/io_descriptors_test.go +++ b/internal/vm/io_descriptors_test.go @@ -127,8 +127,9 @@ func TestIODescriptorsErrors(t *testing.T) { {`require "stringio"; StringIO.new("ab").readlines(0)`, "invalid limit: 0 for readlines"}, {`require "stringio"; StringIO.new("ab").each_line(0) { |x| }`, "invalid limit: 0 for each_line"}, {`require "stringio"; StringIO.new("ab").each(0) { |x| }`, "invalid limit: 0 for each_line"}, - // unget type errors. - {`require "stringio"; StringIO.new("ab").ungetbyte([])`, "into Integer"}, + // unget type errors: ungetbyte coerces a non-Integer with #to_str + // (StringValue), so an Array is an "into String" TypeError, as in MRI. + {`require "stringio"; StringIO.new("ab").ungetbyte([])`, "into String"}, {`require "stringio"; StringIO.new("ab").ungetc([])`, "into String"}, // half-close then read / write. {`require "stringio"; s = StringIO.new("ab"); s.close_read; s.read`, "not opened for reading"}, diff --git a/internal/vm/io_wave19_test.go b/internal/vm/io_wave19_test.go index c7fbb810..6aaa7c04 100644 --- a/internal/vm/io_wave19_test.go +++ b/internal/vm/io_wave19_test.go @@ -148,3 +148,199 @@ func TestIOWave19Sysopen(t *testing.T) { t.Errorf("sysopen with 4 args: got %s", cls) } } + +// TestIOWave19Inspect covers IO#inspect (io.c rb_io_inspect) across the four +// pathv/closed states — an open and a closed path stream, an open plain fd +// wrapper and a closed one — and the standard-stream forms. Asserted against MRI +// Ruby 4.0.5. +func TestIOWave19Inspect(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "inspect.txt")) + cases := []struct{ src, want string }{ + {fmt.Sprintf("f = File.open(%q, \"w\"); s = f.inspect; f.close; p s", p), fmt.Sprintf("\"#\"\n", p)}, + {fmt.Sprintf("f = File.open(%q, \"w\"); f.close; p f.inspect", p), fmt.Sprintf("\"#\"\n", p)}, + {`r, w = IO.pipe; fd = r.fileno; s = r.inspect; r.close; w.close; p s == "#"`, "true\n"}, + {`r, w = IO.pipe; r.close; s = r.inspect; w.close; p s`, "\"#\"\n"}, + {`p $stdout.inspect`, "\"#>\"\n"}, + {`p $stderr.inspect`, "\"#>\"\n"}, + {`p STDIN.inspect`, "\"#>\"\n"}, + {`p IO.instance_method(:inspect).owner`, "IO\n"}, + {`p STDIN.fileno`, "0\n"}, + {`p $stderr.fileno`, "2\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) + } + } +} + +// TestIOWave19CloseOnExec covers IO#close_on_exec? / #close_on_exec= (io.c +// rb_io_close_on_exec_p / _set): the default-set flag, clearing with false/nil, +// setting with a truthy value, the assignment's return value, and the closed- +// stream IOError on both. Asserted against MRI Ruby 4.0.5. +func TestIOWave19CloseOnExec(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "coe.txt")) + setup := fmt.Sprintf("f = File.open(%q, \"w\"); ", p) + cases := []struct{ src, want string }{ + {setup + `p f.close_on_exec?`, "true\n"}, // default set + {setup + `f.close_on_exec = false; p f.close_on_exec?`, "false\n"}, + {setup + `f.close_on_exec = false; f.close_on_exec = :yes; p f.close_on_exec?`, "true\n"}, + {setup + `f.close_on_exec = nil; p f.close_on_exec?`, "false\n"}, + {setup + `p(f.close_on_exec = true)`, "true\n"}, // assignment returns its argument + } + 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) + } + } + for _, src := range []string{setup + `f.close; f.close_on_exec?`, setup + `f.close; f.close_on_exec = true`} { + if cls, msg := evalErr(t, src); cls != "IOError" || msg != "closed stream" { + t.Errorf("src=%q: got %s: %q", src, cls, msg) + } + } +} + +// TestIOWave19Dup covers IO#dup (io.c rb_io_dup): a new descriptor distinct from +// the receiver, independent open/close state in both directions, autoclose and +// close-on-exec always set on the copy, and the closed-stream IOError. Asserted +// against MRI Ruby 4.0.5. +func TestIOWave19Dup(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "dup.txt")) + setup := fmt.Sprintf("f = File.open(%q, \"w+\"); ", p) + cases := []struct{ src, want string }{ + {setup + `i = f.dup; p [i.class == f.class, i.fileno != f.fileno]`, "[true, true]\n"}, + {setup + `i = f.dup; i.close; p [i.closed?, f.closed?]`, "[true, false]\n"}, + {setup + `i = f.dup; f.close; p [i.closed?, f.closed?]`, "[false, true]\n"}, + {setup + `f.close_on_exec = false; d = f.dup; p d.close_on_exec?`, "true\n"}, + {setup + `f.autoclose = false; d = f.dup; p d.autoclose?`, "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) + } + } + if cls, msg := evalErr(t, setup+`f.close; f.dup`); cls != "IOError" || msg != "closed stream" { + t.Errorf("dup on closed: got %s: %q", cls, msg) + } +} + +// TestIOWave19Initialize covers IO#initialize (io.c rb_io_initialize) and the +// shared ioAdoptDescriptor: Integer and #to_int fds reassociate the receiver; +// an explicit mode is honoured or rejected (EINVAL) against the descriptor's +// access half; append mode positions at end; and the arity / TypeError / EBADF / +// closed-stream errors. Asserted against MRI Ruby 4.0.5. +func TestIOWave19Initialize(t *testing.T) { + dir := t.TempDir() + c := filepath.ToSlash(filepath.Join(dir, "content.txt")) // holds "abcd" + tgt := filepath.ToSlash(filepath.Join(dir, "target.txt")) // the target IO's own file + // A target IO plus a source read fd over the content file. The target opens + // its own file so its "w" truncation never touches the content. + base := fmt.Sprintf("File.write(%q, \"abcd\"); io = IO.new(File.open(%q, \"w\").fileno); rfd = File.open(%q, \"r\").fileno; ", c, tgt, c) + cases := []struct{ src, want string }{ + {base + `io.send(:initialize, rfd, "r"); p io.fileno == rfd`, "true\n"}, + {base + `o = Object.new; def o.to_int; @f; end; o.instance_variable_set(:@f, rfd); io.send(:initialize, o, "r"); p io.fileno == rfd`, "true\n"}, + // no explicit mode inherits the descriptor's access half. + {base + `io.send(:initialize, rfd); p io.read`, "\"abcd\"\n"}, + // append mode positions the cursor at end of the adopted buffer. + {fmt.Sprintf("File.write(%q, \"abcd\"); io = IO.new(File.open(%q, \"w\").fileno); afd = File.open(%q, \"a\").fileno; io.send(:initialize, afd, \"a\"); p io.pos", c, tgt, c), "4\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) + } + } + errCases := []struct{ src, class string }{ + {base + `io.send(:initialize)`, "ArgumentError"}, // too few + {base + `io.send(:initialize, rfd, "r", "x")`, "ArgumentError"}, // too many positional + {base + `io.send(:initialize, nil, "r")`, "TypeError"}, + {base + `io.send(:initialize, "4", "r")`, "TypeError"}, + {base + `io.send(:initialize, STDOUT, "r")`, "TypeError"}, + {base + `io.send(:initialize, -1, "r")`, "Errno::EBADF"}, + // an explicit write mode is incompatible with a read-only descriptor. + {base + `io.send(:initialize, rfd, "w")`, "Errno::EINVAL"}, + } + for _, c := range errCases { + if cls, _ := evalErr(t, c.src); cls != c.class { + t.Errorf("src=%q: got %s, want %s", c.src, cls, c.class) + } + } + // A closed descriptor cannot be adopted. + closedSrc := fmt.Sprintf("io = IO.new(File.open(%q, \"w\").fileno); g = File.open(%q, \"r\"); gfd = g.fileno; g.close; io.send(:initialize, gfd)", tgt, c) + if cls, _ := evalErr(t, closedSrc); cls != "IOError" { + t.Errorf("initialize on closed src: got %s", cls) + } +} + +// TestIOWave19SetEncoding covers IO#set_encoding (io.c io_encoding_set): a single +// Encoding, a "ext:int" String, a #to_str-coerced argument, the equal-external/ +// internal drop (enc2=NULL), a separated internal argument, a trailing econv +// options Hash, and the 1..2 arity. Asserted against MRI Ruby 4.0.5. +func TestIOWave19SetEncoding(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "se.txt")) + setup := fmt.Sprintf("File.write(%q, \"\"); f = File.open(%q, \"r+\"); ", p, p) + cases := []struct{ src, want string }{ + {setup + `f.set_encoding(Encoding::UTF_8); p [f.external_encoding.name, f.internal_encoding]`, "[\"UTF-8\", nil]\n"}, + {setup + `f.set_encoding("utf-8:utf-16be"); p [f.external_encoding.name, f.internal_encoding.name]`, "[\"UTF-8\", \"UTF-16BE\"]\n"}, + {setup + `f.set_encoding("utf-8", "utf-16be"); p [f.external_encoding.name, f.internal_encoding.name]`, "[\"UTF-8\", \"UTF-16BE\"]\n"}, + // equal external/internal drops the internal encoding. + {setup + `f.set_encoding(Encoding::UTF_8, Encoding::UTF_8); p [f.external_encoding.name, f.internal_encoding]`, "[\"UTF-8\", nil]\n"}, + {setup + `f.set_encoding("utf-8:utf-8"); p [f.external_encoding.name, f.internal_encoding]`, "[\"UTF-8\", nil]\n"}, + // a #to_str object naming "ext:int". + {setup + `o = Object.new; def o.to_str; "utf-8:utf-16be"; end; f.set_encoding(o); p [f.external_encoding.name, f.internal_encoding.name]`, "[\"UTF-8\", \"UTF-16BE\"]\n"}, + // a trailing econv options Hash is accepted (and not counted as an encoding). + {setup + `f.set_encoding(Encoding::EUC_JP, Encoding::SHIFT_JIS, invalid: :replace, replace: "."); p f.external_encoding.name`, "\"EUC-JP\"\n"}, + {setup + `p f.set_encoding(Encoding::UTF_8).equal?(f)`, "true\n"}, + {setup + `f.set_encoding(nil, nil); p [f.external_encoding, f.internal_encoding]`, "[nil, nil]\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) + } + } + errCases := []struct{ src, class string }{ + {setup + `f.set_encoding()`, "ArgumentError"}, + {setup + `f.set_encoding(1, 2, 3)`, "ArgumentError"}, + {setup + `f.set_encoding(Object.new)`, "TypeError"}, // no #to_str, not an Encoding + } + for _, c := range errCases { + if cls, _ := evalErr(t, c.src); cls != c.class { + t.Errorf("src=%q: got %s, want %s", c.src, cls, c.class) + } + } +} + +// TestIOWave19StreamModes covers the standard streams' access halves (a read of +// $stdout / a write to $stdin raises IOError), IO#to_i's alias identity with +// #fileno, IO mixing in Enumerable, and IO#ungetbyte's Bignum-modulo-256 and +// #to_str coercion. Asserted against MRI Ruby 4.0.5. +func TestIOWave19StreamModes(t *testing.T) { + cases := []struct{ src, want string }{ + {`p IO.instance_method(:to_i) == IO.instance_method(:fileno)`, "true\n"}, + {`p IO.include?(Enumerable)`, "true\n"}, + // ungetbyte reduces a Bignum modulo 256 and coerces a #to_str object. + {`require "stringio"; s = StringIO.new("a"); s.ungetbyte(0x4f7574206f6620636861722072616e67ff); p s.getbyte`, "255\n"}, + {`require "stringio"; s = StringIO.new("a"); o = Object.new; def o.to_str; "dog"; end; s.ungetbyte(o); p [s.getbyte, s.getbyte, s.getbyte]`, "[100, 111, 103]\n"}, + // a negative Integer also reduces into a byte (Ruby modulo). + {`require "stringio"; s = StringIO.new("a"); s.ungetbyte(-1); p s.getbyte`, "255\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) + } + } + errCases := []struct{ src, class, msg string }{ + {`STDOUT.read`, "IOError", "not opened for reading"}, + {`STDERR.readbyte`, "IOError", "not opened for reading"}, + {`STDIN.write("x")`, "IOError", "not opened for writing"}, + {`STDOUT.ungetbyte(42)`, "IOError", "not opened for reading"}, + } + for _, c := range errCases { + if cls, msg := evalErr(t, c.src); cls != c.class || msg != c.msg { + t.Errorf("src=%q: got %s: %q, want %s: %q", c.src, cls, msg, c.class, c.msg) + } + } +} From f7e9fc512f48b020a16d729d41213b50591ea364 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 8 Sep 2026 00:19:54 +0200 Subject: [PATCH 3/4] io: lineno readability + pos/seek/lineno off_t & int range checks (core/io) Continue MRI-Ruby-4.0.x core/io conformance (MRI io.c v3_4_0 + ruby/spec): - IO#lineno / #lineno= (io.c rb_io_lineno / rb_io_set_lineno): a real IO must be char-readable, so a closed stream raises "closed stream" and a write-only one "not opened for reading"; a StringIO still tolerates both (strio has no such check). lineno 7->12. - IO#pos= / IO#seek (NUM2OFFT): a Bignum too large for the off_t position raises RangeError "bignum too big to convert into 'long long'" rather than a TypeError. pos 7->8, seek 14->15. - IO#lineno= (NUM2INT): the line number is a C int, so a Bignum raises "bignum too big to convert into 'long'" and an in-range Integer that still overflows the int raises "integer N too big to convert to 'int'". 100% coverage on internal/vm; all changes verified byte-for-byte against MRI Ruby 4.0.5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 29 ++++++++++++++++++++++++++-- internal/vm/io_descriptors.go | 15 ++++++++++++--- internal/vm/io_wave19_test.go | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index 5f41901c..a082c5c9 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -942,7 +942,7 @@ func defStringIORead(cls *RClass) { if o.closed && !ioIsStringIO(o) { // real IO/File raises; StringIO tolerates it raise("IOError", "closed stream") } - n := int(vm.toIntCoerce(args[0])) + n := vm.ioOfftArg(args[0]) if n < 0 { raise("Errno::EINVAL", "Invalid argument") } @@ -954,7 +954,7 @@ func defStringIORead(cls *RClass) { if o.closed { raise("IOError", "closed stream") } - amount := int(vm.toIntCoerce(args[0])) + amount := vm.ioOfftArg(args[0]) whence := 0 if len(args) > 1 { // IO#seek accepts the :SET/:CUR/:END whence symbols; StringIO#seek @@ -1818,6 +1818,31 @@ func ioClosedRealIO(o *IOObj) { } } +// ioOfftArg coerces a stream position/offset argument the way MRI's NUM2OFFT +// does: an Integer or #to_int value narrowed to the off_t (a long long) the +// position is stored in, raising RangeError for a Bignum too large to fit. +func (vm *VM) ioOfftArg(v object.Value) int { + if _, ok := v.(*object.Bignum); ok { + raise("RangeError", "bignum too big to convert into 'long long'") + } + return int(vm.toIntCoerce(v)) +} + +// ioCIntArg coerces an argument the way MRI's NUM2INT does for a value stored in +// a C int (e.g. #lineno=): an Integer or #to_int value that fits a 32-bit int, +// raising RangeError for a Bignum or for an in-range-Integer that still overflows +// the int. +func (vm *VM) ioCIntArg(v object.Value) int { + if _, ok := v.(*object.Bignum); ok { + raise("RangeError", "bignum too big to convert into 'long'") + } + n := vm.toIntCoerce(v) + if n < -2147483648 || n > 2147483647 { // int32 bounds + raise("RangeError", "integer %d too big to convert to 'int'", n) + } + return int(n) +} + // toInt coerces a small Integer position/length argument to int64 (raising for // anything else, including a Bignum — a stream offset that large is nonsensical). func toInt(v object.Value) int64 { diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 27393d00..21058ade 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -137,11 +137,20 @@ func defIOReadExtra(cls *RClass) { return object.IntValue(int64(o.writeStr(args[0].ToS()))) }) cls.define("lineno", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(self.(*IOObj).lineno)) + o := self.(*IOObj) + if !ioIsStringIO(o) { // rb_io_check_char_readable: a closed/write-only real IO raises + ioCheckReadable(o) + } + return object.IntValue(int64(o.lineno)) }) cls.define("lineno=", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - // rb_io_set_lineno: NUM2INT coerces via #to_int (a Float truncates). - self.(*IOObj).lineno = int(vm.toIntCoerce(args[0])) + // rb_io_set_lineno: the stream must be char-readable (a closed/write-only + // real IO raises), then NUM2INT coerces via #to_int (a Float truncates). + o := self.(*IOObj) + if !ioIsStringIO(o) { + ioCheckReadable(o) + } + o.lineno = vm.ioCIntArg(args[0]) return args[0] }) cls.define("close_read", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/io_wave19_test.go b/internal/vm/io_wave19_test.go index 6aaa7c04..2e27fc9c 100644 --- a/internal/vm/io_wave19_test.go +++ b/internal/vm/io_wave19_test.go @@ -344,3 +344,39 @@ func TestIOWave19StreamModes(t *testing.T) { } } } + +// TestIOWave19LinenoAndOfft covers IO#lineno / #lineno= char-readability checks +// (a closed/write-only real IO raises, a StringIO does not) and the NUM2OFFT / +// NUM2INT range errors of #pos= / #seek / #lineno= (a Bignum, and an Integer that +// overflows a C int). Asserted against MRI Ruby 4.0.5. +func TestIOWave19LinenoAndOfft(t *testing.T) { + dir := t.TempDir() + p := filepath.ToSlash(filepath.Join(dir, "ln.txt")) + rw := fmt.Sprintf("File.write(%q, \"a\\nb\\n\"); f = File.open(%q, \"r+\"); ", p, p) + cases := []struct{ src, want string }{ + // a StringIO tolerates #lineno / #lineno= on a closed stream (no raise). + {`require "stringio"; s = StringIO.new("a"); s.close; p s.lineno`, "0\n"}, + {`require "stringio"; s = StringIO.new("a"); s.close; s.lineno = 3; p s.lineno`, "3\n"}, + {rw + `f.lineno = 2**30; p f.lineno`, "1073741824\n"}, // in-range value is accepted + {rw + `f.pos = 5; p f.pos`, "5\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) + } + } + errCases := []struct{ src, class, msg string }{ + {fmt.Sprintf("File.open(%q, \"w\") { |f| f.lineno }", p), "IOError", "not opened for reading"}, + {fmt.Sprintf("File.open(%q, \"w\") { |f| f.lineno = 1 }", p), "IOError", "not opened for reading"}, + {rw + `f.close; f.lineno`, "IOError", "closed stream"}, + {rw + `f.pos = 2**128`, "RangeError", "bignum too big to convert into 'long long'"}, + {rw + `f.seek(2**128)`, "RangeError", "bignum too big to convert into 'long long'"}, + {rw + `f.lineno = 2**128`, "RangeError", "bignum too big to convert into 'long'"}, + {rw + `f.lineno = 2**32`, "RangeError", "integer 4294967296 too big to convert to 'int'"}, + } + for _, c := range errCases { + if cls, msg := evalErr(t, c.src); cls != c.class || msg != c.msg { + t.Errorf("src=%q: got %s: %q, want %s: %q", c.src, cls, msg, c.class, c.msg) + } + } +} From 0a91b6414fbb28985f38e52478f2a77611f94cdf Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 8 Sep 2026 00:26:03 +0200 Subject: [PATCH 4/4] io: sysread buffer coercion, zero-length return, EOF clears buffer (core/io) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IO#sysread (io.c rb_io_sysread), reading MRI io.c v3_4_0 + ruby/spec: coerce the optional output buffer with #to_str (io_setstrbuf → StringValue); a zero-length read returns the buffer untouched (or a fresh ""); and an end-of-file read empties the given buffer (io_set_read_length to 0) before raising EOFError. sysread 13->16 (the remaining fail needs read-buffer bookkeeping after a buffered read). 100% coverage on internal/vm; verified byte-for-byte against MRI Ruby 4.0.5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io_descriptors.go | 20 ++++++++++++++------ internal/vm/io_wave19_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 21058ade..92f7e377 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -106,7 +106,7 @@ func defIOReadExtra(cls *RClass) { // 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 { + cls.define("sysread", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) o.pipeRefresh() @@ -114,16 +114,24 @@ func defIOReadExtra(cls *RClass) { if n < 0 { raise("ArgumentError", "negative length %d given", n) } + // The optional output buffer is coerced with #to_str (io_setstrbuf → + // StringValue), as for pread. var buf *object.String if len(args) > 1 { - if b, ok := args[1].(*object.String); ok { - buf = b - } + buf = vm.ioBufferArg(args[1]) } - if n == 0 { - return ioReadResult(nil, buf) // a zero-length sysread is "" even at EOF + if n == 0 { // a zero-length sysread returns "" (or the buffer untouched) + if buf != nil { + return buf + } + return ioReadResult(nil, nil) } if o.pos >= len(o.buf) { + // MRI empties the output buffer (io_set_read_length to 0) before + // signalling end-of-file. + if buf != nil { + buf.SetBytes(nil) + } raise("EOFError", "end of file reached") } end := min(o.pos+n, len(o.buf)) diff --git a/internal/vm/io_wave19_test.go b/internal/vm/io_wave19_test.go index 2e27fc9c..d6081892 100644 --- a/internal/vm/io_wave19_test.go +++ b/internal/vm/io_wave19_test.go @@ -345,6 +345,31 @@ func TestIOWave19StreamModes(t *testing.T) { } } +// TestIOWave19Sysread covers IO#sysread's buffer handling (io.c rb_io_sysread): +// #to_str coercion of the buffer, the length==0 early return (buffer untouched, +// or a fresh ""), and the buffer being emptied before an EOFError. Asserted +// against MRI Ruby 4.0.5. +func TestIOWave19Sysread(t *testing.T) { + cases := []struct{ src, want string }{ + // #to_str coercion of the buffer object. + {`require "stringio"; s = StringIO.new("hello"); o = Object.new; def o.to_str; @b ||= +"xx"; end; r = s.sysread(3, o); p [r.equal?(o.to_str), o.to_str]`, "[true, \"hel\"]\n"}, + // length 0 returns the buffer untouched, or a fresh "". + {`require "stringio"; s = StringIO.new("hello"); b = +"keep"; r = s.sysread(0, b); p [r.equal?(b), b]`, "[true, \"keep\"]\n"}, + {`require "stringio"; p StringIO.new("hello").sysread(0)`, "\"\"\n"}, + // EOF empties the given buffer, then raises. + {`require "stringio"; s = StringIO.new(""); b = +"content"; s.sysread(1, b) rescue nil; p b.empty?`, "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) + } + } + // EOF with no buffer still raises EOFError. + if cls, _ := evalErr(t, `require "stringio"; StringIO.new("").sysread(1)`); cls != "EOFError" { + t.Errorf("sysread at EOF: got %s", cls) + } +} + // TestIOWave19LinenoAndOfft covers IO#lineno / #lineno= char-readability checks // (a closed/write-only real IO raises, a StringIO does not) and the NUM2OFFT / // NUM2INT range errors of #pos= / #seek / #lineno= (a Bignum, and an Integer that