Commit df5402ab

df5402ab0b24Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: add basic proc/Proc.new support, module include ordering, nil guard

Adds proc literal compilation using GCC nested functions, Proc.new, proc.call dispatch, and partial &block parameter support. Also fixes module collection ordering (before classes) and adds nil guard in is_obj_type.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
df5402ab0b24
Parent
0a3435fcfc55

spinel_codegen.rbmodifiedView file

@@ -129,6 +129,9 @@
129129 @needs_file_io = 0
130130 @needs_mutable_str = 0
131131 @needs_rb_value = 0
132+ @needs_proc = 0
133+ @proc_counter = 0
134+ @proc_funcs = ""
132135
133136 # Poly tracking: functions with params called with different types
134137 @poly_funcs = "".split(",")
@@ -1342,6 +1345,9 @@
13421345 if rn == "Hash"
13431346 return "str_int_hash"
13441347 end
1348+ if rn == "Proc"
1349+ return "proc"
1350+ end
13451351 return "obj_" + rn
13461352 end
13471353 end
@@ -1459,6 +1465,20 @@
14591465 return mr
14601466 end
14611467
1468+ # proc / Proc.new
1469+ if mname == "proc"
1470+ return "proc"
1471+ end
1472+ if mname == "new"
1473+ if recv >= 0
1474+ if @nd_type[recv] == "ConstantReadNode"
1475+ if @nd_name[recv] == "Proc"
1476+ return "proc"
1477+ end
1478+ end
1479+ end
1480+ end
1481+
14621482 # Check open class methods for receiver type
14631483 if recv >= 0
14641484 rt = infer_type(recv)
@@ -1538,6 +1558,9 @@
15381558 end
15391559
15401560 def is_obj_type(t)
1561+ if t == nil
1562+ return 0
1563+ end
15411564 if t.length > 4
15421565 if t[0] == "o"
15431566 if t[1] == "b"
@@ -1609,6 +1632,9 @@
16091632 if t == "poly"
16101633 return "sp_RbVal"
16111634 end
1635+ if t == "proc"
1636+ return "sp_Proc"
1637+ end
16121638 if is_obj_type(t) == 1
16131639 cname = t[4, t.length - 4]
16141640 return "sp_" + cname + " *"
@@ -1641,6 +1667,9 @@
16411667 if t == "poly"
16421668 return "sp_box_nil()"
16431669 end
1670+ if t == "proc"
1671+ return "sp_proc_new(NULL)"
1672+ end
16441673 if type_is_pointer(t) == 1
16451674 return "NULL"
16461675 end
@@ -2074,6 +2103,16 @@
20742103 result = result + @nd_name[rest]
20752104 end
20762105 end
2106+ # Block parameter (&block)
2107+ blk = @nd_block[params]
2108+ if blk >= 0
2109+ if @nd_type[blk] == "BlockParameterNode"
2110+ if result != ""
2111+ result = result + ","
2112+ end
2113+ result = result + @nd_name[blk]
2114+ end
2115+ end
20772116 result
20782117 end
20792118
@@ -2132,6 +2171,16 @@
21322171 result = result + "int_array"
21332172 end
21342173 end
2174+ # Block parameter (&block)
2175+ blk = @nd_block[params]
2176+ if blk >= 0
2177+ if @nd_type[blk] == "BlockParameterNode"
2178+ if result != ""
2179+ result = result + ","
2180+ end
2181+ result = result + "proc"
2182+ end
2183+ end
21352184 result
21362185 end
21372186
@@ -2188,6 +2237,16 @@
21882237 result = result + "-1"
21892238 end
21902239 end
2240+ # Block param
2241+ blk = @nd_block[params]
2242+ if blk >= 0
2243+ if @nd_type[blk] == "BlockParameterNode"
2244+ if result != ""
2245+ result = result + ","
2246+ end
2247+ result = result + "-1"
2248+ end
2249+ end
21912250 result
21922251 end
21932252
@@ -3895,9 +3954,15 @@
38953954 if rn == "Hash"
38963955 @needs_str_int_hash = 1
38973956 end
3957+ if rn == "Proc"
3958+ @needs_proc = 1
3959+ end
38983960 end
38993961 end
39003962 end
3963+ if mname == "proc"
3964+ @needs_proc = 1
3965+ end
39013966 if mname == "to_a"
39023967 if @nd_receiver[nid] >= 0
39033968 rt = infer_type(@nd_receiver[nid])
@@ -3965,6 +4030,9 @@
39654030 end
39664031 end
39674032 end
4033+ if t == "BlockParameterNode"
4034+ @needs_proc = 1
4035+ end
39684036 # Recurse
39694037 scan_features_children(nid)
39704038 end
@@ -4371,6 +4439,9 @@
43714439 if @needs_file_io == 1
43724440 emit_file_io_runtime
43734441 end
4442+ if @needs_proc == 1
4443+ emit_proc_runtime
4444+ end
43744445 emit_class_structs
43754446 emit_forward_decls
43764447 emit_class_methods
@@ -4605,6 +4676,14 @@
46054676 emit_raw("")
46064677 end
46074678
4679+ def emit_proc_runtime
4680+ emit_raw("typedef mrb_int (*sp_proc_fn_t)(mrb_int);")
4681+ emit_raw("typedef struct { sp_proc_fn_t fn; } sp_Proc;")
4682+ emit_raw("static sp_Proc sp_proc_new(sp_proc_fn_t fn) { sp_Proc p; p.fn = fn; return p; }")
4683+ emit_raw("static mrb_int sp_proc_call(sp_Proc p, mrb_int arg) { return p.fn ? p.fn(arg) : 0; }")
4684+ emit_raw("")
4685+ end
4686+
46084687 # ---- Struct emission ----
46094688 def emit_class_structs
46104689 # Forward declare typedefs
@@ -6336,6 +6415,12 @@
63366415 if mname == "__method__"
63376416 return "\"" + @current_method_name + "\""
63386417 end
6418+ if mname == "proc"
6419+ if @nd_block[nid] >= 0
6420+ @needs_proc = 1
6421+ return compile_proc_literal(nid)
6422+ end
6423+ end
63396424 if mname == "method"
63406425 # method(:name) - record the method reference
63416426 args_id = @nd_arguments[nid]
@@ -6412,6 +6497,23 @@
64126497 if @meth_has_yield[mi] == 1
64136498 yargs = ", NULL, NULL"
64146499 end
6500+ # Check if function has a &block param and caller provides a block
6501+ ptypes = @meth_param_types[mi].split(",")
6502+ has_block_param = 0
6503+ pk = 0
6504+ while pk < ptypes.length
6505+ if ptypes[pk] == "proc"
6506+ has_block_param = 1
6507+ end
6508+ pk = pk + 1
6509+ end
6510+ if has_block_param == 1
6511+ if @nd_block[nid] >= 0
6512+ @needs_proc = 1
6513+ block_proc = compile_proc_literal(nid)
6514+ return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")"
6515+ end
6516+ end
64156517 return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"
64166518 end
64176519 # Check if we're inside an open class method: implicit self.method
@@ -6558,11 +6660,12 @@
65586660 end
65596661 end
65606662
6561- # .call on method reference
6663+ # .call on method reference or proc
65626664 if mname == "call"
65636665 if recv >= 0
65646666 if @nd_type[recv] == "LocalVariableReadNode"
65656667 rname = @nd_name[recv]
6668+ # Check method references
65666669 ri = 0
65676670 while ri < @method_ref_vars.length
65686671 if @method_ref_vars[ri] == rname
@@ -6574,6 +6677,11 @@
65746677 end
65756678 ri = ri + 1
65766679 end
6680+ # Check if it's a proc variable
6681+ vt = find_var_type(rname)
6682+ if vt == "proc"
6683+ return "sp_proc_call(lv_" + rname + ", " + compile_arg0(nid) + ")"
6684+ end
65776685 end
65786686 end
65796687 end
@@ -6698,6 +6806,12 @@
66986806 if mname == "new"
66996807 if @nd_type[recv] == "ConstantReadNode"
67006808 cname = @nd_name[recv]
6809+ if cname == "Proc"
6810+ if @nd_block[nid] >= 0
6811+ @needs_proc = 1
6812+ return compile_proc_literal(nid)
6813+ end
6814+ end
67016815 if cname == "Array"
67026816 @needs_int_array = 1
67036817 @needs_gc = 1
@@ -9123,6 +9237,34 @@
91239237 compile_stmt(nid)
91249238 end
91259239
9240+ def compile_proc_literal(nid)
9241+ blk = @nd_block[nid]
9242+ if blk < 0
9243+ return "sp_proc_new(NULL)"
9244+ end
9245+ bp = get_block_param(nid, 0)
9246+ if bp == ""
9247+ bp = "_unused"
9248+ end
9249+ # Generate a static function for the proc body
9250+ @proc_counter = @proc_counter + 1
9251+ fname = "_sp_proc_fn_" + @proc_counter.to_s
9252+ bbody = @nd_body[blk]
9253+ # Compile body as expression
9254+ bexpr = "0"
9255+ if bbody >= 0
9256+ bs = get_stmts(bbody)
9257+ if bs.length > 0
9258+ push_scope
9259+ declare_var(bp, "int")
9260+ bexpr = compile_expr(bs[bs.length - 1])
9261+ pop_scope
9262+ end
9263+ end
9264+ # Use GCC nested function to define function inline
9265+ return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { return " + bexpr + "; } sp_proc_new(" + fname + "); })"
9266+ end
9267+
91269268 def compile_bracket_assign(nid)
91279269 recv = @nd_receiver[nid]
91289270 rt = infer_type(recv)
@@ -10602,6 +10744,14 @@
1060210744 return
1060310745 end
1060410746 end
10747+ if lmname == "proc"
10748+ if return_type != "void"
10749+ @needs_proc = 1
10750+ val = compile_proc_literal(last)
10751+ emit(" return " + val + ";")
10752+ return
10753+ end
10754+ end
1060510755 compile_stmt(last)
1060610756 if return_type != "void"
1060710757 emit(" return " + c_default_val(return_type) + ";")