diff options
author | Philipp Stephani <phst@google.com> | 2019-04-18 17:42:45 +0200 |
---|---|---|
committer | Philipp Stephani <phst@google.com> | 2019-04-24 11:33:52 +0200 |
commit | bffceab6339fb4042588b893ef754c6264379e75 (patch) | |
tree | 03747b5cb5275a24418aa9fb421977eb6c0ee063 /src/emacs-module.c | |
parent | 5ae407aad4f2564fae7ddce077eb01aa8efa37fb (diff) | |
download | emacs-bffceab6339fb4042588b893ef754c6264379e75.tar.gz emacs-bffceab6339fb4042588b893ef754c6264379e75.tar.bz2 emacs-bffceab6339fb4042588b893ef754c6264379e75.zip |
Add conversions to and from struct timespec to module interface.
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/nil, mod-test-add-nanosecond/invalid): New
unit tests.
* doc/lispref/internals.texi (Module Values): Document time
conversion functions.
Diffstat (limited to 'src/emacs-module.c')
-rw-r--r-- | src/emacs-module.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/emacs-module.c b/src/emacs-module.c index b812fdc2df4..e46af30ce84 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -77,6 +77,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" @@ -737,6 +738,20 @@ 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}); + return lisp_time_argument (value_to_lisp (value)); +} + +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)); +} + /* Subroutines. */ @@ -1140,6 +1155,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; } |