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
if cname == "Array" @needs_gc = 1 args_id = @nd_arguments[nid] # Array.new(n) { |i| ... } -- IntArray-only fast path. We don't # try to introspect the block body to pick a typed container # (calling infer_type from this dispatch perturbs the bootstrap; # see bug-11 commit). Float/String collectors must be built via # explicit `[]` + `N.times { ... << }` instead. if args_id >= 0 && @nd_block[nid] >= 0 arrnew_aargs = get_args(args_id) if arrnew_aargs.length >= 1 arrnew_blk = @nd_block[nid] arrnew_body = @nd_body[arrnew_blk] arrnew_count = compile_expr(arrnew_aargs.first) arrnew_bp = get_block_param(nid, 0) arrnew_tmp = new_temp arrnew_iv = new_temp @needs_int_array = 1 emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();") emit(" for (mrb_int " + arrnew_iv + " = 0; " + arrnew_iv + " < " + arrnew_count + "; " + arrnew_iv + "++) {") if arrnew_bp != "" emit(" lv_" + arrnew_bp + " = " + arrnew_iv + ";") end @indent = @indent + 1 if arrnew_body >= 0 arrnew_stmts2 = get_stmts(arrnew_body) if arrnew_stmts2.length > 0 arrnew_k = 0 while arrnew_k < arrnew_stmts2.length - 1 compile_stmt(arrnew_stmts2[arrnew_k]) arrnew_k = arrnew_k + 1 end arrnew_lastv = compile_expr(arrnew_stmts2.last) emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");") end end @indent = @indent - 1 emit(" }") return arrnew_tmp end end if args_id >= 0 aargs = get_args(args_id) if aargs.length >= 2test/bm_array_new_block.rbaddedView file
Loading diff…