Commit c61deadf

c61deadfe652Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(codegen): dynamic .new on sp_Class value (#404 Phase 3 Tier 5)

Tier 5 lands runtime constructor dispatch over a Class-typed value. Pre-Tier-5 klass.new (where klass is a local / param / ivar carrying a sp_Class) hit the unresolved-call warning and emitted 0; only the constant-receiver form (Foo.new) resolved at codegen time. The delegated_type / STI shape in real-blog needs runtime dispatch because the candidate class is chosen at runtime from a data column.

Shape:

  • Per-user-class no-arg adapter: emit static sp_RbVal sp_<C>_new_dispatch_adapter(void) that calls sp_<C>_new() and boxes the result via sp_box_obj with the unified cls_id. Emitted only when the inherited initialize takes zero positional parameters -- arg-passing adapters need uniform-ABI marshalling and stay out of scope for this tier. Value-type classes are also excluded since their constructor returns a struct, not a pointer.
  • Function-pointer table sp_class_constructors[SP_CLASS_COUNT] indexed by unified cls_id. Slots for built-ins, modules, and user classes that don't qualify point at a sp_class_new_nil_fallback returning sp_box_nil().
  • Runtime entry sp_class_new_dispatch(sp_Class c) does bounds-check + table lookup + call.
  • compile_object_method_expr's <sp_Class>.new arm fires when the receiver is not a ConstantReadNode / ConstantPathNode (those still go through the static sp_<C>_new(args) path) and the call has zero args; analyze's infer_type matches the same guard and returns "poly" so downstream method dispatch on the boxed result threads through the existing poly_method_call cls_id dispatch.

Coverage in test/dynamic_class_new.rb:

  • Three classes (Article / Page / Comment) with method bodies that differ. make(klass) calls klass.new and the result is dispatched via .kind through the cls_id table.
  • Class-typed local: k = Article; obj = k.new; obj.kind.

Out of scope:

  • Arg passing (klass.new(args)). Needs either per-class arity adapters or a uniform-ABI bridge.
  • Multi-arity dispatch / keyword args.
  • Default-arg-only constructors (def initialize(hash = {})). The emitted sp__new still lists every param in its C signature; default filling happens at the static call site via compile_call_args_with_defaults, so a no-arg adapter can't call the constructor without synthesizing the defaults itself.

Optcarrot: checksum 59662, fps 657.9 (within noise). 379 tests pass.

Refs #404 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c61deadfe652
Parent
56fdbe6b0712

spinel_analyze.rbmodifiedView file

@@ -3205,6 +3205,21 @@
32053205 return "bool"
32063206 end
32073207 end
3208+ # Issue #404 Tier 5: `<sp_Class>.new` returns a poly value
3209+ # (boxed user instance) only when the recv is a *dynamic*
3210+ # sp_Class -- a local / param / ivar carrying a class value.
3211+ # Constant-path receivers (Foo, M::Sub, etc.) still go through
3212+ # the static constructor path so `obj = Foo.new(args)` stays
3213+ # typed obj_Foo and the existing argful-construction emit
3214+ # works unchanged.
3215+ if mname == "new"
3216+ if recv >= 0 && infer_type(recv) == "class"
3217+ rty_404n = @nd_type[recv]
3218+ if rty_404n != "ConstantReadNode" && rty_404n != "ConstantPathNode"
3219+ return "poly"
3220+ end
3221+ end
3222+ end
32083223 if mname == "to_i"
32093224 return "int"
32103225 end