unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH 1/2] Add conversions to and from struct timespec to module interface.
@ 2019-04-23 13:17 Philipp Stephani
  2019-04-23 13:17 ` [PATCH 2/2] Add module functions to convert from and to big integers Philipp Stephani
  2019-04-23 15:16 ` [PATCH 1/2] Add conversions to and from struct timespec to module interface Paul Eggert
  0 siblings, 2 replies; 87+ messages in thread
From: Philipp Stephani @ 2019-04-23 13:17 UTC (permalink / raw)
  To: emacs-devel; +Cc: Philipp Stephani

Time values are a fundamental data type, and such conversions are hard
to implement within modules because of the various forms of time
values in Emacs Lisp.  Adding dedicated conversion functions can
significantly simplify module code dealing with times.

This approach uses nanosecond precision.  While Emacs in theory has
support for higher-precision time values, in practice most languages
and standards, such as POSIX, C, Java, and Go, have settled on
nanosecond-precision integers to represent time.

* src/emacs-module.h.in: Add header for struct timespec.

* src/module-env-27.h: Add module functions for time conversion.

* src/emacs-module.c (module_extract_time, module_make_time): New
functions.
(initialize_environment): Use them.

* test/data/emacs-module/mod-test.c (Fmod_test_add_nanosecond): New
test function.
(emacs_module_init): Define it.

* test/src/emacs-module-tests.el (mod-test-add-nanosecond/valid)
(mod-test-add-nanosecond/invalid): New unit tests.

* doc/lispref/internals.texi (Module Values): Document time
conversion functions.
---
 doc/lispref/internals.texi        | 22 ++++++++++++++++++++++
 etc/NEWS                          |  3 +++
 src/emacs-module.c                | 20 ++++++++++++++++++++
 src/emacs-module.h.in             |  1 +
 src/module-env-27.h               |  6 ++++++
 test/data/emacs-module/mod-test.c | 13 +++++++++++++
 test/src/emacs-module-tests.el    | 25 +++++++++++++++++++++++++
 7 files changed, 90 insertions(+)

diff --git a/doc/lispref/internals.texi b/doc/lispref/internals.texi
index 25892d4b57..c2969d2cd1 100644
--- a/doc/lispref/internals.texi
+++ b/doc/lispref/internals.texi
@@ -1387,6 +1387,15 @@ Module Values
 @var{arg}, as a C @code{double} value.
 @end deftypefn
 
+@deftypefn Function struct timespec extract_time (emacs_env *@var{env}, emacs_value @var{time})
+This function interprets @var{time} as an Emacs time value and returns
+the corresponding @code{struct timespec}.  @xref{Time of Day}.
+@var{time} may not be @code{nil}.  This function raises an error if
+@var{time} is out of range for @code{struct timespec}.  If @var{time}
+has higher precision than nanoseconds, then this function truncates it
+to nanosecond precision.
+@end deftypefn
+
 @deftypefn Function bool copy_string_contents (emacs_env *@var{env}, emacs_value @var{arg}, char *@var{buf}, ptrdiff_t *@var{len})
 This function stores the UTF-8 encoded text of a Lisp string specified
 by @var{arg} in the array of @code{char} pointed by @var{buf}, which
@@ -1452,6 +1461,19 @@ Module Values
 corresponding Emacs floating-point value.
 @end deftypefn
 
+@deftypefn Function emacs_value make_time (emacs_env *@var{env}, struct timespec @var{time})
+This function takes a @code{struct timespec} argument @var{time} and
+returns the corresponding Emacs timestamp.  @xref{Time of Day} for the
+possible return value formats.  It is not specified in which timestamp
+format the time is returned, but it is always a valid Emacs timestamp.
+The return value is exactly the same timestamp as @var{time}: all
+input values are representable, and there is never a loss of
+precision.  @code{time.tv_sec} and @code{time.tv_nsec} can be
+arbitrary values.  In particular, there's no requirement that @var{time}
+be normalized.  This means that @code{time.tv_nsec} doesn't have to be
+in the range [0, 999999999].
+@end deftypefn
+
 @deftypefn Function emacs_value make_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{strlen})
 This function creates an Emacs string from C text string pointed by
 @var{str} whose length in bytes, not including the terminating null
