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
result703703
end704704
705+
# Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),706+
# 0 otherwise. Used by &block-forwarding call sites to decide707+
# 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 : 0711+
end712+
705713
# Flatten a constant reference into an internal name.706714
# C -> C707715
# ::C -> C@@ -10357,6 +10365,24 @@
1035710365
01035810366
end1035910367
10368+
# Returns 1 if the (ci, midx) method declares a `&block` parameter,10369+
# 0 otherwise. Ruby syntax requires `&block` to be the trailing10370+
# param, so we check only the last slot — a proc-typed slot in any10371+
# other position is a positional proc argument, not a block param.10372+
# Mirrors cls_method_has_yield: call sites use it to decide whether10373+
# to omit the trailing &block slot from default-padding.10374+
def cls_method_has_block_param(ci, midx)10375+
if ci < 0 || midx < 010376+
return 010377+
end10378+
all_ptypes = @cls_meth_ptypes[ci].split("|")10379+
if midx >= all_ptypes.length10380+
return 010381+
end10382+
pts = all_ptypes[midx].split(",")10383+
(pts.length > 0 && pts.last == "proc") ? 1 : 010384+
end10385+
1036010386
def cls_find_method_direct(ci, mname)1036110387
mnames = @cls_meth_names[ci].split(";")1036210388
j = 0@@ -10980,6 +11006,21 @@
1098011006
result1098111007
end1098211008
11009+
# Builds the trailing portion of a call-args list — each non-empty11010+
# piece prefixed with ", ", empties skipped. Returns "" when both11011+
# are empty. Mirrors build_params_str: callers concatenate the11012+
# 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 + ", " + ca11017+
end11018+
if bp != ""11019+
result = result + ", " + bp11020+
end11021+
result11022+
end11023+
1098311024
# ---- Emit top-level methods ----1098411025
def emit_toplevel_methods1098511026
i = 0@@ -13922,7 +13963,7 @@
1392213963
end1392313964
ca = ""1392413965
if owner_midx >= 013925-
ca = compile_typed_call_args(nid, owner_ci, owner_midx)13966+
ca = compile_typed_call_args(nid, owner_ci, owner_midx, 0)1392613967
else1392713968
ca = compile_call_args(nid)1392813969
end@@ -16790,24 +16831,34 @@
1679016831
if oci2 >= 01679116832
midx2 = cls_find_method_direct(oci2, mname)1679216833
end16834+
# Omit the trailing &block slot from default-padding when the16835+
# callee declares one — we'll fill it explicitly from the16836+
# call site's literal block below.16837+
has_proc = cls_method_has_block_param(oci2, midx2)1679316838
ca = ""1679416839
if midx2 >= 016795-
ca = compile_typed_call_args(nid, oci2, midx2)16840+
ca = compile_typed_call_args(nid, oci2, midx2, has_proc)1679616841
else1679716842
ca = compile_call_args(nid)1679816843
end16799-
if owner == cname16800-
if ca != ""16801-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"16844+
bp = ""16845+
if has_proc == 116846+
if has_literal_block(nid) == 116847+
@needs_proc = 116848+
bp = compile_proc_literal(nid)1680216849
else16803-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"16850+
# The callee declares &block but the call site provides16851+
# none — fill the slot with NULL so the C call has the16852+
# right arity (compile_typed_call_args has already been16853+
# told to skip default-padding for this slot).16854+
bp = "0"1680416855
end16856+
end16857+
tail = build_call_tail(ca, bp)16858+
if owner == cname16859+
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")"1680516860
else16806-
if ca != ""16807-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"16808-
else16809-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"16810-
end16861+
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + tail + ")"1681116862
end1681216863
end1681316864
end@@ -16858,7 +16909,7 @@
1685816909
end1685916910
ca2 = ""1686016911
if midx3 >= 016861-
ca2 = compile_typed_call_args(nid, oci3, midx3)16912+
ca2 = compile_typed_call_args(nid, oci3, midx3, 0)1686216913
else1686316914
ca2 = compile_call_args(nid)1686416915
end@@ -17327,7 +17378,7 @@
1732717378
if init_ci >= 01732817379
init_idx = cls_find_method_direct(init_ci, "initialize")1732917380
if init_idx >= 017330-
return compile_typed_call_args(nid, init_ci, init_idx)17381+
return compile_typed_call_args(nid, init_ci, init_idx, 0)1733117382
end1733217383
end1733317384
return ""@@ -17527,11 +17578,16 @@
1752717578
result1752817579
end1752917580
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 param1753217583
# types AND fills in defaults from @cls_meth_defaults for trailing1753317584
# parameters the caller omitted (issue #49). Returns "" only when the1753417585
# method takes no parameters at all.17586+
#17587+
# `omit_trailing` is the number of trailing param slots to leave out17588+
# entirely — block-forwarding call sites pass 1 so the &block slot17589+
# isn't default-padded with "0" (the actual proc is appended by the17590+
# 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.length1754817604
defaults = all_defaults[target_midx].split(",")1754917605
end17606+
# Drop the trailing slots the caller will fill explicitly (the17607+
# &block slot when block-forwarding) — otherwise a surplus17608+
# positional arg would mis-cast against the omitted slot's type17609+
# and the loop's default-padding would emit "0" for the slot the17610+
# caller is about to fill itself.17611+
if omit_trailing > 017612+
kept = []17613+
pk = 017614+
limit = ptypes.length - omit_trailing17615+
if limit < 017616+
limit = 017617+
end17618+
while pk < limit17619+
kept.push(ptypes[pk])17620+
pk = pk + 117621+
end17622+
ptypes = kept17623+
end1755017624
total = ptypes.length1755117625
if arg_ids.length > total1755217626
total = arg_ids.lengthtest/block_forward_recv_typed.rbaddedView file
Loading diff…