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 = 0130130
@needs_mutable_str = 0131131
@needs_rb_value = 0132+
@needs_proc = 0133+
@proc_counter = 0134+
@proc_funcs = ""132135
133136
# Poly tracking: functions with params called with different types134137
@poly_funcs = "".split(",")@@ -1342,6 +1345,9 @@
13421345
if rn == "Hash"13431346
return "str_int_hash"13441347
end1348+
if rn == "Proc"1349+
return "proc"1350+
end13451351
return "obj_" + rn13461352
end13471353
end@@ -1459,6 +1465,20 @@
14591465
return mr14601466
end14611467
1468+
# proc / Proc.new1469+
if mname == "proc"1470+
return "proc"1471+
end1472+
if mname == "new"1473+
if recv >= 01474+
if @nd_type[recv] == "ConstantReadNode"1475+
if @nd_name[recv] == "Proc"1476+
return "proc"1477+
end1478+
end1479+
end1480+
end1481+
14621482
# Check open class methods for receiver type14631483
if recv >= 014641484
rt = infer_type(recv)@@ -1538,6 +1558,9 @@
15381558
end15391559
15401560
def is_obj_type(t)1561+
if t == nil1562+
return 01563+
end15411564
if t.length > 415421565
if t[0] == "o"15431566
if t[1] == "b"@@ -1609,6 +1632,9 @@
16091632
if t == "poly"16101633
return "sp_RbVal"16111634
end1635+
if t == "proc"1636+
return "sp_Proc"1637+
end16121638
if is_obj_type(t) == 116131639
cname = t[4, t.length - 4]16141640
return "sp_" + cname + " *"@@ -1641,6 +1667,9 @@
16411667
if t == "poly"16421668
return "sp_box_nil()"16431669
end1670+
if t == "proc"1671+
return "sp_proc_new(NULL)"1672+
end16441673
if type_is_pointer(t) == 116451674
return "NULL"16461675
end@@ -2074,6 +2103,16 @@
20742103
result = result + @nd_name[rest]20752104
end20762105
end2106+
# Block parameter (&block)2107+
blk = @nd_block[params]2108+
if blk >= 02109+
if @nd_type[blk] == "BlockParameterNode"2110+
if result != ""2111+
result = result + ","2112+
end2113+
result = result + @nd_name[blk]2114+
end2115+
end20772116
result20782117
end20792118
@@ -2132,6 +2171,16 @@
21322171
result = result + "int_array"21332172
end21342173
end2174+
# Block parameter (&block)2175+
blk = @nd_block[params]2176+
if blk >= 02177+
if @nd_type[blk] == "BlockParameterNode"2178+
if result != ""2179+
result = result + ","2180+
end2181+
result = result + "proc"2182+
end2183+
end21352184
result21362185
end21372186
@@ -2188,6 +2237,16 @@
21882237
result = result + "-1"21892238
end21902239
end2240+
# Block param2241+
blk = @nd_block[params]2242+
if blk >= 02243+
if @nd_type[blk] == "BlockParameterNode"2244+
if result != ""2245+
result = result + ","2246+
end2247+
result = result + "-1"2248+
end2249+
end21912250
result21922251
end21932252
@@ -3895,9 +3954,15 @@
38953954
if rn == "Hash"38963955
@needs_str_int_hash = 138973956
end3957+
if rn == "Proc"3958+
@needs_proc = 13959+
end38983960
end38993961
end39003962
end3963+
if mname == "proc"3964+
@needs_proc = 13965+
end39013966
if mname == "to_a"39023967
if @nd_receiver[nid] >= 039033968
rt = infer_type(@nd_receiver[nid])@@ -3965,6 +4030,9 @@
39654030
end39664031
end39674032
end4033+
if t == "BlockParameterNode"4034+
@needs_proc = 14035+
end39684036
# Recurse39694037
scan_features_children(nid)39704038
end@@ -4371,6 +4439,9 @@
43714439
if @needs_file_io == 143724440
emit_file_io_runtime43734441
end4442+
if @needs_proc == 14443+
emit_proc_runtime4444+
end43744445
emit_class_structs43754446
emit_forward_decls43764447
emit_class_methods@@ -4605,6 +4676,14 @@
46054676
emit_raw("")46064677
end46074678
4679+
def emit_proc_runtime4680+
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+
end4686+
46084687
# ---- Struct emission ----46094688
def emit_class_structs46104689
# Forward declare typedefs@@ -6336,6 +6415,12 @@
63366415
if mname == "__method__"63376416
return "\"" + @current_method_name + "\""63386417
end6418+
if mname == "proc"6419+
if @nd_block[nid] >= 06420+
@needs_proc = 16421+
return compile_proc_literal(nid)6422+
end6423+
end63396424
if mname == "method"63406425
# method(:name) - record the method reference63416426
args_id = @nd_arguments[nid]@@ -6412,6 +6497,23 @@
64126497
if @meth_has_yield[mi] == 164136498
yargs = ", NULL, NULL"64146499
end6500+
# Check if function has a &block param and caller provides a block6501+
ptypes = @meth_param_types[mi].split(",")6502+
has_block_param = 06503+
pk = 06504+
while pk < ptypes.length6505+
if ptypes[pk] == "proc"6506+
has_block_param = 16507+
end6508+
pk = pk + 16509+
end6510+
if has_block_param == 16511+
if @nd_block[nid] >= 06512+
@needs_proc = 16513+
block_proc = compile_proc_literal(nid)6514+
return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")"6515+
end6516+
end64156517
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"64166518
end64176519
# Check if we're inside an open class method: implicit self.method@@ -6558,11 +6660,12 @@
65586660
end65596661
end65606662
6561-
# .call on method reference6663+
# .call on method reference or proc65626664
if mname == "call"65636665
if recv >= 065646666
if @nd_type[recv] == "LocalVariableReadNode"65656667
rname = @nd_name[recv]6668+
# Check method references65666669
ri = 065676670
while ri < @method_ref_vars.length65686671
if @method_ref_vars[ri] == rname@@ -6574,6 +6677,11 @@
65746677
end65756678
ri = ri + 165766679
end6680+
# Check if it's a proc variable6681+
vt = find_var_type(rname)6682+
if vt == "proc"6683+
return "sp_proc_call(lv_" + rname + ", " + compile_arg0(nid) + ")"6684+
end65776685
end65786686
end65796687
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] >= 06811+
@needs_proc = 16812+
return compile_proc_literal(nid)6813+
end6814+
end67016815
if cname == "Array"67026816
@needs_int_array = 167036817
@needs_gc = 1@@ -9123,6 +9237,34 @@
91239237
compile_stmt(nid)91249238
end91259239
9240+
def compile_proc_literal(nid)9241+
blk = @nd_block[nid]9242+
if blk < 09243+
return "sp_proc_new(NULL)"9244+
end9245+
bp = get_block_param(nid, 0)9246+
if bp == ""9247+
bp = "_unused"9248+
end9249+
# Generate a static function for the proc body9250+
@proc_counter = @proc_counter + 19251+
fname = "_sp_proc_fn_" + @proc_counter.to_s9252+
bbody = @nd_body[blk]9253+
# Compile body as expression9254+
bexpr = "0"9255+
if bbody >= 09256+
bs = get_stmts(bbody)9257+
if bs.length > 09258+
push_scope9259+
declare_var(bp, "int")9260+
bexpr = compile_expr(bs[bs.length - 1])9261+
pop_scope9262+
end9263+
end9264+
# Use GCC nested function to define function inline9265+
return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { return " + bexpr + "; } sp_proc_new(" + fname + "); })"9266+
end9267+
91269268
def compile_bracket_assign(nid)91279269
recv = @nd_receiver[nid]91289270
rt = infer_type(recv)@@ -10602,6 +10744,14 @@
1060210744
return1060310745
end1060410746
end10747+
if lmname == "proc"10748+
if return_type != "void"10749+
@needs_proc = 110750+
val = compile_proc_literal(last)10751+
emit(" return " + val + ";")10752+
return10753+
end10754+
end1060510755
compile_stmt(last)1060610756
if return_type != "void"1060710757
emit(" return " + c_default_val(return_type) + ";")