Commit c00129fb

c00129fb33c2Merged

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

Description

fix(runtime,codegen,analyze): Hash#to_a returns poly_array of pairs (#738 partial)

Hash#to_a in CRuby returns Array of [k, v] pairs. spinel's typed hash variants can't carry mixed key/value types in a single typed array, so represent each pair as a poly_array of 2 elements and the outer as poly_array of poly_arrays.

Runtime helpers added for 5 variants (sym_int_hash via emit_raw, str_int / str_str / int_str via sp_runtime.h, sym_str via emit_raw). Each builds a fresh outer sp_PolyArray, pushes a 2-element sp_PolyArray per entry, each containing the boxed key

  • boxed value via the per-type sp_box_* helpers.

Replaced the previous to_a analyze arm that returned a bogus tuple:string,int_ptr_array type with the new poly_array path covering all hash variants via is_hash_type(rt).

invert is the other half of #738; tracking in a follow-up commit.

Refs #738

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
c00129fb33c2
Parent
b2d2d56e9773

lib/sp_runtime.hmodifiedView file

@@ -2052,6 +2052,10 @@
20522052 static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }
20532053 /* Array#compact for poly_array: keep elements whose tag is not SP_TAG_NIL. */
20542054 static sp_PolyArray *sp_PolyArray_compact(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); if (!a) return b; for (mrb_int i = 0; i < a->len; i++) { if (a->data[i].tag != SP_TAG_NIL) sp_PolyArray_push(b, a->data[i]); } return b; }
2055+/* Issue #738: Hash#to_a as poly_array of [key, value] poly_array pairs. */
2056+static sp_PolyArray*sp_StrIntHash_to_a(sp_StrIntHash*h){sp_PolyArray*r=sp_PolyArray_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++){sp_PolyArray*p=sp_PolyArray_new();sp_PolyArray_push(p,sp_box_str(h->order[i]));sp_PolyArray_push(p,sp_box_int(sp_StrIntHash_get(h,h->order[i])));sp_PolyArray_push(r,sp_box_poly_array(p));}return r;}
2057+static sp_PolyArray*sp_StrStrHash_to_a(sp_StrStrHash*h){sp_PolyArray*r=sp_PolyArray_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++){sp_PolyArray*p=sp_PolyArray_new();sp_PolyArray_push(p,sp_box_str(h->order[i]));sp_PolyArray_push(p,sp_box_str(sp_StrStrHash_get(h,h->order[i])));sp_PolyArray_push(r,sp_box_poly_array(p));}return r;}
2058+static sp_PolyArray*sp_IntStrHash_to_a(sp_IntStrHash*h){sp_PolyArray*r=sp_PolyArray_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++){sp_PolyArray*p=sp_PolyArray_new();sp_PolyArray_push(p,sp_box_int(h->order[i]));sp_PolyArray_push(p,sp_box_str(sp_IntStrHash_get(h,h->order[i])));sp_PolyArray_push(r,sp_box_poly_array(p));}return r;}
20552059 /* Array#flatten -- walk into nested array values recursively. Each
20562060 array-tagged element (IntArray / StrArray / SymArray / FloatArray /
20572061 PolyArray) is expanded inline; scalars are appended as-is. Issue