Commit 9f6587ba

9f6587ba9e30Merged

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

Description

Merge pull request #252 from mame/pr/poly-dispatch-arity-truncate

fix(codegen): truncate extra args in poly-dispatch arms

Details

Commit
9f6587ba9e30
Parent
9694d0f25c04
Merge parent
bc14d9772da5

spinel_codegen.rbmodifiedView file

@@ -22515,25 +22515,30 @@
2251522515 cname = @cls_names[i]
2251622516 midx = cls_find_method_direct(i, mname)
2251722517 if midx >= 0
22518- # Pad with default-typed zeros when target method has more
22519- # params than the call site supplied — same shape that C
22520- # demands at every fixed-arity call. Per-arm padding lets us
22521- # poly-dispatch `recv.m()` even when one of the candidate
22522- # classes has a method that takes positional defaults.
22523- arm_arg_strs = arg_strs
22518+ # Match each arm to the target method's *fixed* C arity:
22519+ # pad missing trailing slots with default-typed zeros, and
22520+ # truncate extras (when the call site supplies more args than
22521+ # this candidate accepts — happens when several classes share
22522+ # the method name but disagree on parameter count).
22523+ arm_arg_strs = ""
2252422524 all_ptypes = @cls_meth_ptypes[i].split("|")
22525+ arm_ptypes = "".split(",")
2252522526 if midx < all_ptypes.length
2252622527 arm_ptypes = all_ptypes[midx].split(",")
22527- pk = arg_compiled.length
22528- while pk < arm_ptypes.length
22528+ end
22529+ pk = 0
22530+ while pk < arm_ptypes.length
22531+ if pk < arg_compiled.length
22532+ arm_arg_strs = arm_arg_strs + ", " + arg_compiled[pk]
22533+ else
2252922534 pad = "0"
2253022535 pt_base = base_type(arm_ptypes[pk])
2253122536 if pt_base == "poly"
2253222537 pad = "sp_box_nil()"
2253322538 end
2253422539 arm_arg_strs = arm_arg_strs + ", " + pad
22535- pk = pk + 1
2253622540 end
22541+ pk = pk + 1
2253722542 end
2253822543 call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arm_arg_strs + ")"
2253922544 rhs = call_expr