Commit 97d74410
97d744103498Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: Hash#replace on sym/str-keyed typed hashes
Clear the receiver's slots then copy each entry from `other` in its original order. Returns the receiver.
Variants wired: sym_int_hash, sym_str_hash, str_int_hash, str_str_hash. sym-keyed uses keys[i] = -1 as the empty marker; str-keyed uses NULL. Poly-value variants and the int-keyed *_int_hash family share the same shape but the inline emit is specialized per variant so the layout/empty-marker stays correct. Adding the rest is mechanical once a real callsite surfaces them.
Fixes #955
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 97d744103498
- Parent
- 13d04491ba21
spinel_codegen.rbmodifiedView file
@@ -23422,6 +23422,14 @@
2342223422
# get / delete all see the same sp_sym.2342323423
return "({ sp_sym _k = " + compile_arg0(nid) + "; mrb_int _v = sp_SymIntHash_has_key(" + rc + ", _k) ? sp_SymIntHash_get(" + rc + ", _k) : 0; sp_SymIntHash_delete(" + rc + ", _k); _v; })"2342423424
end23425+
# Hash#replace(other) — empty receiver, then copy each entry23426+
# from `other` in order. Returns the receiver.23427+
if mname == "replace"23428+
ra_arg = compile_arg0(nid)23429+
tmp_rep = new_temp23430+
emit(" { sp_SymIntHash *_d = " + rc + "; sp_SymIntHash *_s = " + ra_arg + "; for (mrb_int _ci = 0; _ci < _d->cap; _ci++) _d->keys[_ci] = -1; _d->len = 0; for (mrb_int _ri = 0; _ri < _s->len; _ri++) sp_SymIntHash_set(_d, _s->order[_ri], sp_SymIntHash_get(_s, _s->order[_ri])); }")23431+
return rc23432+
end2342523433
end2342623434
if recv_type == "sym_str_hash"2342723435
if mname == "[]"@@ -23552,6 +23560,10 @@
2355223560
if mname == "delete"2355323561
return "({ sp_sym _k = " + compile_arg0(nid) + "; const char *_v = sp_SymStrHash_has_key(" + rc + ", _k) ? sp_SymStrHash_get(" + rc + ", _k) : NULL; sp_SymStrHash_delete(" + rc + ", _k); _v; })"2355423562
end23563+
if mname == "replace"23564+
emit(" { sp_SymStrHash *_d = " + rc + "; sp_SymStrHash *_s = " + compile_arg0(nid) + "; for (mrb_int _ci = 0; _ci < _d->cap; _ci++) _d->keys[_ci] = -1; _d->len = 0; for (mrb_int _ri = 0; _ri < _s->len; _ri++) sp_SymStrHash_set(_d, _s->order[_ri], sp_SymStrHash_get(_s, _s->order[_ri])); }")23565+
return rc23566+
end2355523567
end2355623568
if recv_type == "sym_poly_hash"2355723569
if mname == "[]"@@ -24033,6 +24045,10 @@
2403324045
return tmp2403424046
end2403524047
end24048+
if mname == "replace"24049+
emit(" { sp_StrIntHash *_d = " + rc + "; sp_StrIntHash *_s = " + compile_arg0(nid) + "; for (mrb_int _ci = 0; _ci < _d->cap; _ci++) _d->keys[_ci] = NULL; _d->len = 0; for (mrb_int _ri = 0; _ri < _s->len; _ri++) sp_StrIntHash_set(_d, _s->order[_ri], sp_StrIntHash_get(_s, _s->order[_ri])); }")24050+
return rc24051+
end2403624052
end2403724053
if recv_type == "int_int_hash"2403824054
if mname == "[]"@@ -24230,6 +24246,10 @@
2423024246
if mname == "default="2423124247
return "(" + rc + "->default_v = " + compile_expr_as_string(get_args(@nd_arguments[nid])[0]) + ")"2423224248
end24249+
if mname == "replace"24250+
emit(" { sp_StrStrHash *_d = " + rc + "; sp_StrStrHash *_s = " + compile_arg0(nid) + "; for (mrb_int _ci = 0; _ci < _d->cap; _ci++) _d->keys[_ci] = NULL; _d->len = 0; for (mrb_int _ri = 0; _ri < _s->len; _ri++) sp_StrStrHash_set(_d, _s->order[_ri], sp_StrStrHash_get(_s, _s->order[_ri])); }")24251+
return rc24252+
end2423324253
end2423424254
""2423524255
endtest/hash_replace_variants.rbaddedView file
Loading diff…
test/hash_replace_variants.rb.expectedaddedView file
Loading diff…