Commit d7c4dca7

d7c4dca7ae74Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

Merge pull request #211 from mame/pr/ptr-array-map

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

Details

Commit
d7c4dca7ae74
Parent
b0969b06b62d
Merge parent
f9b8038d7796

spinel_codegen.rbmodifiedView file

@@ -26964,6 +26964,8 @@
2696426964 elsif container_type == "poly_array"
2696526965 @needs_rb_value = 1
2696626966 emit(" sp_PolyArray_push(" + target + ", sp_box_nil());")
26967+ elsif container_type == "ptr_array"
26968+ emit(" sp_PtrArray_push(" + target + ", NULL);")
2696726969 else
2696826970 emit(" sp_IntArray_push(" + target + ", 0);")
2696926971 end
@@ -27421,6 +27423,85 @@
2742127423 pop_scope
2742227424 return tmp_arr
2742327425 end
27426+ # ptr_array recv #map: iterate sp_PtrArray_get and build a fresh
27427+ # accumulator. Like poly_array (handled above), master fell
27428+ # through to "0" for ptr_array recv, so any `.map { ... }` on a
27429+ # typed `obj_X[]` ivar/local got NULL-derefed downstream.
27430+ if is_ptr_array_type(rt) == 1
27431+ @needs_gc = 1
27432+ @needs_ptr_array = 1
27433+ elem_t = ptr_array_elem_type(rt)
27434+ cast_t = c_type(elem_t)
27435+ # Empty / missing block leaves block_ret_p as "" so the
27436+ # accumulator falls into the catch-all PtrArray branch —
27437+ # matches infer_type's prediction for ptr_array.map with no
27438+ # expression in the block.
27439+ block_ret_p = ""
27440+ blk_p = @nd_block[nid]
27441+ if blk_p >= 0
27442+ body_p = @nd_body[blk_p]
27443+ if body_p >= 0
27444+ stmts_p = get_stmts(body_p)
27445+ if stmts_p.length > 0
27446+ block_ret_p = infer_type(stmts_p.last)
27447+ end
27448+ end
27449+ end
27450+ push_scope
27451+ p_container = ""
27452+ if block_ret_p == "string"
27453+ @needs_str_array = 1
27454+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27455+ p_container = "str_array"
27456+ elsif block_ret_p == "float"
27457+ @needs_float_array = 1
27458+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
27459+ p_container = "float_array"
27460+ elsif block_ret_p == "int" || block_ret_p == "bool"
27461+ @needs_int_array = 1
27462+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27463+ p_container = "int_array"
27464+ else
27465+ # obj/ptr/poly block return: keep a PtrArray of the result.
27466+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
27467+ p_container = "ptr_array"
27468+ end
27469+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
27470+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_PtrArray_length(" + rc + "); " + tmp_i + "++) {")
27471+ declare_var(bp1, elem_t)
27472+ # Inline C declaration so the synthetic `_x` placeholder (no
27473+ # spinel-side block param) gets a typed binding inside the
27474+ # for-loop body.
27475+ emit(" " + cast_t + " lv_" + bp1 + " = (" + cast_t + ")sp_PtrArray_get(" + rc + ", " + tmp_i + ");")
27476+ @indent = @indent + 1
27477+ if blk_p >= 0
27478+ body_p2 = @nd_body[blk_p]
27479+ stmts_p2 = body_p2 >= 0 ? get_stmts(body_p2) : []
27480+ k_p = 0
27481+ while k_p < stmts_p2.length - 1
27482+ compile_stmt(stmts_p2[k_p])
27483+ k_p = k_p + 1
27484+ end
27485+ if stmts_p2.length > 0
27486+ lastv_p = compile_expr(stmts_p2.last)
27487+ if block_ret_p == "string"
27488+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")
27489+ elsif block_ret_p == "float"
27490+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")
27491+ elsif block_ret_p == "int" || block_ret_p == "bool"
27492+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")
27493+ else
27494+ emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + lastv_p + "));")
27495+ end
27496+ else
27497+ emit_map_default_push(tmp_arr, p_container)
27498+ end
27499+ end
27500+ @indent = @indent - 1
27501+ emit(" }")
27502+ pop_scope
27503+ return tmp_arr
27504+ end
2742427505 "0"
2742527506 end
2742627507