diff --git a/internal/vm/io.go b/internal/vm/io.go index e81e9214..ffb8e568 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -32,6 +32,17 @@ type IOObj struct { intEnc string // internal encoding name, "" ⇒ none (nil) fd int // synthetic file descriptor for #fileno (0 ⇒ not yet assigned) + // 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 + // same object (identity) and writes/truncation are visible through it. buf is + // kept in sync with strObj on every mutation. strNil marks a StringIO whose + // backing string has been dropped (set to nil), as StringIO.open does after + // yielding: #string then returns nil. + strObj *object.String + strNil bool + rdModeOff bool // StringIO opened without a read half (write-only mode) — #close_read raises + wrModeOff bool // StringIO opened without a write half (read-only mode) — #close_write raises + // 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 // in this VM is synchronous (Process.spawn / Kernel.exec run to completion @@ -86,6 +97,7 @@ func (o *IOObj) writeBytes(p []byte) int { } copy(o.buf[o.pos:], p) o.pos += len(p) + o.syncStr() return len(p) } n, _ := o.w.Write(p) @@ -94,6 +106,16 @@ func (o *IOObj) writeBytes(p []byte) int { func (o *IOObj) writeStr(s string) int { return o.writeBytes([]byte(s)) } +// syncStr mirrors the working buffer back into the backing String object of a +// StringIO so that #string (which returns that very object) and any external +// reference to it observe writes and truncation. It is a no-op for a real IO or +// a File (no backing String object). +func (o *IOObj) syncStr() { + if o.strObj != nil { + o.strObj.SetBytes(o.buf) + } +} + // pipeRefresh snapshots a pipe reader's shared buffer into the IOObj's own // buf/pos view so the existing StringIO read methods (read/gets/eof?) operate on // the latest pipe contents. It is a no-op for non-pipe streams. @@ -174,43 +196,7 @@ func (vm *VM) registerIO() { defIOWrite(cStringIO) defStringIORead(cStringIO) defIOReadExtra(cStringIO) - cStringIO.smethods["new"] = &Method{name: "new", owner: cStringIO, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - o := &IOObj{cls: cStringIO, isStr: true} - // A trailing options Hash may carry mode: (StringIO.new(str, mode: "r")). - mode, modeGiven := "", false - if h, ok := lastHash(args); ok { - if v, ok := h.Get(object.Symbol("mode")); ok { - mode, modeGiven = stringIOModeString(v), true - } - args = args[:len(args)-1] - } - frozenSrc := false - if len(args) > 0 && !object.IsNil(args[0]) { - s, ok := args[0].(*object.String) - if !ok { - raise("TypeError", "no implicit conversion of %s into String", classNameOf(args[0])) - } - o.buf = append([]byte(nil), s.Bytes()...) - frozenSrc = s.Frozen - } - if len(args) > 1 && !object.IsNil(args[1]) { - mode, modeGiven = stringIOModeString(args[1]), true - } - if !modeGiven { - // With no explicit mode, a frozen backing string opens read-only; a - // mutable one (or none) is read/write, matching MRI. - mode = "r+" - if frozenSrc { - mode = "r" - } - } - read, write, trunc, appnd := stringIOModeFlags(mode) - o.rdClosed, o.wrClosed, o.appendMode = !read, !write, appnd - if trunc { - o.buf = nil - } - return o - }} + defStringIOExtra(vm, cStringIO) stdout := &IOObj{cls: cIO, w: vm.out, label: "STDOUT"} stderr := &IOObj{cls: cIO, w: vm.errOut, label: "STDERR"} @@ -407,13 +393,39 @@ func fileMode(args []object.Value) string { return "r" } -// stringIOModeString normalises a StringIO mode argument (a String like "r+" or -// an Integer bit-OR of File::Constants flags) to an access-mode string. -func stringIOModeString(v object.Value) string { - if i, ok := v.(object.Integer); ok { - return flagsToMode(int64(i)) +// stringIOModeVal resolves a StringIO mode argument to either an Integer flag set +// or a String access mode: Integer and String are taken directly, and any other +// object is converted via #to_str (raising TypeError otherwise). Keeping the +// Integer form (rather than mapping it to an fopen-style string) preserves the +// O_TRUNC / access bits that a lossy string mapping would drop. +func stringIOModeVal(vm *VM, v object.Value) object.Value { + switch v.(type) { + case object.Integer, *object.String: + return v + } + if vm.respondsToDynamic(v, "to_str") { + if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok { + return s + } + } + raise("TypeError", "no implicit conversion of %s into String", classNameOf(v)) + return nil +} + +// stringIOFlagBits decodes an Integer StringIO mode (a bit-OR of IO/File open +// flags) into its capabilities. The access bits (RDONLY/WRONLY/RDWR) set read / +// write; O_TRUNC empties the buffer; O_APPEND forces writes to the end. Unlike a +// mapping through an fopen-style string, a bare WRONLY does not imply truncation. +func stringIOFlagBits(flags int64) (read, write, trunc, appnd bool) { + switch flags & 0x3 { // O_RDONLY / O_WRONLY / O_RDWR + case fO_WRONLY: + read, write = false, true + case fO_RDWR: + read, write = true, true + default: // fO_RDONLY + read, write = true, false } - return strArg(v) + return read, write, flags&fO_TRUNC != 0, flags&fO_APPEND != 0 } // stringIOModeFlags decodes a StringIO access mode ("r", "r+", "w", "w+", "a", @@ -633,7 +645,8 @@ func defIOWrite(cls *RClass) { return object.Bool(self.(*IOObj).closed) }) cls.define("tty?", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(false) }) - cls.define("isatty", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(false) }) + // #isatty is a true alias of #tty?, as in MRI. + cls.methods["isatty"] = cls.methods["tty?"] cls.define("binmode", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return self }) // external_encoding: the stream's external encoding — the one set explicitly @@ -692,63 +705,91 @@ func defIOWrite(cls *RClass) { // content methods, on StringIO. func defStringIORead(cls *RClass) { cls.define("string", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.NewString(string(self.(*IOObj).buf)) - }) - cls.define("size", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(len(self.(*IOObj).buf))) + o := self.(*IOObj) + if o.strNil { // backing string dropped (StringIO.open after its block) + return object.NilV + } + if o.strObj != nil { // return the backing object itself, kept in sync + o.syncStr() + return o.strObj + } + if o.cls != nil && o.cls.name == "StringIO" { // bare-allocated, not yet initialised + raise("IOError", "uninitialized stream") + } + return object.NewString(string(o.buf)) }) cls.define("length", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.IntValue(int64(len(self.(*IOObj).buf))) }) + // #size is a true alias of #length (identical UnboundMethod), as in MRI. + cls.methods["size"] = cls.methods["length"] cls.define("eof?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { o := self.(*IOObj) o.pipeRefresh() return object.Bool(o.pos >= len(o.buf)) }) - cls.define("eof", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - o := self.(*IOObj) - 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)) }) - cls.define("tell", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(self.(*IOObj).pos)) - }) - cls.define("pos=", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - self.(*IOObj).pos = int(toInt(args[0])) + // #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 { + n := int(vm.toIntCoerce(args[0])) + if n < 0 { + raise("Errno::EINVAL", "Invalid argument") + } + self.(*IOObj).pos = n return args[0] }) - cls.define("seek", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("seek", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) - amount := int(toInt(args[0])) + if o.closed { + raise("IOError", "closed stream") + } + amount := int(vm.toIntCoerce(args[0])) whence := 0 if len(args) > 1 { - whence = int(toInt(args[1])) + whence = int(vm.toIntCoerce(args[1])) } + var newPos int switch whence { case 1: // SEEK_CUR - o.pos += amount + newPos = o.pos + amount case 2: // SEEK_END - o.pos = len(o.buf) + amount - default: // SEEK_SET - o.pos = amount + newPos = len(o.buf) + amount + case 0: // SEEK_SET + newPos = amount + default: + raise("Errno::EINVAL", "Invalid argument") + } + if newPos < 0 { + raise("Errno::EINVAL", "Invalid argument") } + o.pos = newPos return object.IntValue(0) }) cls.define("rewind", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - self.(*IOObj).pos = 0 + o := self.(*IOObj) + o.pos, o.lineno = 0, 0 // #rewind resets both the position and the line number return object.IntValue(0) }) - cls.define("truncate", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("truncate", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) - n := int(toInt(args[0])) + if o.wrClosed { // a read-only stream cannot be truncated + raise("IOError", "not opened for writing") + } + n := int(vm.toIntCoerce(args[0])) + if n < 0 { + raise("Errno::EINVAL", "Invalid argument") + } if n < len(o.buf) { o.buf = o.buf[:n] } else if n > len(o.buf) { o.buf = append(o.buf, make([]byte, n-len(o.buf))...) } + o.syncStr() return object.IntValue(0) }) cls.define("read", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -941,6 +982,253 @@ func defStringIORead(cls *RClass) { cls.methods["each"] = cls.methods["each_line"] } +// 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. +// 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() { + if c, ok := vm.consts["StringIO"].(*RClass); ok { + if en, ok := vm.consts["Enumerable"].(*RClass); ok { + c.includes = append(c.includes, en) + } + } +} + +func defStringIOExtra(vm *VM, cls *RClass) { + // StringIO::VERSION — the bundled stringio gem version (specs guard behaviour on + // it via version_is / guard blocks). Matches MRI 4.0.6's stringio 3.2.0. + cls.consts["VERSION"] = object.NewFrozenStringView("3.2.0") + // StringIO.allocate returns a bare, uninitialised StringIO (an IOObj rather + // than the generic RObject) so a following #initialize / #reopen can populate + // it — the StringIO.allocate; io.send(:initialize, ...) construction MRI allows. + cls.smethods["allocate"] = &Method{name: "allocate", owner: cls, native: func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return &IOObj{cls: self.(*RClass), isStr: true} + }} + cls.smethods["new"] = &Method{name: "new", owner: cls, native: func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := &IOObj{cls: self.(*RClass), isStr: true} + stringIOSetup(vm, o, args) + return o + }} + // StringIO.open(...) { |io| ... } opens like .new and, when a block is given, + // yields self, then closes it AND drops its backing string (#string ⇒ nil) + // afterwards — even if the block raises — returning the block's value. Without + // a block it behaves exactly like .new. + cls.smethods["open"] = &Method{name: "open", owner: cls, native: func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + o := &IOObj{cls: self.(*RClass), isStr: true} + stringIOSetup(vm, o, args) + if blk == nil { + return o + } + defer func() { o.closed, o.strNil, o.strObj = true, true, nil }() + return vm.callBlock(blk, []object.Value{o}) + }} + cls.define("initialize", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + stringIOSetup(vm, self.(*IOObj), args) + return self + }) + vm.setInstanceVisibility(cls, "initialize", visPrivate) // MRI keeps #initialize private + cls.define("reopen", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + // A single non-String argument is (converted to) a StringIO whose backing + // string and mode self adopts — MRI tries #to_strio here, never #to_str. + if len(args) == 1 && !object.IsNil(args[0]) { + if _, isStr := args[0].(*object.String); !isStr { + src, ok := stringIOToStrIO(vm, args[0]) + if !ok { + raise("TypeError", "no implicit conversion of %s into StringIO", classNameOf(args[0])) + } + o.buf, o.strObj, o.strNil = src.buf, src.strObj, false + o.pos, o.lineno, o.appendMode = 0, 0, src.appendMode + o.rdClosed, o.wrClosed, o.closed = src.rdClosed, src.wrClosed, false + o.rdModeOff, o.wrModeOff = src.rdModeOff, src.wrModeOff + return o + } + } + stringIOSetup(vm, o, args) + return o + }) + // #string= replaces the backing string (coercing via #to_str), resetting the + // position and line number, and returns the assigned string. + cls.define("string=", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + s := stringIOStrArg(vm, args[0]) + o.strObj, o.buf, o.strNil = s, s.MutableBytes(), false + 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) + }) + cls.define("sync=", func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + return args[0] + }) + // StringIO#binmode sets the external encoding to ASCII-8BIT (BINARY), clears + // any internal encoding, and returns self. + cls.define("binmode", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + o.extEnc, o.intEnc = "ASCII-8BIT", "" + return self + }) + // StringIO#close_read / #close_write (the mode-off IOError guard) live in the + // shared defIOReadExtra, keyed on rdModeOff/wrModeOff which only StringIO sets. + // StringIO#fcntl is unsupported, as in MRI. + cls.define("fcntl", func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + raise("NotImplementedError", "fcntl() function is unimplemented on this machine") + return object.NilV + }) +} + +// stringIOSetup populates (or resets, for #reopen / #initialize on an existing +// object) a StringIO IOObj from its (string, mode, options) arguments, matching +// MRI's StringIO#initialize: the passed String becomes the live backing object, +// the mode (a String, an Integer flag set, or a :mode option) fixes the read / +// write / append / truncate capabilities, and a frozen backing string raises +// Errno::EACCES for a writable mode or FrozenError for a truncating one. +func stringIOSetup(vm *VM, o *IOObj, args []object.Value) { + o.isStr = true + o.pos, o.lineno = 0, 0 + o.closed, o.strNil, o.appendMode = false, false, false + o.extEnc, o.intEnc = "", "" + + var modeVal object.Value // Integer flag set or String access mode, once resolved + binGiven, binVal, textGiven, encGiven := false, false, false, false + if h, ok := lastHash(args); ok { + args = args[:len(args)-1] + if v, ok := h.Get(object.Symbol("mode")); ok && !object.IsNil(v) { + modeVal = stringIOModeVal(vm, v) + } + if v, ok := h.Get(object.Symbol("binmode")); ok { + binGiven, binVal = true, v.Truthy() + } + if _, ok := h.Get(object.Symbol("textmode")); ok { + textGiven = true + } + for _, k := range []string{"encoding", "external_encoding", "internal_encoding"} { + if _, ok := h.Get(object.Symbol(k)); ok { + encGiven = true + } + } + } + // A second positional argument is the mode, overriding a :mode option. + if len(args) > 1 && !object.IsNil(args[1]) { + modeVal = stringIOModeVal(vm, args[1]) + } + // The ":"/"b"/"t" flags only exist on a String mode; an Integer flag set never + // carries them, so these checks and the binary detection apply to a String mode. + modeStr, _ := modeVal.(*object.String) + modeBase := "" + if modeStr != nil { + modeBase = modeStr.Str() + if i := strings.IndexByte(modeBase, ':'); i >= 0 { + modeBase = modeBase[:i] + } + } + modeHasBT := strings.ContainsAny(modeBase, "bt") + // Reject the ways MRI forbids specifying an encoding or the binary/text choice + // twice: an encoding both in the mode string and as an option; binmode / + // textmode both as a b/t mode flag and as an option, or as two options at once. + if encGiven && modeStr != nil && strings.Contains(modeStr.Str(), ":") { + raise("ArgumentError", "encoding specified twice") + } + if (binGiven || textGiven) && modeHasBT { + raise("ArgumentError", "mode specified twice") + } + if binGiven && textGiven { + raise("ArgumentError", "both textmode and binmode specified") + } + + var s *object.String + if len(args) > 0 && !object.IsNil(args[0]) { + s = stringIOStrArg(vm, args[0]) + } + read, write, trunc, appnd := true, true, false, false + switch { + case modeVal == nil: + // With no explicit mode, a frozen backing string opens read-only; a mutable + // one (or none) is read/write, matching MRI. + if s != nil && s.Frozen { + write = false + } + case modeStr != nil: + read, write, trunc, appnd = stringIOModeFlags(modeStr.Str()) + default: // Integer flag set (IO::RDONLY | IO::TRUNC | ...) + read, write, trunc, appnd = stringIOFlagBits(int64(modeVal.(object.Integer))) + } + if s != nil && s.Frozen { + if write { + raise("Errno::EACCES", "Permission denied") + } + if trunc { + raise("FrozenError", "can't modify frozen String: %s", s.Inspect()) + } + } + if s == nil { + s = object.NewString("") + } else if trunc { + s.SetBytes(nil) // truncate the backing string in place, keeping its identity/encoding + } + o.strObj = s + o.buf = s.MutableBytes() + if strings.Contains(modeBase, "b") || (binGiven && binVal) { + o.extEnc = "ASCII-8BIT" + } + o.rdClosed, o.wrClosed = !read, !write + o.rdModeOff, o.wrModeOff = !read, !write + o.appendMode = appnd +} + +// stringIOStrArg coerces a StringIO backing-string argument to a String object, +// returning the very object passed (so #string preserves identity) or the result +// of #to_str, and raising TypeError when neither applies. +func stringIOStrArg(vm *VM, v object.Value) *object.String { + if s, ok := v.(*object.String); ok { + return s + } + if vm.respondsToDynamic(v, "to_str") { + if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok { + return s + } + } + raise("TypeError", "no implicit conversion of %s into String", classNameOf(v)) + return nil +} + +// stringIOToStrIO resolves a #reopen source that must be a StringIO: the object +// itself when it is one, or the result of #to_strio. It never falls back to +// #to_str — MRI's single-argument #reopen does not try a String conversion. +func stringIOToStrIO(vm *VM, v object.Value) (*IOObj, bool) { + if o, ok := v.(*IOObj); ok && o.isStr { + return o, true + } + if vm.respondsToDynamic(v, "to_strio") { + if o, ok := vm.send(v, "to_strio", nil, nil).(*IOObj); ok { + return o, true + } + } + return nil, false +} + // ioGets reads one line from o using MRI's (sep, limit, chomp:) argument shapes, // returning nil at end of input and advancing #lineno on a successful read. It is // the single-shot entry used by ARGF#gets and the IRB reader; the coercion and diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index a52dccb7..2f5c6348 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -137,6 +137,9 @@ func defIOReadExtra(cls *RClass) { }) cls.define("close_read", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + if o.rdModeOff { // a StringIO opened write-only has no read half to close (MRI) + raise("IOError", "not opened for reading") + } o.rdClosed = true if o.wrClosed { // both halves shut ⇒ the stream is fully closed o.closed = true @@ -145,6 +148,9 @@ func defIOReadExtra(cls *RClass) { }) cls.define("close_write", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { o := self.(*IOObj) + if o.wrModeOff { // a StringIO opened read-only has no write half to close (MRI) + raise("IOError", "not opened for writing") + } ioFlush(o) o.wrClosed = true if o.rdClosed { diff --git a/internal/vm/stringio_readextra_coverage_test.go b/internal/vm/stringio_readextra_coverage_test.go new file mode 100644 index 00000000..ac9737b8 --- /dev/null +++ b/internal/vm/stringio_readextra_coverage_test.go @@ -0,0 +1,59 @@ +package vm_test + +import "testing" + +// TestStringIOReadExtraCoverage exercises the buffer-backed byte/char readers +// (getbyte / readbyte / readchar) across their normal and end-of-file branches +// and the ungetbyte / ungetc push-back methods across their Integer, String, +// nil-no-op and wrong-type branches, so defIOReadExtra stays fully covered. +// Verified against MRI Ruby 4.0.6. +func TestStringIOReadExtraCoverage(t *testing.T) { + const req = "require 'stringio'\n" + ok := []struct{ src, want string }{ + // getbyte: two bytes then nil at EOF. + {req + `s = StringIO.new("AB"); p [s.getbyte, s.getbyte, s.getbyte]`, "[65, 66, nil]\n"}, + // readbyte / readchar: a value then EOFError. + {req + `s = StringIO.new("A"); p s.readbyte`, "65\n"}, + {req + `s = StringIO.new("A"); p s.readchar`, "\"A\"\n"}, + // ungetbyte: Integer, String (pushes each byte), and a nil no-op. + {req + `s = StringIO.new("X"); s.getbyte; s.ungetbyte(65); p s.getbyte`, "65\n"}, + {req + `s = StringIO.new("X"); s.getbyte; s.ungetbyte("AB"); p [s.getbyte, s.getbyte]`, "[65, 66]\n"}, + {req + `s = StringIO.new("X"); p s.ungetbyte(nil)`, "nil\n"}, + // ungetc: Integer codepoint and String. + {req + `s = StringIO.new("X"); s.getc; s.ungetc(90); p s.getc`, "\"Z\"\n"}, + {req + `s = StringIO.new("X"); s.getc; s.ungetc("Z"); p s.getc`, "\"Z\"\n"}, + } + for _, c := range ok { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } + + errs := []struct{ src, want string }{ + // End-of-file raises EOFError for readbyte / readchar. + {req + `StringIO.new("").readbyte`, "end of file"}, + {req + `StringIO.new("").readchar`, "end of file"}, + // A wrong-type push-back argument raises TypeError (the default branch). + {req + `StringIO.new("X").ungetbyte([1])`, "TypeError"}, + {req + `StringIO.new("X").ungetc([1])`, "TypeError"}, + } + for _, c := range errs { + err := runErr(t, c.src) + if err == nil { + t.Errorf("runErr(%q) = nil, want error containing %q", c.src, c.want) + continue + } + if !containsStr(err.Error(), c.want) { + t.Errorf("runErr(%q) error = %q, want containing %q", c.src, err.Error(), c.want) + } + } +} + +func containsStr(s, sub string) bool { + for i := 0; i+len(sub) <= len(s); i++ { + if s[i:i+len(sub)] == sub { + return true + } + } + return false +} diff --git a/internal/vm/stringio_residuals_test.go b/internal/vm/stringio_residuals_test.go new file mode 100644 index 00000000..91daa5bc --- /dev/null +++ b/internal/vm/stringio_residuals_test.go @@ -0,0 +1,151 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestStringIOResiduals covers the StringIO library methods brought in line with +// MRI Ruby 4.0.6 (stringio 3.2.0): allocate/#initialize/.open construction, the +// backing-string identity and in-place mutation semantics, #reopen, #string=, +// #each_codepoint, #seek/#pos=/#truncate argument coercion and bounds, the +// #sync/#binmode/#close_read/#close_write/#fcntl overrides, and the true aliases +// (#size/#tell/#eof/#isatty). Every expectation was checked against MRI 4.0.6. +func TestStringIOResiduals(t *testing.T) { + const req = `require "stringio"; ` + cases := []struct{ src, want string }{ + // --- backing-string identity and in-place mutation --------------------- + {req + `p StringIO.new(y = +"hi").string.equal?(y)`, "true\n"}, + {req + `s = StringIO.new(x = +"abcd"); s.write("XY"); p x`, "\"XYcd\"\n"}, + {req + `s = StringIO.new(x = +"123456789"); s.truncate(4); p x`, "\"1234\"\n"}, + // StringIO.open drops the backing string (#string ⇒ nil) after its block. + {req + `io = nil; StringIO.open(+"z") { |s| io = s }; p io.string`, "nil\n"}, + // A real IO's #string reports its buffer view (rbgo shares the reader). + {req + `p $stdin.string`, "\"\"\n"}, + + // --- aliases (identical UnboundMethods) -------------------------------- + {req + `p StringIO.instance_method(:size) == StringIO.instance_method(:length)`, "true\n"}, + {req + `p StringIO.instance_method(:tell) == StringIO.instance_method(:pos)`, "true\n"}, + {req + `p StringIO.instance_method(:eof) == StringIO.instance_method(:eof?)`, "true\n"}, + {req + `p StringIO.instance_method(:isatty) == StringIO.instance_method(:tty?)`, "true\n"}, + {req + `s = StringIO.new("abcd"); p [s.size, s.length, s.pos, s.tell]`, "[4, 4, 0, 0]\n"}, + {req + `s = StringIO.new(""); p [s.eof?, s.eof, s.isatty]`, "[true, true, false]\n"}, + + // --- pos= --------------------------------------------------------------- + {req + `s = StringIO.new("hello"); s.pos = 2; p s.pos`, "2\n"}, + {req + `o = Object.new; def o.to_int; 3; end; s = StringIO.new("hello"); s.pos = o; p s.pos`, "3\n"}, + + // --- seek --------------------------------------------------------------- + {req + `s = StringIO.new("12345678"); a = (s.seek(2); s.pos); s.seek(1, IO::SEEK_CUR); b = s.pos; s.seek(-2, IO::SEEK_END); p [a, b, s.pos]`, + "[2, 3, 6]\n"}, + {req + `o = Object.new; def o.to_int; 2; end; s = StringIO.new("12345678"); s.seek(o); p s.pos`, "2\n"}, + + // --- rewind resets position and line number ---------------------------- + {req + `s = StringIO.new("a\nb\n"); s.gets; s.rewind; p [s.pos, s.lineno]`, "[0, 0]\n"}, + + // --- truncate ----------------------------------------------------------- + {req + `s = StringIO.new(+"12345"); s.truncate(2); p s.string`, "\"12\"\n"}, + {req + `s = StringIO.new(+"12"); s.truncate(4); p s.string.bytes`, "[49, 50, 0, 0]\n"}, + {req + `o = Object.new; def o.to_int; 2; end; s = StringIO.new(+"12345"); s.truncate(o); p s.string`, "\"12\"\n"}, + + // --- mode: string, integer flags, #to_str coercion --------------------- + {req + `p StringIO.new("x", mode: "r").closed_write?`, "true\n"}, + {req + `o = Object.new; def o.to_str; "r"; end; p StringIO.new(+"x", o).closed_write?`, "true\n"}, + {req + `p StringIO.new(+"data", IO::WRONLY).closed_read?`, "true\n"}, + {req + `p StringIO.new(+"data", IO::RDWR).string`, "\"data\"\n"}, + {req + `p StringIO.new(+"data", IO::RDONLY).closed_write?`, "true\n"}, + {req + `p StringIO.new(+"data", IO::WRONLY | IO::TRUNC).string`, "\"\"\n"}, + {req + `s = StringIO.new(+"data", IO::WRONLY | IO::APPEND); s.write("X"); p s.string`, "\"dataX\"\n"}, + // binary detection: a "b" mode flag or a :binmode option sets ASCII-8BIT. + {req + `p [StringIO.new(+"", "wb").external_encoding.to_s, StringIO.new(+"", "w", binmode: true).external_encoding.to_s]`, + "[\"ASCII-8BIT\", \"ASCII-8BIT\"]\n"}, + // :textmode / :encoding options are accepted (no b/t or ":" conflict here). + {req + `p [StringIO.new(+"", "w", textmode: true).closed_read?, StringIO.new(+"", "w", encoding: "UTF-8").closed_read?]`, + "[true, true]\n"}, + + // --- string backend defaults ------------------------------------------- + {req + `p StringIO.new("f".freeze).closed_write?`, "true\n"}, + {req + `p StringIO.new(+"m").closed_write?`, "false\n"}, + {req + `o = Object.new; def o.to_str; "hi"; end; p StringIO.new(o).string`, "\"hi\"\n"}, + {req + `p StringIO.new.string`, "\"\"\n"}, + + // --- allocate + private #initialize ------------------------------------ + {req + `io = StringIO.allocate; io.send(:initialize, +"ex", "r"); p [io.string, io.closed_write?]`, + "[\"ex\", true]\n"}, + {req + `p StringIO.private_instance_methods(false).include?(:initialize)`, "true\n"}, + + // --- open --------------------------------------------------------------- + {req + `p StringIO.open("x") { 42 }`, "42\n"}, + {req + `p StringIO.open("x").string`, "\"x\"\n"}, + {req + `io = nil; (StringIO.open(+"x") { |s| io = s; raise "e" } rescue nil); p [io.closed?, io.string]`, + "[true, nil]\n"}, + + // --- string= ------------------------------------------------------------ + {req + `s = StringIO.new("a\nb"); s.pos = 2; s.lineno = 1; r = (s.string = "new"); p [r, s.string, s.pos, s.lineno]`, + "[\"new\", \"new\", 0, 0]\n"}, + {req + `o = Object.new; def o.to_str; "cvt"; end; s = StringIO.new("x"); s.string = o; p s.string`, "\"cvt\"\n"}, + + // --- reopen ------------------------------------------------------------- + {req + `s = StringIO.new("orig"); s.read(2); s.reopen("new", "r"); p [s.string, s.pos, s.closed_write?]`, + "[\"new\", 0, true]\n"}, + {req + `s = StringIO.new("x"); s.close; s.reopen; p [s.closed_read?, s.closed_write?]`, "[false, false]\n"}, + {req + `s = StringIO.new("x"); s.reopen(+"reop"); p s.string`, "\"reop\"\n"}, + {req + `a = StringIO.new("src"); b = StringIO.new("x"); b.reopen(a); p b.string`, "\"src\"\n"}, + {req + `o = Object.new; def o.to_strio; StringIO.new("conv"); end; b = StringIO.new("x"); b.reopen(o); p b.string`, + "\"conv\"\n"}, + + // --- each_codepoint ----------------------------------------------------- + {req + `r = []; StringIO.new("ab").each_codepoint { |c| r << c }; p r`, "[97, 98]\n"}, + {req + `p StringIO.new("ab").each_codepoint.to_a`, "[97, 98]\n"}, + {req + `s = StringIO.new("a"); p s.each_codepoint { |c| }.equal?(s)`, "true\n"}, + + // --- sync / binmode ----------------------------------------------------- + {req + `s = StringIO.new(+""); a = s.sync; s.sync = false; p [a, s.sync]`, "[true, true]\n"}, + {req + `s = StringIO.new(+""); s.binmode; p s.external_encoding.to_s`, "\"ASCII-8BIT\"\n"}, + + // --- close_read / close_write ------------------------------------------- + {req + `s = StringIO.new(+"x"); p [s.close_read, s.close_read]`, "[nil, nil]\n"}, + {req + `s = StringIO.new(+"x"); p s.close_write`, "nil\n"}, + {req + `s = StringIO.new(+"x"); s.close_read; s.close_write; p s.closed?`, "true\n"}, + {req + `s = StringIO.new(+"x"); s.close_write; s.close_read; p s.closed?`, "true\n"}, + + // --- Enumerable mixin --------------------------------------------------- + {req + `p StringIO.include?(Enumerable)`, "true\n"}, + {req + `p StringIO.new("a\nb\n").map { |l| l.chomp }`, "[\"a\", \"b\"]\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } + + // Error cases: the class of the raised exception (or its message) as checked + // against MRI 4.0.6. + errs := []struct{ src, want string }{ + {req + `StringIO.new("x").pos = -1`, "Invalid argument"}, + {req + `StringIO.new("x").seek(0, 3)`, "Invalid argument"}, + {req + `StringIO.new("x").seek(-5)`, "Invalid argument"}, + {req + `s = StringIO.new("x"); s.close; s.seek(0)`, "closed stream"}, + {req + `StringIO.new(+"x").truncate(-1)`, "Invalid argument"}, + {req + `StringIO.new("x", "r").truncate(0)`, "not opened for writing"}, + {req + `StringIO.new(+"x", Object.new)`, "no implicit conversion"}, + {req + `StringIO.new(Object.new)`, "no implicit conversion"}, + {req + `StringIO.new("x").reopen(Object.new)`, "no implicit conversion of Object into StringIO"}, + {req + `StringIO.new("f".freeze, "w")`, "Permission denied"}, + {req + `StringIO.new("f".freeze, IO::TRUNC)`, "FrozenError"}, + {req + `StringIO.allocate.string`, "uninitialized stream"}, + {req + `StringIO.new(+"", "w:UTF-8", encoding: "UTF-8")`, "encoding specified twice"}, + {req + `StringIO.new(+"", "wb", binmode: true)`, "mode specified twice"}, + {req + `StringIO.new(+"", "w", binmode: true, textmode: true)`, "both textmode and binmode"}, + {req + `StringIO.new("x", "w").each_codepoint { |c| }`, "not opened for reading"}, + {req + `StringIO.new("λ").tap { |s| s.pos = 1 }.each_codepoint { |c| }`, "invalid byte sequence"}, + {req + `StringIO.new(+"x", "w").close_read`, "not opened for reading"}, + {req + `StringIO.new(+"x", "r").close_write`, "not opened for writing"}, + {req + `StringIO.new("x").fcntl(1, 1)`, "unimplemented"}, + } + for _, c := range errs { + if err := runErr(t, c.src); err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("src=%q got=%v want error containing %q", c.src, err, c.want) + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index d615889e..e0d06c9c 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -855,19 +855,20 @@ func New(out io.Writer) *VM { vm.consts["ARGV"] = argv vm.globals["$*"] = argv vm.installPrelude() - vm.registerEnumerator() // after the prelude so it can mix in Enumerable - vm.registerActiveSupport() // ActiveSupport::Inflector + core extensions (require "active_support" / "active_support/all"), backed by go-ruby-activesupport; after the prelude so the Enumerable module (which its core-ext extends) exists - vm.registerActionView() // ActionView::Base view context (tag/url/form/text/number helpers + render) + FormBuilder + PartialIteration + ActiveSupport::SafeBuffer / String#html_safe (require "action_view"), backed by go-ruby-actionview; the URLFor (routes) and RenderTemplate seams wire to Ruby callables, the inline-render default evals ERB through the already-bound go-ruby-erb compiler; after registerActiveSupport (SafeBuffer nests under ActiveSupport) and after the bootstrap ERB/Erubi registration (escaping/compiler surface); #render stays a dispatchable method for a later actionpack/actionmailer binding - vm.registerLazy() // after Enumerator (Enumerator::Lazy is built on it) - vm.mixinTimeComparable() // Time includes Comparable (its #<=> drives Comparable); after the prelude so the module exists - vm.registerFileStat() // File::Stat / FileTest; after the prelude so File::Stat can mix in Comparable - vm.registerIPAddr() // IPAddr (require "ipaddr"), backed by go-ruby-ipaddr; after the prelude so IPAddr can mix in Comparable - vm.registerPathname() // Pathname lexical ops (cleanpath/relative_path_from/...), backed by go-ruby-pathname; after the prelude so it reopens the prelude-defined class - vm.registerOstruct() // OpenStruct data ops (to_h/inspect/dig/delete_field/==), backed by go-ruby-ostruct; after the prelude so it reopens the prelude-defined class - vm.registerLogger() // Logger (require "logger"), backed by go-ruby-logger; after the prelude so Logger::Error etc. can subclass the exception hierarchy - vm.registerPStore() // PStore (require "pstore"), backed by go-ruby-pstore; after the prelude so PStore::Error < StandardError - vm.includeMySQLEnumerable() // Mysql2::Result mixes in Enumerable; after the prelude so the module exists - vm.includeWeakMapEnumerable() // ObjectSpace::WeakMap mixes in Enumerable; after the prelude so the module exists + vm.registerEnumerator() // after the prelude so it can mix in Enumerable + vm.registerActiveSupport() // ActiveSupport::Inflector + core extensions (require "active_support" / "active_support/all"), backed by go-ruby-activesupport; after the prelude so the Enumerable module (which its core-ext extends) exists + vm.registerActionView() // ActionView::Base view context (tag/url/form/text/number helpers + render) + FormBuilder + PartialIteration + ActiveSupport::SafeBuffer / String#html_safe (require "action_view"), backed by go-ruby-actionview; the URLFor (routes) and RenderTemplate seams wire to Ruby callables, the inline-render default evals ERB through the already-bound go-ruby-erb compiler; after registerActiveSupport (SafeBuffer nests under ActiveSupport) and after the bootstrap ERB/Erubi registration (escaping/compiler surface); #render stays a dispatchable method for a later actionpack/actionmailer binding + vm.registerLazy() // after Enumerator (Enumerator::Lazy is built on it) + vm.mixinTimeComparable() // Time includes Comparable (its #<=> drives Comparable); after the prelude so the module exists + vm.registerFileStat() // File::Stat / FileTest; after the prelude so File::Stat can mix in Comparable + vm.registerIPAddr() // IPAddr (require "ipaddr"), backed by go-ruby-ipaddr; after the prelude so IPAddr can mix in Comparable + vm.registerPathname() // Pathname lexical ops (cleanpath/relative_path_from/...), backed by go-ruby-pathname; after the prelude so it reopens the prelude-defined class + vm.registerOstruct() // OpenStruct data ops (to_h/inspect/dig/delete_field/==), backed by go-ruby-ostruct; after the prelude so it reopens the prelude-defined class + vm.registerLogger() // Logger (require "logger"), backed by go-ruby-logger; after the prelude so Logger::Error etc. can subclass the exception hierarchy + vm.registerPStore() // PStore (require "pstore"), backed by go-ruby-pstore; after the prelude so PStore::Error < StandardError + vm.includeMySQLEnumerable() // Mysql2::Result mixes in Enumerable; after the prelude so the module exists + vm.includeWeakMapEnumerable() // ObjectSpace::WeakMap mixes in Enumerable; after the prelude so the module exists + vm.includeStringIOEnumerable() // StringIO mixes in Enumerable; after the prelude so the module exists vm.installHashKeyHook() // The prelude and built-ins are loaded; arm the level-2 AOT top level so the // next Run (the user program) dispatches to the compiled aotMain, if one was