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
nil59495949
end59505950
5951+
# Per-variant info for typed-hash iteration: returns a 5-tuple5952+
# [key_type, val_type, key_c_type, val_c_type, value_getter] or5953+
# nil for the unsupported variants (poly_poly_hash is handled5954+
# 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"]; end5957+
if t == "sym_str_hash"; return ["symbol", "string", "sp_sym", "const char *", "sp_SymStrHash_get"]; end5958+
if t == "sym_poly_hash"; return ["symbol", "poly", "sp_sym", "sp_RbVal", "sp_SymPolyHash_get"]; end5959+
if t == "str_int_hash"; return ["string", "int", "const char *", "mrb_int", "sp_StrIntHash_get"]; end5960+
if t == "str_str_hash"; return ["string", "string", "const char *", "const char *", "sp_StrStrHash_get"]; end5961+
if t == "str_poly_hash"; return ["string", "poly", "const char *", "sp_RbVal", "sp_StrPolyHash_get"]; end5962+
if t == "int_str_hash"; return ["int", "string", "mrb_int", "const char *", "sp_IntStrHash_get"]; end5963+
if t == "int_int_hash"; return ["int", "int", "mrb_int", "mrb_int", "sp_IntIntHash_get"]; end5964+
nil5965+
end59515966
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
end3971439729
end3971539730
39716-
# `hash.each_value { |v| ... }` for poly_poly_hash. Other hash39717-
# shapes can be added here once they surface; the underlying39718-
# representation is the same `vals[order[i]]` walk.39731+
# `hash.each_value { |v| ... }`. poly_poly_hash has its own39732+
# slot-based walk (`vals[order[i]]`); every other variant is39733+
# dispatched through hash_iter_variant_info below.3971939734
if mname == "each_value" && recv >= 0 && @nd_block[nid] >= 03972039735
rt_ev = infer_type(recv)3972139736
if rt_ev == "poly_poly_hash"@@ -39745,28 +39760,15 @@
3974539760
emit(" }")3974639761
return 13974739762
end39748-
# Issue #851: each_value on the common typed-hash variants.39749-
# The shape is the same: walk h->order[i] and dispatch the39750-
# variant's _get. Only int and string value parts here; the39751-
# poly variants flow through the poly_poly_hash arm above39752-
# 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 = 139759-
elsif rt_ev == "str_int_hash"39760-
pfx_ev = "StrIntHash"; vty_ev = "mrb_int"; vtag_ev = "int"39761-
@needs_str_int_hash = 139762-
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-
end39769-
if pfx_ev != ""39763+
# each_value on the typed-hash variants, dispatched through the39764+
# shared hash_iter_variant_info helper (the same map that drives39765+
# each_key). Walk h->order[i] and read the value via the variant's39766+
# _get. Covers int, string, and poly value parts; poly_poly_hash is39767+
# handled by its own slot-based arm above.39768+
hv_ev2 = hash_iter_variant_info(rt_ev)39769+
if hv_ev2 != nil39770+
@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
end3977539777
tmp_ev2 = new_temp3977639778
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 + 139781+
push_scope39782+
declare_var(bp_ev2, hv_ev2[1])39783+
compile_stmts_body(@nd_body[@nd_block[nid]])39784+
pop_scope39785+
@indent = @indent - 139786+
emit(" }")39787+
return 139788+
end39789+
end39790+
39791+
# `hash.each_key { |k| ... }` for typed hash variants. Walks39792+
# h->order in insertion order and binds the key. Sym-keyed39793+
# variants cast back to sp_sym (order stores the underlying39794+
# mrb_int); str-keyed pass through as const char *.39795+
if mname == "each_key" && recv >= 0 && @nd_block[nid] >= 039796+
rt_ek = infer_type(recv)39797+
hv_ek = hash_iter_variant_info(rt_ek)39798+
if hv_ek != nil39799+
rc_ek = compile_expr_gc_rooted(recv)39800+
bp_ek = get_block_param(nid, 0)39801+
has_bp_ek = 139802+
if bp_ek == ""39803+
has_bp_ek = 039804+
bp_ek = "_k"39805+
end39806+
tmp_ek = new_temp39807+
emit(" for (mrb_int " + tmp_ek + " = 0; " + tmp_ek + " < " + rc_ek + "->len; " + tmp_ek + "++) {")39808+
if has_bp_ek == 139809+
if hv_ek[0] == "symbol"39810+
emit(" sp_sym lv_" + bp_ek + " = (sp_sym)" + rc_ek + "->order[" + tmp_ek + "];")39811+
else39812+
emit(" " + hv_ek[2] + " lv_" + bp_ek + " = " + rc_ek + "->order[" + tmp_ek + "];")39813+
end39814+
end3977839815
@indent = @indent + 13977939816
push_scope39780-
declare_var(bp_ev2, vtag_ev)39817+
if has_bp_ek == 139818+
declare_var(bp_ek, hv_ek[0])39819+
end39820+
redo_label_ek = push_redo_label39821+
emit_redo_label(redo_label_ek)3978139822
compile_stmts_body(@nd_body[@nd_block[nid]])39823+
pop_redo_label39824+
pop_scope39825+
@indent = @indent - 139826+
emit(" }")39827+
return 139828+
end39829+
# poly_poly_hash each_key: the helper returns nil for it (keys and39830+
# values are sp_RbVal indexed by the slot in h->order). Mirrors the39831+
# 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 = 139836+
if bp_pk == ""39837+
has_bp_pk = 039838+
bp_pk = "_k"39839+
end39840+
tmp_pk = new_temp39841+
emit(" for (mrb_int " + tmp_pk + " = 0; " + tmp_pk + " < " + rc_pk + "->len; " + tmp_pk + "++) {")39842+
if has_bp_pk == 139843+
emit(" sp_RbVal lv_" + bp_pk + " = " + rc_pk + "->keys[" + rc_pk + "->order[" + tmp_pk + "]];")39844+
end39845+
@indent = @indent + 139846+
push_scope39847+
if has_bp_pk == 139848+
declare_var(bp_pk, "poly")39849+
end39850+
redo_label_pk = push_redo_label39851+
emit_redo_label(redo_label_pk)39852+
compile_stmts_body(@nd_body[@nd_block[nid]])39853+
pop_redo_label3978239854
pop_scope3978339855
@indent = @indent - 13978439856
emit(" }")test/hash_each_key_value.rbaddedView file
Loading diff…
test/hash_each_key_value.rb.expectedaddedView file
Loading diff…