Commit b1f7cf6a

b1f7cf6aa4d0Merged

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

Description

feat(codegen+runtime): Array#slice!(from, n) for every typed array

infer_method_name_type had no slice! branch (the result type fell through to int), and compile_array_dispatch had no codegen branch — so a typed arr.slice!(from, n) returned 0 and no mutation happened, regardless of the receiver's element type.

Add the type rule (mirror of slice — same element type as the receiver) and a codegen branch that picks the right runtime helper per receiver type:

  • IntArray → sp_IntArray_slice_bang (uses start for O(1) head peel)
  • SymArray → sp_IntArray_slice_bang (symbols stored as int IDs)
  • FloatArray → sp_FloatArray_slice_bang
  • StrArray → sp_StrArray_slice_bang
  • PtrArray → sp_PtrArray_slice_bang (preserves scan_elem)
  • PolyArray → sp_PolyArray_slice_bang

Each helper returns a fresh array of n elements starting at from and removes them from the receiver. The non-IntArray helpers shift the tail down to fill the hole (no start field to advance).

Originally driven by optcarrot's ROM.parse_header peeling the 16-byte iNES header off an IntArray byte buffer; covering the other element types in the same PR avoids leaving lopsided support behind.

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

Details

Commit
b1f7cf6aa4d0
Parent
d920667f4cc7

lib/sp_runtime.hmodifiedView file

@@ -694,6 +694,18 @@
694694 static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }
695695 static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }
696696 static sp_PolyArray *sp_PolyArray_slice(sp_PolyArray *a, mrb_int start, mrb_int len) { if (start < 0) start += a->len; if (start < 0) start = 0; sp_PolyArray *b = sp_PolyArray_new(); if (start >= a->len || len <= 0) return b; if (start + len > a->len) len = a->len - start; for (mrb_int i = 0; i < len; i++) sp_PolyArray_push(b, a->data[start + i]); return b; }
697+static sp_PolyArray *sp_PolyArray_slice_bang(sp_PolyArray *a, mrb_int from, mrb_int n) {
698+ if (from < 0) from += a->len;
699+ if (from < 0) from = 0;
700+ if (from > a->len) from = a->len;
701+ if (n < 0) n = 0;
702+ if (from + n > a->len) n = a->len - from;
703+ sp_PolyArray *r = sp_PolyArray_new();
704+ for (mrb_int i = 0; i < n; i++) sp_PolyArray_push(r, a->data[from + i]);
705+ for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];
706+ a->len -= n;
707+ return r;
708+}
697709 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; }
698710 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; } }
699711 static void sp_PolyArray_rotate_bang(sp_PolyArray*a,mrb_int n){if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;sp_RbVal*tmp=(sp_RbVal*)malloc(sizeof(sp_RbVal)*a->len);for(mrb_int i=0;i<a->len;i++)tmp[i]=a->data[(i+n)%a->len];for(mrb_int i=0;i<a->len;i++)a->data[i]=tmp[i];free(tmp);}
@@ -783,6 +795,64 @@
783795 static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }
784796 static const char *sp_file_basename(const char *path) { const char *s = strrchr(path, '/'); if (s) return s + 1; return path; }
785797
798+/* `arr.slice!(from, n)` — returns a fresh array of `n` elements
799+ starting at `from` and removes them from `a`. IntArray uses its
800+ `start` field for an O(1) head peel (from == 0); the others
801+ shift the tail down to fill the hole. */
802+static sp_IntArray *sp_IntArray_slice_bang(sp_IntArray *a, mrb_int from, mrb_int n) {
803+ if (from < 0) from += a->len;
804+ if (from < 0) from = 0;
805+ if (from > a->len) from = a->len;
806+ if (n < 0) n = 0;
807+ if (from + n > a->len) n = a->len - from;
808+ sp_IntArray *r = sp_IntArray_new();
809+ for (mrb_int i = 0; i < n; i++) sp_IntArray_push(r, a->data[a->start + from + i]);
810+ if (from == 0) {
811+ a->start += n;
812+ a->len -= n;
813+ } else {
814+ for (mrb_int i = from; i + n < a->len; i++) a->data[a->start + i] = a->data[a->start + i + n];
815+ a->len -= n;
816+ }
817+ return r;
818+}
819+static sp_FloatArray *sp_FloatArray_slice_bang(sp_FloatArray *a, mrb_int from, mrb_int n) {
820+ if (from < 0) from += a->len;
821+ if (from < 0) from = 0;
822+ if (from > a->len) from = a->len;
823+ if (n < 0) n = 0;
824+ if (from + n > a->len) n = a->len - from;
825+ sp_FloatArray *r = sp_FloatArray_new();
826+ for (mrb_int i = 0; i < n; i++) sp_FloatArray_push(r, a->data[from + i]);
827+ for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];
828+ a->len -= n;
829+ return r;
830+}
831+static sp_StrArray *sp_StrArray_slice_bang(sp_StrArray *a, mrb_int from, mrb_int n) {
832+ if (from < 0) from += a->len;
833+ if (from < 0) from = 0;
834+ if (from > a->len) from = a->len;
835+ if (n < 0) n = 0;
836+ if (from + n > a->len) n = a->len - from;
837+ sp_StrArray *r = sp_StrArray_new();
838+ for (mrb_int i = 0; i < n; i++) sp_StrArray_push(r, a->data[from + i]);
839+ for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];
840+ a->len -= n;
841+ return r;
842+}
843+static sp_PtrArray *sp_PtrArray_slice_bang(sp_PtrArray *a, mrb_int from, mrb_int n) {
844+ if (from < 0) from += a->len;
845+ if (from < 0) from = 0;
846+ if (from > a->len) from = a->len;
847+ if (n < 0) n = 0;
848+ if (from + n > a->len) n = a->len - from;
849+ sp_PtrArray *r = sp_PtrArray_new_scan(a->scan_elem);
850+ for (mrb_int i = 0; i < n; i++) sp_PtrArray_push(r, a->data[from + i]);
851+ for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];
852+ a->len -= n;
853+ return r;
854+}
855+
786856 typedef struct sp_Proc { void *fn; void *cap; void (*cap_scan)(void *); } sp_Proc;
787857 static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }
788858 static sp_Proc *sp_proc_new(void *fn, void *cap, void (*cap_scan)(void *)) { sp_Proc *p = (sp_Proc *)sp_gc_alloc(sizeof(sp_Proc), NULL, sp_Proc_scan); p->fn = fn; p->cap = cap; p->cap_scan = cap_scan; return p; }