Commit b59ea876

b59ea876f10fMerged

Chris Hasiński <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): support block form of Array.new(N) { |i| ... }

Array.new(5) { |i| i * 2 } returned an empty array because the Array.new dispatch handled only no-args, 1-arg (n), and 2-arg (n, val) forms. The block-form fell through to the no-args path that emitted sp_IntArray_new() with no loop.

Adds an IntArray-only block path that mirrors the N.times.map { ... } codegen: allocate IntArray, loop count times binding the block param to the index, push the block's last expression each iteration.

Limited to IntArray for now: introspecting the block body via infer_type to pick a typed container (StrArray/FloatArray) destabilises the bootstrap (same compiler-state issue as bug-11). Float/String collectors must be built manually with [] + N.times { ... << }.

Details

Commit
b59ea876f10f
Parent
0244bcebe851

spinel_codegen.rbmodifiedView file

@@ -13274,6 +13274,44 @@
1327413274 if cname == "Array"
1327513275 @needs_gc = 1
1327613276 args_id = @nd_arguments[nid]
13277+ # Array.new(n) { |i| ... } -- IntArray-only fast path. We don't
13278+ # try to introspect the block body to pick a typed container
13279+ # (calling infer_type from this dispatch perturbs the bootstrap;
13280+ # see bug-11 commit). Float/String collectors must be built via
13281+ # explicit `[]` + `N.times { ... << }` instead.
13282+ if args_id >= 0 && @nd_block[nid] >= 0
13283+ arrnew_aargs = get_args(args_id)
13284+ if arrnew_aargs.length >= 1
13285+ arrnew_blk = @nd_block[nid]
13286+ arrnew_body = @nd_body[arrnew_blk]
13287+ arrnew_count = compile_expr(arrnew_aargs.first)
13288+ arrnew_bp = get_block_param(nid, 0)
13289+ arrnew_tmp = new_temp
13290+ arrnew_iv = new_temp
13291+ @needs_int_array = 1
13292+ emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();")
13293+ emit(" for (mrb_int " + arrnew_iv + " = 0; " + arrnew_iv + " < " + arrnew_count + "; " + arrnew_iv + "++) {")
13294+ if arrnew_bp != ""
13295+ emit(" lv_" + arrnew_bp + " = " + arrnew_iv + ";")
13296+ end
13297+ @indent = @indent + 1
13298+ if arrnew_body >= 0
13299+ arrnew_stmts2 = get_stmts(arrnew_body)
13300+ if arrnew_stmts2.length > 0
13301+ arrnew_k = 0
13302+ while arrnew_k < arrnew_stmts2.length - 1
13303+ compile_stmt(arrnew_stmts2[arrnew_k])
13304+ arrnew_k = arrnew_k + 1
13305+ end
13306+ arrnew_lastv = compile_expr(arrnew_stmts2.last)
13307+ emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
13308+ end
13309+ end
13310+ @indent = @indent - 1
13311+ emit(" }")
13312+ return arrnew_tmp
13313+ end
13314+ end
1327713315 if args_id >= 0
1327813316 aargs = get_args(args_id)
1327913317 if aargs.length >= 2