Commit 318a93a3
318a93a30188Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
feat(codegen): Time#iso8601 + Time#strftime (#414)
Both methods used to emit cannot resolve call to '...' on time (emitting 0) warnings. Downstream .length/string-concat sites
either cascaded more warnings or hit C-compile errors at any
typed-string sink.
Runtime additions in lib/sp_runtime.h:
-
sp_time_strftime(t, fmt)delegates to libc strftime against the local-time broken-down value, copies into a 256-byte buffer, then sp_str_dup_external's into the str heap. -
sp_time_iso8601(t)formats with libc as "%Y-%m-%dT%H:%M:%S%z" (the closest portable spelling of RFC 3339), then splices a colon into the offset's HHMM tail so the output matches CRuby's "+HH:MM" form. Sub-second precision is intentionally omitted -- CRuby drops it too unless the caller passes a precision arg, which Phase 1 doesn't support.
Inference: infer_method_name_type returns "string" when mname
is "iso8601" / "strftime" AND recv resolves to "time". Gated on
recv_type so user classes that happen to define methods of those
names still flow through normal resolution.
Codegen: compile_object_method_expr's recv_type == "time"
arm dispatches to the new helpers alongside the existing to_i /
to_f arms.
Test time_iso8601_strftime.rb checks shape (length, separator positions) rather than exact strings, since the output depends on the system clock and local-time offset.
Closes #414
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 318a93a30188
- Parent
- 25a2866f5602
lib/sp_runtime.hmodifiedView file
return (sp_Time){ sec, (int32_t)(frac * 1e9) };}typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; } sp_gc_hdr;static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024; }}/* Issue #414: Time#strftime — format the time per `fmt` using libc strftime against the local-time broken-down value. Returns a freshly-allocated string (sp_str_dup_external'd into the str heap so GC tracks it). The 256-byte buffer is enough for every format CRuby builds in (the longest is %c which produces ~25 bytes); custom formats that exceed it get truncated. */static const char *sp_time_strftime(sp_Time t, const char *fmt) { char buf[256]; time_t s = (time_t)t.tv_sec; struct tm *lt = localtime(&s); if (lt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), fmt, lt); if (n == 0) return SPL(""); buf[n] = 0; return sp_str_dup_external(buf);}/* Issue #414: Time#iso8601 — RFC 3339 style "%Y-%m-%dT%H:%M:%S%:z". strftime's %z gives the offset as ±HHMM, so we splice in the colon manually to match CRuby's iso8601 output. Sub-second precision is intentionally omitted: CRuby's iso8601 also drops it unless the caller passes a precision arg, which Phase 1 doesn't support. */static const char *sp_time_iso8601(sp_Time t) { char buf[64]; time_t s = (time_t)t.tv_sec; struct tm *lt = localtime(&s); if (lt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", lt); if (n == 0) return SPL(""); /* Insert a colon between the offset's hour and minute halves (e.g. "-0400" -> "-04:00"). strftime %z always emits 5 chars at the tail; if it's missing for some libc, leave the buffer alone. */ if (n >= 5 && (buf[n - 5] == '+' || buf[n - 5] == '-')) { char m1 = buf[n - 2], m2 = buf[n - 1]; buf[n - 2] = ':'; buf[n - 1] = m1; buf[n] = m2; buf[n + 1] = 0; n++; } else { buf[n] = 0; } return sp_str_dup_external(buf);}#define SP_GC_STACK_MAX 65536static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;/* GC root tracking. SP_GC_ROOT registers a stack-resident root with aspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/time_iso8601_strftime.rbaddedView file
Loading diff…
test/time_iso8601_strftime.rb.expectedaddedView file
Loading diff…