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
23 changes: 15 additions & 8 deletions internal/vm/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions internal/vm/file_constants_internal_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
12 changes: 11 additions & 1 deletion internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down