Commit 84d241a6

84d241a676e0Merged

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

Description

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

Same shape as the Range fix in a330dbf: sp_Time (12+ bytes: tv_sec int64 + tv_nsec int32) is wider than sp_RbVal's 8-byte union, so a Time value in a poly hash / poly array used to fall through box_non_nullable_value_to_poly's default branch and emit sp_box_int(sp_time_at_int(...)), which C rejected.

Add SP_BUILTIN_TIME (-11) and sp_box_time(sp_Time): heap-copy the stack Time (no scanner — Time has no internal pointers) and box through SP_TAG_OBJ. Wire sp_poly_puts / sp_poly_to_s through a new sp_Time_inspect that prints the UTC form via gmtime + strftime — CRuby uses localtime with a numeric tz offset, but the spinel runtime keeps Time timezone-naive, and UTC sidesteps platform tzdata variance.

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

Details

Commit
84d241a676e0
Parent
dfed63fbad94

lib/sp_runtime.hmodifiedView file

@@ -691,6 +691,7 @@
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 */
693693 #define SP_BUILTIN_RANGE (-10) /* sp_Range *, heap copy of stack-typed sp_Range when crossing into poly */
694+#define SP_BUILTIN_TIME (-11) /* sp_Time *, heap copy of stack-typed sp_Time when crossing into poly */
694695 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;
695696 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; }
696697 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; }
@@ -725,6 +726,29 @@
725726 snprintf(buf, 48, "%lld..%lld", (long long)r->first, (long long)r->last);
726727 return buf;
727728 }
729+/* Same heap-box rationale as sp_Range: sp_Time is 12+ bytes (tv_sec +
730+ tv_nsec), wider than sp_RbVal's 8-byte union. No internal pointers
731+ so no scanner is needed. */
732+static sp_RbVal sp_box_time(sp_Time v) {
733+ sp_Time *p = (sp_Time *)sp_gc_alloc(sizeof(sp_Time), NULL, NULL);
734+ *p = v;
735+ return sp_box_obj(p, SP_BUILTIN_TIME);
736+}
737+static const char *sp_Time_inspect(sp_Time *t) {
738+ /* "YYYY-MM-DD HH:MM:SS UTC" form via gmtime. CRuby uses localtime
739+ with a numeric tz offset, but the spinel runtime keeps Time
740+ timezone-naive — UTC is the unambiguous choice that doesn't need
741+ the platform's tzdata. Buffer is 32 chars + a margin. */
742+ char *buf = sp_str_alloc_raw(40);
743+ time_t sec = (time_t)t->tv_sec;
744+ struct tm *tm_ = gmtime(&sec);
745+ if (tm_) {
746+ strftime(buf, 40, "%Y-%m-%d %H:%M:%S UTC", tm_);
747+ } else {
748+ snprintf(buf, 40, "Time(%lld)", (long long)t->tv_sec);
749+ }
750+ return buf;
751+}
728752 static void sp_poly_puts(sp_RbVal v) {
729753 switch (v.tag) {
730754 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;
@@ -745,6 +769,7 @@
745769 case SP_BUILTIN_SYM_ARRAY: puts(sp_SymArray_inspect((sp_IntArray *)v.v.p)); break;
746770 case SP_BUILTIN_PTR_ARRAY: puts(sp_PtrArray_inspect((sp_PtrArray *)v.v.p)); break;
747771 case SP_BUILTIN_RANGE: puts(sp_Range_inspect((sp_Range *)v.v.p)); break;
772+ case SP_BUILTIN_TIME: puts(sp_Time_inspect((sp_Time *)v.v.p)); break;
748773 default: printf("#<Object:0x%p>\n", v.v.p); break;
749774 }
750775 break;
@@ -770,6 +795,7 @@
770795 case SP_BUILTIN_SYM_ARRAY: return sp_SymArray_inspect((sp_IntArray *)v.v.p);
771796 case SP_BUILTIN_PTR_ARRAY: return sp_PtrArray_inspect((sp_PtrArray *)v.v.p);
772797 case SP_BUILTIN_RANGE: return sp_Range_inspect((sp_Range *)v.v.p);
798+ case SP_BUILTIN_TIME: return sp_Time_inspect((sp_Time *)v.v.p);
773799 default: return sp_str_empty;
774800 }
775801 default: return sp_str_empty;