Commit da9191be

da9191becf08Merged

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

Description

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

compile_map_expr had no poly_array recv branch, so a heterogeneous-element array ([1, "two", :three]) called .map { ... } and the dispatch fell through to the "0" placeholder. The map's result type was inferred as int_array (per infer_method_name_type), spinel emitted lv_out = 0, and any .length / [i] on the typed accumulator dereferenced NULL — runtime SIGSEGV.

Add a poly_array branch that walks sp_PolyArray_get (returning sp_RbVal), declares the block param as poly, and pushes each block result into a fresh accumulator chosen by the block's return type:

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

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

Test: test/poly_array_map.rb covers int / string / mixed-int block returns over a heterogeneous [Int, String, Symbol, Float] recv.

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

Details

Commit
da9191becf08
Parent
5a321da268b9

spinel_codegen.rbmodifiedView file

@@ -27315,6 +27315,73 @@
2731527315 pop_scope
2731627316 return tmp_arr
2731727317 end
27318+ # poly_array recv #map: iterate sp_PolyArray_get (sp_RbVal
27319+ # elements) and build a fresh container based on the block's
27320+ # return type. Without this branch the call falls through to
27321+ # `"0"`, the result is assigned as `lv_out = 0`, and any
27322+ # `.length` / `[i]` on the typed accumulator dereferences NULL.
27323+ if rt == "poly_array"
27324+ @needs_gc = 1
27325+ block_ret_p = "int"
27326+ blk_p = @nd_block[nid]
27327+ if blk_p >= 0
27328+ body_p = @nd_body[blk_p]
27329+ if body_p >= 0
27330+ stmts_p = get_stmts(body_p)
27331+ if stmts_p.length > 0
27332+ block_ret_p = infer_type(stmts_p.last)
27333+ end
27334+ end
27335+ end
27336+ push_scope
27337+ if block_ret_p == "string"
27338+ @needs_str_array = 1
27339+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27340+ elsif block_ret_p == "float"
27341+ @needs_float_array = 1
27342+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
27343+ elsif block_ret_p == "int" || block_ret_p == "bool"
27344+ @needs_int_array = 1
27345+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27346+ else
27347+ # Heterogeneous / pointer-typed block return: keep the
27348+ # poly shape so any element type round-trips through
27349+ # sp_RbVal.
27350+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
27351+ end
27352+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
27353+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_PolyArray_length(" + rc + "); " + tmp_i + "++) {")
27354+ declare_var(bp1, "poly")
27355+ emit(" lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp_i + ");")
27356+ @indent = @indent + 1
27357+ if blk_p >= 0
27358+ body_p2 = @nd_body[blk_p]
27359+ if body_p2 >= 0
27360+ stmts_p2 = get_stmts(body_p2)
27361+ k_p = 0
27362+ while k_p < stmts_p2.length - 1
27363+ compile_stmt(stmts_p2[k_p])
27364+ k_p = k_p + 1
27365+ end
27366+ if stmts_p2.length > 0
27367+ lastv_p = compile_expr(stmts_p2.last)
27368+ if block_ret_p == "string"
27369+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")
27370+ elsif block_ret_p == "float"
27371+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")
27372+ elsif block_ret_p == "int" || block_ret_p == "bool"
27373+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")
27374+ else
27375+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_p, lastv_p) + ");")
27376+ end
27377+ end
27378+ end
27379+ end
27380+ @indent = @indent - 1
27381+ emit(" }")
27382+ pop_scope
27383+ return tmp_arr
27384+ end
2731827385 "0"
2731927386 end
2732027387