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, aConstantPathNode), 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 viag_cbody_direct(analyze) /g_class_body_id(codegen), both -1 inside a method body whereselfis 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
/* Class index of the class/module body currently being analyzed (set around ClassNode/ModuleNode body traversal). -1 outside any class body. */extern int g_cbody_class_id;/* Like g_cbody_class_id, but cleared when descending into a method/block scope: the class index only while walking a class/module body's *direct* statements, where `self` is the class object. -1 inside method bodies and at top level. Mirrors codegen's g_class_body_id so a self/bare `class_eval` reopen is recognized identically by analyze and codegen. */extern int g_cbody_direct;/* Scan a subtree for BreakNode values and return their unified type. Stops at DefNode/BlockNode boundaries (inner blocks have their own break scope). */src/analyze_scope.cmodifiedView file
Loading diff…
src/analyze_util.cmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…
test/class_eval_reopen.rbaddedView file
Loading diff…
test/class_eval_reopen.rb.expectedaddedView file
Loading diff…