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