Commit 2da3481c

2da3481cf349Merged

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

Description

Merge pull request #1318 from ryanseys/rs-hash-each-key-value

codegen: Hash#each_key with a block on typed hashes

Details

Commit
2da3481cf349
Parent
494c4ee109c6
Merge parent
b0ca8f7c52ab

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_int_hash"; return ["int", "int", "mrb_int", "mrb_int", "sp_IntIntHash_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.
@@ -39713,9 +39728,9 @@
3971339728 end
3971439729 end
3971539730
39716- # `hash.each_value { |v| ... }` for poly_poly_hash. Other hash
39717- # shapes can be added here once they surface; the underlying
39718- # representation is the same `vals[order[i]]` walk.
39731+ # `hash.each_value { |v| ... }`. poly_poly_hash has its own
39732+ # slot-based walk (`vals[order[i]]`); every other variant is
39733+ # dispatched through hash_iter_variant_info below.
3971939734 if mname == "each_value" && recv >= 0 && @nd_block[nid] >= 0
3972039735 rt_ev = infer_type(recv)
3972139736 if rt_ev == "poly_poly_hash"
@@ -39745,28 +39760,15 @@
3974539760 emit(" }")
3974639761 return 1
3974739762 end
39748- # Issue #851: each_value on the common typed-hash variants.
39749- # The shape is the same: walk h->order[i] and dispatch the
39750- # variant's _get. Only int and string value parts here; the
39751- # poly variants flow through the poly_poly_hash arm above
39752- # already after boxing.
39753- pfx_ev = ""
39754- vty_ev = ""
39755- vtag_ev = ""
39756- if rt_ev == "sym_int_hash"
39757- pfx_ev = "SymIntHash"; vty_ev = "mrb_int"; vtag_ev = "int"
39758- @needs_sym_int_hash = 1
39759- elsif rt_ev == "str_int_hash"
39760- pfx_ev = "StrIntHash"; vty_ev = "mrb_int"; vtag_ev = "int"
39761- @needs_str_int_hash = 1
39762- elsif rt_ev == "int_str_hash"
39763- pfx_ev = "IntStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39764- elsif rt_ev == "str_str_hash"
39765- pfx_ev = "StrStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39766- elsif rt_ev == "sym_str_hash"
39767- pfx_ev = "SymStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39768- end
39769- if pfx_ev != ""
39763+ # each_value on the typed-hash variants, dispatched through the
39764+ # shared hash_iter_variant_info helper (the same map that drives
39765+ # each_key). Walk h->order[i] and read the value via the variant's
39766+ # _get. Covers int, string, and poly value parts; poly_poly_hash is
39767+ # handled by its own slot-based arm above.
39768+ hv_ev2 = hash_iter_variant_info(rt_ev)
39769+ if hv_ev2 != nil
39770+ @needs_sym_int_hash = 1 if rt_ev == "sym_int_hash"
39771+ @needs_str_int_hash = 1 if rt_ev == "str_int_hash"
3977039772 rc_ev2 = compile_expr_gc_rooted(recv)
3977139773 bp_ev2 = get_block_param(nid, 0)
3977239774 if bp_ev2 == ""
@@ -39774,11 +39776,81 @@
3977439776 end
3977539777 tmp_ev2 = new_temp
3977639778 emit(" for (mrb_int " + tmp_ev2 + " = 0; " + tmp_ev2 + " < " + rc_ev2 + "->len; " + tmp_ev2 + "++) {")
39777- emit(" " + vty_ev + " lv_" + bp_ev2 + " = sp_" + pfx_ev + "_get(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);")
39779+ emit(" " + hv_ev2[3] + " lv_" + bp_ev2 + " = " + hv_ev2[4] + "(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);")
39780+ @indent = @indent + 1
39781+ push_scope
39782+ declare_var(bp_ev2, hv_ev2[1])
39783+ compile_stmts_body(@nd_body[@nd_block[nid]])
39784+ pop_scope
39785+ @indent = @indent - 1
39786+ emit(" }")
39787+ return 1
39788+ end
39789+ end
39790+
39791+ # `hash.each_key { |k| ... }` for typed hash variants. Walks
39792+ # h->order in insertion order and binds the key. Sym-keyed
39793+ # variants cast back to sp_sym (order stores the underlying
39794+ # mrb_int); str-keyed pass through as const char *.
39795+ if mname == "each_key" && recv >= 0 && @nd_block[nid] >= 0
39796+ rt_ek = infer_type(recv)
39797+ hv_ek = hash_iter_variant_info(rt_ek)
39798+ if hv_ek != nil
39799+ rc_ek = compile_expr_gc_rooted(recv)
39800+ bp_ek = get_block_param(nid, 0)
39801+ has_bp_ek = 1
39802+ if bp_ek == ""
39803+ has_bp_ek = 0
39804+ bp_ek = "_k"
39805+ end
39806+ tmp_ek = new_temp
39807+ emit(" for (mrb_int " + tmp_ek + " = 0; " + tmp_ek + " < " + rc_ek + "->len; " + tmp_ek + "++) {")
39808+ if has_bp_ek == 1
39809+ if hv_ek[0] == "symbol"
39810+ emit(" sp_sym lv_" + bp_ek + " = (sp_sym)" + rc_ek + "->order[" + tmp_ek + "];")
39811+ else
39812+ emit(" " + hv_ek[2] + " lv_" + bp_ek + " = " + rc_ek + "->order[" + tmp_ek + "];")
39813+ end
39814+ end
3977839815 @indent = @indent + 1
3977939816 push_scope
39780- declare_var(bp_ev2, vtag_ev)
39817+ if has_bp_ek == 1
39818+ declare_var(bp_ek, hv_ek[0])
39819+ end
39820+ redo_label_ek = push_redo_label
39821+ emit_redo_label(redo_label_ek)
3978139822 compile_stmts_body(@nd_body[@nd_block[nid]])
39823+ pop_redo_label
39824+ pop_scope
39825+ @indent = @indent - 1
39826+ emit(" }")
39827+ return 1
39828+ end
39829+ # poly_poly_hash each_key: the helper returns nil for it (keys and
39830+ # values are sp_RbVal indexed by the slot in h->order). Mirrors the
39831+ # each_value poly arm above but binds h->keys[order[i]].
39832+ if rt_ek == "poly_poly_hash"
39833+ rc_pk = compile_expr_gc_rooted(recv)
39834+ bp_pk = get_block_param(nid, 0)
39835+ has_bp_pk = 1
39836+ if bp_pk == ""
39837+ has_bp_pk = 0
39838+ bp_pk = "_k"
39839+ end
39840+ tmp_pk = new_temp
39841+ emit(" for (mrb_int " + tmp_pk + " = 0; " + tmp_pk + " < " + rc_pk + "->len; " + tmp_pk + "++) {")
39842+ if has_bp_pk == 1
39843+ emit(" sp_RbVal lv_" + bp_pk + " = " + rc_pk + "->keys[" + rc_pk + "->order[" + tmp_pk + "]];")
39844+ end
39845+ @indent = @indent + 1
39846+ push_scope
39847+ if has_bp_pk == 1
39848+ declare_var(bp_pk, "poly")
39849+ end
39850+ redo_label_pk = push_redo_label
39851+ emit_redo_label(redo_label_pk)
39852+ compile_stmts_body(@nd_body[@nd_block[nid]])
39853+ pop_redo_label
3978239854 pop_scope
3978339855 @indent = @indent - 1
3978439856 emit(" }")