Commit 2dea1fbc

2dea1fbc799fMerged

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

Description

Merge pull request #108 from ryanseys/rs-block-forward-recv-typed

fix(codegen): forward &block at typed-receiver call sites

Details

Commit
2dea1fbc799f
Parent
000c15df2ed2
Merge parent
848774d4ced4

spinel_codegen.rbmodifiedView file

@@ -702,6 +702,14 @@
702702 result
703703 end
704704
705+ # Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),
706+ # 0 otherwise. Used by &block-forwarding call sites to decide
707+ # whether to emit a proc literal for the trailing block slot.
708+ def has_literal_block(nid)
709+ blk = @nd_block[nid]
710+ (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0
711+ end
712+
705713 # Flatten a constant reference into an internal name.
706714 # C -> C
707715 # ::C -> C
@@ -10357,6 +10365,24 @@
1035710365 0
1035810366 end
1035910367
10368+ # Returns 1 if the (ci, midx) method declares a `&block` parameter,
10369+ # 0 otherwise. Ruby syntax requires `&block` to be the trailing
10370+ # param, so we check only the last slot — a proc-typed slot in any
10371+ # other position is a positional proc argument, not a block param.
10372+ # Mirrors cls_method_has_yield: call sites use it to decide whether
10373+ # to omit the trailing &block slot from default-padding.
10374+ def cls_method_has_block_param(ci, midx)
10375+ if ci < 0 || midx < 0
10376+ return 0
10377+ end
10378+ all_ptypes = @cls_meth_ptypes[ci].split("|")
10379+ if midx >= all_ptypes.length
10380+ return 0
10381+ end
10382+ pts = all_ptypes[midx].split(",")
10383+ (pts.length > 0 && pts.last == "proc") ? 1 : 0
10384+ end
10385+
1036010386 def cls_find_method_direct(ci, mname)
1036110387 mnames = @cls_meth_names[ci].split(";")
1036210388 j = 0
@@ -10980,6 +11006,21 @@
1098011006 result
1098111007 end
1098211008
11009+ # Builds the trailing portion of a call-args list — each non-empty
11010+ # piece prefixed with ", ", empties skipped. Returns "" when both
11011+ # are empty. Mirrors build_params_str: callers concatenate the
11012+ # result onto a self/recv prefix to form the full arg list.
11013+ def build_call_tail(ca, bp)
11014+ result = ""
11015+ if ca != ""
11016+ result = result + ", " + ca
11017+ end
11018+ if bp != ""
11019+ result = result + ", " + bp
11020+ end
11021+ result
11022+ end
11023+
1098311024 # ---- Emit top-level methods ----
1098411025 def emit_toplevel_methods
1098511026 i = 0
@@ -13922,7 +13963,7 @@
1392213963 end
1392313964 ca = ""
1392413965 if owner_midx >= 0
13925- ca = compile_typed_call_args(nid, owner_ci, owner_midx)
13966+ ca = compile_typed_call_args(nid, owner_ci, owner_midx, 0)
1392613967 else
1392713968 ca = compile_call_args(nid)
1392813969 end
@@ -16790,24 +16831,34 @@
1679016831 if oci2 >= 0
1679116832 midx2 = cls_find_method_direct(oci2, mname)
1679216833 end
16834+ # Omit the trailing &block slot from default-padding when the
16835+ # callee declares one — we'll fill it explicitly from the
16836+ # call site's literal block below.
16837+ has_proc = cls_method_has_block_param(oci2, midx2)
1679316838 ca = ""
1679416839 if midx2 >= 0
16795- ca = compile_typed_call_args(nid, oci2, midx2)
16840+ ca = compile_typed_call_args(nid, oci2, midx2, has_proc)
1679616841 else
1679716842 ca = compile_call_args(nid)
1679816843 end
16799- if owner == cname
16800- if ca != ""
16801- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"
16844+ bp = ""
16845+ if has_proc == 1
16846+ if has_literal_block(nid) == 1
16847+ @needs_proc = 1
16848+ bp = compile_proc_literal(nid)
1680216849 else
16803- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"
16850+ # The callee declares &block but the call site provides
16851+ # none — fill the slot with NULL so the C call has the
16852+ # right arity (compile_typed_call_args has already been
16853+ # told to skip default-padding for this slot).
16854+ bp = "0"
1680416855 end
16856+ end
16857+ tail = build_call_tail(ca, bp)
16858+ if owner == cname
16859+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")"
1680516860 else
16806- if ca != ""
16807- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"
16808- else
16809- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"
16810- end
16861+ return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + tail + ")"
1681116862 end
1681216863 end
1681316864 end
@@ -16858,7 +16909,7 @@
1685816909 end
1685916910 ca2 = ""
1686016911 if midx3 >= 0
16861- ca2 = compile_typed_call_args(nid, oci3, midx3)
16912+ ca2 = compile_typed_call_args(nid, oci3, midx3, 0)
1686216913 else
1686316914 ca2 = compile_call_args(nid)
1686416915 end
@@ -17327,7 +17378,7 @@
1732717378 if init_ci >= 0
1732817379 init_idx = cls_find_method_direct(init_ci, "initialize")
1732917380 if init_idx >= 0
17330- return compile_typed_call_args(nid, init_ci, init_idx)
17381+ return compile_typed_call_args(nid, init_ci, init_idx, 0)
1733117382 end
1733217383 end
1733317384 return ""
@@ -17527,11 +17578,16 @@
1752717578 result
1752817579 end
1752917580
17530- def compile_typed_call_args(nid, target_ci, target_midx)
17581+ def compile_typed_call_args(nid, target_ci, target_midx, omit_trailing)
1753117582 # Like compile_call_args but casts arguments to match target method param
1753217583 # types AND fills in defaults from @cls_meth_defaults for trailing
1753317584 # parameters the caller omitted (issue #49). Returns "" only when the
1753417585 # method takes no parameters at all.
17586+ #
17587+ # `omit_trailing` is the number of trailing param slots to leave out
17588+ # entirely — block-forwarding call sites pass 1 so the &block slot
17589+ # isn't default-padded with "0" (the actual proc is appended by the
17590+ # caller after this returns).
1753517591 args_id = @nd_arguments[nid]
1753617592 arg_ids = []
1753717593 if args_id >= 0
@@ -17547,6 +17603,24 @@
1754717603 if target_midx < all_defaults.length
1754817604 defaults = all_defaults[target_midx].split(",")
1754917605 end
17606+ # Drop the trailing slots the caller will fill explicitly (the
17607+ # &block slot when block-forwarding) — otherwise a surplus
17608+ # positional arg would mis-cast against the omitted slot's type
17609+ # and the loop's default-padding would emit "0" for the slot the
17610+ # caller is about to fill itself.
17611+ if omit_trailing > 0
17612+ kept = []
17613+ pk = 0
17614+ limit = ptypes.length - omit_trailing
17615+ if limit < 0
17616+ limit = 0
17617+ end
17618+ while pk < limit
17619+ kept.push(ptypes[pk])
17620+ pk = pk + 1
17621+ end
17622+ ptypes = kept
17623+ end
1755017624 total = ptypes.length
1755117625 if arg_ids.length > total
1755217626 total = arg_ids.length