Commit d5773c1d
d5773c1d778cMerged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support AliasMethodNode (alias greet hello)
Compile-time method-name aliasing inside a class body. Spinel's methods are static C functions; we register a duplicate entry in @cls_meth_* under the new name pointing to the SAME body_id, so dispatch on `.greet` lands on the same C function as `.hello`.
This matches CRuby's snapshot semantics: copying body_id (a number, not a reference) means a later `def hello` redefinition would assign a new body_id to the original slot, but the alias slot retains the snapshot. The current commit doesn't exercise late redef, but the implementation supports it correctly.
Three pieces:
-
Parser case in spinel_parse.c -- emits new_name and old_name as ref children (both SymbolNode literals).
-
symbol_node_literal helper -- extracts the name from a SymbolNode. Returns "" for InterpolatedSymbolNode (out of scope).
-
collect_class_method_alias(ci, new, old) helper + a third class-body pass in collect_class_with_prefix that runs after includes (so module-method aliases work). Walks @cls_meth_params/ptypes/returns/bodies/defaults split-strings to clone the source slot into a new entry.
compile_stmt arm is a no-op (the alias took effect at collect time).
Out of scope (each documented inline):
- alias of an interpolated symbol (`alias :"#{x}" :"#{y}"`)
- alias inside a method body (the class scan happens once at collect time)
Test exercises (test/alias_method.rb):
- `alias greet hello` -- both names dispatch to same C function.
- Two aliases of the same source: `alias get value; alias read value`.
Tests: 276 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d5773c1d778c
- Parent
- 002de75cb713
spinel_codegen.rbmodifiedView file
end } # Third pass: handle alias / undef inside the class body. Must # run AFTER all own methods + included module methods are # registered, so the alias source can be located. body_stmts.each { |sid| if @nd_type[sid] == "AliasMethodNode" nn = symbol_node_literal(@nd_new_name[sid]) on = symbol_node_literal(@nd_old_name[sid]) if nn != "" && on != "" collect_class_method_alias(ci, nn, on) end end } # Collect ivars. Pin the lexical scope to this class so any # `@x = Foo.new(...)` inside its methods resolves `Foo` against # the same scope chain the call site sees — without this, @current_class_idx = saved_idx end # Extract the literal name from a SymbolNode argument. Returns "" # for InterpolatedSymbolNode and other shapes Spinel doesn't # support as alias source/target at compile time. def symbol_node_literal(nid) if nid < 0 return "" end if @nd_type[nid] == "SymbolNode" v = @nd_content[nid] if v == "" v = @nd_name[nid] end return v end "" end # `alias new old` -- copy the existing class method's slot to a # new name in @cls_meth_*. CRuby snapshots the method body at # alias time; copying the body_id (a number, not a reference) # gives the same snapshot semantics: a later `def old` redefinition # would assign a new body_id to the old slot but the alias slot # keeps the original. def collect_class_method_alias(ci, new_name, old_name) src = cls_find_method_direct(ci, old_name) if src < 0 $stderr.puts "Spinel: alias `" + new_name + "` -> `" + old_name + "`: source method not found in class " + @cls_names[ci] exit(1) end pnames_all = @cls_meth_params[ci].split("|") ptypes_all = @cls_meth_ptypes[ci].split("|") rets_all = @cls_meth_returns[ci].split(";") bodies_all = @cls_meth_bodies[ci].split(";") defs_all = @cls_meth_defaults[ci].split("|") params = pnames_all[src] || "" ptypes = ptypes_all[src] || "" ret = rets_all[src] || "int" body_id = bodies_all[src].to_i defs = defs_all[src] || "" append_cls_meth(ci, new_name, params, ptypes, ret, body_id, defs) if @cls_meth_has_yield[ci] != "" @cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";0" end end def collect_module_methods_into_class(ci, mod_name) # Find the module and add its methods to the class mi = 0 # rewrite. Nothing to emit at runtime. return end if t == "AliasMethodNode" # `alias new old` -- compile-time only. The class-collect pass # already registered a duplicate method-table entry under the # new name pointing to the same body_id; subsequent dispatch # finds it like any other method. return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/alias_method.rbaddedView file
Loading diff…
test/alias_method.rb.expectedaddedView file
Loading diff…