Commit cddabab4

cddabab4d828Merged

Yusuke Endoh <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

codegen+runtime: add Hash#merge for sym_poly_hash

Spinel had no dispatcher for non-mutating Hash#merge when the receiver is a sym_poly_hash (Symbol-keyed, heterogeneously valued). The call previously fell through to a fallback that emitted an empty hash, so every subsequent merged[:key] lookup returned nil — silently losing data.

Add three pieces:

  1. lib/sp_runtime.h: new sp_SymPolyHash_merge(a, b) that allocates a fresh hash, copies a's entries first, then sets b's entries (so b's values override a's). Mirrors the existing sp_StrIntHash_merge shape.

  2. spinel_codegen.rb (compile_hash_method_expr): dispatch sym_poly_hash#merge to the new runtime helper when the argument is statically sym_poly_hash. When the argument is statically poly but the runtime value is a SymPolyHash (typical when an is_a? branch widened the static type), emit a cls_id-guarded unbox and call. The guard avoids treating other Sym-keyed hash variants (sym_int_hash / sym_str_hash) as SymPolyHash — those share Sym keys but have different value-array layouts; if the runtime arg isn't a SymPolyHash the override is silently dropped (consistent with the pre-existing behaviour for unsupported argument types).

  3. spinel_codegen.rb (emit_poly_builtin_dispatch): symbol-key [] arm for poly receivers that carry a SymPolyHash at runtime. The int-key arm already handled IntArray / StrArray / Method dispatch; the symbol-key arm was missing, so a poly_local[:k] read on a hash-typed runtime value returned nil. Now dispatches to sp_SymPolyHash_get when cls_id == SP_BUILTIN_SYM_POLY_HASH.

Test in test/sym_poly_hash_merge.rb covers both code paths.

Details

Commit
cddabab4d828
Parent
9153a62f417c

lib/sp_runtime.hmodifiedView file

@@ -1047,6 +1047,7 @@
10471047 static mrb_int sp_SymPolyHash_length(sp_SymPolyHash*h){return h->len;}
10481048 static sp_IntArray*sp_SymPolyHash_keys(sp_SymPolyHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,(mrb_int)h->order[i]);return a;}
10491049 static sp_PolyArray*sp_SymPolyHash_values(sp_SymPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_SymPolyHash_get(h,h->order[i]));return a;}
1050+static sp_SymPolyHash*sp_SymPolyHash_merge(sp_SymPolyHash*a,sp_SymPolyHash*b){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_SymPolyHash_get(b,b->order[i]));return r;}
10501051
10511052 /* PolyPolyHash: heterogeneous keys + values (both sp_RbVal). For
10521053 primitives the hash/eql is tag-based (value equality); for OBJ tag