Commit dd47cc17

dd47cc17ca3eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Add Time arithmetic/comparison and hash fallback to poly

  • sp_runtime.h: add sp_time_add_f/i, sp_time_sub_i/t, sp_time_cmp
  • analyze.c: Time < > <= >= == != isdst return correct TyKind (BOOL/INT) Time - Time returns TY_FLOAT; hash unknown key+val combos fall back to TY_POLY_POLY_HASH instead of TY_UNKNOWN
  • codegen.c: emit Time <, >, <=, >=, ==, !=, <=>, +, -, puts via sp_time_cmp / sp_time_add_i / sp_time_sub_i / sp_time_add_f

Fixes bundle_misc_c_33 and hash_float_value_preserved.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
dd47cc17ca3e
Parent
d88dbea5d7d2

lib/sp_runtime.hmodifiedView file

@@ -698,6 +698,41 @@
698698 sp_time_inspect_to(t, buf, sizeof(buf));
699699 return sp_str_dup_external(buf);
700700 }
701+static inline int sp_time_cmp(sp_Time a, sp_Time b) {
702+ if (a.tv_sec < b.tv_sec) return -1;
703+ if (a.tv_sec > b.tv_sec) return 1;
704+ if (a.tv_nsec < b.tv_nsec) return -1;
705+ if (a.tv_nsec > b.tv_nsec) return 1;
706+ return 0;
707+}
708+/* Time + seconds (int or float), Time - seconds */
709+static inline sp_Time sp_time_add_f(sp_Time t, double secs) {
710+ long long ns = (long long)(secs * 1000000000.0);
711+ long long total_ns = (long long)t.tv_sec * 1000000000LL + t.tv_nsec + ns;
712+ sp_Time r;
713+ r.tv_sec = (time_t)(total_ns / 1000000000LL);
714+ r.tv_nsec = (int32_t)(total_ns % 1000000000LL);
715+ if (r.tv_nsec < 0) { r.tv_sec--; r.tv_nsec += 1000000000; }
716+ r.is_utc = t.is_utc;
717+ return r;
718+}
719+static inline sp_Time sp_time_add_i(sp_Time t, mrb_int secs) {
720+ sp_Time r;
721+ r.tv_sec = t.tv_sec + (time_t)secs;
722+ r.tv_nsec = t.tv_nsec;
723+ r.is_utc = t.is_utc;
724+ return r;
725+}
726+static inline sp_Time sp_time_sub_i(sp_Time t, mrb_int secs) {
727+ sp_Time r;
728+ r.tv_sec = t.tv_sec - (time_t)secs;
729+ r.tv_nsec = t.tv_nsec;
730+ r.is_utc = t.is_utc;
731+ return r;
732+}
733+static inline double sp_time_sub_t(sp_Time a, sp_Time b) {
734+ return (double)(a.tv_sec - b.tv_sec) + (double)(a.tv_nsec - b.tv_nsec) / 1e9;
735+}
701736
702737 /* SP_GC_STACK_MAX, sp_gc_roots, sp_gc_nroots come from sp_gc.h / lib/sp_gc.c. */
703738 /* Cooperative-fiber GC root storage (issue #636).