Commit eadc38a2

eadc38a27532Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: Hash#each_with_object iterates k,v pairs into the block

`h.each_with_object([]) { |(k,v), memo| ... }` previously fell through compile_each_with_object_block's array-only path and returned the seed unchanged.

Add a hash branch that walks order[i] for keys, calls the variant-specific _get for values, and binds bp_k / bp_v / memo inside the loop. Both block param shapes accepted:

  • destructured |(k,v), memo| (MultiTargetNode with two names)
  • flat |k, v, memo| (three required params)

Wired for all 8 typed hash variants (sym/str/int x int/str/poly). Note: int_array seed with non-int block result widens incorrectly; use a typed seed when the block produces non-int values (this is a broader empty-array typing limit, not specific to each_with_object).

Fixes #958

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
eadc38a27532
Parent
bd17fb16727b

spinel_codegen.rbmodifiedView file

@@ -39559,6 +39559,66 @@
3955939559 emit(" " + result + " = lv_" + bp2 + ";")
3956039560 @indent = @indent - 1
3956139561 emit(" }")
39562+ elsif is_hash_type(rt) == 1
39563+ # Hash#each_with_object — yields `(pair, memo)` where pair is
39564+ # [key, value]. Common idiom destructures `|(k,v), memo|`; the
39565+ # MultiTargetNode for `(k,v)` carries the two names. The
39566+ # bp1 / bp2 we already extracted are the destructured names
39567+ # when the user writes `|k, v, memo|` (rare, 3 flat params) or
39568+ # we look at the MultiTargetNode directly.
39569+ key_name = "_k"
39570+ val_name = "_v"
39571+ blk_id = @nd_block[nid]
39572+ params_h = blk_id >= 0 ? @nd_parameters[blk_id] : -1
39573+ inner_h = params_h >= 0 ? @nd_parameters[params_h] : -1
39574+ reqs_h = inner_h >= 0 ? parse_id_list(@nd_requireds[inner_h]) : []
39575+ memo_name = bp2
39576+ if reqs_h.length == 2 && @nd_type[reqs_h[0]] == "MultiTargetNode"
39577+ mt_targets = parse_id_list(@nd_targets[reqs_h[0]])
39578+ if mt_targets.length >= 1 && @nd_name[mt_targets[0]] != ""
39579+ key_name = @nd_name[mt_targets[0]]
39580+ end
39581+ if mt_targets.length >= 2 && @nd_name[mt_targets[1]] != ""
39582+ val_name = @nd_name[mt_targets[1]]
39583+ end
39584+ memo_name = @nd_name[reqs_h[1]]
39585+ elsif reqs_h.length >= 3
39586+ key_name = @nd_name[reqs_h[0]]
39587+ val_name = @nd_name[reqs_h[1]]
39588+ memo_name = @nd_name[reqs_h[2]]
39589+ end
39590+ key_t = (rt == "sym_int_hash" || rt == "sym_str_hash" || rt == "sym_poly_hash") ? "symbol" : ((rt == "str_int_hash" || rt == "str_str_hash" || rt == "str_poly_hash") ? "string" : "int")
39591+ val_t = (rt == "sym_int_hash" || rt == "str_int_hash" || rt == "int_int_hash") ? "int" : ((rt == "sym_str_hash" || rt == "str_str_hash" || rt == "int_str_hash") ? "string" : "poly")
39592+ hash_pfx = ""
39593+ hash_pfx = "SymIntHash" if rt == "sym_int_hash"
39594+ hash_pfx = "SymStrHash" if rt == "sym_str_hash"
39595+ hash_pfx = "SymPolyHash" if rt == "sym_poly_hash"
39596+ hash_pfx = "StrIntHash" if rt == "str_int_hash"
39597+ hash_pfx = "StrStrHash" if rt == "str_str_hash"
39598+ hash_pfx = "StrPolyHash" if rt == "str_poly_hash"
39599+ hash_pfx = "IntIntHash" if rt == "int_int_hash"
39600+ hash_pfx = "IntStrHash" if rt == "int_str_hash"
39601+ emit(" {")
39602+ @indent = @indent + 1
39603+ emit(" " + obj_ct + " lv_" + memo_name + " = " + result + ";")
39604+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + rc + "->len; " + tmp_i + "++) {")
39605+ emit(" " + c_type(key_t) + " lv_" + key_name + " = " + rc + "->order[" + tmp_i + "];")
39606+ emit(" " + c_type(val_t) + " lv_" + val_name + " = sp_" + hash_pfx + "_get(" + rc + ", lv_" + key_name + ");")
39607+ @indent = @indent + 1
39608+ push_scope
39609+ declare_var(key_name, key_t)
39610+ declare_var(val_name, val_t)
39611+ declare_var(memo_name, obj_t)
39612+ redo_label = push_redo_label
39613+ emit_redo_label(redo_label)
39614+ compile_stmts_body(@nd_body[@nd_block[nid]])
39615+ pop_redo_label
39616+ pop_scope
39617+ @indent = @indent - 1
39618+ emit(" }")
39619+ emit(" " + result + " = lv_" + memo_name + ";")
39620+ @indent = @indent - 1
39621+ emit(" }")
3956239622 end
3956339623 @in_loop = old
3956439624 result