Bug #22331
openRuby::Box: a box's top self lacks the top-level definition methods
Description
Under RUBY_BOX=1, loading a file into a box fails on private, public, include, using, define_method and ruby2_keywords.
$ RUBY_BOX=1 ruby -W:no-experimental -e 'Ruby::Box.new.load(File.expand_path("toplevel.rb"))'
toplevel.rb:1:in '<top (required)>': undefined method 'private' for #<Object:0x000000012421c180> (NoMethodError)
$ RUBY_BOX=1 ruby -W:no-experimental -e 'load(File.expand_path("toplevel.rb"), true)'
toplevel.rb:1:in '<top (required)>': undefined method 'private' for #<Object:0x00000001204bc180> (NoMethodError)
Without a box the same wrapped load only warns. Asking the loaded file what it has shows the difference:
$ ruby -e 'load(File.expand_path("show.rb"), true)'
[:define_method, :include, :private, :public, :ruby2_keywords, :using]
$ RUBY_BOX=1 ruby -W:no-experimental -e 'Ruby::Box.new.load(File.expand_path("show.rb"))'
[]
The six methods are defined during setup on the singleton class of rb_vm_top_self() (eval.c:2311, vm_method.c:3737, proc.c:5477), before the root box exists, so all six land on the master box's top self. box_entry_initialize() (box.c:170) then gives every other box a fresh object carrying only to_s and inspect, and load_wrapping() (load.c:848) clones the current box's one. Top level still sees the master box's object, so the gap shows only from a file loaded into a box.
So the question is what a box's top self should be. Three shapes I can see:
- give every box a clone of the master box's top self, and let root and main share the master's object itself (https://github.com/ruby/ruby/pull/18707);
- define the six methods on each box's own top self when the box is created, the way https://github.com/ruby/ruby/pull/18713 loads the prelude per box;
- let every box share the master box's top self, making
mainone object process-wide.
They differ in whether a singleton method added to main in one box is visible in another. Is there another shape you would prefer?
The same identity gap also breaks four autoload specs under a box.
Reproduced on master 3f6143715a and on released 4.0.7.
Related to #22275
Updated by hsbt (Hiroshi SHIBATA) 3 days ago
- Related to Misc #22275: Ruby::Box support plan for RubyGems and Bundler added