Commit f809e55c
f809e55cf47fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · ~4 modified
Description
Merge pull request #237 from tomoyukiinoue/pr/time-struct-restore
feat(codegen): restore sp_Time struct, fix Time.at dispatch
Details
- Commit
- f809e55cf47f
- Parent
- 0514bd4a7e79
- Merge parent
- df7954c5bed6
lib/sp_runtime.hmodifiedView file
@@ -85,6 +85,32 @@
8585
typedef struct{mrb_int first;mrb_int last;}sp_Range;8686
static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}8787
88+
/* ---- Time runtime ---- */89+
/* sp_Time keeps Time.now / Time.at as value-typed structs. d78149b's90+
sub-second precision is preserved by inlining tv_sec + tv_nsec/1e991+
at every Time#to_f / Time#- call site (see spinel_codegen.rb). */92+
typedef struct { int64_t tv_sec; int32_t tv_nsec; } sp_Time;93+
static inline sp_Time sp_time_now(void) {94+
struct timespec ts;95+
clock_gettime(CLOCK_REALTIME, &ts);96+
return (sp_Time){ ts.tv_sec, (int32_t)ts.tv_nsec };97+
}98+
static inline sp_Time sp_time_at_int(mrb_int sec) {99+
return (sp_Time){ (int64_t)sec, 0 };100+
}101+
/* POSIX convention: keep tv_nsec in [0, 1e9). For negative epoch with a102+
non-integer fractional part, decrement tv_sec and roll the fraction103+
into the positive nsec range — so Time.at(-0.5).to_i returns -1, not 0. */104+
static inline sp_Time sp_time_at_float(mrb_float epoch) {105+
int64_t sec = (int64_t)epoch;106+
mrb_float frac = epoch - (mrb_float)sec;107+
if (frac < 0.0) {108+
sec -= 1;109+
frac += 1.0;110+
}111+
return (sp_Time){ sec, (int32_t)(frac * 1e9) };112+
}113+
88114
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;89115
static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;90116
spinel_codegen.rbmodifiedView file
Loading diff…
test/time.rbmodifiedView file
Loading diff…
test/time.rb.expectedmodifiedView file
Loading diff…