Commit 347d13c0
347d13c0cef4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: pass bm_features, bm_introspect, bm_time, bm_system, bm_kwargs
- Add method support returning current method name string
- Add freeze/frozen? support (AOT treats all as frozen)
- Add Integer methods: even?, odd?, zero?, gcd, clamp, chr, to_i (identity)
- Add positive?/negative? predicates
- Add is_a? with class hierarchy checking
- Add respond_to? with method/attr_reader lookup
- Add nil? proper handling for nil, poly, pointer, and value types
- Add Time.now as time(NULL), include time.h
- Fix system() to flush stdout before exec
- Fix splat param detection in detect_poly_params
- Add Float::INFINITY/NAN constants
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 347d13c0cef4
- Parent
- a3b55d4c1128
spinel_codegen.rbmodifiedView file
@@ -1003,6 +1003,42 @@
10031003
if mname == "end_with?"10041004
return "bool"10051005
end1006+
if mname == "even?"1007+
return "bool"1008+
end1009+
if mname == "odd?"1010+
return "bool"1011+
end1012+
if mname == "zero?"1013+
return "bool"1014+
end1015+
if mname == "frozen?"1016+
return "bool"1017+
end1018+
if mname == "is_a?"1019+
return "bool"1020+
end1021+
if mname == "respond_to?"1022+
return "bool"1023+
end1024+
if mname == "chr"1025+
return "string"1026+
end1027+
if mname == "gcd"1028+
return "int"1029+
end1030+
if mname == "clamp"1031+
return "int"1032+
end1033+
if mname == "__method__"1034+
return "string"1035+
end1036+
if mname == "positive?"1037+
return "bool"1038+
end1039+
if mname == "negative?"1040+
return "bool"1041+
end10061042
if mname == "empty?"10071043
return "bool"10081044
end@@ -1297,6 +1333,19 @@
12971333
"int"12981334
end12991335
1336+
def is_class_or_ancestor(cname, target)1337+
if cname == target1338+
return 11339+
end1340+
ci = find_class_idx(cname)1341+
if ci >= 01342+
if @cls_parents[ci] != ""1343+
return is_class_or_ancestor(@cls_parents[ci], target)1344+
end1345+
end1346+
01347+
end1348+
13001349
def is_operator_name(name)13011350
if name == "+"13021351
return 1@@ -3440,6 +3489,11 @@
34403489
at = infer_type(arg_ids[k])34413490
if k < ptypes.length34423491
ct = ptypes[k]3492+
# Skip rest/splat params (int_array) - they handle multiple args3493+
if ct == "int_array"3494+
k = k + 13495+
next3496+
end34433497
if ct != at34443498
if ct != "poly"34453499
# Only mark as poly if both types are meaningful@@ -3668,6 +3722,7 @@
36683722
emit_raw("#include <stdint.h>")36693723
emit_raw("#include <ctype.h>")36703724
emit_raw("#include <stdarg.h>")3725+
emit_raw("#include <time.h>")36713726
emit_raw("")36723727
emit_raw("typedef int64_t mrb_int;")36733728
emit_raw("typedef double mrb_float;")@@ -3690,6 +3745,9 @@
36903745
emit_raw(" return r;")36913746
emit_raw("}")36923747
emit_raw("")3748+
emit_raw("static mrb_int sp_gcd(mrb_int a,mrb_int b){if(a<0)a=-a;if(b<0)b=-b;while(b){mrb_int t=b;b=a%b;a=t;}return a;}")3749+
emit_raw("static mrb_int sp_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}")3750+
emit_raw("static const char*sp_int_chr(mrb_int n){char*s=(char*)malloc(2);s[0]=(char)n;s[1]=0;return s;}")36933751
emit_raw("typedef struct{mrb_int first;mrb_int last;}sp_Range;")36943752
emit_raw("static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}")36953753
emit_raw("static int sp_last_status = 0;")@@ -5363,6 +5421,9 @@
53635421
end53645422
return "0"53655423
end5424+
if mname == "__method__"5425+
return "\"" + @current_method_name + "\""5426+
end53665427
mi = find_method_idx(mname)53675428
if mi >= 053685429
yargs = ""@@ -5603,6 +5664,9 @@
56035664
if mname == "freeze"56045665
return rc56055666
end5667+
if mname == "frozen?"5668+
return "TRUE"5669+
end56065670
if mname == "sub"56075671
args_id = @nd_arguments[nid]56085672
arg1 = "\"\""@@ -5715,12 +5779,43 @@
57155779
@needs_string_helpers = 157165780
return "sp_int_to_s(" + rc + ")"57175781
end5782+
if mname == "to_i"5783+
return rc5784+
end57185785
if mname == "to_f"57195786
return "(mrb_float)(" + rc + ")"57205787
end57215788
if mname == "abs"57225789
return "((" + rc + ") < 0 ? -(" + rc + ") : (" + rc + "))"57235790
end5791+
if mname == "even?"5792+
return "((" + rc + ") % 2 == 0)"5793+
end5794+
if mname == "odd?"5795+
return "((" + rc + ") % 2 != 0)"5796+
end5797+
if mname == "zero?"5798+
return "((" + rc + ") == 0)"5799+
end5800+
if mname == "gcd"5801+
return "sp_gcd(" + rc + ", " + compile_arg0(nid) + ")"5802+
end5803+
if mname == "clamp"5804+
args_id = @nd_arguments[nid]5805+
if args_id >= 05806+
a = get_args(args_id)5807+
if a.length >= 25808+
return "sp_clamp(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"5809+
end5810+
end5811+
end5812+
if mname == "frozen?"5813+
return "TRUE"5814+
end5815+
if mname == "chr"5816+
@needs_string_helpers = 15817+
return "sp_int_chr(" + rc + ")"5818+
end57245819
end57255820
57265821
if recv_type == "float"@@ -5882,6 +5977,12 @@
58825977
return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"58835978
end58845979
end5980+
# Time5981+
if rcname == "Time"5982+
if mname == "now"5983+
return "((mrb_int)time(NULL))"5984+
end5985+
end58855986
# ENV58865987
if rcname == "ENV"58875988
if mname == "[]"@@ -5925,6 +6026,87 @@
59256026
return compile_poly_method_call(nid, rc, mname)59266027
end59276028
6029+
# is_a? - check class hierarchy6030+
if mname == "is_a?"6031+
if is_obj_type(recv_type) == 16032+
cname = recv_type[4, recv_type.length - 4]6033+
arg0 = ""6034+
args_id = @nd_arguments[nid]6035+
if args_id >= 06036+
a = get_args(args_id)6037+
if a.length > 06038+
arg0 = @nd_name[a[0]]6039+
end6040+
end6041+
# Check if cname is or inherits from arg06042+
if is_class_or_ancestor(cname, arg0) == 16043+
return "TRUE"6044+
else6045+
return "FALSE"6046+
end6047+
end6048+
return "FALSE"6049+
end6050+
6051+
# respond_to? - check if method exists6052+
if mname == "respond_to?"6053+
if is_obj_type(recv_type) == 16054+
cname = recv_type[4, recv_type.length - 4]6055+
ci = find_class_idx(cname)6056+
if ci >= 06057+
arg0 = ""6058+
args_id = @nd_arguments[nid]6059+
if args_id >= 06060+
a = get_args(args_id)6061+
if a.length > 06062+
arg0 = @nd_content[a[0]]6063+
end6064+
end6065+
if cls_find_method(ci, arg0) >= 06066+
return "TRUE"6067+
end6068+
# Check attr_readers6069+
readers = @cls_attr_readers[ci].split(";")6070+
rk = 06071+
while rk < readers.length6072+
if readers[rk] == arg06073+
return "TRUE"6074+
end6075+
rk = rk + 16076+
end6077+
return "FALSE"6078+
end6079+
end6080+
return "FALSE"6081+
end6082+
6083+
# nil?6084+
if mname == "nil?"6085+
if recv_type == "nil"6086+
return "TRUE"6087+
end6088+
if recv_type == "poly"6089+
return "sp_poly_nil_p(" + rc + ")"6090+
end6091+
if type_is_pointer(recv_type) == 16092+
return "(" + rc + " == NULL)"6093+
end6094+
return "FALSE"6095+
end6096+
6097+
# frozen? on any type6098+
if mname == "frozen?"6099+
return "TRUE"6100+
end6101+
6102+
# positive? / negative?6103+
if mname == "positive?"6104+
return "(" + rc + " > 0)"6105+
end6106+
if mname == "negative?"6107+
return "(" + rc + " < 0)"6108+
end6109+
59286110
# Object method calls59296111
if is_obj_type(recv_type) == 159306112
cname = recv_type[4, recv_type.length - 4]@@ -7060,6 +7242,7 @@
70607242
if mname == "system"70617243
if recv < 070627244
@needs_file_io = 17245+
emit(" fflush(stdout);")70637246
emit(" sp_last_status = system(" + compile_arg0(nid) + ");")70647247
return70657248
end