Skip to content

Hosted top-level alias/undef/visibility/define_method act on Object too; CRuby labels for main's singleton methods (fixes #177) - #179

Merged
Largo merged 2 commits into
masterfrom
claude/hosted-toplevel-ops
Nov 9, 2026
Merged

Largo merged 2 commits into
masterfrom
claude/hosted-toplevel-ops

Conversation

@Largo

@Largo Largo commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

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) and ScriptSource.Execute(scope). #174 made a top-level def define the method on Object as well. The other top-level method-table operations still acted on main's singleton class only. For example, after def foo = 1; alias bar foo, calling bar from 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 Object as well as on main's singleton class:

  • alias
  • undef
  • public :x / private :x
  • main.define_method (block, Proc, Method and UnboundMethod forms)

RubyOps.GetHostedTopLevelMirror decides when this applies. It uses the same top-level test as #174 (GetMethodDefinitionOwner(out atTopLevel)). So main.instance_eval { ... } still acts only on main's singleton class, as in CRuby.

  • Errors: when neither class has the method, alias and undef raise CRuby's NameError, undefined method 'nope' for class 'Object'.
  • Singleton methods of main: a method defined with def self.x exists only in main's singleton class. CRuby refuses to alias, undef, change the visibility of, or define_method such a method onto Object: it raises NameError, or TypeError for define_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)) when foo is a hosted top-level def: method(:foo) finds the scope's copy, which is held by a singleton class. Binding that copy to Object would raise TypeError. So Object gets the same method from its own copy, as CRuby does.
  • A name the scope undefined but Object has again: suppose this scope ran undef e and a later scope defined e again. Then alias, public and private act on Object's e and 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:

  • The host still reads methods from the scope (RubyHosting1A).
  • Each scope's top level still finds its own method first (RubyHosting1D). An instance of another class finds the method in 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)

QualifyFrameLabel labelled every method in a hosted main's singleton class Object#x. That included def self.x and a def inside main.instance_eval, which CRuby labels x.

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 def is compiled:

  • The copy now records that it runs as Object's method, in the new RubyMethodInfo.BodyModule. This works the same way as the existing module-function copy.
  • It is compiled for Object, so its frames are labelled Object#x.
  • Main's singleton class no longer needs a special case in QualifyFrameLabel. Real singleton methods of main get the plain label x.
  • The two label sites that start from a RubyMethodInfo (arity callee frame, unused-block warning) and :methods coverage now use BodyModule.

A side effect matches CRuby better: super from the scope's copy now continues from Object. Before, it went through Object's own copy of the same method first, which added a duplicate Object#x frame.

Tests

  • New RubyHosting_TopLevelOps1: a hosted script whose output is compared with CRuby 4.0.9 on the same file. It covers:
    • alias, undef, public/private with names, and all three define_method forms, called from another class;
    • CRuby's NameError messages;
    • instance_eval { alias };
    • singleton methods of main;
    • scope isolation for alias and undef;
    • the host reading the methods from the scope.
  • New Backtrace_HostedSingletonLabels1: the labels of def self.x, instance_eval { def y }, a top-level def and its alias, and caller from the top level and from inside a class, plus the super chain. It runs the same code through CompilerTest, Execute(code, scope) and Execute(code). The expected output is CRuby's.
  • Full IronRuby.Tests on net8.0: PASSED, 0 failures. On net10.0, all 44 Hosting*/Backtrace* tests pass.
  • The mspec files for core/main, language/{alias,undef,def} and core/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

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

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

CodeRabbit couldn't verify your organization's usage billing state, so no usage-priced review was started. Retry after the billing service recovers.

  • Ask an admin to enable usage-based reviews

Open in CodeRabbit

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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 66 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 91fa636 and cbd01aa.

Files selected for processing: 9

  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/BacktraceTests.cs
  • Src/IronRuby.Tests/Runtime/HostingTests.cs
  • Src/Libraries/Builtins/ModuleOps.cs
  • Src/Libraries/Builtins/SingletonOps.cs
  • Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs
  • Src/Ruby/Runtime/Calls/MetaObjectBuilder.cs
  • Src/Ruby/Runtime/Calls/RubyMethodInfo.cs
  • Src/Ruby/Runtime/RubyOps.cs
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

…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]>
@Largo

Largo commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Largo

Largo commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Largo
Largo merged commit 2628ecc into master Nov 9, 2026
8 checks passed
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 top-level alias/undef/visibility/define_method still act only on main's singleton class

1 participant