Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions internal/vm/io_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ type VM struct {
cEnumeratorProduct *RClass
cIOBuffer *RClass
cARGF *RClass
nextFd int
cYielder *RClass
cWeakMap *RClass
cWeakKeyMap *RClass
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16170
16180