Commit 77a68413

77a68413b2c6Merged

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

Description

runtime,analyze,codegen: Hash.new {} default proc

Hash.new { |hash, key| ... } ignored the block and returned 0 on a missing key. Lower the block to a dedicated C fn sp_RbVal (*)(sp_StrPolyHash *, const char *) with typed params (hash: str_poly_hash, key: string) so the body's use of key as a string compiles correctly — sidestepping both the generic proc-param-int-typing wall and the sp_proc_call mrb_int return ABI.

Hash.new{} now maps to the existing str_poly_hash (sym keys are stringified, values poly); the fn pointer is stored in a new dproc field and sp_StrPolyHash_get calls it on a miss. Memoization (h[k] = v alongside the proc) and int/string block returns all work.

Limitation: a block that captures outer locals can't become a top-level fn, so it falls back to a plain hash (missing key → nil) rather than emitting a broken capture reference.

Fixes #912

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

Details

Commit
77a68413b2c6
Parent
fa1564e40f3b

lib/sp_runtime.hmodifiedView file

@@ -3127,13 +3127,19 @@
31273127 }
31283128
31293129 /* StrPolyHash: string keys, sp_RbVal values — for hashes with mixed value types. */
3130-typedef struct{const char**keys;sp_RbVal*vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;sp_RbVal default_v;}sp_StrPolyHash;
3130+/* `dproc` holds a Hash.new{} default block, lowered to a dedicated C
3131+ fn `sp_RbVal (*)(sp_StrPolyHash *self, const char *key)` with typed
3132+ params (codegen emits it). Called by _get on a miss. Issue #912. */
3133+typedef struct sp_StrPolyHash sp_StrPolyHash;
3134+typedef sp_RbVal (*sp_strpoly_dproc_t)(sp_StrPolyHash *, const char *);
3135+struct sp_StrPolyHash{const char**keys;sp_RbVal*vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;sp_RbVal default_v;sp_strpoly_dproc_t dproc;};
31313136 static void sp_StrPolyHash_fin(void*p){sp_StrPolyHash*h=(sp_StrPolyHash*)p;free(h->keys);free(h->vals);free(h->order);}
31323137 static void sp_StrPolyHash_scan(void*p){sp_StrPolyHash*h=(sp_StrPolyHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i]){sp_mark_string(h->keys[i]);sp_mark_rbval(h->vals[i]);}}sp_mark_rbval(h->default_v);}
31333138 static sp_StrPolyHash*sp_StrPolyHash_new(void){sp_StrPolyHash*h=(sp_StrPolyHash*)sp_gc_alloc(sizeof(sp_StrPolyHash),sp_StrPolyHash_fin,sp_StrPolyHash_scan);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;h->default_v=sp_box_nil();return h;}
31343139 static sp_StrPolyHash*sp_StrPolyHash_new_with_default(sp_RbVal d){sp_StrPolyHash*h=sp_StrPolyHash_new();h->default_v=d;return h;}
3140+static sp_StrPolyHash*sp_StrPolyHash_new_dproc(sp_strpoly_dproc_t fn){sp_StrPolyHash*h=sp_StrPolyHash_new();h->dproc=fn;return h;}
31353141 static void sp_StrPolyHash_grow(sp_StrPolyHash*h){mrb_int oc=h->cap;const char**ok=h->keys;sp_RbVal*ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}
3136-static sp_RbVal sp_StrPolyHash_get(sp_StrPolyHash*h,const char*k){if(!h)return sp_box_nil();mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}return h->default_v;}
3142+static sp_RbVal sp_StrPolyHash_get(sp_StrPolyHash*h,const char*k){if(!h)return sp_box_nil();mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}if(h->dproc)return h->dproc(h,k);return h->default_v;}
31373143 static void sp_StrPolyHash_set(sp_StrPolyHash*h,const char*k,sp_RbVal v){if(h->len*2>=h->cap)sp_StrPolyHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}
31383144 static mrb_bool sp_StrPolyHash_has_key(sp_StrPolyHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}
31393145 static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}