Skip to content

Hosted top-level defs are Object methods too: missing frame and stale file in backtraces (fixes #163) - #174

Merged
Largo merged 1 commit into
masterfrom
claude/hosted-backtraces-2
Nov 9, 2026
Merged

Largo merged 1 commit into
masterfrom
claude/hosted-backtraces-2

Conversation

@Largo

@Largo Largo commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

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 def only in main's singleton class, which is the scope's own class. That covers ScriptCode.Run(Scope) (used by CompilerTest), ScriptEngine.Execute(code, scope) and ScriptSource.Execute(scope). An instance of another class could not call such a method. In the issue's snippet, Foo#bar calls many, so:

  • Item 1: in a fresh runtime, Foo#bar raised NameError and many never ran. The backtrace was correct for that NameError, so it had no Object#many frame.
  • Item 2 (file): a later run found the Object#many that an earlier run without a scope (Engine.Execute(code)) had defined. It reported that run's (eval) file even when the code came from hosted.rb.

CRuby makes a top-level def a private method of Object, so any object can call it.

Fix

In a bound hosted scope, a top-level def now defines the method in two places:

  • On Object: with the visibility the def has (private at the top level, public for a def nested in a method), as CRuby does.
  • In main's singleton class, as before:
    • The host still reads the method from the scope (scope.GetVariable<Func<..>>("z"), RubyHosting1A).
    • The top level of each scope still calls its own method first. Two scopes that define foo still 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 def inside main.instance_eval { } still defines only a singleton method of main, as in CRuby. RubyScope.GetMethodDefinitionOwner(out atTopLevel) tells the two cases apart.

ir is unaffected. Its top-level scope is not bound, so its defs already went to Object.

What changes for hosts

This changes the hosting behaviour that RubyHosting1D asserted: Object used to have no method bar after Execute("def bar; 1; end", scope). Now Object has a private bar. What stays the same:

  • the scope variables;
  • top-level isolation between scopes;
  • everything that worked before.

Two things change:

  • Calls from inside classes that raised NameError now work.
  • When two scopes define the same name, a call from inside a class gets the one defined last. In CRuby there is only one such method anyway.

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 MethodLookupModule as before:

  • alias
  • undef
  • public / private with names
  • main.define_method

So the Object copy does not follow them. Only def mirrors to Object.

Not changed

The issue's side note is still open: an explicit def self.x in hosted top-level code is labelled Object#x, where CRuby prints x. The label is computed in four places from the declaring module alone (QualifyFrameLabel), and those places do not know whether the def had a target. It is a label issue, not one of the four numbered items, and could get its own issue.

Tests

  • New: Backtrace_HostedTopLevelMethods1. It checks:
    • the full backtrace of the issue's snippet when it runs first through CompilerTest in a fresh runtime (item 1);
    • then through Engine.Execute, then through ScriptSource("hosted.rb").Execute(scope). Each run reports its own file (item 2);
    • private/public placement on Object against CRuby 4.0.9 output (top-level, nested def, def self., instance_eval);
    • per-scope isolation at the top level, last-one-wins from inside a class, and that host delegates read each scope's own method.
  • Updated:
    • Backtrace_HostedPathlessLines1: the CompilerTest run after Engine.Execute now reports its own file for Object#many, not (eval).
    • RubyHosting1D: Object now has a private bar.
  • Without the fix, the new test and both updated tests fail.
  • Full IronRuby.Tests, Debug, net8.0: PASSED, 1121 tests.
  • On net10.0, every Backtrace* and RubyHosting* test passes (40 tests).
  • The hosted tests also pass with /debug, /interpret, /sync1 and /noadaptive.
  • I did not run ruby/spec: ir does not use bound hosted scopes, so this change does not reach it.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Top-level methods defined while executing code in a host scope are now also available as private methods on Object, while remaining accessible through their defining scope.
    • Corrected method visibility and lookup behavior across hosted scopes, nested definitions, singleton methods, and class instances.
    • Backtraces for hosted scripts now show the script filename instead of (eval) in the affected case.

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]>
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Hosted top-level method definitions now also create a private method on Object. Tests cover method behavior across scopes and update a hosted backtrace source-path expectation.

Changes: Hosted Top-Level Methods

