Commit a787f55f

a787f55fe397Merged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: typed-hash #each_key with block

The each_value side for sym_int / sym_str / str_int / str_str / int_str variants landed upstream as part of issue #851; this follow-up adds the matching each_key arm so h.each_key { |k| ... } works across the 8 typed-hash variants (sym_int, sym_str, sym_poly, str_int, str_str, str_poly, int_str, int_poly).

A small hash_iter_variant_info helper maps each variant to its (key_type, val_type, key_c_type, val_c_type, value_getter). The key walk reads h->order[i] directly. Sym-keyed variants cast back to sp_sym (order stores the underlying mrb_int); str- and int-keyed pass through their native C type.

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

Details

Commit
a787f55fe397
Parent
e314f84e9ac8

spinel_codegen.rbmodifiedView file

@@ -5948,6 +5948,21 @@
59485948 nil
59495949 end
59505950
5951+ # Per-variant info for typed-hash iteration: returns a 5-tuple
5952+ # [key_type, val_type, key_c_type, val_c_type, value_getter] or
5953+ # nil for the unsupported variants (poly_poly_hash is handled
5954+ # by its own slot-based walker). Used by each_key / each_value.
5955+ def hash_iter_variant_info(t)
5956+ if t == "sym_int_hash"; return ["symbol", "int", "sp_sym", "mrb_int", "sp_SymIntHash_get"]; end
5957+ if t == "sym_str_hash"; return ["symbol", "string", "sp_sym", "const char *", "sp_SymStrHash_get"]; end
5958+ if t == "sym_poly_hash"; return ["symbol", "poly", "sp_sym", "sp_RbVal", "sp_SymPolyHash_get"]; end
5959+ if t == "str_int_hash"; return ["string", "int", "const char *", "mrb_int", "sp_StrIntHash_get"]; end
5960+ if t == "str_str_hash"; return ["string", "string", "const char *", "const char *", "sp_StrStrHash_get"]; end
5961+ if t == "str_poly_hash"; return ["string", "poly", "const char *", "sp_RbVal", "sp_StrPolyHash_get"]; end
5962+ if t == "int_str_hash"; return ["int", "string", "mrb_int", "const char *", "sp_IntStrHash_get"]; end
5963+ if t == "int_poly_hash"; return ["int", "poly", "mrb_int", "sp_RbVal", "sp_IntPolyHash_get"]; end
5964+ nil
5965+ end
59515966
59525967 # CRuby returns nil for static mismatches (e.g. `{a: 1}.dig("a")`)
59535968 # since no key compares equal — Hash#dig short-circuits to nil here.
@@ -39779,6 +39794,46 @@
3977939794 end
3978039795 end
3978139796
39797+ # `hash.each_key { |k| ... }` for typed hash variants. Walks
39798+ # h->order in insertion order and binds the key. Sym-keyed
39799+ # variants cast back to sp_sym (order stores the underlying
39800+ # mrb_int); str-keyed pass through as const char *.
39801+ if mname == "each_key" && recv >= 0 && @nd_block[nid] >= 0
39802+ rt_ek = infer_type(recv)
39803+ hv_ek = hash_iter_variant_info(rt_ek)
39804+ if hv_ek != nil
39805+ rc_ek = compile_expr_gc_rooted(recv)
39806+ bp_ek = get_block_param(nid, 0)
39807+ has_bp_ek = 1
39808+ if bp_ek == ""
39809+ has_bp_ek = 0
39810+ bp_ek = "_k"
39811+ end
39812+ tmp_ek = new_temp
39813+ emit(" for (mrb_int " + tmp_ek + " = 0; " + tmp_ek + " < " + rc_ek + "->len; " + tmp_ek + "++) {")
39814+ if has_bp_ek == 1
39815+ if hv_ek[0] == "symbol"
39816+ emit(" sp_sym lv_" + bp_ek + " = (sp_sym)" + rc_ek + "->order[" + tmp_ek + "];")
39817+ else
39818+ emit(" " + hv_ek[2] + " lv_" + bp_ek + " = " + rc_ek + "->order[" + tmp_ek + "];")
39819+ end
39820+ end
39821+ @indent = @indent + 1
39822+ push_scope
39823+ if has_bp_ek == 1
39824+ declare_var(bp_ek, hv_ek[0])
39825+ end
39826+ redo_label_ek = push_redo_label
39827+ emit_redo_label(redo_label_ek)
39828+ compile_stmts_body(@nd_body[@nd_block[nid]])
39829+ pop_redo_label
39830+ pop_scope
39831+ @indent = @indent - 1
39832+ emit(" }")
39833+ return 1
39834+ end
39835+ end
39836+
3978239837 if mname == "each_slice"
3978339838 if @nd_block[nid] >= 0
3978439839 compile_each_slice_block(nid)