diff --git a/etc/NEWS b/etc/NEWS
index b13ab47768..2534262b62 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1910,6 +1910,9 @@ returns a regexp that never matches anything, which is an identity for
 this operation.  Previously, the empty string was returned in this
 case.
 
+** New module environment functions 'make_time' and 'extract_time' to
+convert between timespec structures and Emacs time values.
+
 \f
 * Changes in Emacs 27.1 on Non-Free Operating Systems
 
diff --git a/src/emacs-module.c b/src/emacs-module.c
index 20dcff2b67..fc5a912d85 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -74,6 +74,7 @@ To add a new module function, proceed as follows:
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <time.h>
 
 #include "lisp.h"
 #include "dynlib.h"
@@ -731,6 +732,22 @@ module_process_input (emacs_env *env)
   return emacs_process_input_continue;
 }
 
+static struct timespec
+module_extract_time (emacs_env *env, emacs_value value)
+{
+  MODULE_FUNCTION_BEGIN ((struct timespec) {0});
+  Lisp_Object time = value_to_lisp (value);
+  CHECK_TYPE (!NILP (time), Qtimep, time);
+  return lisp_time_argument (time);
+}
+
+static emacs_value
+module_make_time (emacs_env *env, struct timespec time)
+{
+  MODULE_FUNCTION_BEGIN (NULL);
+  return lisp_to_value (env, make_lisp_time (time));
+}
+
 \f
 /* Subroutines.  */
 
@@ -1134,6 +1151,8 @@ initialize_environment (emacs_env *env, struct emacs_env_private *priv)
   env->vec_size = module_vec_size;
   env->should_quit = module_should_quit;
   env->process_input = module_process_input;
+  env->extract_time = module_extract_time;
+  env->make_time = module_make_time;
   Vmodule_environments = Fcons (make_mint_ptr (env), Vmodule_environments);
   return env;
 }
@@ -1296,6 +1315,7 @@ syms_of_module (void)
         build_pure_c_string ("Invalid function arity"));
 
   DEFSYM (Qmodule_function_p, "module-function-p");
+  DEFSYM (Qtimep, "timep");
 
   defsubr (&Smodule_load);
 }
diff --git a/src/emacs-module.h.in b/src/emacs-module.h.in
index 009d1583fe..bfbe226dd9 100644
--- a/src/emacs-module.h.in
+++ b/src/emacs-module.h.in
@@ -22,6 +22,7 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #include <stdint.h>
 #include <stddef.h>
+#include <time.h>
 
 #ifndef __cplusplus
 #include <stdbool.h>
diff --git a/src/module-env-27.h b/src/module-env-27.h
index b491b60fbb..e63843f8d6 100644
--- a/src/module-env-27.h
+++ b/src/module-env-27.h
@@ -2,3 +2,9 @@
      function should quit.  */
   enum emacs_process_input_result (*process_input) (emacs_env *env)
     EMACS_ATTRIBUTE_NONNULL (1);
+
+  struct timespec (*extract_time) (emacs_env *env, emacs_value value)
+    EMACS_ATTRIBUTE_NONNULL (1);
+
+  emacs_value (*make_time) (emacs_env *env, struct timespec time)
+    EMACS_ATTRIBUTE_NONNULL (1);
diff --git a/test/data/emacs-module/mod-test.c b/test/data/emacs-module/mod-test.c
index a39e41afee..44a28fa18f 100644
--- a/test/data/emacs-module/mod-test.c
+++ b/test/data/emacs-module/mod-test.c
@@ -366,6 +366,18 @@ Fmod_test_sleep_until (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
   return env->intern (env, "finished");
 }
 
+static emacs_value
+Fmod_test_add_nanosecond (emacs_env *env, ptrdiff_t nargs, emacs_value *args,
+                          void *data)
+{
+  assert (nargs == 1);
+  struct timespec time = env->extract_time (env, args[0]);
+  assert (time.tv_nsec >= 0);
+  assert (time.tv_nsec < 1000000000);
+  time.tv_nsec++;
+  return env->make_time (env, time);
+}
+
 /* Lisp utilities for easier readability (simple wrappers).  */
 
 /* Provide FEATURE to Emacs.  */
