aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPhilipp Stephani2019-04-18 17:42:45 +0200
committerPhilipp Stephani2019-04-24 11:33:52 +0200
commitbffceab6339fb4042588b893ef754c6264379e75 (patch)
tree03747b5cb5275a24418aa9fb421977eb6c0ee063 /src
parent5ae407aad4f2564fae7ddce077eb01aa8efa37fb (diff)
downloademacs-bffceab6339fb4042588b893ef754c6264379e75.tar.gz
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')
-rw-r--r--src/emacs-module.c17
-rw-r--r--src/emacs-module.h.in1
-rw-r--r--src/module-env-27.h6
3 files changed, 24 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:
77#include <stdint.h> 77#include <stdint.h>
78#include <stdio.h> 78#include <stdio.h>
79#include <stdlib.h> 79#include <stdlib.h>
80#include <time.h>
80 81
81#include "lisp.h" 82#include "lisp.h"
82#include "dynlib.h" 83#include "dynlib.h"
@@ -737,6 +738,20 @@ module_process_input (emacs_env *env)
737 return emacs_process_input_continue; 738 return emacs_process_input_continue;
738} 739}
739 740
741static struct timespec
742module_extract_time (emacs_env *env, emacs_value value)
743{
744 MODULE_FUNCTION_BEGIN ((struct timespec) {0});
745 return lisp_time_argument (value_to_lisp (value));
746}
747
748static emacs_value
749module_make_time (emacs_env *env, struct timespec time)
750{
751 MODULE_FUNCTION_BEGIN (NULL);
752 return lisp_to_value (env, make_lisp_time (time));
753}
754
740 755
741/* Subroutines. */ 756/* Subroutines. */
742 757
@@ -1140,6 +1155,8 @@ initialize_environment (emacs_env *env, struct emacs_env_private *priv)
1140 env->vec_size = module_vec_size; 1155 env->vec_size = module_vec_size;
1141 env->should_quit = module_should_quit; 1156 env->should_quit = module_should_quit;
1142 env->process_input = module_process_input; 1157 env->process_input = module_process_input;
1158 env->extract_time = module_extract_time;
1159 env->make_time = module_make_time;
1143 Vmodule_environments = Fcons (make_mint_ptr (env), Vmodule_environments); 1160 Vmodule_environments = Fcons (make_mint_ptr (env), Vmodule_environments);
1144 return env; 1161 return env;
1145} 1162}
diff --git a/src/emacs-module.h.in b/src/emacs-module.h.in
index 009d1583fef..bfbe226dd90 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/>. */
22 22
23#include <stdint.h> 23#include <stdint.h>
24#include <stddef.h> 24#include <stddef.h>
25#include <time.h>
25 26
26#ifndef __cplusplus 27#ifndef __cplusplus
27#include <stdbool.h> 28#include <stdbool.h>
diff --git a/src/module-env-27.h b/src/module-env-27.h
index b491b60fbbc..e63843f8d63 100644
--- a/src/module-env-27.h
+++ b/src/module-env-27.h
@@ -2,3 +2,9 @@
2 function should quit. */ 2 function should quit. */
3 enum emacs_process_input_result (*process_input) (emacs_env *env) 3 enum emacs_process_input_result (*process_input) (emacs_env *env)
4 EMACS_ATTRIBUTE_NONNULL (1); 4 EMACS_ATTRIBUTE_NONNULL (1);
5
6 struct timespec (*extract_time) (emacs_env *env, emacs_value value)
7 EMACS_ATTRIBUTE_NONNULL (1);
8
9 emacs_value (*make_time) (emacs_env *env, struct timespec time)
10 EMACS_ATTRIBUTE_NONNULL (1);