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 = 12696626966
emit(" sp_PolyArray_push(" + target + ", sp_box_nil());")26967+
elsif container_type == "ptr_array"26968+
emit(" sp_PtrArray_push(" + target + ", NULL);")2696726969
else2696826970
emit(" sp_IntArray_push(" + target + ", 0);")2696926971
end@@ -27421,6 +27423,85 @@
2742127423
pop_scope2742227424
return tmp_arr2742327425
end27426+
# ptr_array recv #map: iterate sp_PtrArray_get and build a fresh27427+
# accumulator. Like poly_array (handled above), master fell27428+
# through to "0" for ptr_array recv, so any `.map { ... }` on a27429+
# typed `obj_X[]` ivar/local got NULL-derefed downstream.27430+
if is_ptr_array_type(rt) == 127431+
@needs_gc = 127432+
@needs_ptr_array = 127433+
elem_t = ptr_array_elem_type(rt)27434+
cast_t = c_type(elem_t)27435+
# Empty / missing block leaves block_ret_p as "" so the27436+
# accumulator falls into the catch-all PtrArray branch —27437+
# matches infer_type's prediction for ptr_array.map with no27438+
# expression in the block.27439+
block_ret_p = ""27440+
blk_p = @nd_block[nid]27441+
if blk_p >= 027442+
body_p = @nd_body[blk_p]27443+
if body_p >= 027444+
stmts_p = get_stmts(body_p)27445+
if stmts_p.length > 027446+
block_ret_p = infer_type(stmts_p.last)27447+
end27448+
end27449+
end27450+
push_scope27451+
p_container = ""27452+
if block_ret_p == "string"27453+
@needs_str_array = 127454+
emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")27455+
p_container = "str_array"27456+
elsif block_ret_p == "float"27457+
@needs_float_array = 127458+
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 = 127462+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")27463+
p_container = "int_array"27464+
else27465+
# 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+
end27469+
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 (no27473+
# spinel-side block param) gets a typed binding inside the27474+
# for-loop body.27475+
emit(" " + cast_t + " lv_" + bp1 + " = (" + cast_t + ")sp_PtrArray_get(" + rc + ", " + tmp_i + ");")27476+
@indent = @indent + 127477+
if blk_p >= 027478+
body_p2 = @nd_body[blk_p]27479+
stmts_p2 = body_p2 >= 0 ? get_stmts(body_p2) : []27480+
k_p = 027481+
while k_p < stmts_p2.length - 127482+
compile_stmt(stmts_p2[k_p])27483+
k_p = k_p + 127484+
end27485+
if stmts_p2.length > 027486+
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+
else27494+
emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + lastv_p + "));")27495+
end27496+
else27497+
emit_map_default_push(tmp_arr, p_container)27498+
end27499+
end27500+
@indent = @indent - 127501+
emit(" }")27502+
pop_scope27503+
return tmp_arr27504+
end2742427505
"0"2742527506
end2742627507
test/map_empty_block.rbmodifiedView file
Loading diff…
test/map_empty_block.rb.expectedmodifiedView file
Loading diff…
test/ptr_array_map.rbaddedView file
Loading diff…
test/ptr_array_map.rb.expectedaddedView file
Loading diff…