Repository navigation
Hosted top-level defs are Object methods too: missing frame and stale file in backtraces (fixes #163) - #174
Conversation
Code run in a host's scope (ScriptCode.Run(Scope), which CompilerTest uses; ScriptEngine.Execute(code, scope); ScriptSource.Execute(scope)) defined a top-level `def' only in main's singleton class, the scope's. An instance of another class could not call it: in the issue's snippet Foo#bar raised NameError, so the first run of it in such a scope had no 'Object#many' frame at all. A later run found an Object#many that an earlier scope-less run had defined, and reported that run's file, "(eval)", for a run of hosted.rb. Such a `def' now also defines a method of Object, with the same visibility (private at the top level), as CRuby does. The scope keeps its own copy in main's singleton class: the host still reads it from the scope, and the top level of each scope still calls its own first, so two scopes defining the same name do not step on each other there (RubyHosting1D). An instance of another class calls Object's, which is the last one defined, as in CRuby. A `def' in main.instance_eval stays a singleton method of main only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Fixes #163. #169 fixed items 3 and 4 and the line half of item 2. This PR fixes item 1 and the file half of item 2. Both had one cause.
Cause
Code run in a host's scope defined a top-level
defonly in main's singleton class, which is the scope's own class. That coversScriptCode.Run(Scope)(used byCompilerTest),ScriptEngine.Execute(code, scope)andScriptSource.Execute(scope). An instance of another class could not call such a method. In the issue's snippet,Foo#barcallsmany, so:Foo#barraisedNameErrorandmanynever ran. The backtrace was correct for that NameError, so it had noObject#manyframe.Object#manythat an earlier run without a scope (Engine.Execute(code)) had defined. It reported that run's(eval)file even when the code came fromhosted.rb.CRuby makes a top-level
defa private method ofObject, so any object can call it.Fix
In a bound hosted scope, a top-level
defnow defines the method in two places:Object: with the visibility thedefhas (private at the top level, public for adefnested in a method), as CRuby does.scope.GetVariable<Func<..>>("z"), RubyHosting1A).foostill do not step on each other there (RubyHosting1D).An instance of another class calls
Object's method, which is the one defined last, as in CRuby.A
definsidemain.instance_eval { }still defines only a singleton method of main, as in CRuby.RubyScope.GetMethodDefinitionOwner(out atTopLevel)tells the two cases apart.iris unaffected. Its top-level scope is not bound, so itsdefs already went toObject.What changes for hosts
This changes the hosting behaviour that RubyHosting1D asserted:
Objectused to have no methodbarafterExecute("def bar; 1; end", scope). NowObjecthas a privatebar. What stays the same:Two things change:
If scope isolation should also cover calls from inside classes, a different design is needed: method lookup that falls back to the scope the call was compiled in.
Known asymmetry
Some top-level operations in a bound scope still act on main's singleton class only, because they resolve the owner through
MethodLookupModuleas before:aliasundefpublic/privatewith namesmain.define_methodSo the
Objectcopy does not follow them. Onlydefmirrors toObject.Not changed
The issue's side note is still open: an explicit
def self.xin hosted top-level code is labelledObject#x, where CRuby printsx. The label is computed in four places from the declaring module alone (QualifyFrameLabel), and those places do not know whether thedefhad a target. It is a label issue, not one of the four numbered items, and could get its own issue.Tests
Backtrace_HostedTopLevelMethods1. It checks:CompilerTestin a fresh runtime (item 1);Engine.Execute, then throughScriptSource("hosted.rb").Execute(scope). Each run reports its own file (item 2);Objectagainst CRuby 4.0.9 output (top-level, nesteddef,def self.,instance_eval);Backtrace_HostedPathlessLines1: theCompilerTestrun afterEngine.Executenow reports its own file forObject#many, not(eval).RubyHosting1D:Objectnow has a privatebar.Backtrace*andRubyHosting*test passes (40 tests)./debug,/interpret,/sync1and/noadaptive.irdoes not use bound hosted scopes, so this change does not reach it.🤖 Generated with Claude Code
Summary by CodeRabbit
Object, while remaining accessible through their defining scope.(eval)in the affected case.