Layer / File(s) Summary
Identify and install hosted methods
Src/Ruby/Runtime/RubyScope.cs, Src/Ruby/Runtime/RubyOps.cs
RubyScope reports whether method ownership is top-level. RubyOps.DefineMethod uses this information to install hosted top-level methods on Object and invoke the new method’s method_added callback.
Test hosted method behavior
Src/IronRuby.Tests/Runtime/BacktraceTests.cs, Src/IronRuby.Tests/Runtime/HostingTests.cs, Src/IronRuby.Tests/RubyTests.cs
Tests check hosted method availability, visibility, and behavior across scopes. The CompilerTest backtrace expectation now uses the script filename instead of (eval).

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RubyOps.DefineMethod
  participant RubyScope
  participant Object
  RubyOps.DefineMethod->>RubyScope: Get method owner and top-level status
  RubyScope-->>RubyOps.DefineMethod: Return owner and atTopLevel
  RubyOps.DefineMethod->>Object: Install hosted top-level method
  RubyOps.DefineMethod->>Object: Invoke method_added for the new method
Loading

Possibly related PRs

Suggested reviewers: claude, sholden

Merge Risk: Minimal · up to 18a87

The changed hosted-method behavior has coverage for the reported backtrace regression. No identified issue remains to block merge after normal checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main behavior change: hosted top-level def now also defines an Object method. It also names the backtrace fixes it addresses (missing frame and stale file). The summary and objec…
Linked Issues check Passed Directly linked issue #163 lists four numbered problems plus one known label difference. Status by item:

- Item 1, missing frame [#163]: Implemented. In RubyOps.DefineMethod, a top-level def in a bou…
Out of Scope Changes check Passed Changes in the reviewed diff:

- Src/Ruby/Runtime/RubyOps.cs: the hostedTopLevel branch adds the Object copy and its method_added call. This implements item 1 and the file half of item 2 [#163], so it…
Full details: Title check

Explanation

The title names the main behavior change: hosted top-level def now also defines an Object method. It also names the backtrace fixes it addresses (missing frame and stale file). The summary and objectives confirm both parts. The title is specific and matches the changeset. It is long, and it names only part of the work (the line portion was fixed in #169, and the PR does not cover alias, undef, or visibility changes), but it is not misleading.

Full details: Linked Issues check

Explanation

Directly linked issue #163 lists four numbered problems plus one known label difference. Status by item:

  • Item 1, missing frame [#163]: Implemented. In RubyOps.DefineMethod, a top-level def in a bound hosted scope (hostedTopLevel) now also defines the method on Object, with the def's instance visibility. A class instance (for example Foo#bar calling many) now resolves Object#many, so the NameError no longer occurs and the raising frame appears. The PR's new test Backtrace_HostedTopLevelMethods1 covers the fresh-runtime CompilerTest run.
  • Item 2, stale file [#163]: Implemented for the file portion. Each run's top-level def overwrites Object's copy, so a later run reports its own file and not the earlier (eval) file. The line portion was fixed by #169 per the PR description.
  • Items 3 and 4, line 0 and CLR frames below <main> [#163]: Not changed by this PR. #169 (merge e815798) is in the merge-base history, and the base test list includes Backtrace_HostedNoHostFrames1. I did not read the #169 hunks, so this relies on the merge history and the PR description.
  • Known label difference (explicit def self.x shown as Object#x) [#163]: Not addressed. The issue text presents it as a known difference left by #158, not as one of the four numbered problems. The PR defers it and states it could get its own issue. I treated it as outside the numbered requirements. Closing #163 on merge leaves this label difference unaddressed.

The change to the def-target branch is limited to non-target defs, so def self.x does not gain an Object copy.

Full details: Out of Scope Changes check

Explanation

Changes in the reviewed diff:

  • Src/Ruby/Runtime/RubyOps.cs: the hostedTopLevel branch adds the Object copy and its method_added call. This implements item 1 and the file half of item 2 [#163], so it is in scope.
  • Src/Ruby/Runtime/RubyScope.cs: a GetMethodDefinitionOwner overload that reports atTopLevel. The PR summary says the existing overload delegates to it, and RubyOps is its only caller shown. This supports the hostedTopLevel decision, so it is in scope. I could not read this file's diff (changed blobs unavailable), so this rests on the PR summary and the RubyOps call site.
  • Tests: Backtrace_HostedTopLevelMethods1 (new), Backtrace_HostedPathlessLines1 (updated to expect the script file for Object#many), RubyHosting1D (updated to expect a private bar on Object), and one registration line in RubyTests.cs. Each supports the new behavior. The RubyHosting1D change replaces an assertion that the fix contradicts, which the PR states.

The 'Known asymmetry' items (alias, undef, named public/private, main.define_method) are documented limits of the existing behavior, not new changes. No change in the reviewed diff is unrelated to #163.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8cc413b and 18a87a0.

Files selected for processing: 5

  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/BacktraceTests.cs
  • Src/IronRuby.Tests/Runtime/HostingTests.cs
  • Src/Ruby/Runtime/RubyOps.cs
  • Src/Ruby/Runtime/RubyScope.cs

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hosted code (ScriptEngine.Execute): backtrace frames missing, stale file/line, line 0, CLR frames below <main>

1 participant