Commit 22c0a270
22c0a2700010Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
runtime,codegen: typed-hash #inspect / #each_value for non-poly variants
Hash#inspect dispatch only reached the sym_int_hash arm; every other typed hash hit "cannot resolve call to 'inspect'". Route through compile_inspect_for and add inspect helpers for str_int_hash, str_str_hash, sym_str_hash, int_str_hash, and str_poly_hash.
Hash#each_value previously only handled poly_poly_hash. Extend to the five common int/string-value variants.
Refs #851
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 22c0a2700010
- Parent
- 0b35f2a667b9
lib/sp_runtime.hmodifiedView file
@@ -1794,6 +1794,14 @@
17941794
/* PtrArray elements are object pointers without a per-element class17951795
tag, so we render them as `#<Object>` rather than recursing. */17961796
static const char*sp_PtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,"#<Object>");}sp_String_append(s,"]");return s->data;}1797+
/* Issue #851: Hash#inspect for typed-hash variants beyond1798+
sym_int_hash. Renders Ruby's `{"k"=>v, ...}` (string keys),1799+
`{42=>"v", ...}` (int keys), or `{:k=>v, ...}` (sym keys but1800+
non-int value, since the bare `k: v` shorthand only applies1801+
when values are inspectable as one-liners — match CRuby). */1802+
static const char*sp_StrIntHash_inspect(sp_StrIntHash*h){sp_String*s=sp_String_new("{");if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(h->order[i]));sp_String_append(s,"=>");sp_String_append(s,sp_int_to_s(sp_StrIntHash_get(h,h->order[i])));}}sp_String_append(s,"}");return s->data;}1803+
static const char*sp_StrStrHash_inspect(sp_StrStrHash*h){sp_String*s=sp_String_new("{");if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(h->order[i]));sp_String_append(s,"=>");sp_String_append(s,sp_str_inspect(sp_StrStrHash_get(h,h->order[i])));}}sp_String_append(s,"}");return s->data;}1804+
static const char*sp_IntStrHash_inspect(sp_IntStrHash*h){sp_String*s=sp_String_new("{");if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_int_to_s(h->order[i]));sp_String_append(s,"=>");sp_String_append(s,sp_str_inspect(sp_IntStrHash_get(h,h->order[i])));}}sp_String_append(s,"}");return s->data;}17971805
/* Nested-array inspect: when codegen knows the ptr_array's element17981806
type is one of the four built-in T_array shapes, recurse into the17991807
matching primitive inspect . */@@ -2657,6 +2665,8 @@
26572665
static sp_StrPolyHash*sp_StrPolyHash_merge(sp_StrPolyHash*a,sp_StrPolyHash*b){sp_StrPolyHash*r=sp_StrPolyHash_new();r->default_v=a->default_v;for(mrb_int i=0;i<a->len;i++)sp_StrPolyHash_set(r,a->order[i],sp_StrPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_StrPolyHash_set(r,b->order[i],sp_StrPolyHash_get(b,b->order[i]));return r;}26582666
static sp_StrPolyHash*sp_StrPolyHash_dup(sp_StrPolyHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_StrPolyHash_set(r,h->order[i],sp_StrPolyHash_get(h,h->order[i]));return r;}26592667
static mrb_bool sp_StrPolyHash_eq(sp_StrPolyHash*a,sp_StrPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){const char*k=a->order[i];if(!sp_StrPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_StrPolyHash_get(a,k),sp_StrPolyHash_get(b,k)))return FALSE;}return TRUE;}2668+
/* Issue #851: inspect for str_poly_hash. */2669+
static const char*sp_StrPolyHash_inspect(sp_StrPolyHash*h){sp_String*s=sp_String_new("{");if(h){for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(h->order[i]));sp_String_append(s,"=>");sp_String_append(s,sp_poly_inspect(sp_StrPolyHash_get(h,h->order[i])));}}sp_String_append(s,"}");return s->data;}26602670
/* Convert a narrower StrStrHash to a StrPolyHash. Needed when the26612671
analyzer widens an LV slot to sp_StrPolyHash* (e.g. later poly-value26622672
writes) but the initial RHS is a sibling narrower hash variant —spinel_codegen.rbmodifiedView file
Loading diff…
test/typed_hash_inspect_and_each_value.rbaddedView file
Loading diff…
test/typed_hash_inspect_and_each_value.rb.expectedaddedView file
Loading diff…