…elow <main> (#163)
- A source without a path (ScriptEngine.Execute(string), an interactive line) had no
SymbolDocument, so the compiler emitted no line information for it: a method's frame
was reported at its `def' line and top-level frames (and Array#each called from them)
at line 0. It now gets a document named "(eval)", the name its frames already carried.
- The CLR frames of the host below the outermost Ruby frame (CompilerTest, RunTestCase,
Main, ...) are no longer reported in backtraces or caller; CRuby has nothing below
<main>. A CLR frame between Ruby frames is still reported, and -X:ExceptionDetail
keeps everything.
The "missing Object#many frame" under CompilerTest is not a backtrace bug: a top-level
def run in a host's scope (ScriptCode.Run(Scope)) defines a singleton method of that
scope's main object, so Foo#bar raises NameError before `many' runs; a later run then
finds the Object#many an earlier unbound Engine.Execute defined, which is the stale file.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Part of #163. Items 3 and 4 are fixed. The line half of item 2 is fixed too. Items 1 and the stale-file half of 2 are not backtrace bugs, so this PR leaves them alone (see below).
Snippet from the issue, with the backtrace each path printed before and after:
Engine.Execute(code)(eval):2:in 'Object#many'…(eval):0:in 'Array#each',(eval):0:in '<main>', then:0:in '<Probe>b__98_1',RedirectOutput…Main(eval):3:in 'Object#many'…(eval):12:in 'Array#each',(eval):12:in '<main>'and nothing below itScriptSource("hosted.rb").Execute()<main>ir hosted.rband CRuby 4.0.9 (checked by hand)CompilerTest(code):0:in 'CompilerTest'×3 …RunTestCase…Main<main>Item 3: line 0 and the def line
A source without a path, such as
ScriptEngine.Execute(string)or an interactive line, has noSourceUnit.Document. Without a document,AstGenerator.AddDebugInfoemits no sequence points, so that code had no line information at all:defline;Array#each), got line 0.RubyContext.TransformTreenow gives such a source aSymbolDocumentnamed(eval), the name its frames already carried (AstGenerator.SourcePath). A side effect:Method#source_locationof a method defined there is now["(eval)", line]instead of nil. CRuby gives["(eval at …)", line]for eval'd code.Item 4: CLR host frames below
<main>RubyStackTraceBuilderremembers where the outermost frame that is not a visible CLR frame ends.RubyTracedrops the CLR frames after it: these are the frames of the host that ran the Ruby code. This applies to exception backtraces,callerandcaller_locations. A skipped Ruby frame counts, socallerat the top level of hosted code returns[], as in CRuby, not the host's frames.-X:ExceptionDetailkeeps every frame.iris unaffected, because its own frames were never reported.Items 1 and 2: why there is no backtrace fix
Under
CompilerTestin a fresh runtime,eis not the ArgumentError. It isNameError: undefined local variable or method 'many' for an instance of Foo, raised inFoo#barbeforemanyruns. So no frame is missing.CompilerTestruns the code withScriptCode.Run(new Scope()). That bound hosted scope deliberately defines top-leveldefs as singleton methods of the scope's main object (RubyTopLevelScope.CreateHostedTopLevelScope,MainSingleton). This lets the host read them from the scope (scope.GetVariable<Func<int>>("z"), see RubyHosting1A) and keeps two scopes' methods apart. Methods on main's singleton cannot be called from an instance of another class.The stale file in item 2 comes from the same cause. A later
CompilerTestrun reopensFooand calls theObject#manythat an earlier unboundEngine.ExecuteorScriptSource.Execute()defined, because those run withScriptCode.Run(). So(eval)is the correct file for the method that actually ran. Only its line was wrong (item 3), and that is fixed.Making these methods behave as CRuby's (private methods of
Object) would change the hosting API's scope isolation. That is a design decision, not a backtrace fix, so this PR does not make it.Tests
New tests in IronRuby.Tests:
Backtrace_HostedPathlessLines1: the fullEngine.Executebacktrace, theCompilerTest-after-Engine.Executecase from item 2, andsource_locationandcaller(0)in path-less code.Backtrace_HostedNoHostFrames1: the fullhosted.rbbacktrace,caller/caller(0)/e.backtraceunderCompilerTest, and a method the host calls through a scope delegate. That method gets one frame fromcaller(0)and none fromcaller(1).Results:
/debug,/interpret,/sync1and/noadaptive.ir:send_spec"into"/"to" wording error that is already on master.🤖 Generated with Claude Code
Summary by CodeRabbit
(eval)as the source, while hosted code retains its named-file locations.