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
170 changes: 132 additions & 38 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (o *IOObj) writeBytes(p []byte) int {
copy(o.buf[o.pos:], p)
o.pos += len(p)
o.syncStr()
if o.sync { // sync=true: a File's writes reach disk immediately (no buffering)
ioFlush(o)
}
return len(p)
}
n, _ := o.w.Write(p)
Expand Down Expand Up @@ -201,7 +204,8 @@ func (vm *VM) registerIO() {
defStringIOExtra(vm, cStringIO)

stdout := &IOObj{cls: cIO, w: vm.out, label: "STDOUT"}
stderr := &IOObj{cls: cIO, w: vm.errOut, label: "STDERR"}
// 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
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 @@ -604,34 +608,51 @@ func defIOWrite(cls *RClass) {
if len(args) == 0 {
raise("ArgumentError", "wrong number of arguments (given 0, expected 1+)")
}
o.writeStr(vm.formatString(args[0].ToS(), args[1:]))
// rb_io_printf: the format is coerced with StringValue (#to_str).
o.writeStr(vm.formatString(string(vm.strToStr(args[0])), args[1:]))
return object.NilV
})
cls.define("putc", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
cls.define("putc", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
ioCheckOpen(o)
switch a := args[0].(type) {
case object.Integer:
o.writeBytes([]byte{byte(a)})
case *object.String:
if len(a.Bytes()) > 0 {
o.writeBytes(a.Bytes()[:1])
// rb_io_putc: a String yields its first character; anything else is coerced
// with NUM2CHR (#to_int, then the low byte), so nil/false/true raise
// TypeError. The single-character String is written through #write (so a
// subclass' or mock's #write observes it), and the original argument returns.
var ch []byte
if s, ok := args[0].(*object.String); ok {
if b := s.Bytes(); len(b) > 0 {
_, sz := utf8.DecodeRune(b)
ch = b[:sz]
}
default:
raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(args[0]))
} else {
ch = []byte{byte(vm.toIntCoerce(args[0]))}
}
vm.send(self, "write", []object.Value{object.NewStringBytes(ch)}, nil)
return args[0]
})
cls.define("flush", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
ioFlush(self.(*IOObj))
o := self.(*IOObj)
ioClosedRealIO(o) // MRI: IO#flush raises on a closed stream; StringIO#flush does not
ioFlush(o)
return self
})
cls.define("fsync", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { return object.IntValue(0) })
cls.define("fsync", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
ioClosedRealIO(self.(*IOObj)) // as flush: real IO raises on close, StringIO does not
return object.IntValue(0)
})
// These sync/sync= entries serve a real IO; StringIO overrides both in
// defStringIOExtra (its #sync is always true), so the closed-stream guard here
// only ever fires for a real IO/File, which MRI makes raise IOError.
cls.define("sync", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.Bool(self.(*IOObj).sync)
o := self.(*IOObj)
ioClosedRealIO(o)
return object.Bool(o.sync)
})
cls.define("sync=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
self.(*IOObj).sync = args[0].Truthy()
o := self.(*IOObj)
ioClosedRealIO(o)
o.sync = args[0].Truthy()
return args[0]
})
cls.define("close", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
Expand All @@ -646,7 +667,10 @@ func defIOWrite(cls *RClass) {
cls.define("closed?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.Bool(self.(*IOObj).closed)
})
cls.define("tty?", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(false) })
cls.define("tty?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
ioClosedRealIO(self.(*IOObj)) // real IO raises on a closed stream; StringIO does not
return object.Bool(false)
})
// #isatty is a true alias of #tty?, as in MRI.
cls.methods["isatty"] = cls.methods["tty?"]
// IO#binmode marks the stream binary (#binmode? true) and, as MRI's
Expand Down Expand Up @@ -738,22 +762,32 @@ func defStringIORead(cls *RClass) {
cls.methods["size"] = cls.methods["length"]
cls.define("eof?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
ioCheckReadable(o) // MRI: eof? raises on a closed or non-readable stream
o.pipeRefresh()
return object.Bool(o.pos >= len(o.buf))
})
// #eof is a true alias of #eof?, as in MRI.
cls.methods["eof"] = cls.methods["eof?"]
cls.define("pos", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.IntValue(int64(self.(*IOObj).pos))
o := self.(*IOObj)
// A real IO/File raises on a closed stream; StringIO#pos tolerates it (MRI).
if o.closed && !ioIsStringIO(o) {
raise("IOError", "closed stream")
}
return object.IntValue(int64(o.pos))
})
// #tell is a true alias of #pos, as in MRI.
cls.methods["tell"] = cls.methods["pos"]
cls.define("pos=", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
if o.closed && !ioIsStringIO(o) { // real IO/File raises; StringIO tolerates it
raise("IOError", "closed stream")
}
n := int(vm.toIntCoerce(args[0]))
if n < 0 {
raise("Errno::EINVAL", "Invalid argument")
}
self.(*IOObj).pos = n
o.pos = n
return args[0]
})
cls.define("seek", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
Expand All @@ -764,7 +798,13 @@ func defStringIORead(cls *RClass) {
amount := int(vm.toIntCoerce(args[0]))
whence := 0
if len(args) > 1 {
whence = int(vm.toIntCoerce(args[1]))
// IO#seek accepts the :SET/:CUR/:END whence symbols; StringIO#seek
// (strio_seek) does not — a Symbol there raises the Integer TypeError.
if ioIsStringIO(o) {
whence = int(vm.toIntCoerce(args[1]))
} else {
whence = vm.seekWhence(args[1])
}
}
var newPos int
switch whence {
Expand All @@ -785,6 +825,7 @@ func defStringIORead(cls *RClass) {
})
cls.define("rewind", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
ioClosedRealIO(o) // real IO/File raises on a closed stream; StringIO tolerates it
o.pos, o.lineno = 0, 0 // #rewind resets both the position and the line number
return object.IntValue(0)
})
Expand Down Expand Up @@ -990,17 +1031,72 @@ func defStringIORead(cls *RClass) {
}
return self
})
// #each_codepoint (rb_io_each_codepoint, io.c) yields each character's integer
// codepoint from the current position; MRI raises ArgumentError on an invalid /
// incomplete byte sequence and returns an Enumerator (size nil) with no block.
// It lives here on the shared read protocol so File/IO carry it, not only
// StringIO.
cls.define("each_codepoint", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value {
o := self.(*IOObj)
if blk == nil { // no block ⇒ an Enumerator (buildable even on a closed stream)
return enumForSized(self, "each_codepoint", enumSizeNil)
}
ioCheckReadable(o) // iterating an unreadable stream raises, as in MRI
for o.pos < len(o.buf) {
r, sz := utf8.DecodeRune(o.buf[o.pos:])
if r == utf8.RuneError && sz <= 1 { // a lone continuation / invalid byte
raise("ArgumentError", "invalid byte sequence in UTF-8")
}
o.pos += sz
vm.callBlock(blk, []object.Value{object.IntValue(int64(r))})
}
return self
})
// #each is a true alias of #each_line — it must share the method entry so
// IO.instance_method(:each) == IO.instance_method(:each_line), as in MRI.
cls.methods["each"] = cls.methods["each_line"]
}

