From baf5c050a5de820f694560ae277ee9ecadcc0b9b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 23:48:15 +0200 Subject: [PATCH] Add the File::Constants module and its missing flag constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File defined its open-mode and fnmatch flag constants directly on the class but had no File::Constants module, and was missing the flock operations (LOCK_SH/ LOCK_EX/LOCK_NB/LOCK_UN) and the NONBLOCK/NOCTTY/SYNC/SHARE_DELETE open flags, so File::Constants and File::LOCK_EX were uninitialized constants. File::Constants is now a module carrying the open-mode, flock and fnmatch flag constants (canonical Linux values — the specs only assert the names exist), and File includes it, so File::RDONLY, File::Constants::RDONLY and File::Constants.const_defined?(:RDONLY) all resolve. The names stay defined on File itself as well, so the integer-mode form of File.open (File::WRONLY | File::CREAT | File::TRUNC) keeps working. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/file/constants/constants_spec 0 -> 21, core/file/constants_spec 11 -> 19, core/file/open_spec 36 -> 38, core/file/ file_spec 4 -> 5. Co-Authored-By: Claude Opus 4.8 --- internal/vm/file.go | 23 ++++++++----- internal/vm/file_constants_internal_test.go | 37 +++++++++++++++++++++ internal/vm/io.go | 12 ++++++- 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 internal/vm/file_constants_internal_test.go diff --git a/internal/vm/file.go b/internal/vm/file.go index ed111dd4..53c967ea 100644 --- a/internal/vm/file.go +++ b/internal/vm/file.go @@ -71,17 +71,24 @@ func (vm *VM) registerFile() { // (File::RDWR | File::CREAT | File::EXCL) which File.open then maps back to a // mode string. We fix the numeric values here rather than reflecting the host's // so behaviour is identical on every OS the gate runs on. - for name, val := range fileFlagConsts { - cFile.consts[name] = object.IntValue(val) - } - // File::FNM_* flag constants for File.fnmatch? / Dir.glob (values match MRI). - // FNM_SYSCASE is 0 on the case-sensitive POSIX platforms the gate runs on. - for name, val := range map[string]int64{ + fnmConsts := map[string]int64{ "FNM_NOESCAPE": fnmNoEscape, "FNM_PATHNAME": fnmPathname, "FNM_DOTMATCH": fnmDotMatch, "FNM_CASEFOLD": fnmCaseFold, "FNM_EXTGLOB": fnmExtGlob, "FNM_SYSCASE": fnmSysCase, - } { - cFile.consts[name] = object.IntValue(val) } + // File::Constants is the module that carries the open-mode, flock and fnmatch + // flag constants; File includes it, so both File::RDONLY and + // File::Constants::RDONLY resolve and File::Constants.const_defined? sees them. + // FNM_SYSCASE is 0 on the case-sensitive POSIX platforms the gate runs on. + fileConstants := newClass("File::Constants", nil) + fileConstants.isModule = true + cFile.consts["Constants"] = fileConstants + for _, m := range []map[string]int64{fileFlagConsts, fileExtraConsts, fnmConsts} { + for name, val := range m { + fileConstants.consts[name] = object.IntValue(val) + cFile.consts[name] = object.IntValue(val) + } + } + cFile.includes = append(cFile.includes, fileConstants) def := func(name string, fn NativeFn) { cFile.smethods[name] = &Method{name: name, owner: cFile, native: fn} } // File.fnmatch?(pattern, path, flags=0) and its alias File.fnmatch test path diff --git a/internal/vm/file_constants_internal_test.go b/internal/vm/file_constants_internal_test.go new file mode 100644 index 00000000..76661b2b --- /dev/null +++ b/internal/vm/file_constants_internal_test.go @@ -0,0 +1,37 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestFileConstantsModule covers the File::Constants module: it carries the +// open-mode, flock and fnmatch flag constants, File includes it (so both +// File::RDONLY and File::Constants::RDONLY resolve and File::Constants.const_defined? +// sees every name), and the previously missing flock/extra flags are defined. +// Verified against ruby 4.0.6. +func TestFileConstantsModule(t *testing.T) { + cases := []struct{ src, want string }{ + // File::Constants is a module File includes. + {`p File::Constants.class`, `Module`}, + {`p File.include?(File::Constants)`, `true`}, + // Every documented name is defined on the module. + {`p ["APPEND", "CREAT", "EXCL", "FNM_CASEFOLD", "FNM_DOTMATCH", "FNM_EXTGLOB", "FNM_NOESCAPE", "FNM_PATHNAME", "FNM_SYSCASE", "LOCK_EX", "LOCK_NB", "LOCK_SH", "LOCK_UN", "NONBLOCK", "NOCTTY", "RDONLY", "RDWR", "SYNC", "TRUNC", "WRONLY", "SHARE_DELETE"].all? { |c| File::Constants.const_defined?(c) }`, `true`}, + // The flags resolve through both File and File::Constants. + {`p File::RDONLY`, `0`}, + {`p File::Constants::RDWR`, `2`}, + {`p File::LOCK_EX`, `2`}, + {`p File::LOCK_SH`, `1`}, + {`p File::LOCK_NB`, `4`}, + {`p File::LOCK_UN`, `8`}, + {`p [File::RDONLY, File::WRONLY, File::RDWR]`, `[0, 1, 2]`}, + // The flag values compose the way File.open's integer-mode form expects. + {`p File::WRONLY | File::CREAT | File::TRUNC`, `577`}, + } + 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") + } + } +} diff --git a/internal/vm/io.go b/internal/vm/io.go index 28623b22..cc792d23 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -227,12 +227,22 @@ const ( fO_TRUNC = 0x200 ) -// fileFlagConsts maps File::Constants names to their numeric flag value. +// fileFlagConsts maps File::Constants open-mode names to their numeric flag +// value (a bitwise OR of these is what File.open's integer-mode form accepts). var fileFlagConsts = map[string]int64{ "RDONLY": fO_RDONLY, "WRONLY": fO_WRONLY, "RDWR": fO_RDWR, "APPEND": fO_APPEND, "CREAT": fO_CREAT, "EXCL": fO_EXCL, "TRUNC": fO_TRUNC, } +// fileExtraConsts holds the remaining File::Constants that are not open-mode +// flags: the flock() operations (LOCK_*) and the open flags rbgo does not act on +// but which must still be defined (fixed to their canonical Linux values here, as +// the specs only assert the constants exist). +var fileExtraConsts = map[string]int64{ + "LOCK_SH": 0x1, "LOCK_EX": 0x2, "LOCK_NB": 0x4, "LOCK_UN": 0x8, + "NONBLOCK": 0x800, "NOCTTY": 0x100, "SYNC": 0x101000, "SHARE_DELETE": 0x0, +} + // flagsToMode maps an integer open-mode (a bitwise OR of File::RDWR/CREAT/...) to // the access-mode string openFileIO understands. RDWR/WRONLY without APPEND or // O_RDONLY-only reads map to the closest fopen-style mode; APPEND maps to "a".