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_scope
2732627326 return tmp_arr
2732727327 end
27328+ # poly_array recv #map: iterate sp_PolyArray_get (sp_RbVal
27329+ # elements) and build a fresh container based on the block's
27330+ # return type. Without this branch the call falls through to
27331+ # `"0"`, the result is assigned as `lv_out = 0`, and any
27332+ # `.length` / `[i]` on the typed accumulator dereferences NULL.
27333+ if rt == "poly_array"
27334+ @needs_gc = 1
27335+ block_ret_p = "int"
27336+ blk_p = @nd_block[nid]
27337+ if blk_p >= 0
27338+ body_p = @nd_body[blk_p]
27339+ if body_p >= 0
27340+ stmts_p = get_stmts(body_p)
27341+ if stmts_p.length > 0
27342+ block_ret_p = infer_type(stmts_p.last)
27343+ end
27344+ end
27345+ end
27346+ push_scope
27347+ if block_ret_p == "string"
27348+ @needs_str_array = 1
27349+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27350+ elsif block_ret_p == "float"
27351+ @needs_float_array = 1
27352+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
27353+ elsif block_ret_p == "int" || block_ret_p == "bool"
27354+ @needs_int_array = 1
27355+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27356+ else
27357+ # Heterogeneous / pointer-typed block return: keep the
27358+ # poly shape so any element type round-trips through
27359+ # sp_RbVal.
27360+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
27361+ end
27362+ 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 + 1
27367+ if blk_p >= 0
27368+ body_p2 = @nd_body[blk_p]
27369+ if body_p2 >= 0
27370+ stmts_p2 = get_stmts(body_p2)
27371+ k_p = 0
27372+ while k_p < stmts_p2.length - 1
27373+ compile_stmt(stmts_p2[k_p])
27374+ k_p = k_p + 1
27375+ end
27376+ if stmts_p2.length > 0
27377+ 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+ else
27385+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_p, lastv_p) + ");")
27386+ end
27387+ end
27388+ end
27389+ end
27390+ @indent = @indent - 1
27391+ emit(" }")
27392+ pop_scope
27393+ return tmp_arr
27394+ end
2732827395 "0"
2732927396 end
2733027397