@@ -95,6 +95,24 @@ DEF_BACKEND_SET(supported, ev_supported_backends)
9595DEF_BACKEND_SET (recommended , ev_recommended_backends )
9696DEF_BACKEND_SET (embeddable , ev_embeddable_backends )
9797
98+ static const ev_tstamp lev_ns_per_second = 1000000000.0 ;
99+
100+ static ev_tstamp lev_time_to_seconds (value v_time ) {
101+ return ((ev_tstamp )Long_val (v_time )) / lev_ns_per_second ;
102+ }
103+
104+ static ev_tstamp lev_span_to_seconds (value v_span ) {
105+ return ((ev_tstamp )Long_val (v_span )) / lev_ns_per_second ;
106+ }
107+
108+ static value lev_time_of_seconds (ev_tstamp seconds ) {
109+ return Val_long ((intnat )(seconds * lev_ns_per_second ));
110+ }
111+
112+ static value lev_span_of_seconds (ev_tstamp seconds ) {
113+ return Val_long ((intnat )(seconds * lev_ns_per_second ));
114+ }
115+
98116#define DEF_STOP (__name ) \
99117 CAMLprim value lev_##__name##_stop(value v_w, value v_ev) { \
100118 CAMLparam2(v_w, v_ev); \
@@ -277,13 +295,13 @@ CAMLprim value lev_ev_now(value v_ev) {
277295 CAMLparam1 (v_ev );
278296 struct ev_loop * loop = (struct ev_loop * )Nativeint_val (v_ev );
279297 ev_tstamp now = ev_now (loop );
280- CAMLreturn (caml_copy_double (now ));
298+ CAMLreturn (lev_time_of_seconds (now ));
281299}
282300
283301CAMLprim value lev_sleep (value v_ts ) {
284302 CAMLparam1 (v_ts );
285303 caml_release_runtime_system ();
286- ev_sleep (Double_val (v_ts ));
304+ ev_sleep (lev_span_to_seconds (v_ts ));
287305 caml_acquire_runtime_system ();
288306 CAMLreturn (Val_unit );
289307}
@@ -324,8 +342,8 @@ static ev_tstamp lev_periodic_reschedule_cb(ev_periodic *w, ev_tstamp now) {
324342 CAMLparam0 ();
325343 CAMLlocal1 (v_stamp );
326344 struct periodic_cbs * cbs = (struct periodic_cbs * )w -> data ;
327- v_stamp = caml_callback (cbs -> reschedule , caml_copy_double (now ));
328- double result = Double_val (v_stamp );
345+ v_stamp = caml_callback (cbs -> reschedule , lev_time_of_seconds (now ));
346+ ev_tstamp result = lev_time_to_seconds (v_stamp );
329347 CAMLdrop ;
330348 return result ;
331349}
@@ -393,8 +411,8 @@ CAMLprim value lev_timer_create(value v_cb, value v_after, value v_repeat) {
393411 CAMLparam3 (v_cb , v_after , v_repeat );
394412 CAMLlocal2 (v_timer , v_cb_applied );
395413 ev_timer * timer = caml_stat_alloc (sizeof (ev_timer ));
396- ev_timer_init (timer , Cb_for (ev_timer ), Double_val (v_after ),
397- Double_val (v_repeat ));
414+ ev_timer_init (timer , Cb_for (ev_timer ), lev_span_to_seconds (v_after ),
415+ lev_span_to_seconds (v_repeat ));
398416 v_timer = caml_alloc_custom (& watcher_ops , sizeof (struct ev_timer * ), 0 , 1 );
399417 Ev_timer_val (v_timer ) = timer ;
400418 v_cb_applied = caml_callback (v_cb , v_timer );
@@ -407,7 +425,7 @@ CAMLprim value lev_timer_remaining(value v_timer, value v_ev) {
407425 CAMLparam2 (v_timer , v_ev );
408426 ev_timer * timer = Ev_timer_val (v_timer );
409427 struct ev_loop * ev = (struct ev_loop * )Nativeint_val (v_ev );
410- CAMLreturn (caml_copy_double (ev_timer_remaining (ev , timer )));
428+ CAMLreturn (lev_span_of_seconds (ev_timer_remaining (ev , timer )));
411429}
412430
413431CAMLprim value lev_timer_again (value v_timer , value v_ev ) {
@@ -423,8 +441,8 @@ CAMLprim value lev_periodic_create_regular(value v_cb, value v_offset,
423441 CAMLparam3 (v_cb , v_offset , v_interval );
424442 CAMLlocal2 (v_periodic , v_cb_applied );
425443 ev_periodic * periodic = caml_stat_alloc (sizeof (ev_periodic ));
426- ev_periodic_init (periodic , Cb_for (ev_periodic ), Double_val (v_offset ),
427- Double_val (v_interval ), NULL );
444+ ev_periodic_init (periodic , Cb_for (ev_periodic ), lev_time_to_seconds (v_offset ),
445+ lev_span_to_seconds (v_interval ), NULL );
428446 v_periodic =
429447 caml_alloc_custom (& watcher_ops , sizeof (struct ev_periodic * ), 0 , 1 );
430448 Ev_periodic_val (v_periodic ) = periodic ;
@@ -663,7 +681,7 @@ CAMLprim value lev_stat_create(value v_cb, value v_path, value v_interval) {
663681 ev_stat * w = caml_stat_alloc (sizeof (ev_stat ));
664682 struct lev_stat_data * data = caml_stat_alloc (sizeof (struct lev_stat_data ));
665683 const char * path = caml_stat_strdup (String_val (v_path ));
666- ev_stat_init (w , lev_stat_cb , path , Double_val (v_interval ));
684+ ev_stat_init (w , lev_stat_cb , path , lev_span_to_seconds (v_interval ));
667685 v_w = caml_alloc_custom (& watcher_ops , sizeof (struct ev_stat * ), 0 , 1 );
668686 Ev_val (ev_stat , v_w ) = w ;
669687 v_cb_applied = caml_callback (v_cb , v_w );
0 commit comments