Repository navigation
Hosted top-level alias/undef/visibility/define_method act on Object too; CRuby labels for main's singleton methods (fixes #177) - #179
Conversation
…oo; label singleton methods of main as CRuby does (#177) Code run in a host's scope (ScriptCode.Run(Scope), ScriptEngine.Execute(code, scope), ScriptSource.Execute(scope)) keeps its top-level methods in main's singleton class, the scope's. #174 made a top-level `def' define the method on Object as well. Top-level `alias', `undef', `public :x'/`private :x' and main.define_method still acted on main's singleton class alone, so an instance of another class did not see what they did. They now act on Object too (RubyOps.GetHostedTopLevelMirror), with CRuby's NameError when neither has the method. A singleton method of main (`def self.x'), which CRuby refuses to alias, undef or define_method onto Object, is handled in main's singleton class alone, as before. The scope's copy of a top-level `def' now runs as Object's method (RubyMethodInfo.BodyModule): it is compiled for Object, so its frames are "Object#x" and its `super' goes on from Object rather than through Object's own copy first. QualifyFrameLabel no longer labels everything in a hosted main's singleton class "Object#x", so `def self.x' and a `def' in main.instance_eval are labelled "x", as in CRuby. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Warning Review limit reachedCodeRabbit couldn't verify your organization's usage billing state, so no usage-priced review was started. Retry after the billing service recovers.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Next included review available in 22 minutes. View limit details
Finishing Touches
|
…scope undef'ed its own Found in self-review of #179. Scope A runs `def e = 1; undef e`, then scope B runs `def e = 2`. After that, `alias f e` or `public :e` in scope A aliased the method on Object. It then raised NameError for main's singleton class, which has `e` undefined. CRuby has B's `e` and aliases it. Now main's singleton class is skipped for a name it does not have. Visibility changes are applied name by name, as CRuby applies them. When neither class has a name, the NameError names Object, as CRuby's does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@coderabbitai review |
Action not completed
|
|
@coderabbitai review |
Action not completed
|
Fixes #177. Follows #174.
Background
Code run in a host's scope keeps its top-level methods in main's singleton class, which is the scope's own class. This covers
ScriptCode.Run(Scope),ScriptEngine.Execute(code, scope)andScriptSource.Execute(scope). #174 made a top-leveldefdefine the method onObjectas well. The other top-level method-table operations still acted on main's singleton class only. For example, afterdef foo = 1; alias bar foo, callingbarfrom inside a class raised NameError.Fix 1: alias, undef, public/private and define_method also act on Object
In a bound hosted scope, these top-level operations now act on
Objectas well as on main's singleton class:aliasundefpublic :x/private :xmain.define_method(block, Proc, Method and UnboundMethod forms)RubyOps.GetHostedTopLevelMirrordecides when this applies. It uses the same top-level test as #174 (GetMethodDefinitionOwner(out atTopLevel)). Somain.instance_eval { ... }still acts only on main's singleton class, as in CRuby.aliasandundefraise CRuby's NameError,undefined method 'nope' for class 'Object'.def self.xexists only in main's singleton class. CRuby refuses to alias, undef, change the visibility of, ordefine_methodsuch a method ontoObject: it raises NameError, or TypeError fordefine_method. In a hosted scope, these operations still work on main's singleton class only, as they did before. This keeps existing hosts working. CRuby has no per-scope singleton anyway.define_method(:x, method(:foo))whenfoois a hosted top-level def:method(:foo)finds the scope's copy, which is held by a singleton class. Binding that copy toObjectwould raise TypeError. SoObjectgets the same method from its own copy, as CRuby does.undef eand a later scope definedeagain. Thenalias,publicandprivateact on Object'seand skip main's singleton class, instead of raising NameError there. This was found in self-review and fixed in the second commit.alias_method/undef_method: neither exists at the top level in CRuby 4 (undefined method 'alias_method' for main), and neither exists in IronRuby. There is nothing to change.Unchanged guarantees:
Object, which is whichever scope defined it last. In CRuby there is only one such method.Fix 2: backtrace labels (the issue's second point)
QualifyFrameLabellabelled every method in a hosted main's singleton classObject#x. That includeddef self.xand adefinsidemain.instance_eval, which CRuby labelsx.The four places that compute the label only know the declaring module. Instead of passing them more information, this PR changes how the scope's copy of a hosted top-level
defis compiled:Object's method, in the newRubyMethodInfo.BodyModule. This works the same way as the existing module-function copy.Object, so its frames are labelledObject#x.QualifyFrameLabel. Real singleton methods of main get the plain labelx.RubyMethodInfo(arity callee frame, unused-block warning) and:methodscoverage now useBodyModule.A side effect matches CRuby better:
superfrom the scope's copy now continues fromObject. Before, it went throughObject's own copy of the same method first, which added a duplicateObject#xframe.Tests
RubyHosting_TopLevelOps1: a hosted script whose output is compared with CRuby 4.0.9 on the same file. It covers:alias,undef,public/privatewith names, and all threedefine_methodforms, called from another class;instance_eval { alias };aliasandundef;Backtrace_HostedSingletonLabels1: the labels ofdef self.x,instance_eval { def y }, a top-level def and its alias, andcallerfrom the top level and from inside a class, plus thesuperchain. It runs the same code throughCompilerTest,Execute(code, scope)andExecute(code). The expected output is CRuby's.Hosting*/Backtrace*tests pass.core/main,language/{alias,undef,def}andcore/module/{define_method,private,public}: 265 examples, 0 failures, 1 error. The error is the existing "convert ... into String" message wording in define_method_spec.🤖 Generated with Claude Code