Commit 1f9b7c16
1f9b7c16d448Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #114 from ryanseys/rs-block-given-amp-block
feat(codegen): support block_given? in &block-only methods
Details
- Commit
- 1f9b7c16d448
- Parent
- 9f101b63bdce
- Merge parent
- 75cb71cb48e9
spinel_codegen.rbmodifiedView file
@@ -148,6 +148,7 @@
148148
@current_method_name = ""149149
@current_lexical_scope = ""150150
@current_method_return = ""151+
@current_method_block_param = ""151152
@in_main = 0152153
@in_loop = 0153154
@hoisted_strlen_var = ""@@ -10048,6 +10049,20 @@
1004810049
(pts.length > 0 && pts.last == "proc") ? 1 : 01004910050
end1005010051
10052+
# Returns the name of a method's `&block` parameter (the trailing10053+
# proc-typed slot in pnames), or "" if the method doesn't take10054+
# one. Ruby syntax requires `&block` to be the trailing param, so10055+
# a proc-typed slot in any other position is a positional proc10056+
# argument. Mirrors cls_method_has_block_param's trailing-only10057+
# check. Used at method-emit time to set @current_method_block_param10058+
# so block_given? can resolve to (lv_<name> != NULL).10059+
def find_block_param_name(pnames, ptypes)10060+
if ptypes.length > 0 && ptypes.last == "proc"10061+
return pnames.last10062+
end10063+
""10064+
end10065+
1005110066
def cls_find_method_direct(ci, mname)1005210067
mnames = @cls_meth_names[ci].split(";")1005310068
j = 0@@ -10533,6 +10548,7 @@
1053310548
@current_class_idx = ci1053410549
@current_method_name = mname1053510550
@current_method_return = rt10551+
@current_method_block_param = find_block_param_name(pnames, ptypes)1053610552
@indent = 11053710553
@in_gc_scope = 01053810554
@@ -10595,6 +10611,7 @@
1059510611
pop_scope1059610612
@current_class_idx = -11059710613
@current_method_name = ""10614+
@current_method_block_param = ""1059810615
@in_yield_method = 01059910616
@indent = 01060010617
emit_raw(" return " + c_return_default(rt) + ";")@@ -10607,6 +10624,7 @@
1060710624
@current_class_idx = ci1060810625
@current_method_name = mname1060910626
@current_method_return = rt10627+
@current_method_block_param = find_block_param_name(pnames, ptypes)1061010628
@indent = 11061110629
@in_gc_scope = 01061210630
@@ -10631,6 +10649,7 @@
1063110649
pop_scope1063210650
@current_class_idx = -11063310651
@current_method_name = ""10652+
@current_method_block_param = ""1063410653
@indent = 01063510654
emit_raw(" return " + c_return_default(rt) + ";")1063610655
emit_raw("}")@@ -10723,6 +10742,7 @@
1072310742
1072410743
pnames = @meth_param_names[mi].split(",")1072510744
ptypes = @meth_param_types[mi].split(",")10745+
@current_method_block_param = find_block_param_name(pnames, ptypes)1072610746
1072710747
yp = ""1072810748
if @meth_has_yield[mi] == 1@@ -10784,6 +10804,7 @@
1078410804
rt = @meth_return_types[mi]1078510805
pop_scope1078610806
@current_method_name = ""10807+
@current_method_block_param = ""1078710808
@in_yield_method = 01078810809
@indent = 01078910810
emit_raw(" return " + c_return_default(rt) + ";")@@ -13373,6 +13394,15 @@
1337313394
end1337413395
end1337513396
if mname == "block_given?"13397+
# &block parameter form takes priority: when the enclosing method13398+
# declares `def m(&block)`, the block is bound to `lv_block`, not13399+
# the implicit yield slot — so check the explicit param first.13400+
# `body_has_yield` flags any method containing block_given? as a13401+
# yield-method, which would otherwise route through the `_block`13402+
# slot and miss the actually-bound `&block` param.13403+
if @current_method_block_param != ""13404+
return "(lv_" + @current_method_block_param + " != NULL)"13405+
end1337613406
if @in_yield_method == 11337713407
return "(_block != NULL)"1337813408
endtest/block_given_block_param.rbaddedView file
Loading diff…