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 callssp_<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 asp_class_new_nil_fallbackreturning 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>.newarm fires when the receiver is not a ConstantReadNode / ConstantPathNode (those still go through the staticsp_<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)callsklass.newand 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
return "bool" end end # Issue #404 Tier 5: `<sp_Class>.new` returns a poly value # (boxed user instance) only when the recv is a *dynamic* # sp_Class -- a local / param / ivar carrying a class value. # Constant-path receivers (Foo, M::Sub, etc.) still go through # the static constructor path so `obj = Foo.new(args)` stays # typed obj_Foo and the existing argful-construction emit # works unchanged. if mname == "new" if recv >= 0 && infer_type(recv) == "class" rty_404n = @nd_type[recv] if rty_404n != "ConstantReadNode" && rty_404n != "ConstantPathNode" return "poly" end end end if mname == "to_i" return "int" endspinel_codegen.rbmodifiedView file
Loading diff…
test/dynamic_class_new.rbaddedView file
Loading diff…
test/dynamic_class_new.rb.expectedaddedView file
Loading diff…