// seekWhence resolves a seek / sysseek whence argument to a SEEK_* constant,
// mirroring MRI's interpret_seek_whence (io.c): the symbols :SET/:CUR/:END and
// :DATA/:HOLE map to their constants, and anything else is coerced through
// #to_int (a Symbol that is not one of those raises the Integer TypeError).
func (vm *VM) seekWhence(v object.Value) int {
if s, ok := v.(object.Symbol); ok {
switch string(s) {
case "SET":
return 0
case "CUR":
return 1
case "END":
return 2
case "DATA":
return 3
case "HOLE":
return 4
}
raise("TypeError", "no implicit conversion of Symbol into Integer")
}
return int(vm.toIntCoerce(v))
}

// ioIsStringIO reports whether o is a StringIO (or a subclass), which — unlike a
// real IO/File — tolerates #pos / #pos= on a closed stream rather than raising.
func ioIsStringIO(o *IOObj) bool {
for c := o.cls; c != nil; c = c.super {
if c.name == "StringIO" {
return true
}
}
return false
}

// defStringIOExtra installs the StringIO-only surface that MRI adds on top of the
// shared read/write protocol: the allocate/new/open class methods, the private
// #initialize and #reopen instance methods (which share the backing-string setup
// in stringIOSetup), #string=, #each_codepoint, and the StringIO-specific
// overrides of #sync/#binmode/#close_read/#close_write/#fcntl whose behaviour
// differs from a real IO's.
// 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.
func (vm *VM) includeStringIOEnumerable() {
Expand Down Expand Up @@ -1073,22 +1169,6 @@ func defStringIOExtra(vm *VM, cls *RClass) {
o.pos, o.lineno = 0, 0
return s
})
cls.define("each_codepoint", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value {
o := self.(*IOObj)
if blk == nil { // no block ⇒ an Enumerator (buildable even on a closed stream)
return enumForSized(self, "each_codepoint", enumSizeNil)
}
ioCheckReadable(o) // iterating an unreadable stream raises, as in MRI
for o.pos < len(o.buf) {
r, sz := utf8.DecodeRune(o.buf[o.pos:])
if r == utf8.RuneError && sz <= 1 { // a lone continuation / invalid byte
raise("ArgumentError", "invalid byte sequence in UTF-8")
}
o.pos += sz
vm.callBlock(blk, []object.Value{object.IntValue(int64(r))})
}
return self
})
// StringIO#sync is always true and cannot be turned off, unlike a real IO's.
cls.define("sync", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value {
return object.Bool(true)
Expand Down Expand Up @@ -1283,6 +1363,11 @@ func (vm *VM) resolveGetsArgs(args []object.Value) (sep getsSep, limit int, chom
}
args = args[:len(args)-1]
}
// gets/readline/each_line take at most a separator and a limit (chomp: is a
// keyword, stripped above); more positional arguments raise ArgumentError.
if len(args) > 2 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 0..2)", len(args))
}
if len(args) > 0 {
switch a := args[0].(type) {
case object.Integer:
Expand Down Expand Up @@ -1564,6 +1649,15 @@ func ioCheckReadable(o *IOObj) {
}
}

// ioClosedRealIO raises IOError "closed stream" when a real IO/File is closed. It
// is a no-op for a StringIO, whose #flush/#fsync/#tty?/#sync tolerate a closed
// stream where a real IO raises (MRI).
func ioClosedRealIO(o *IOObj) {
if o.closed && !ioIsStringIO(o) {
raise("IOError", "closed stream")
}
}

// 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
5 changes: 5 additions & 0 deletions internal/vm/io_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ func (vm *VM) ioReadFile(args []object.Value, forceBinary bool) object.Value {
var offset int64
if len(pos) > 2 && pos[2] != object.NilV {
if offset = intArg(pos[2]); offset < 0 {
// IO.binread reaches a negative offset through rb_io_seek (Errno::EINVAL);
// IO.read validates it in Ruby and raises ArgumentError.
if forceBinary {
raise("Errno::EINVAL", "Invalid argument @ rb_io_seek - %s", name)
}
raise("ArgumentError", "negative offset %d given", offset)
}
}
Expand Down
55 changes: 32 additions & 23 deletions internal/vm/io_descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ func defIOReadExtra(cls *RClass) {
}
return object.NilV
})
cls.define("ungetc", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
cls.define("ungetc", 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(string(rune(a))))
case *object.String:
ioUnget(o, a.Bytes())
// rb_io_ungetc: an Integer is the codepoint (encoded to bytes); anything else
// is coerced with StringValue (#to_str), so nil / a non-String object raises
// "no implicit conversion of <x> into String". StringIO#ungetc (strio_ungetc)
// diverges only for nil, which it treats as a no-op.
switch {
case object.IsNil(args[0]) && ioIsStringIO(o):
// StringIO#ungetc(nil): no-op.
default:
if args[0] != object.NilV {
raise("TypeError", "no implicit conversion of %s into String", classNameOf(args[0]))
if a, ok := args[0].(object.Integer); ok {
ioUnget(o, []byte(string(rune(a))))
} else {
ioUnget(o, vm.strToStr(args[0]))
}
}
return object.NilV
Expand Down Expand Up @@ -131,8 +135,9 @@ func defIOReadExtra(cls *RClass) {
cls.define("lineno", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.IntValue(int64(self.(*IOObj).lineno))
})
cls.define("lineno=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
self.(*IOObj).lineno = int(intArg(args[0]))
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]))
return args[0]
})
cls.define("close_read", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
Expand Down Expand Up @@ -215,10 +220,14 @@ func defIOSeekable(cls *RClass) {
o.writable = true // a written File flushes its buffer back on flush/close
return object.IntValue(int64(len(data)))
})
cls.define("sysseek", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
cls.define("sysseek", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
o := self.(*IOObj)
amount := int(intArg(args[0]))
switch whenceArg(args) {
amount := int(vm.toIntCoerce(args[0])) // NUM2OFFT: coerces via #to_int
whence := 0
if len(args) > 1 {
whence = vm.seekWhence(args[1]) // accepts :SET/:CUR/:END symbols
}
switch whence {
case 1: // SEEK_CUR
o.pos += amount
case 2: // SEEK_END
Expand All @@ -236,25 +245,25 @@ func defIOSeekable(cls *RClass) {
return object.Bool(o.binmode)
})
cls.define("autoclose?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.Bool(!self.(*IOObj).noAutoclose)
o := self.(*IOObj)
if o.closed { // MRI: #autoclose? cannot be queried on a closed IO
raise("IOError", "closed stream")
}
return object.Bool(!o.noAutoclose)
})
cls.define("autoclose=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
self.(*IOObj).noAutoclose = !args[0].Truthy()
o := self.(*IOObj)
if o.closed { // MRI: #autoclose= cannot be set on a closed IO
raise("IOError", "closed stream")
}
o.noAutoclose = !args[0].Truthy()
return args[0]
})
cls.define("fdatasync", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value {
return object.IntValue(0)
})
}

// whenceArg returns the SEEK_* whence of a seek-style argument list (default 0).
func whenceArg(args []object.Value) int {
if len(args) > 1 {
return int(intArg(args[1]))
}
return 0
}

// ioUnget inserts p immediately before the cursor (leaving the cursor on the
// re-inserted bytes) so a following read returns them first — the pushback model
// shared by ungetbyte and ungetc.
Expand Down
Loading