Commit efb61dee
efb61dee8240Merged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix(codegen): extend Array#shuffle to sym_array and poly_array
shuffle / shuffle! only matched int_array | str_array | float_array.
A sym_array fell through to the bitwise/pointer fallback and the
result temp ended up 0; a poly_array had no runtime helper at all.
puts [:a, :b, :c, :d].shuffle.length # 4 in Ruby, "" in Spinel puts [1, "a", :s, 2.0].shuffle.length # 4 in Ruby, "" in Spinel
Follow-up to the introduction of poly_array (Array#inspect for typed arrays, Implement inspect for heterogeneous arrays): each Array
method that touches the type dispatch needs to be re-checked for the
new type. shuffle was missed.
Add sp_PolyArray_dup, sp_PolyArray_shuffle{_bang} to the runtime
and extend the dispatcher to accept sym_array (uses sp_IntArray_*
internally) and poly_array.
Adds test/array_shuffle_sym_poly.rb.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- efb61dee8240
- Parent
- 5c9ac0cedf10
lib/sp_runtime.hmodifiedView file
static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (a->len >= a->cap) { a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); } a->data[a->len++] = v; }static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }static void sp_PolyArray_shuffle_bang(sp_PolyArray *a) { for (mrb_int i = a->len - 1; i > 0; i--) { mrb_int j = (mrb_int)(rand() % (i + 1)); sp_RbVal t = a->data[i]; a->data[i] = a->data[j]; a->data[j] = t; } }static sp_PolyArray *sp_PolyArray_shuffle(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_shuffle_bang(b); return b; }/* Object#inspect for a tagged sp_RbVal. Dispatches on the runtime tag; each branch reuses the matching primitive inspect helper. Falls backspinel_codegen.rbmodifiedView file
Loading diff…
test/array_shuffle_sym_poly.rbaddedView file
Loading diff…