Commit 0c3c5d91
0c3c5d918fc5Merged
Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
chore(codegen): introduce is_array_type helper and use it at converged sites
After the per-method poly_array fixes (Array#shuffle, Array#+,
Array#concat, Array#each_with_object, Array#flat_map), eight call
sites independently spelled out the same int_array | str_array | float_array | sym_array | poly_array predicate. Factor it into
is_array_type and use it at those sites. *_ptr_array stays out of
the helper for now — the existing dispatchers don't route ptr_array
correctly even on master and adding it would be a separate concern.
No behavior change.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 0c3c5d918fc5
- Parent
- 2951d3d713b2
spinel_codegen.rbmodifiedView file
@@ -1676,7 +1676,7 @@
16761676
if lt == "poly"16771677
return "poly"16781678
end1679-
if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array" || lt == "poly_array"1679+
if is_array_type(lt) == 116801680
return lt16811681
end16821682
if lt == "float"@@ -2380,7 +2380,7 @@
23802380
if bbs.length > 023812381
bret = infer_type(bbs.last)23822382
# If block returns an array type, use it as result type2383-
if bret == "int_array" || bret == "str_array" || bret == "float_array" || bret == "sym_array" || bret == "poly_array"2383+
if is_array_type(bret) == 123842384
return bret23852385
end23862386
end@@ -3539,6 +3539,20 @@
35393539
"IntArray"35403540
end35413541
3542+
# The canonical "is this an array type?" check. Use this when you need3543+
# to dispatch a method that's defined for every typed array — `+`,3544+
# `concat`, `shuffle`, `each_with_object`, `flat_map`, etc. Covers the3545+
# 5 typed arrays (int/str/float/sym/poly). *_ptr_array is intentionally3546+
# excluded for now: sp_PtrArray lacks `_dup`/`_shuffle` and several3547+
# other helpers, and the existing dispatchers don't route ptr_array3548+
# through this path correctly even on master.3549+
def is_array_type(t)3550+
if t == "int_array" || t == "str_array" || t == "float_array" || t == "sym_array" || t == "poly_array"3551+
return 13552+
end3553+
03554+
end3555+
35423556
# ---- Collection pass ----35433557
def collect_all35443558
root = @root_id@@ -13814,7 +13828,7 @@
1381413828
@needs_string_helpers = 11381513829
return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"1381613830
end13817-
if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array" || lt == "poly_array"13831+
if is_array_type(lt) == 11381813832
rc = compile_expr_gc_rooted(recv)1381913833
arg = compile_arg0(nid)1382013834
pfx = array_c_prefix(lt)@@ -14903,7 +14917,7 @@
1490314917
# the same method name (e.g. (poly).to_s, (int).to_s) fall through1490414918
# to their own scalar dispatchers.1490514919
if mname == "inspect" || mname == "to_s"14906-
if recv_type == "int_array" || recv_type == "float_array" || recv_type == "str_array" || recv_type == "sym_array" || recv_type == "poly_array"14920+
if is_array_type(recv_type) == 11490714921
r = compile_inspect_for(recv_type, rc)1490814922
if r != ""1490914923
return r@@ -15089,14 +15103,12 @@
1508915103
pfx = array_c_prefix(recv_type)1509015104
return "sp_" + pfx + "_get(" + rc + ", rand() % sp_" + pfx + "_length(" + rc + "))"1509115105
end15092-
if mname == "shuffle" &&15093-
(recv_type == "int_array" || recv_type == "str_array" || recv_type == "float_array" || recv_type == "sym_array" || recv_type == "poly_array")15106+
if mname == "shuffle" && is_array_type(recv_type) == 11509415107
@needs_rand = 11509515108
pfx = array_c_prefix(recv_type)1509615109
return "sp_" + pfx + "_shuffle(" + rc + ")"1509715110
end15098-
if mname == "shuffle!" &&15099-
(recv_type == "int_array" || recv_type == "str_array" || recv_type == "float_array" || recv_type == "sym_array" || recv_type == "poly_array")15111+
if mname == "shuffle!" && is_array_type(recv_type) == 11510015112
@needs_rand = 11510115113
pfx = array_c_prefix(recv_type)1510215114
emit(" sp_" + pfx + "_shuffle_bang(" + rc + ");")@@ -18670,7 +18682,7 @@
1867018682
if mname == "concat"1867118683
if recv >= 01867218684
rt = infer_type(recv)18673-
if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "poly_array"18685+
if is_array_type(rt) == 11867418686
rc = compile_expr_gc_rooted(recv)1867518687
arg = compile_arg0(nid)1867618688
pfx = array_c_prefix(rt)@@ -20965,7 +20977,7 @@
2096520977
result = new_temp2096620978
emit(" " + obj_ct + " " + result + " = " + obj_arg + ";")2096720979
tmp_i = new_temp20968-
if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "poly_array"20980+
if is_array_type(rt) == 12096920981
pfx = array_c_prefix(rt)2097020982
emit(" {")2097120983
@indent = @indent + 1@@ -21726,7 +21738,7 @@
2172621738
end2172721739
end2172821740
# Fall back to receiver type if block doesn't return an array21729-
if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array" && block_ret != "sym_array" && block_ret != "poly_array"21741+
if is_array_type(block_ret) == 02173021742
block_ret = rt2173121743
end2173221744
@needs_gc = 1