Commit abe0cdc1
abe0cdc196baMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +1 added · ~2 modified · -1 deleted
Description
Merge pull request #300 from mame/pr/map-block-nested-array
feat(codegen): map block returning nested array preserves cls_id chain
Details
- Commit
- abe0cdc196ba
- Parent
- 9f9082b5133b
- Merge parent
- 705f5df8df2f
spinel_codegen.rbmodifiedView file
@@ -3608,21 +3608,22 @@
36083608
if is_obj_type(bret) == 136093609
return bret + "_ptr_array"36103610
end3611-
# Block returns an array (e.g.3612-
# `[1, 6].map { (0..n).map { ... } }` — each outer3613-
# element is itself an int_array). The outer map's3614-
# result is then an array of arrays, encoded as3615-
# `<inner>_ptr_array`. Only fire this for array-shaped3616-
# recv (where the elements have a definite shape and3617-
# ivar typing reaches the ptr_array placeholder); for3618-
# range/int recv we fall through to the int_array3619-
# placeholder below so a follow-up int-typed assignment3620-
# to the same ivar still type-checks.3611+
# Block returns a 1D array (e.g.3612+
# `[1, 6].map { (0..n).map { i } }` or3613+
# `(0..3).map { |i| (0..3).map { ... } }`) — each3614+
# outer element is itself a 1D typed array. Encode3615+
# as `<inner>_ptr_array` for the standard3616+
# `arr[i][j]` dispatch shape.36213617
if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"3622-
rt_recv_p = infer_type(recv)3623-
if rt_recv_p != "range" && rt_recv_p != "int"3624-
return bret + "_ptr_array"3625-
end3618+
return bret + "_ptr_array"3619+
end3620+
# Block returns a deeper-nested array3621+
# (`int_array_ptr_array`, `poly_array`). Encode as3622+
# `poly_array` — cls_id tagging chain on the inner3623+
# PolyArray pushes preserves elem type for3624+
# `arr[i][j][k]` 3D dispatch.3625+
if is_ptr_array_type(bret) == 1 || bret == "poly_array"3626+
return "poly_array"36263627
end36273628
# poly_array bret intentionally falls through. Returning36283629
# poly_array_ptr_array would be more accurate, but ivars@@ -31513,13 +31514,15 @@
3151331514
end3151431515
end3151531516
end31516-
# Only handle scalar block returns inline; an array-returning31517-
# block (`(0..n).map { |i| [i, ...] }`) would make this an31518-
# array-of-arrays, which the int/str/float push helpers can't31519-
# express. Fall through to the "0" placeholder for those — the31520-
# generic int_array typing keeps any follow-up `@x = [...]`31521-
# assignment to the same slot type-checking.31522-
if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float"31517+
# Scalar block returns push into the matching typed array.31518+
# Array-returning blocks (`(0..n).map { |i| [...] }`) produce31519+
# an array-of-arrays — store each block result as a pointer31520+
# in `<X>_ptr_array`. Block return types we can't express31521+
# fall through to a "0" placeholder.31522+
block_is_typed_array = (block_ret_r == "int_array" || block_ret_r == "str_array" ||31523+
block_ret_r == "float_array" || block_ret_r == "sym_array" ||31524+
is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array")31525+
if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_is_typed_array == false3152331526
return "0"3152431527
end3152531528
@needs_int_array = 1@@ -31531,6 +31534,19 @@
3153131534
elsif block_ret_r == "float"3153231535
@needs_float_array = 13153331536
emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")31537+
elsif block_is_typed_array31538+
# Array-of-<X>: 1D-block-return uses PtrArray (matches31539+
# the inferred `<bret>_ptr_array` type). Deeper nesting31540+
# (block already returns ptr_array / poly_array) uses31541+
# PolyArray with `box_value_to_poly` boxing — the31542+
# cls_id chain stays tagged for `arr[i][j][k]...`31543+
# dispatch.31544+
if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"31545+
@needs_rb_value = 131546+
emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")31547+
else31548+
emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")31549+
end3153431550
else3153531551
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")3153631552
end@@ -31594,6 +31610,12 @@
3159431610
emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")3159531611
elsif block_ret_r == "float"3159631612
emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")31613+
elsif block_is_typed_array31614+
if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"31615+
emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_r, lastv_r) + ");")31616+
else31617+
emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)" + lastv_r + ");")31618+
end3159731619
else3159831620
emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")3159931621
end@@ -31692,9 +31714,15 @@
3169231714
# `[1, 6].map { (0..n).map { ... } }`) — accumulator must3169331715
# be a PtrArray so GC keeps each element alive (an IntArray3169431716
# accumulator with pointer-payload mrb_ints would let the31695-
# inner arrays be collected).31717+
# inner arrays be collected). Deeper nesting (block_ret is31718+
# already a ptr_array / poly_array) uses PolyArray boxing31719+
# so the cls_id chain stays tagged for `arr[i][j][k]...`.3169631720
ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array")31697-
if ret_is_arr231721+
ret_is_deep_arr2 = (is_ptr_array_type(block_ret) == 1 || block_ret == "poly_array")31722+
if ret_is_deep_arr231723+
@needs_rb_value = 131724+
emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")31725+
elsif ret_is_arr23169831726
@needs_ptr_array = 13169931727
emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")3170031728
else@@ -31715,7 +31743,28 @@
3171531743
if stmts3.length > 03171631744
last = stmts3[stmts3.length - 1]3171731745
val = compile_expr(last)31718-
if ret_is_arr231746+
if ret_is_deep_arr231747+
# The block returns a typed `<X>_ptr_array` (e.g.31748+
# `int_array_ptr_array`). The static type carries31749+
# elem-type info but `sp_box_ptr_array(val)` would31750+
# erase it (cls_id PTR_ARRAY only). Convert to31751+
# PolyArray inline so each inner element gets its31752+
# proper `sp_box_<elem>` tag — `arr[i][j][k]`31753+
# dispatch then chains correctly.31754+
if is_ptr_array_type(block_ret) == 131755+
inner_t = ptr_array_elem_type(block_ret)31756+
conv = new_temp31757+
ii = new_temp31758+
emit(" sp_PolyArray *" + conv + " = sp_PolyArray_new();")31759+
emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_PtrArray_length(" + val + "); " + ii + "++) {")31760+
inner_get = "(" + c_type(inner_t) + ")sp_PtrArray_get(" + val + ", " + ii + ")"31761+
emit(" sp_PolyArray_push(" + conv + ", " + box_value_to_poly(inner_t, inner_get) + ");")31762+
emit(" }")31763+
emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + conv + "));")31764+
else31765+
emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret, val) + ");")31766+
end31767+
elsif ret_is_arr23171931768
emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")3172031769
else3172131770
emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")test/map_block_returns_nested_array.rbaddedView file
Loading diff…
test/map_range_recv_array_block.rbmodifiedView file
Loading diff…
test/map_range_recv_array_block.rb.expecteddeleted
Loading diff…