Commit 5e916b66
5e916b66b8e4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(runtime): *StrHash _get returns NULL for missing keys (Phase 2)
Ruby semantics: h[missing_key] returns nil. Previously
sp_StrStrHash_get / sp_IntStrHash_get / sp_SymStrHash_get returned
sp_str_empty ("") for missing keys with no Hash.new(default), making
if h[:missing]; ...; end falsely truthy in spinel.
Change the miss fallback from h->default_v ? h->default_v : sp_str_empty
to h->default_v (which is NULL when not set via Hash.new(default)).
Internal callers (values/dup/eq/inspect/merge/update/invert/keys)
iterate h->order[i] which holds only existing keys, so they never
trigger the miss path and stay unchanged.
User-visible call paths:
puts h[missing]: emit is NULL-safe (else putchar('\n'))if h[missing]; ...: NULL is falsy in C, matching Ruby's nilh[missing] + "x": sp_str_concat is NULL-safe (coalesce to empty)h[missing].length: sp_str_length(NULL) returns 0h[missing] == "": sp_str_eq(NULL, "") returns 0 (matches Ruby's nil != "")Hash.new("default"): still returns default (h->default_v preserved)
Test: str_hash_missing_returns_nil.rb covers all three variants + Hash.new(default) + h.default= setter paths.
Phase 2 of docs/HASH-NULLABLE.md rollout.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 5e916b66b8e4
- Parent
- 04174a708e87
lib/sp_runtime.hmodifiedView file
static sp_StrStrHash*sp_StrStrHash_new(void){sp_StrStrHash*h=(sp_StrStrHash*)sp_gc_alloc(sizeof(sp_StrStrHash),sp_StrStrHash_fin,sp_StrStrHash_scan);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;h->default_v=NULL;return h;}static sp_StrStrHash*sp_StrStrHash_new_with_default(const char*d){sp_StrStrHash*h=sp_StrStrHash_new();h->default_v=d;return h;}static void sp_StrStrHash_grow(sp_StrStrHash*h){mrb_int oc=h->cap;const char**ok=h->keys;const char**ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));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);}static const char*sp_StrStrHash_get(sp_StrStrHash*h,const char*k){if(!h)return sp_str_empty;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?h->default_v:sp_str_empty;}/* Returns NULL for missing keys (Ruby `h[missing] == nil` semantics). Internal callers (values/dup/eq/...) read via h->order[i] which is always an existing key, so they never see the miss path. */static const char*sp_StrStrHash_get(sp_StrStrHash*h,const char*k){if(!h)return NULL;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;}static void sp_StrStrHash_set(sp_StrStrHash*h,const char*k,const char*v){if(h->len*2>=h->cap)sp_StrStrHash_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++;}static mrb_bool sp_StrStrHash_has_key(sp_StrStrHash*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;}static mrb_int sp_StrStrHash_length(sp_StrStrHash*h){return h->len;}static inline mrb_int _sp_istr_idx(mrb_int mask,mrb_int k){return(mrb_int)(((uint64_t)(unsigned long long)k*11400714819323198485ULL)&(uint64_t)mask);}static void sp_IntStrHash_grow(sp_IntStrHash*h){mrb_int oc=h->cap,ol=h->len;mrb_int*ok=h->keys;const char**ov=h->vals;mrb_bool*ou=h->used;mrb_int*oo=h->order;h->cap*=2;h->mask=h->cap-1;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=ol;for(mrb_int i=0;i<oc;i++){if(!ou[i])continue;mrb_int k=ok[i];const char*v=ov[i];mrb_int di=_sp_istr_idx(h->mask,k);while(h->used[di])di=(di+1)&h->mask;h->used[di]=TRUE;h->keys[di]=k;h->vals[di]=v;}for(mrb_int i=0;i<ol;i++)h->order[i]=oo[i];free(ok);free(ov);free(ou);free(oo);}static void sp_IntStrHash_set(sp_IntStrHash*h,mrb_int k,const char*v){if(h->len*2>=h->cap)sp_IntStrHash_grow(h);mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->used[idx]=TRUE;h->keys[idx]=k;h->vals[idx]=v;h->order[h->len++]=k;}static const char*sp_IntStrHash_get(sp_IntStrHash*h,mrb_int k){if(!h)return sp_str_empty;mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return h->default_v?h->default_v:sp_str_empty;}/* Returns NULL for missing keys (Ruby `h[missing] == nil` semantics). */static const char*sp_IntStrHash_get(sp_IntStrHash*h,mrb_int k){if(!h)return NULL;mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return h->default_v;}static mrb_bool sp_IntStrHash_has_key(sp_IntStrHash*h,mrb_int k){mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}static mrb_int sp_IntStrHash_length(sp_IntStrHash*h){return h->len;}static sp_IntArray*sp_IntStrHash_keys(sp_IntStrHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,h->order[i]);return a;}spinel_codegen.rbmodifiedView file
Loading diff…
test/str_hash_missing_returns_nil.expectedaddedView file
Loading diff…
test/str_hash_missing_returns_nil.rbaddedView file
Loading diff…