From 4471d790c7343b0fe58f439fdedfacd546b17d59 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 26 Aug 2026 09:26:40 +0200 Subject: [PATCH] Add the IO seek constants and mix File::Constants into IO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IO had no SEEK_SET/SEEK_CUR/SEEK_END (nor SEEK_DATA/SEEK_HOLE) constants, and did not mix in File::Constants, so IO::SEEK_SET and IO::RDONLY were uninitialized constants. The seek-whence constants are now defined on IO — File inherits them since File < IO — and IO includes the File::Constants module, so the open-mode flags resolve through IO (IO::RDONLY, IO.include?(File::Constants)) as in MRI. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/io/constants_spec 0 -> 3, and the newly-defined whence constants also unblock core/io/seek_spec 2 -> 8 and core/io/sysseek_spec 1 -> 5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 10 +++++++ internal/vm/io_constants_internal_test.go | 35 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 internal/vm/io_constants_internal_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index cc792d23..f6e595d6 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -110,6 +110,16 @@ func (o *IOObj) pipeWriterClosed() bool { func (vm *VM) registerIO() { cIO := newClass("IO", vm.cObject) vm.consts["IO"] = cIO + // The IO#seek whence constants live on IO (File inherits them, File < IO). IO + // also mixes in File::Constants so IO::RDONLY and friends resolve, as in MRI. + for name, val := range map[string]int64{ + "SEEK_SET": 0, "SEEK_CUR": 1, "SEEK_END": 2, "SEEK_DATA": 3, "SEEK_HOLE": 4, + } { + cIO.consts[name] = object.IntValue(val) + } + if fc, ok := vm.consts["File"].(*RClass).consts["Constants"].(*RClass); ok { + cIO.includes = append(cIO.includes, fc) + } defIOWrite(cIO) defStringIORead(cIO) // IO carries the read protocol too ($stdin, File streams) defIOReadExtra(cIO) diff --git a/internal/vm/io_constants_internal_test.go b/internal/vm/io_constants_internal_test.go new file mode 100644 index 00000000..6c980545 --- /dev/null +++ b/internal/vm/io_constants_internal_test.go @@ -0,0 +1,35 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestIOConstants covers the IO seek-whence constants (SEEK_SET/CUR/END, plus +// SEEK_DATA/HOLE) defined on IO, that File inherits them (File < IO), and that IO +// mixes in File::Constants so the open-mode flags resolve as IO::RDONLY too. +// Verified against ruby 4.0.6. +func TestIOConstants(t *testing.T) { + cases := []struct{ src, want string }{ + {`p IO::SEEK_SET`, `0`}, + {`p IO::SEEK_CUR`, `1`}, + {`p IO::SEEK_END`, `2`}, + {`p [IO::SEEK_SET, IO::SEEK_CUR, IO::SEEK_END]`, `[0, 1, 2]`}, + {`p IO.const_defined?(:SEEK_SET) && IO.const_defined?(:SEEK_CUR) && IO.const_defined?(:SEEK_END)`, `true`}, + {`p IO.const_defined?(:SEEK_DATA) && IO.const_defined?(:SEEK_HOLE)`, `true`}, + // File inherits the seek constants (File < IO). + {`p File::SEEK_SET`, `0`}, + {`p File::SEEK_END`, `2`}, + // IO mixes in File::Constants, so the open-mode flags resolve through IO. + {`p IO.include?(File::Constants)`, `true`}, + {`p IO::RDONLY`, `0`}, + {`p [IO::RDONLY, IO::WRONLY, IO::RDWR]`, `[0, 1, 2]`}, + {`p IO.const_defined?(:LOCK_EX)`, `true`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}