Commit f9b8038d

f9b8038d7796Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): ptr_array#map { |x| ... }

Follow-up to #210 (poly_array#map). compile_map_expr had no ptr_array recv branch either — .map { ... } on a typed obj_X[] ivar/local fell through to the trailing "0" placeholder. The map's result type was inferred as e.g. int_array, spinel emitted lv_out = 0, and any .length / [i] on the typed accumulator dereferenced NULL — runtime SIGSEGV (same shape as the poly_array bug).

Add a ptr_array branch that walks sp_PtrArray_get and casts to the recv's element type. Block return picks the accumulator:

  • string-return → StrArray
  • float-return → FloatArray
  • int / bool → IntArray
  • everything else → PtrArray (preserves the obj/ptr/poly shape)

The accumulator is rooted with SP_GC_ROOT to match the other map branches.

Test: test/ptr_array_map.rb mirrors poly_array_map.rb for the homogeneous obj_X case — a Box ivar array with int-return and string-return block shapes.

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

Details

Commit
f9b8038d7796
Parent
b0969b06b62d

spinel_codegen.rbmodifiedView file

@@ -27421,6 +27421,73 @@
2742127421 pop_scope
2742227422 return tmp_arr
2742327423 end
27424+ # ptr_array recv #map: iterate sp_PtrArray_get and build a fresh
27425+ # accumulator. Like poly_array (handled above), master fell
27426+ # through to "0" for ptr_array recv, so any `.map { ... }` on a
27427+ # typed `obj_X[]` ivar/local got NULL-derefed downstream.
27428+ if is_ptr_array_type(rt) == 1
27429+ @needs_gc = 1
27430+ @needs_ptr_array = 1
27431+ elem_t = ptr_array_elem_type(rt)
27432+ block_ret_p = "int"
27433+ blk_p = @nd_block[nid]
27434+ if blk_p >= 0
27435+ body_p = @nd_body[blk_p]
27436+ if body_p >= 0
27437+ stmts_p = get_stmts(body_p)
27438+ if stmts_p.length > 0
27439+ block_ret_p = infer_type(stmts_p.last)
27440+ end
27441+ end
27442+ end
27443+ push_scope
27444+ if block_ret_p == "string"
27445+ @needs_str_array = 1
27446+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27447+ elsif block_ret_p == "float"
27448+ @needs_float_array = 1
27449+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
27450+ elsif block_ret_p == "int" || block_ret_p == "bool"
27451+ @needs_int_array = 1
27452+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27453+ else
27454+ # obj/ptr/poly block return: keep a PtrArray of the result.
27455+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
27456+ end
27457+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
27458+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_PtrArray_length(" + rc + "); " + tmp_i + "++) {")
27459+ declare_var(bp1, elem_t)
27460+ cast_t = c_type(elem_t)
27461+ emit(" lv_" + bp1 + " = (" + cast_t + ")sp_PtrArray_get(" + rc + ", " + tmp_i + ");")
27462+ @indent = @indent + 1
27463+ if blk_p >= 0
27464+ body_p2 = @nd_body[blk_p]
27465+ if body_p2 >= 0
27466+ stmts_p2 = get_stmts(body_p2)
27467+ k_p = 0
27468+ while k_p < stmts_p2.length - 1
27469+ compile_stmt(stmts_p2[k_p])
27470+ k_p = k_p + 1
27471+ end
27472+ if stmts_p2.length > 0
27473+ lastv_p = compile_expr(stmts_p2.last)
27474+ if block_ret_p == "string"
27475+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")
27476+ elsif block_ret_p == "float"
27477+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")
27478+ elsif block_ret_p == "int" || block_ret_p == "bool"
27479+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")
27480+ else
27481+ emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + lastv_p + "));")
27482+ end
27483+ end
27484+ end
27485+ end
27486+ @indent = @indent - 1
27487+ emit(" }")
27488+ pop_scope
27489+ return tmp_arr
27490+ end
2742427491 "0"
2742527492 end
2742627493