From 41203ac5166ec3b845667d9a39b6cf32e5d79434 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 21 Aug 2026 20:02:25 +0200 Subject: [PATCH] Implement top-level private/public/protected MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the top level, self is `main` (a plain Object), which had no private/public/protected methods, so `private :foo` / a bare `private` raised NoMethodError ("undefined method 'private' for Object"). Any spec file whose fixtures use them at the top level — e.g. core/main/fixtures/classes.rb doing `public :m` / `private :m` — failed to load entirely, taking the whole file's examples with it. main's singleton class now gets private/public/protected, delegating to Object (the top-level default definee): with name arguments they set the visibility of the named Object instance methods (returning the argument or array, as MRI does), and the bare form sets the default visibility of subsequent top-level defs. This reuses the same setVis logic the Module methods use, targeting Object. Verified against ruby 4.0.6; this unblocks the six previously unloadable core/main spec files (define_method, include, private, public, ruby2_keywords, using) so their examples run. Co-Authored-By: Claude Opus 4.8 --- internal/vm/modulefunc.go | 21 +++++++++++ .../vm/toplevel_visibility_internal_test.go | 37 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 internal/vm/toplevel_visibility_internal_test.go diff --git a/internal/vm/modulefunc.go b/internal/vm/modulefunc.go index 4d75a368..eb587b61 100644 --- a/internal/vm/modulefunc.go +++ b/internal/vm/modulefunc.go @@ -82,6 +82,27 @@ func (vm *VM) registerModuleExtras() { return setVis(vm, self, args, visProtected) }) + // Top-level private/public/protected: at the top level self is `main`, whose + // default definee is Object, so `private :foo` (or a bare `private`) sets the + // visibility of Object's instance methods — exactly as MRI's main object does + // through private methods on its singleton class. + if sc, ok := vm.ensureSingleton(vm.main); ok { + for _, tv := range []struct { + name string + vis visibility + }{ + {"private", visPrivate}, + {"public", visPublic}, + {"protected", visProtected}, + } { + vis := tv.vis + sc.methods[tv.name] = &Method{name: tv.name, owner: sc, + native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + return setVis(vm, vm.cObject, args, vis) + }} + } + } + // private_class_method / public_class_method: set the named class methods' // visibility — including ones inherited from Class such as `new`, recorded as a // per-receiver override (see setClassMethodVisibility). Returns self, as MRI. diff --git a/internal/vm/toplevel_visibility_internal_test.go b/internal/vm/toplevel_visibility_internal_test.go new file mode 100644 index 00000000..046c98b7 --- /dev/null +++ b/internal/vm/toplevel_visibility_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" + +// TestTopLevelVisibility covers the top-level private/public/protected methods: +// at the top level self is `main`, whose default definee is Object, so a +// name-argument form sets the visibility of the named Object methods and a bare +// form sets the default visibility of subsequent top-level defs. Verified +// against ruby 4.0.6. +func TestTopLevelVisibility(t *testing.T) { + cases := []struct{ src, want string }{ + // private :name hides the method from a public (no-receiver-style) call. + {`def a1; end; private :a1; p respond_to?(:a1)`, `false`}, + {`def a2; end; private :a2; p respond_to?(:a2, true)`, `true`}, + // public :name (re-)exposes it. + {`def b1; end; private :b1; public :b1; p respond_to?(:b1)`, `true`}, + // A bare `private` sets the default visibility of following defs. + {`private; def c1; end; p respond_to?(:c1)`, `false`}, + {`private; def d1; end; public; def d2; end; p [respond_to?(:d1), respond_to?(:d2)]`, `[false, true]`}, + // protected :name likewise hides it from a plain call. + {`def e1; end; protected :e1; p respond_to?(:e1)`, `false`}, + {`def e2; end; protected :e2; p respond_to?(:e2, true)`, `true`}, + // The name-argument form returns its argument (a single symbol). + {`def f1; end; p(private :f1)`, `:f1`}, + // An Array argument marks each and is returned. + {`def g1; end; def g2; end; p(private [:g1, :g2])`, `[:g1, :g2]`}, + } + 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") + } + } +}