From bad05796e1f8a66d5d98d3b7d91e38c24092bdf5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 29 Aug 2026 15:34:13 +0200 Subject: [PATCH] IO: add fileno / to_i (synthetic descriptors) IO#fileno (and its #to_i alias) returns the stream's descriptor: the standard streams report 0/1/2, and any other IO gets a distinct synthetic descriptor, assigned on first request and stable thereafter. rbgo has no real OS file descriptors, so this is an identity rather than a kernel fd, and it is defined only on IO (not StringIO). A closed stream raises IOError. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 16170 -> 16180 (core/io fileno +2, new +7, plus open/for_fd). The real-fd operations (IO.for_fd round-trips) remain out of reach of the in-memory IO model. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 28 ++++++++++++++++++++++++++++ internal/vm/io_internal_test.go | 17 +++++++++++++++++ internal/vm/vm.go | 1 + scripts/conformance/rubyspec/FLOOR | 2 +- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index cb139558..77ff3a5f 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -29,6 +29,7 @@ type IOObj struct { wrClosed bool // #close_write was called — writes raise "not opened for writing" extEnc string // external encoding name, "" ⇒ Encoding.default_external intEnc string // internal encoding name, "" ⇒ none (nil) + fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) // Pipe ends (IO.pipe) share a single byte buffer in *pipe. The write end // appends; the read end drains from pipe.rpos. Because subprocess execution @@ -132,6 +133,33 @@ func (vm *VM) registerIO() { defIOReadExtra(cIO) defIOSeekable(cIO) // pread/pwrite/sysseek/binmode?/autoclose — IO+File, not StringIO + // fileno / to_i: the stream's descriptor. The standard streams are 0/1/2; any + // other IO gets a distinct synthetic descriptor on first request. rbgo has no + // real file descriptors, so this is an identity, not an OS fd. StringIO has no + // #fileno, so it is defined only here on IO. A closed stream raises IOError. + vm.nextFd = 2 // synthetic descriptors for non-standard streams start at 3 + fileno := func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.closed { + raise("IOError", "closed stream") + } + switch o.label { + case "STDIN": + return object.IntValue(0) + case "STDOUT": + return object.IntValue(1) + case "STDERR": + return object.IntValue(2) + } + if o.fd == 0 { + vm.nextFd++ + o.fd = vm.nextFd + } + return object.IntValue(int64(o.fd)) + } + cIO.define("fileno", fileno) + cIO.define("to_i", fileno) + cStringIO := newClass("StringIO", vm.cObject) vm.consts["StringIO"] = cStringIO defIOWrite(cStringIO) diff --git a/internal/vm/io_internal_test.go b/internal/vm/io_internal_test.go index 50762137..41c1dccc 100644 --- a/internal/vm/io_internal_test.go +++ b/internal/vm/io_internal_test.go @@ -43,3 +43,20 @@ func TestIOEncodingMethods(t *testing.T) { } } } + +// TestIOFileno covers IO#fileno / #to_i: the standard streams report 0/1/2, any +// other stream gets a distinct descriptor that is stable across calls, and a +// closed stream raises IOError. Asserted against MRI Ruby 4.0.6. +func TestIOFileno(t *testing.T) { + cases := []struct{ src, want string }{ + {`p [$stdin.fileno, $stdout.fileno, $stderr.fileno]`, "[0, 1, 2]\n"}, + {`p $stdout.to_i`, "1\n"}, + {`r, w = IO.pipe; p [r.fileno.is_a?(Integer), r.fileno != w.fileno, r.fileno == r.fileno]`, "[true, true, true]\n"}, + {`r, w = IO.pipe; r.close; begin; r.fileno; rescue IOError => e; p e.class; end`, "IOError\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 7567c636..a7573e3e 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -387,6 +387,7 @@ type VM struct { cEnumeratorProduct *RClass cIOBuffer *RClass cARGF *RClass + nextFd int cYielder *RClass cWeakMap *RClass cWeakKeyMap *RClass diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index ff8cb537..f827ae6d 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16170 +16180