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
53 changes: 53 additions & 0 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions internal/vm/io_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15905
16080