Commit 9ccdbe27

9ccdbe27e8f7Merged

Ryan Seys <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified

Description

feat(class): reopen a class via class_eval/module_eval

Klass.class_eval do ... end (and its module_eval alias) reopens the class: each def and define_method(:lit) { } in the block body becomes an instance method on the class, exactly as a class Klass ... end reopen would. A method may read an existing ivar or assign a new one (which gets its own slot), and subclasses inherit the added methods.

walk_scope detects the reopen and sets the block body's child_class to the target, so the generic recursion registers the defs there and register_locals interns any ivars first assigned inside them -- reusing the existing class-body collection rather than a parallel path. Codegen emits the call as a no-op (the methods are emitted separately).

The receiver may be:

  • a plain constant (Klass),
  • a namespaced constant (M::Klass, a ConstantPathNode), resolved by short name like the rest of analyze, or
  • a bare / self. receiver inside a class body, which reopens the enclosing class. This is restricted to direct class-body statements via g_cbody_direct (analyze) / g_class_body_id (codegen), both -1 inside a method body where self is an instance, so the two passes agree on exactly which calls are reopens.

Detection (class_eval_reopen_class) is shared by analyze and codegen and restricted to blocks whose statements are purely method definitions. The purity test mirrors walk_scope's exact registration gate via dm_registerable_name: a define_method counts only when it has a literal symbol/string name AND a block. A class_eval containing any other statement -- a blockless define_method, a dynamic-name define_method, or arbitrary code -- falls through to the normal (unsupported) path and errors, rather than being silently dropped.

test/class_eval_reopen.rb covers def + symbol/string define_method, the module_eval alias, reading vs assigning an ivar, subclass inheritance, a namespaced M::Widget receiver, and bare + self. receivers inside a class body.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
9ccdbe27e8f7
Parent
d40bc31b97a3

src/analyze_internal.hmodifiedView file

@@ -56,6 +56,12 @@
5656 /* Class index of the class/module body currently being analyzed (set around
5757 ClassNode/ModuleNode body traversal). -1 outside any class body. */
5858 extern int g_cbody_class_id;
59+/* Like g_cbody_class_id, but cleared when descending into a method/block scope:
60+ the class index only while walking a class/module body's *direct* statements,
61+ where `self` is the class object. -1 inside method bodies and at top level.
62+ Mirrors codegen's g_class_body_id so a self/bare `class_eval` reopen is
63+ recognized identically by analyze and codegen. */
64+extern int g_cbody_direct;
5965
6066 /* Scan a subtree for BreakNode values and return their unified type.
6167 Stops at DefNode/BlockNode boundaries (inner blocks have their own break scope). */