@@ -434,6 +446,7 @@ emacs_module_init (struct emacs_runtime *ert)
   DEFUN ("mod-test-invalid-finalizer", Fmod_test_invalid_finalizer, 0, 0,
          NULL, NULL);
   DEFUN ("mod-test-sleep-until", Fmod_test_sleep_until, 2, 2, NULL, NULL);
+  DEFUN ("mod-test-add-nanosecond", Fmod_test_add_nanosecond, 1, 1, NULL, NULL);
 
 #undef DEFUN
 
diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests.el
index 35aaaa64b6..6b986a96e2 100644
--- a/test/src/emacs-module-tests.el
+++ b/test/src/emacs-module-tests.el
@@ -310,4 +310,29 @@ module--test-assertion
                       'finished))
         (quit)))))
 
+(ert-deftest mod-test-add-nanosecond/valid ()
+  (dolist (input (list
+                  ;; Some realistic examples.
+                  (current-time) (time-to-seconds)
+                  (encode-time 12 34 5 6 7 2019 t)
+                  ;; Various legacy timestamp forms.
+                  '(123 456) '(123 456 789) '(123 456 789 6000)
+                  ;; Corner case: this will result in a nanosecond
+                  ;; value of 1000000000 after addition.  The module
+                  ;; code should handle this correctly.
+                  '(123 65535 999999 999000)
+                  ;; Seconds since the epoch.
+                  123 123.45
+                  ;; New (TICKS . HZ) format.
+                  '(123456789 . 1000000000)))
+    (ert-info ((format "input: %s" input))
+      (should (time-equal-p (mod-test-add-nanosecond input)
+                            (time-add input '(0 0 0 1000)))))))
+
+(ert-deftest mod-test-add-nanosecond/invalid ()
+  (dolist (input '(#x10000000000000000  ; out of range
+                   (123) (123.45 6 7) nil "foo" [1 2]))
+    (ert-info ((format "input: %s" input))
+      (should-error (mod-test-add-nanosecond input)))))
+
 ;;; emacs-module-tests.el ends here
-- 
2.20.1 (Apple Git-117)




^ permalink raw reply related	[flat|nested] 87+ messages in thread

end of thread, other threads:[~2019-12-14 19:54 UTC | newest]

Thread overview: 87+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-23 13:17 [PATCH 1/2] Add conversions to and from struct timespec to module interface Philipp Stephani
2019-04-23 13:17 ` [PATCH 2/2] Add module functions to convert from and to big integers Philipp Stephani
2019-04-23 14:30   ` Eli Zaretskii
2019-04-23 14:51   ` Paul Eggert
2019-04-23 15:12     ` Philipp Stephani
2019-04-23 15:48       ` Paul Eggert
2019-04-23 15:54         ` Philipp Stephani
2019-11-02 19:17           ` Philipp Stephani
2019-11-03 19:38             ` Stefan Monnier
2019-11-13 18:46               ` Philipp Stephani
2019-11-17 18:38                 ` [PATCH] Change module interface to no longer use GMP objects directly Philipp Stephani
2019-11-18 21:21                   ` Paul Eggert
2019-11-19 21:12                     ` Philipp Stephani
2019-11-19 21:54                       ` Paul Eggert
2019-11-20 21:06                         ` Philipp Stephani
2019-11-20 21:24                           ` Paul Eggert
2019-11-20 21:30                             ` Philipp Stephani
2019-11-21  1:12                               ` Paul Eggert
2019-11-21 20:31                                 ` Philipp Stephani
2019-11-23  2:13                                   ` Paul Eggert
2019-11-23 20:08                                     ` Philipp Stephani
2019-11-23 23:10                                       ` Paul Eggert
2019-11-23 20:46                                     ` Stefan Monnier
2019-11-23 23:10                                       ` Paul Eggert
2019-11-24  9:28                                         ` Andreas Schwab
2019-11-25 21:03                                           ` Paul Eggert
2019-11-25 21:59                                             ` Philipp Stephani
2019-12-04 20:31                                               ` Philipp Stephani
2019-12-05 14:43                                                 ` Eli Zaretskii
2019-12-08 20:28                                                   ` Philipp Stephani
2019-12-09  3:26                                                     ` Eli Zaretskii
2019-12-09  4:58                                                       ` Paul Eggert
2019-12-09 13:22                                                         ` Eli Zaretskii
2019-12-09 23:15                                                       ` Philipp Stephani
2019-12-10  0:22                                                         ` Paul Eggert
2019-12-10 13:15                                                           ` Philipp Stephani
2019-12-10 15:57                                                           ` Eli Zaretskii
2019-12-14 16:06                                                             ` Philipp Stephani
2019-12-14 19:54                                                               ` Paul Eggert
2019-12-09  0:35                                                   ` Paul Eggert
2019-12-09 13:19                                                     ` Eli Zaretskii
2019-04-23 15:16 ` [PATCH 1/2] Add conversions to and from struct timespec to module interface Paul Eggert
2019-04-23 21:32   ` Philipp Stephani
     [not found]     ` <20190423213218.35618-2-phst@google.com>
2019-04-23 21:43       ` [PATCH 2/2] Add module functions to convert from and to big integers Paul Eggert
2019-04-24 16:03       ` Eli Zaretskii
2019-04-24 16:37         ` Philipp Stephani
2019-04-24 16:51           ` Eli Zaretskii
2019-04-24 16:57           ` Philipp Stephani
2019-04-24 17:11             ` Eli Zaretskii
2019-04-24 17:15               ` Philipp Stephani
2019-04-24 17:23                 ` Eli Zaretskii
2019-04-24 17:28                   ` Philipp Stephani
2019-04-24 17:51                     ` [PATCH] Unbreak build when building without GMP support Philipp Stephani
2019-04-24 18:41                       ` Eli Zaretskii
2019-04-24 18:49                         ` Philipp Stephani
2019-04-24 19:06                           ` Eli Zaretskii
2019-04-24 19:19                             ` Philipp Stephani
2019-04-24 19:30                               ` Eli Zaretskii
2019-04-24 21:15                                 ` Philipp Stephani
2019-04-25  6:04                                   ` Eli Zaretskii
2019-04-25  6:39                                     ` Eli Zaretskii
2019-04-25 10:24                                       ` Philipp Stephani
2019-04-24 21:34                       ` Philipp Stephani
2019-04-24 19:44                     ` [PATCH 2/2] Add module functions to convert from and to big integers Stefan Monnier
2019-04-24 20:15                       ` Paul Eggert
2019-04-24 20:57                         ` Stefan Monnier
2019-04-24 21:17                           ` Philipp Stephani
2019-04-24 23:32                             ` Paul Eggert
2019-04-24 21:19                       ` Philipp Stephani
2019-04-25  0:00                         ` Paul Eggert
2019-04-25  5:33                           ` Eli Zaretskii
2019-04-25 10:41                             ` Philipp Stephani
2019-04-25 13:46                               ` [PATCH 1/2] Require full GMP when building module support Philipp Stephani
2019-04-25 13:46                                 ` [PATCH 2/2] Check for __attribute__ ((cleanup)) during configuration Philipp Stephani
2019-04-25 21:18                                   ` Paul Eggert
2019-04-28 18:12                                     ` Philipp Stephani
2019-04-25 14:37                                 ` [PATCH 1/2] Require full GMP when building module support Eli Zaretskii
2019-04-25 15:06                                   ` Philipp Stephani
2019-04-25 16:14                                     ` Eli Zaretskii
2019-04-25 17:09                                       ` [PATCH] Require full GMP for big integer module functions Philipp Stephani
2019-04-25 21:00                                         ` Paul Eggert
2019-04-28 18:14                                           ` Philipp Stephani
2019-04-28 20:18                                             ` Paul Eggert
2019-04-28 17:10                                       ` [PATCH 1/2] Require full GMP when building module support Philipp Stephani
2019-04-24  7:21     ` [PATCH 1/2] Add conversions to and from struct timespec to module interface Eli Zaretskii
2019-04-24 11:03       ` Philipp Stephani
2019-04-24 11:44         ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).