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
265 changes: 225 additions & 40 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<NAME>" 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
Expand All @@ -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
Expand Down Expand Up @@ -175,26 +218,118 @@ 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)
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 {
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 "<STDIN>"/"<STDOUT>"/
// "<STDERR>" 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): "#<Class:PATH>" when a path is known — a closed
// path stream appends " (closed)" — else "#<Class:fd N>" for an open plain
// descriptor and "#<Class:(closed)>" 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
Expand All @@ -203,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

Expand Down Expand Up @@ -718,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
})
}

Expand Down Expand Up @@ -783,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")
}
Expand All @@ -795,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
Expand Down Expand Up @@ -952,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
Expand Down Expand Up @@ -1097,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)
}
}
}
}
Expand Down Expand Up @@ -1658,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 {
Expand Down
Loading