Commit 87090924

87090924f784Merged

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

Description

fix(codegen): extend Array#shuffle to ptr_array

shuffle / shuffle! only matched the typed-array list (after the poly_array follow-ups: int/str/float/sym/poly). For an obj_<C>_ptr_array receiver the dispatcher fell through, the result temp ended up 0, and the runtime didn't have a PtrArray shuffle helper to call anyway:

class Bar def initialize(x); @x = x; end attr_accessor :x end puts [Bar.new(1), Bar.new(2), Bar.new(3)].shuffle.length # ruby: 3, spinel: "" (lv_arr=0)

Follow-up to the introduction of ptr_array, mirroring the per-method poly_array fixes: each Array method that touches the type dispatch needs to be re-checked for the new type. shuffle was missed for ptr_array.

Add sp_PtrArray_dup (preserving the source's scan_elem callback so GC tracking stays correct) and sp_PtrArray_shuffle{_bang} to the runtime, and extend the dispatcher to accept ptr_array.

Adds test/array_shuffle_ptr.rb.

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

Details

Commit
87090924f784
Parent
7a35565ca0c1

lib/sp_runtime.hmodifiedView file

@@ -251,6 +251,9 @@
251251 static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}
252252 static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}
253253 static void sp_PtrArray_reverse_bang(sp_PtrArray*a){for(mrb_int i=0,j=a->len-1;i<j;i++,j--){void*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}
254+static sp_PtrArray*sp_PtrArray_dup(sp_PtrArray*a){sp_PtrArray*b=sp_PtrArray_new_scan(a->scan_elem);for(mrb_int i=0;i<a->len;i++)sp_PtrArray_push(b,a->data[i]);return b;}
255+static void sp_PtrArray_shuffle_bang(sp_PtrArray*a){for(mrb_int i=a->len-1;i>0;i--){mrb_int j=(mrb_int)(rand()%(i+1));void*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}
256+static sp_PtrArray*sp_PtrArray_shuffle(sp_PtrArray*a){sp_PtrArray*b=sp_PtrArray_dup(a);sp_PtrArray_shuffle_bang(b);return b;}
254257
255258 /* Small-array optimization: keep the first SP_STRARR_INLINE elements
256259 * inside the struct so empty/short StrArrays skip the data malloc.