Commit 014ef53d

014ef53d48f1Merged

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

Description

Add poly_arr_get_hash/set_hash for PolyPolyHash integer-key access

sp_poly_arr_get/set did not dispatch on SP_BUILTIN_POLY_POLY_HASH, causing int-indexed access on poly hashes to silently return nil or no-op. Add sp_poly_arr_get_hash and sp_poly_arr_set_hash as strict supersets that also handle PolyPolyHash via sp_box_int key. Switch all codegen emit sites to the _hash variants.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
014ef53d48f1
Parent
dd47cc17ca3e

lib/sp_runtime.hmodifiedView file

@@ -3784,6 +3784,27 @@
37843784 static void sp_PolyPolyHash_set(sp_PolyPolyHash*h,sp_RbVal k,sp_RbVal v){if(h->len*2>=h->cap)sp_PolyPolyHash_grow(h);mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx]){if(sp_rbval_eql_key(h->keys[idx],k)){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->occ[idx]=TRUE;h->order[h->len]=idx;h->len++;}
37853785 static mrb_bool sp_PolyPolyHash_has_key(sp_PolyPolyHash*h,sp_RbVal k){mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx]){if(sp_rbval_eql_key(h->keys[idx],k))return TRUE;idx=(idx+1)&h->mask;}return FALSE;}
37863786 static mrb_int sp_PolyPolyHash_length(sp_PolyPolyHash*h){return h->len;}
3787+/* poly_arr_get/set for PolyPolyHash with integer index key. */
3788+static sp_RbVal sp_poly_arr_get_hash(sp_RbVal a, mrb_int i) {
3789+ if (a.tag == SP_TAG_OBJ && a.cls_id == SP_BUILTIN_POLY_POLY_HASH)
3790+ return sp_PolyPolyHash_get((sp_PolyPolyHash*)a.v.p, sp_box_int(i));
3791+ return sp_poly_arr_get(a, i);
3792+}
3793+static sp_RbVal sp_poly_arr_set_hash(sp_RbVal v, mrb_int idx, sp_RbVal val) {
3794+ if (v.tag != SP_TAG_OBJ) return val;
3795+ switch (v.cls_id) {
3796+ case SP_BUILTIN_INT_ARRAY: sp_IntArray_set((sp_IntArray*)v.v.p, idx,
3797+ val.tag == SP_TAG_INT ? val.v.i : (mrb_int)val.v.f); break;
3798+ case SP_BUILTIN_FLT_ARRAY: sp_FloatArray_set((sp_FloatArray*)v.v.p, idx,
3799+ val.tag == SP_TAG_FLT ? val.v.f : (mrb_float)val.v.i); break;
3800+ case SP_BUILTIN_STR_ARRAY: sp_StrArray_set((sp_StrArray*)v.v.p, idx,
3801+ val.tag == SP_TAG_STR ? val.v.s : NULL); break;
3802+ case SP_BUILTIN_POLY_ARRAY: sp_PolyArray_set((sp_PolyArray*)v.v.p, idx, val); break;
3803+ case SP_BUILTIN_POLY_POLY_HASH: sp_PolyPolyHash_set((sp_PolyPolyHash*)v.v.p, sp_box_int(idx), val); break;
3804+ default: break;
3805+ }
3806+ return val;
3807+}
37873808 /* poly_val[str_key] = val: runtime dispatch for poly recv `[]=` with string key. */
37883809 static sp_RbVal sp_poly_set_str(sp_RbVal v, const char *key, sp_RbVal val) {
37893810 if (v.tag != SP_TAG_OBJ) return val;