Commit a39c6aae
a39c6aae6520Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #210 from mame/pr/poly-array-map
feat(codegen): poly_array#map { |x| ... }
Details
- Commit
- a39c6aae6520
- Parent
- b5758a8afeb5
- Merge parent
- da9191becf08
spinel_codegen.rbmodifiedView file
@@ -27325,6 +27325,73 @@
2732527325
pop_scope2732627326
return tmp_arr2732727327
end27328+
# poly_array recv #map: iterate sp_PolyArray_get (sp_RbVal27329+
# elements) and build a fresh container based on the block's27330+
# return type. Without this branch the call falls through to27331+
# `"0"`, the result is assigned as `lv_out = 0`, and any27332+
# `.length` / `[i]` on the typed accumulator dereferences NULL.27333+
if rt == "poly_array"27334+
@needs_gc = 127335+
block_ret_p = "int"27336+
blk_p = @nd_block[nid]27337+
if blk_p >= 027338+
body_p = @nd_body[blk_p]27339+
if body_p >= 027340+
stmts_p = get_stmts(body_p)27341+
if stmts_p.length > 027342+
block_ret_p = infer_type(stmts_p.last)27343+
end27344+
end27345+
end27346+
push_scope27347+
if block_ret_p == "string"27348+
@needs_str_array = 127349+
emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")27350+
elsif block_ret_p == "float"27351+
@needs_float_array = 127352+
emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")27353+
elsif block_ret_p == "int" || block_ret_p == "bool"27354+
@needs_int_array = 127355+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")27356+
else27357+
# Heterogeneous / pointer-typed block return: keep the27358+
# poly shape so any element type round-trips through27359+
# sp_RbVal.27360+
emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")27361+
end27362+
emit(" SP_GC_ROOT(" + tmp_arr + ");")27363+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_PolyArray_length(" + rc + "); " + tmp_i + "++) {")27364+
declare_var(bp1, "poly")27365+
emit(" lv_" + bp1 + " = sp_PolyArray_get(" + rc + ", " + tmp_i + ");")27366+
@indent = @indent + 127367+
if blk_p >= 027368+
body_p2 = @nd_body[blk_p]27369+
if body_p2 >= 027370+
stmts_p2 = get_stmts(body_p2)27371+
k_p = 027372+
while k_p < stmts_p2.length - 127373+
compile_stmt(stmts_p2[k_p])27374+
k_p = k_p + 127375+
end27376+
if stmts_p2.length > 027377+
lastv_p = compile_expr(stmts_p2.last)27378+
if block_ret_p == "string"27379+
emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_p + ");")27380+
elsif block_ret_p == "float"27381+
emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_p + ");")27382+
elsif block_ret_p == "int" || block_ret_p == "bool"27383+
emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_p + ");")27384+
else27385+
emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_p, lastv_p) + ");")27386+
end27387+
end27388+
end27389+
end27390+
@indent = @indent - 127391+
emit(" }")27392+
pop_scope27393+
return tmp_arr27394+
end2732827395
"0"2732927396
end2733027397
test/poly_array_map.rbaddedView file
Loading diff…
test/poly_array_map.rb.expectedaddedView file
Loading diff…