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:
-
lib/sp_runtime.h: newsp_SymPolyHash_merge(a, b)that allocates a fresh hash, copiesa's entries first, then setsb's entries (sob's values overridea's). Mirrors the existingsp_StrIntHash_mergeshape. -
spinel_codegen.rb(compile_hash_method_expr): dispatchsym_poly_hash#mergeto the new runtime helper when the argument is staticallysym_poly_hash. When the argument is staticallypolybut the runtime value is a SymPolyHash (typical when anis_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). -
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 apoly_local[:k]read on a hash-typed runtime value returned nil. Now dispatches tosp_SymPolyHash_getwhencls_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
static mrb_int sp_SymPolyHash_length(sp_SymPolyHash*h){return h->len;}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;}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;}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;}/* PolyPolyHash: heterogeneous keys + values (both sp_RbVal). For primitives the hash/eql is tag-based (value equality); for OBJ tagspinel_codegen.rbmodifiedView file
Loading diff…
test/sym_poly_hash_merge.rbaddedView file
Loading diff…
test/sym_poly_hash_merge.rb.expectedaddedView file
Loading diff…