From 19a86d93f89bf0078b6524cee4fc698a4fe40db5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 28 Aug 2026 20:34:00 +0200 Subject: [PATCH] IO: add external_encoding, internal_encoding and set_encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IO streams now carry an external/internal encoding pair (new IOObj.extEnc / intEnc fields): - external_encoding returns the explicitly-set encoding, else Encoding .default_external — except a write-only stream with no encoding and no default internal encoding, which reports nil, as MRI does. - internal_encoding returns the set internal encoding, or nil. - set_encoding(ext, int = nil) accepts an Encoding, an encoding name, or a combined "external:internal" string (a nil argument clears that side) and returns self; an unknown name raises ArgumentError and a non-String/Encoding argument a TypeError. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15905 -> 16080 (core/io external_encoding 0->59, internal_encoding 0->79, set_encoding 0->21). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 53 ++++++++++++++++++++++++++++++ internal/vm/io_internal_test.go | 25 ++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/internal/vm/io.go b/internal/vm/io.go index d964a6fb..cb139558 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -27,6 +27,8 @@ type IOObj struct { lineno int // #lineno — advanced by each successful line read (gets/readline) rdClosed bool // #close_read was called — reads raise "not opened for reading" 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) // 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 @@ -511,6 +513,57 @@ func defIOWrite(cls *RClass) { 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) }) 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 + // (at creation or via #set_encoding), else Encoding.default_external. + cls.define("external_encoding", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.extEnc != "" { + if e, ok := vm.findEncoding(o.extEnc); ok { + return e + } + } + // A write-only stream with no explicit encoding reports nil (unless a + // default internal encoding forces transcoding); a readable one reports + // Encoding.default_external. + if o.writable && object.IsNil(vm.send(vm.cEncoding, "default_internal", nil, nil)) { + return object.NilV + } + return vm.send(vm.cEncoding, "default_external", nil, nil) + }) + // internal_encoding: the encoding reads are transcoded to, or nil when none. + cls.define("internal_encoding", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + o := self.(*IOObj) + if o.intEnc != "" { + if e, ok := vm.findEncoding(o.intEnc); ok { + return e + } + } + return object.NilV + }) + // set_encoding(ext, int = nil): set the external (and optional internal) + // encoding. ext may be an Encoding, an encoding name, or a combined + // "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) + 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 { + 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 + } + } else { + o.extEnc = vm.encodingArg(args[0]).name + } + } + if len(args) > 1 && !object.IsNil(args[1]) { + o.intEnc = vm.encodingArg(args[1]).name + } + return o + }) } // defStringIORead defines the reading half of the protocol, plus the cursor and diff --git a/internal/vm/io_internal_test.go b/internal/vm/io_internal_test.go index 6615cab5..50762137 100644 --- a/internal/vm/io_internal_test.go +++ b/internal/vm/io_internal_test.go @@ -18,3 +18,28 @@ func TestIODisplayMarkers(t *testing.T) { } } } + +// TestIOEncodingMethods covers IO#external_encoding / #internal_encoding / +// #set_encoding on a pipe: a readable end defaults to Encoding.default_external +// with no internal encoding, set_encoding accepts a name, a combined +// "ext:int" string and an Encoding pair (returning self), a nil argument clears +// a side, and a non-String/Encoding argument is a TypeError. Asserted against +// MRI Ruby 4.0.6. +func TestIOEncodingMethods(t *testing.T) { + cases := []struct{ src, want string }{ + {`r, w = IO.pipe; p [r.external_encoding.name, r.internal_encoding]`, "[\"UTF-8\", nil]\n"}, + {`r, w = IO.pipe; r.set_encoding("US-ASCII"); p [r.external_encoding.name, r.internal_encoding]`, "[\"US-ASCII\", nil]\n"}, + {`r, w = IO.pipe; r.set_encoding("UTF-8", "UTF-16LE"); p [r.external_encoding.name, r.internal_encoding.name]`, "[\"UTF-8\", \"UTF-16LE\"]\n"}, + {`r, w = IO.pipe; r.set_encoding("UTF-8:UTF-16LE"); p [r.external_encoding.name, r.internal_encoding.name]`, "[\"UTF-8\", \"UTF-16LE\"]\n"}, + {`r, w = IO.pipe; r.set_encoding(Encoding::UTF_8, Encoding::UTF_16LE); p r.internal_encoding.name`, "\"UTF-16LE\"\n"}, + {`r, w = IO.pipe; p r.set_encoding("UTF-8").equal?(r)`, "true\n"}, + {`r, w = IO.pipe; r.set_encoding("UTF-8", "UTF-16LE"); r.set_encoding("US-ASCII"); p [r.external_encoding.name, r.internal_encoding]`, "[\"US-ASCII\", nil]\n"}, + // A non-String/Encoding argument raises TypeError (via the encoding coercion). + {`r, w = IO.pipe; begin; r.set_encoding(42); rescue TypeError => e; p e.message; end`, "\"no implicit conversion of Integer into String\"\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/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index cf516350..83dece49 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15905 +16080