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 = 0
152153 @in_loop = 0
153154 @hoisted_strlen_var = ""
@@ -10048,6 +10049,20 @@
1004810049 (pts.length > 0 && pts.last == "proc") ? 1 : 0
1004910050 end
1005010051
10052+ # Returns the name of a method's `&block` parameter (the trailing
10053+ # proc-typed slot in pnames), or "" if the method doesn't take
10054+ # one. Ruby syntax requires `&block` to be the trailing param, so
10055+ # a proc-typed slot in any other position is a positional proc
10056+ # argument. Mirrors cls_method_has_block_param's trailing-only
10057+ # check. Used at method-emit time to set @current_method_block_param
10058+ # 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.last
10062+ end
10063+ ""
10064+ end
10065+
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 = ci
1053410549 @current_method_name = mname
1053510550 @current_method_return = rt
10551+ @current_method_block_param = find_block_param_name(pnames, ptypes)
1053610552 @indent = 1
1053710553 @in_gc_scope = 0
1053810554
@@ -10595,6 +10611,7 @@
1059510611 pop_scope
1059610612 @current_class_idx = -1
1059710613 @current_method_name = ""
10614+ @current_method_block_param = ""
1059810615 @in_yield_method = 0
1059910616 @indent = 0
1060010617 emit_raw(" return " + c_return_default(rt) + ";")
@@ -10607,6 +10624,7 @@
1060710624 @current_class_idx = ci
1060810625 @current_method_name = mname
1060910626 @current_method_return = rt
10627+ @current_method_block_param = find_block_param_name(pnames, ptypes)
1061010628 @indent = 1
1061110629 @in_gc_scope = 0
1061210630
@@ -10631,6 +10649,7 @@
1063110649 pop_scope
1063210650 @current_class_idx = -1
1063310651 @current_method_name = ""
10652+ @current_method_block_param = ""
1063410653 @indent = 0
1063510654 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_scope
1078610806 @current_method_name = ""
10807+ @current_method_block_param = ""
1078710808 @in_yield_method = 0
1078810809 @indent = 0
1078910810 emit_raw(" return " + c_return_default(rt) + ";")
@@ -13373,6 +13394,15 @@
1337313394 end
1337413395 end
1337513396 if mname == "block_given?"
13397+ # &block parameter form takes priority: when the enclosing method
13398+ # declares `def m(&block)`, the block is bound to `lv_block`, not
13399+ # the implicit yield slot — so check the explicit param first.
13400+ # `body_has_yield` flags any method containing block_given? as a
13401+ # 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+ end
1337613406 if @in_yield_method == 1
1337713407 return "(_block != NULL)"
1337813408 end