Commit dfed63fb

dfed63fbad94Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(runtime): heap-box sp_Range when crossing into poly slots

sp_Range is a 16-byte value type (two mrb_int); sp_RbVal's union is 8 bytes wide, so a stack-typed Range can't fit. Heterogeneous hash literals like { ok: 200, success: 200..299 } and poly arrays like [200..299, 200] were falling through box_non_nullable_value_to_poly's default branch and emitting sp_box_int(sp_range_new(...)), which C rejected as a struct/int mismatch.

Add SP_BUILTIN_RANGE (-10) and sp_box_range(sp_Range): copy the stack range onto the GC heap (no scanner needed since Range has no internal pointers) and box the pointer through SP_TAG_OBJ. Wire up sp_poly_puts and sp_poly_to_s to dispatch through a new sp_Range_inspect for the "first..last" form.

box_non_nullable_value_to_poly now has a range case so every poly-write site (hash/array literals, ivar widening, param boxing, etc.) routes through the heap-box path. The old if/elsif chain in the PolyArray literal emit was hand-rolling the same dispatch and missed range entirely; replace it with a box_value_to_poly call so the centralized table is the single source of truth.

Fixes #249

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

Details

Commit
dfed63fbad94
Parent
46129aa59c6c

lib/sp_runtime.hmodifiedView file

@@ -690,6 +690,7 @@
690690 #define SP_BUILTIN_PTR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_OBJ) /* -6 */
691691 #define SP_BUILTIN_SYM_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_SYM) /* -7 */
692692 #define SP_BUILTIN_PROC (-9) /* sp_Proc *, distinct from any tag-based id */
693+#define SP_BUILTIN_RANGE (-10) /* sp_Range *, heap copy of stack-typed sp_Range when crossing into poly */
693694 typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;
694695 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
695696 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
@@ -708,6 +709,22 @@
708709 static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }
709710 static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }
710711 static sp_RbVal sp_box_proc(void *p) { return sp_box_obj(p, SP_BUILTIN_PROC); }
712+/* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union
713+ (max 8 bytes). When a Range crosses into a poly slot (heterogeneous
714+ hash / array / param / ivar), copy it onto the GC heap and box the
715+ pointer via SP_BUILTIN_RANGE. The Range has no internal pointer fields
716+ so no scanner is needed. */
717+static sp_RbVal sp_box_range(sp_Range v) {
718+ sp_Range *p = (sp_Range *)sp_gc_alloc(sizeof(sp_Range), NULL, NULL);
719+ *p = v;
720+ return sp_box_obj(p, SP_BUILTIN_RANGE);
721+}
722+static const char *sp_Range_inspect(sp_Range *r) {
723+ /* "first..last" form. Buffer sized for two int64s plus the dots. */
724+ char *buf = sp_str_alloc_raw(48);
725+ snprintf(buf, 48, "%lld..%lld", (long long)r->first, (long long)r->last);
726+ return buf;
727+}
711728 static void sp_poly_puts(sp_RbVal v) {
712729 switch (v.tag) {
713730 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;
@@ -727,6 +744,7 @@
727744 case SP_BUILTIN_STR_ARRAY: puts(sp_StrArray_inspect((sp_StrArray *)v.v.p)); break;
728745 case SP_BUILTIN_SYM_ARRAY: puts(sp_SymArray_inspect((sp_IntArray *)v.v.p)); break;
729746 case SP_BUILTIN_PTR_ARRAY: puts(sp_PtrArray_inspect((sp_PtrArray *)v.v.p)); break;
747+ case SP_BUILTIN_RANGE: puts(sp_Range_inspect((sp_Range *)v.v.p)); break;
730748 default: printf("#<Object:0x%p>\n", v.v.p); break;
731749 }
732750 break;
@@ -751,6 +769,7 @@
751769 case SP_BUILTIN_STR_ARRAY: return sp_StrArray_inspect((sp_StrArray *)v.v.p);
752770 case SP_BUILTIN_SYM_ARRAY: return sp_SymArray_inspect((sp_IntArray *)v.v.p);
753771 case SP_BUILTIN_PTR_ARRAY: return sp_PtrArray_inspect((sp_PtrArray *)v.v.p);
772+ case SP_BUILTIN_RANGE: return sp_Range_inspect((sp_Range *)v.v.p);
754773 default: return sp_str_empty;
755774 }
756775 default: return sp_str_empty;