Commit 1f58f1a9

1f58f1a9deccMerged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

fix(codegen+runtime): FloatArray#shift was silently a no-op

Array#shift works on IntArray, StrArray, SymArray, and FloatArray already supports pop — but the FloatArray method handler in spinel_codegen.rb had no shift arm, so a [1.5, ...].shift call fell through to the unknown-method 0 fallback. The codegen emitted a warning: cannot resolve call to 'shift' on float_array (emitting 0), the call evaluated to the literal 0, the array was never modified, and a while arr.length > 0; arr.shift; end loop became infinite (length stays at 4, shift keeps returning 0).

Three small additions parallel to what already exists for pop / the other typed arrays:

  • Runtime helper sp_FloatArray_shift in lib/sp_runtime.h, alongside sp_FloatArray_pop. Removes data[0], shifts the rest down, decrements len, returns the removed value (or 0.0 on empty).
  • Codegen dispatch arm in the FloatArray method handler that emits sp_FloatArray_shift(rc).
  • infer_call_type entry so arr.shift on float_array is typed as float (not the int default), matching the existing int_array, str_array, sym_array arms.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
1f58f1a9decc
Parent
a09a6b8b339b

lib/sp_runtime.hmodifiedView file

@@ -272,6 +272,7 @@
272272 static mrb_float sp_FloatArray_sum(sp_FloatArray*a){mrb_float s=0;for(mrb_int i=0;i<a->len;i++)s+=a->data[i];return s;}
273273 static void sp_FloatArray_replace(sp_FloatArray*dst,sp_FloatArray*src){dst->len=0;if(src->len>dst->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)dst-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*dst->cap;h->size-=sizeof(mrb_float)*dst->cap;void*nd=realloc(dst->data,sizeof(mrb_float)*src->len);if(!nd){perror("realloc");exit(1);}dst->data=(mrb_float*)nd;dst->cap=src->len;h->size+=sizeof(mrb_float)*dst->cap;sp_gc_bytes+=sizeof(mrb_float)*dst->cap;}memcpy(dst->data,src->data,sizeof(mrb_float)*src->len);dst->len=src->len;}
274274 static inline mrb_float sp_FloatArray_pop(sp_FloatArray*a){return a->data[--a->len];}
275+static inline mrb_float sp_FloatArray_shift(sp_FloatArray*a){if(a->len==0)return 0.0;mrb_float v=a->data[0];for(mrb_int i=0;i+1<a->len;i++)a->data[i]=a->data[i+1];a->len--;return v;}
275276 static inline mrb_int sp_FloatArray_length(sp_FloatArray*a){return a->len;}
276277 static inline mrb_bool sp_FloatArray_empty(sp_FloatArray*a){return a->len==0;}
277278 static inline mrb_float sp_FloatArray_get(sp_FloatArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}