Commit 678580f9

678580f9611cMerged

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

Description

fix(runtime): sp_poly_shl dispatches Array#<< push by recv cls_id

<< on a poly recv used to always lower to sp_poly_shl, which was Integer-bit-shift only. An IntArray / PolyArray / PtrArray boxed into a poly slot (e.g. an ivar widened to poly because it received both nil and an array) had its << push silently turn into a bit-shift of the encoded pointer/length, dropping the rhs.

The fix adds a runtime cls_id dispatch in sp_poly_shl: each typed array kind invokes its matching push helper (sp_IntArray_push, sp_PolyArray_push, sp_PtrArray_push, sp_FloatArray_push, sp_StrArray_push). Falls through to bit-shift only when the recv is a non-array (genuine Integer << int).

Forward-declared so the existing sp_PolyArray_push definition further down the header is visible inside the body.

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

Details

Commit
678580f9611c
Parent
2e1c2c409741

lib/sp_runtime.hmodifiedView file

@@ -910,7 +910,10 @@
910910 static sp_RbVal sp_poly_div(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(sp_poly_to_f(a) / sp_poly_to_f(b)); return sp_box_int(sp_idiv(sp_poly_to_i(a), sp_poly_to_i(b))); }
911911 static sp_RbVal sp_poly_mod(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(fmod(sp_poly_to_f(a), sp_poly_to_f(b))); return sp_box_int(sp_imod(sp_poly_to_i(a), sp_poly_to_i(b))); }
912912 static sp_RbVal sp_poly_pow(sp_RbVal a, sp_RbVal b) { double r = pow((double)sp_poly_to_f(a), (double)sp_poly_to_f(b)); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT && b.v.i >= 0) return sp_box_int((mrb_int)r); return sp_box_float((mrb_float)r); }
913-static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b)); }
913+/* sp_poly_shl is defined after sp_PolyArray_push (below) so the
914+ push-dispatch path can call it directly. The Integer-bit-shift
915+ semantics fall through when the recv isn't an array. */
916+static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b);
914917 static sp_RbVal sp_poly_shr(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) >> sp_poly_to_i(b)); }
915918 static sp_RbVal sp_poly_band(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) & sp_poly_to_i(b)); }
916919 static sp_RbVal sp_poly_bor(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) | sp_poly_to_i(b)); }
@@ -924,6 +927,35 @@
924927 static void sp_PolyArray_fin(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; free(a->data); }
925928 static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), sp_PolyArray_fin, sp_PolyArray_scan); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); a->len = 0; { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } return a; }
926929 static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (a->len >= a->cap) { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } a->data[a->len++] = v; }
930+static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) {
931+ /* Dispatch by recv cls_id: an IntArray / PolyArray / etc. boxed
932+ into a poly slot still wants Array#<< (push), not Integer#<<
933+ (bit-shift). Falls through to bit-shift only when the recv is
934+ a non-array. Returns the recv (matching `<<`s chainability). */
935+ if (a.tag == SP_TAG_OBJ) {
936+ if (a.cls_id == SP_BUILTIN_INT_ARRAY) {
937+ sp_IntArray_push((sp_IntArray *)a.v.p, b.tag == SP_TAG_INT ? b.v.i : sp_poly_to_i(b));
938+ return a;
939+ }
940+ if (a.cls_id == SP_BUILTIN_POLY_ARRAY) {
941+ sp_PolyArray_push((sp_PolyArray *)a.v.p, b);
942+ return a;
943+ }
944+ if (a.cls_id == SP_BUILTIN_PTR_ARRAY) {
945+ sp_PtrArray_push((sp_PtrArray *)a.v.p, b.v.p);
946+ return a;
947+ }
948+ if (a.cls_id == SP_BUILTIN_FLT_ARRAY) {
949+ sp_FloatArray_push((sp_FloatArray *)a.v.p, b.tag == SP_TAG_FLT ? b.v.f : (mrb_float)sp_poly_to_i(b));
950+ return a;
951+ }
952+ if (a.cls_id == SP_BUILTIN_STR_ARRAY) {
953+ sp_StrArray_push((sp_StrArray *)a.v.p, b.tag == SP_TAG_STR ? (const char *)b.v.p : "");
954+ return a;
955+ }
956+ }
957+ return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b));
958+}
927959 static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }
928960 static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }
929961 static void sp_PolyArray_set(sp_PolyArray *a, mrb_int i, sp_RbVal v) { if (i < 0) i += a->len; a->data[i] = v; }