Commit c1a5d304

c1a5d3048017Merged

Richard Ramsden <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Make Time.now return float with sub-second precision

Time.now was hardcoded to (mrb_int)time(NULL), so Time.now.to_f * 1000 gave only whole-second multiples. Switch to clock_gettime(CLOCK_REALTIME) returning float seconds so the standard CRuby idiom yields real milliseconds.

Also add float#to_f as identity (was missing in both compile_float_method_expr and the open-class dispatch table) — surfaced by the new test.

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

Details

Commit
c1a5d3048017
Parent
311d03089966

spinel_codegen.rbmodifiedView file

@@ -3545,6 +3545,11 @@
35453545 return "float"
35463546 end
35473547 end
3548+ if rcname == "Time"
3549+ if mname == "now"
3550+ return "float"
3551+ end
3552+ end
35483553 if rcname == "File"
35493554 if mname == "read" || mname == "binread"
35503555 return "string"
@@ -17106,6 +17111,9 @@
1710617111 if mname == "to_i"
1710717112 return "(mrb_int)(self)"
1710817113 end
17114+ if mname == "to_f"
17115+ return "(self)"
17116+ end
1710917117 if mname == "to_s"
1711017118 return "sp_float_to_s(self)"
1711117119 end
@@ -18867,6 +18875,9 @@
1886718875 if mname == "to_i"
1886818876 return "(mrb_int)(" + rc + ")"
1886918877 end
18878+ if mname == "to_f"
18879+ return "(" + rc + ")"
18880+ end
1887018881 # ceil/floor/round/truncate with precision arg use a GCC stmt-expr so
1887118882 # the argument expression is compiled-and-emitted once on the Ruby
1887218883 # side and pow(10, n) is evaluated once at runtime — the original
@@ -20311,10 +20322,12 @@
2031120322 return "sp_file_basename(" + compile_arg0(nid) + ")"
2031220323 end
2031320324 end
20314- # Time
20325+ # Time.now — wall-clock seconds-since-epoch as float, matching
20326+ # CRuby's Time#to_f semantics so `Time.now.to_f * 1000` actually
20327+ # yields milliseconds with sub-second precision.
2031520328 if rcname == "Time"
2031620329 if mname == "now"
20317- return "((mrb_int)time(NULL))"
20330+ return "({ struct timespec _ts; clock_gettime(CLOCK_REALTIME, &_ts); (mrb_float)_ts.tv_sec + (mrb_float)_ts.tv_nsec / 1e9; })"
2031820331 end
2031920332 end
2032020333 # Process.clock_gettime — assume CLOCK_MONOTONIC; the clock_id