Commit d0938bda
d0938bda3d92Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
analyze,codegen,runtime: Float#step / Integer#step no-block returns typed array
1.0.step(3.0, 0.5).to_a.inspect SEGV'd because step without a
block fell through to the generic int dispatch (warning, emit 0),
then .to_a tried to chase that int as a pointer. CRuby returns
an Enumerator; spinel materialises the result directly into a
FloatArray/IntArray since .to_a is the dominant chain.
- analyze: infer_call_type returns float_array for int/float receiver when any operand is float, else int_array.
- codegen: add no-block step arms in compile_int_method_expr and compile_float_method_expr emitting sp_IntArray_from_range_step or sp_FloatArray_from_step.
- runtime: sp_FloatArray_from_step (s, e, k) walks until v crosses e in the direction of k; k==0 yields empty.
- codegen: float_array gets a
to_aarm so the chain doesn't fall through to unresolved-call.
Block form unchanged; negative-step int block iteration is a separate pre-existing limitation.
Fixes #964
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- d0938bda3d92
- Parent
- 860bad77bae8
lib/sp_runtime.hmodifiedView file
@@ -1286,6 +1286,9 @@
12861286
static void sp_FloatArray_fin(void*p){sp_FloatArray*a=(sp_FloatArray*)p;sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;free(a->data);}12871287
static sp_FloatArray*sp_FloatArray_new(void){sp_FloatArray*a=(sp_FloatArray*)sp_gc_alloc(sizeof(sp_FloatArray),sp_FloatArray_fin,NULL);a->cap=16;a->data=(mrb_float*)malloc(sizeof(mrb_float)*a->cap);if(!a->data)sp_oom_die();a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_float)*a->cap;sp_gc_bytes+=sizeof(mrb_float)*a->cap;}return a;}12881288
static inline void sp_FloatArray_push(sp_FloatArray*a,mrb_float v){if(a->len>=a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*a->cap;h->size-=sizeof(mrb_float)*a->cap;a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);h->size+=sizeof(mrb_float)*a->cap;sp_gc_bytes+=sizeof(mrb_float)*a->cap;}a->data[a->len++]=v;}1289+
/* Float#step materialised as a FloatArray. Direction follows the1290+
sign of k; k==0 yields an empty array to avoid an infinite loop. */1291+
static sp_FloatArray*sp_FloatArray_from_step(mrb_float s,mrb_float e,mrb_float k){sp_FloatArray*a=sp_FloatArray_new();if(k==0.0)return a;mrb_float v=s;if(k>0){while(v<=e){sp_FloatArray_push(a,v);v+=k;}}else{while(v>=e){sp_FloatArray_push(a,v);v+=k;}}return a;}12891292
static mrb_float sp_FloatArray_min(sp_FloatArray*a){if(a->len==0)return 0;mrb_float m=a->data[0];for(mrb_int i=1;i<a->len;i++)if(a->data[i]<m)m=a->data[i];return m;}12901293
static mrb_float sp_FloatArray_max(sp_FloatArray*a){if(a->len==0)return 0;mrb_float m=a->data[0];for(mrb_int i=1;i<a->len;i++)if(a->data[i]>m)m=a->data[i];return m;}12911294
static mrb_float sp_FloatArray_sum(sp_FloatArray*a,mrb_float init){mrb_float s=init;for(mrb_int i=0;i<a->len;i++)s+=a->data[i];return s;}spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/float_step_to_a.rbaddedView file
Loading diff…
test/float_step_to_a.rb.expectedaddedView file
Loading diff…