Commit 42215659

42215659e109Merged

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

Description

runtime,codegen: Hash#to_proc for int-valued sym/str hashes

h.to_proc on a sym_int_hash / str_int_hash now returns a proc that maps key -> value, so h.to_proc.call(:k) yields h[:k]. Emit a per-variant lookup fn (sp_SymIntHash_proc_fn / sp_StrIntHash_proc_fn) whose cap is the hash itself; sp_hashproc_cap_scan marks it so the hash survives as long as the proc. analyze types h.to_proc as proc for these int-valued variants (the proc-call ABI returns mrb_int).

Scoped to int-valued sym/str hashes. String/poly-valued variants and the array.map(&hash) block-pass form (a separate pre-existing gap in the &proc-to-iterator path) are out of scope.

Fixes #957

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

Details

Commit
42215659e109
Parent
c99456f66821

lib/sp_runtime.hmodifiedView file

@@ -1885,6 +1885,8 @@
18851885 non-int value, since the bare `k: v` shorthand only applies
18861886 when values are inspectable as one-liners — match CRuby). */
18871887 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;}
1888+/* Hash#to_proc lookup fn — cap is the hash, args[0] the string key. */
1889+static mrb_int sp_StrIntHash_proc_fn(void *cap, mrb_int *args) { return sp_StrIntHash_get((sp_StrIntHash *)cap, (const char *)(uintptr_t)args[0]); }
18881890 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;}
18891891 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;}
18901892 static const char*sp_IntIntHash_inspect(sp_IntIntHash*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_int_to_s(sp_IntIntHash_get(h,h->order[i])));}}sp_String_append(s,"}");return s->data;}
@@ -3492,6 +3494,11 @@
34923494 c->inner = inner;
34933495 return sp_proc_new_meta((void *)sp_proc_compose_fn, c, sp_proc_compose_scan, 1, TRUE, 1, NULL, NULL);
34943496 }
3497+/* Hash#to_proc cap-scan: the proc's `cap` field IS the source hash
3498+ (a single GC pointer), so marking it keeps the hash alive for the
3499+ proc's lifetime. The per-variant lookup fn is emitted by codegen
3500+ alongside the hash type it closes over. */
3501+static void sp_hashproc_cap_scan(void *p) { sp_gc_mark(p); }
34953502
34963503 /* ---- StringIO runtime ---- */
34973504 typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;