Commit ff20bd87
ff20bd874b7fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
codegen: Hash#map block form on sym_int_hash
The previous fallthrough emitted a stale `_t = 0` IntArray pointer and crashed on .inspect — sym_int_hash.map had no direct dispatch arm. Walk h->order, bind (k, v) to the block params, accumulate into the right typed array (str for String-returning blocks, float for Float, int otherwise).
Closes the Hash#map portion of #851; transform_keys, the remaining poly-hash variants, and select/reject-with-block on every variant are still tracked there.
Refs #851
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- ff20bd874b7f
- Parent
- 4062f8264c14
spinel_codegen.rbmodifiedView file
@@ -22234,6 +22234,60 @@
2223422234
end2223522235
end2223622236
if recv_type == "sym_int_hash"22237+
if mname == "map" && @nd_block[nid] >= 022238+
# Hash#map { |k, v| ... } walks the hash and accumulates the22239+
# block's return into a result array. Result type comes from22240+
# the block body's last expr (string → str_array, int → int_array,22241+
# else poly_array). The previous fallthrough emitted a stale22242+
# `_t = 0` and crashed on .inspect.22243+
rc_hm = rc22244+
bp_k_hm = get_block_param(nid, 0)22245+
bp_k_hm = "_k" if bp_k_hm == ""22246+
bp_v_hm = get_block_param(nid, 1)22247+
bp_v_hm = "_v" if bp_v_hm == ""22248+
blk_id_hm = @nd_block[nid]22249+
body_id_hm = @nd_body[blk_id_hm]22250+
body_stmts_hm = body_id_hm >= 0 ? get_stmts(body_id_hm) : []22251+
body_ret_t_hm = "int"22252+
if body_stmts_hm.length > 022253+
body_ret_t_hm = infer_type(body_stmts_hm.last)22254+
end22255+
out_hm = new_temp22256+
iter_hm = new_temp22257+
out_pfx = "IntArray"22258+
if body_ret_t_hm == "string"22259+
out_pfx = "StrArray"22260+
@needs_str_array = 122261+
elsif body_ret_t_hm == "float"22262+
out_pfx = "FloatArray"22263+
@needs_float_array = 122264+
else22265+
@needs_int_array = 122266+
end22267+
emit(" sp_" + out_pfx + " *" + out_hm + " = sp_" + out_pfx + "_new();")22268+
emit(" SP_GC_ROOT(" + out_hm + ");")22269+
emit(" for (mrb_int " + iter_hm + " = 0; " + iter_hm + " < " + rc_hm + "->len; " + iter_hm + "++) {")22270+
emit(" sp_sym lv_" + bp_k_hm + " = " + rc_hm + "->order[" + iter_hm + "];")22271+
emit(" mrb_int lv_" + bp_v_hm + " = sp_SymIntHash_get(" + rc_hm + ", lv_" + bp_k_hm + ");")22272+
@indent = @indent + 122273+
push_scope22274+
declare_var(bp_k_hm, "symbol")22275+
declare_var(bp_v_hm, "int")22276+
bexpr_hm = "0"22277+
if body_stmts_hm.length > 022278+
k_hm = 022279+
while k_hm < body_stmts_hm.length - 122280+
compile_stmt(body_stmts_hm[k_hm])22281+
k_hm = k_hm + 122282+
end22283+
bexpr_hm = compile_expr(body_stmts_hm.last)22284+
end22285+
emit(" sp_" + out_pfx + "_push(" + out_hm + ", " + bexpr_hm + ");")22286+
pop_scope22287+
@indent = @indent - 122288+
emit(" }")22289+
return out_hm22290+
end2223722291
if mname == "[]"2223822292
args_id0 = @nd_arguments[nid]2223922293
if args_id0 >= 0test/hash_map_block.rbaddedView file
Loading diff…