emacs-module.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* emacs-module.h - GNU Emacs module API.
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef EMACS_MODULE_H
  15. #define EMACS_MODULE_H
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. #ifndef __cplusplus
  19. #include <stdbool.h>
  20. #endif
  21. #if defined __cplusplus && __cplusplus >= 201103L
  22. # define EMACS_NOEXCEPT noexcept
  23. #else
  24. # define EMACS_NOEXCEPT
  25. #endif
  26. #ifdef __has_attribute
  27. #if __has_attribute(__nonnull__)
  28. # define EMACS_ATTRIBUTE_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  29. #endif
  30. #endif
  31. #ifndef EMACS_ATTRIBUTE_NONNULL
  32. # define EMACS_ATTRIBUTE_NONNULL(...)
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /* Current environment. */
  38. typedef struct emacs_env_26 emacs_env;
  39. /* Opaque pointer representing an Emacs Lisp value.
  40. BEWARE: Do not assume NULL is a valid value! */
  41. typedef struct emacs_value_tag *emacs_value;
  42. enum { emacs_variadic_function = -2 };
  43. /* Struct passed to a module init function (emacs_module_init). */
  44. struct emacs_runtime
  45. {
  46. /* Structure size (for version checking). */
  47. ptrdiff_t size;
  48. /* Private data; users should not touch this. */
  49. struct emacs_runtime_private *private_members;
  50. /* Return an environment pointer. */
  51. emacs_env *(*get_environment) (struct emacs_runtime *ert)
  52. EMACS_ATTRIBUTE_NONNULL(1);
  53. };
  54. /* Possible Emacs function call outcomes. */
  55. enum emacs_funcall_exit
  56. {
  57. /* Function has returned normally. */
  58. emacs_funcall_exit_return = 0,
  59. /* Function has signaled an error using `signal'. */
  60. emacs_funcall_exit_signal = 1,
  61. /* Function has exit using `throw'. */
  62. emacs_funcall_exit_throw = 2
  63. };
  64. struct emacs_env_25
  65. {
  66. /* Structure size (for version checking). */
  67. ptrdiff_t size;
  68. /* Private data; users should not touch this. */
  69. struct emacs_env_private *private_members;
  70. /* Memory management. */
  71. emacs_value (*make_global_ref) (emacs_env *env,
  72. emacs_value any_reference)
  73. EMACS_ATTRIBUTE_NONNULL(1);
  74. void (*free_global_ref) (emacs_env *env,
  75. emacs_value global_reference)
  76. EMACS_ATTRIBUTE_NONNULL(1);
  77. /* Non-local exit handling. */
  78. enum emacs_funcall_exit (*non_local_exit_check) (emacs_env *env)
  79. EMACS_ATTRIBUTE_NONNULL(1);
  80. void (*non_local_exit_clear) (emacs_env *env)
  81. EMACS_ATTRIBUTE_NONNULL(1);
  82. enum emacs_funcall_exit (*non_local_exit_get)
  83. (emacs_env *env,
  84. emacs_value *non_local_exit_symbol_out,
  85. emacs_value *non_local_exit_data_out)
  86. EMACS_ATTRIBUTE_NONNULL(1, 2, 3);
  87. void (*non_local_exit_signal) (emacs_env *env,
  88. emacs_value non_local_exit_symbol,
  89. emacs_value non_local_exit_data)
  90. EMACS_ATTRIBUTE_NONNULL(1);
  91. void (*non_local_exit_throw) (emacs_env *env,
  92. emacs_value tag,
  93. emacs_value value)
  94. EMACS_ATTRIBUTE_NONNULL(1);
  95. /* Function registration. */
  96. emacs_value (*make_function) (emacs_env *env,
  97. ptrdiff_t min_arity,
  98. ptrdiff_t max_arity,
  99. emacs_value (*function) (emacs_env *env,
  100. ptrdiff_t nargs,
  101. emacs_value args[],
  102. void *)
  103. EMACS_NOEXCEPT
  104. EMACS_ATTRIBUTE_NONNULL(1),
  105. const char *documentation,
  106. void *data)
  107. EMACS_ATTRIBUTE_NONNULL(1, 4);
  108. emacs_value (*funcall) (emacs_env *env,
  109. emacs_value function,
  110. ptrdiff_t nargs,
  111. emacs_value args[])
  112. EMACS_ATTRIBUTE_NONNULL(1);
  113. emacs_value (*intern) (emacs_env *env,
  114. const char *symbol_name)
  115. EMACS_ATTRIBUTE_NONNULL(1, 2);
  116. /* Type conversion. */
  117. emacs_value (*type_of) (emacs_env *env,
  118. emacs_value value)
  119. EMACS_ATTRIBUTE_NONNULL(1);
  120. bool (*is_not_nil) (emacs_env *env, emacs_value value)
  121. EMACS_ATTRIBUTE_NONNULL(1);
  122. bool (*eq) (emacs_env *env, emacs_value a, emacs_value b)
  123. EMACS_ATTRIBUTE_NONNULL(1);
  124. intmax_t (*extract_integer) (emacs_env *env, emacs_value value)
  125. EMACS_ATTRIBUTE_NONNULL(1);
  126. emacs_value (*make_integer) (emacs_env *env, intmax_t value)
  127. EMACS_ATTRIBUTE_NONNULL(1);
  128. double (*extract_float) (emacs_env *env, emacs_value value)
  129. EMACS_ATTRIBUTE_NONNULL(1);
  130. emacs_value (*make_float) (emacs_env *env, double value)
  131. EMACS_ATTRIBUTE_NONNULL(1);
  132. /* Copy the content of the Lisp string VALUE to BUFFER as an utf8
  133. null-terminated string.
  134. SIZE must point to the total size of the buffer. If BUFFER is
  135. NULL or if SIZE is not big enough, write the required buffer size
  136. to SIZE and return true.
  137. Note that SIZE must include the last null byte (e.g. "abc" needs
  138. a buffer of size 4).
  139. Return true if the string was successfully copied. */
  140. bool (*copy_string_contents) (emacs_env *env,
  141. emacs_value value,
  142. char *buffer,
  143. ptrdiff_t *size_inout)
  144. EMACS_ATTRIBUTE_NONNULL(1, 4);
  145. /* Create a Lisp string from a utf8 encoded string. */
  146. emacs_value (*make_string) (emacs_env *env,
  147. const char *contents, ptrdiff_t length)
  148. EMACS_ATTRIBUTE_NONNULL(1, 2);
  149. /* Embedded pointer type. */
  150. emacs_value (*make_user_ptr) (emacs_env *env,
  151. void (*fin) (void *) EMACS_NOEXCEPT,
  152. void *ptr)
  153. EMACS_ATTRIBUTE_NONNULL(1);
  154. void *(*get_user_ptr) (emacs_env *env, emacs_value uptr)
  155. EMACS_ATTRIBUTE_NONNULL(1);
  156. void (*set_user_ptr) (emacs_env *env, emacs_value uptr, void *ptr)
  157. EMACS_ATTRIBUTE_NONNULL(1);
  158. void (*(*get_user_finalizer) (emacs_env *env, emacs_value uptr))
  159. (void *) EMACS_NOEXCEPT EMACS_ATTRIBUTE_NONNULL(1);
  160. void (*set_user_finalizer) (emacs_env *env,
  161. emacs_value uptr,
  162. void (*fin) (void *) EMACS_NOEXCEPT)
  163. EMACS_ATTRIBUTE_NONNULL(1);
  164. /* Vector functions. */
  165. emacs_value (*vec_get) (emacs_env *env, emacs_value vec, ptrdiff_t i)
  166. EMACS_ATTRIBUTE_NONNULL(1);
  167. void (*vec_set) (emacs_env *env, emacs_value vec, ptrdiff_t i,
  168. emacs_value val)
  169. EMACS_ATTRIBUTE_NONNULL(1);
  170. ptrdiff_t (*vec_size) (emacs_env *env, emacs_value vec)
  171. EMACS_ATTRIBUTE_NONNULL(1);
  172. };
  173. struct emacs_env_26
  174. {
  175. /* Structure size (for version checking). */
  176. ptrdiff_t size;
  177. /* Private data; users should not touch this. */
  178. struct emacs_env_private *private_members;
  179. /* Memory management. */
  180. emacs_value (*make_global_ref) (emacs_env *env,
  181. emacs_value any_reference)
  182. EMACS_ATTRIBUTE_NONNULL(1);
  183. void (*free_global_ref) (emacs_env *env,
  184. emacs_value global_reference)
  185. EMACS_ATTRIBUTE_NONNULL(1);
  186. /* Non-local exit handling. */
  187. enum emacs_funcall_exit (*non_local_exit_check) (emacs_env *env)
  188. EMACS_ATTRIBUTE_NONNULL(1);
  189. void (*non_local_exit_clear) (emacs_env *env)
  190. EMACS_ATTRIBUTE_NONNULL(1);
  191. enum emacs_funcall_exit (*non_local_exit_get)
  192. (emacs_env *env,
  193. emacs_value *non_local_exit_symbol_out,
  194. emacs_value *non_local_exit_data_out)
  195. EMACS_ATTRIBUTE_NONNULL(1, 2, 3);
  196. void (*non_local_exit_signal) (emacs_env *env,
  197. emacs_value non_local_exit_symbol,
  198. emacs_value non_local_exit_data)
  199. EMACS_ATTRIBUTE_NONNULL(1);
  200. void (*non_local_exit_throw) (emacs_env *env,
  201. emacs_value tag,
  202. emacs_value value)
  203. EMACS_ATTRIBUTE_NONNULL(1);
  204. /* Function registration. */
  205. emacs_value (*make_function) (emacs_env *env,
  206. ptrdiff_t min_arity,
  207. ptrdiff_t max_arity,
  208. emacs_value (*function) (emacs_env *env,
  209. ptrdiff_t nargs,
  210. emacs_value args[],
  211. void *)
  212. EMACS_NOEXCEPT
  213. EMACS_ATTRIBUTE_NONNULL(1),
  214. const char *documentation,
  215. void *data)
  216. EMACS_ATTRIBUTE_NONNULL(1, 4);
  217. emacs_value (*funcall) (emacs_env *env,
  218. emacs_value function,
  219. ptrdiff_t nargs,
  220. emacs_value args[])
  221. EMACS_ATTRIBUTE_NONNULL(1);
  222. emacs_value (*intern) (emacs_env *env,
  223. const char *symbol_name)
  224. EMACS_ATTRIBUTE_NONNULL(1, 2);
  225. /* Type conversion. */
  226. emacs_value (*type_of) (emacs_env *env,
  227. emacs_value value)
  228. EMACS_ATTRIBUTE_NONNULL(1);
  229. bool (*is_not_nil) (emacs_env *env, emacs_value value)
  230. EMACS_ATTRIBUTE_NONNULL(1);
  231. bool (*eq) (emacs_env *env, emacs_value a, emacs_value b)
  232. EMACS_ATTRIBUTE_NONNULL(1);
  233. intmax_t (*extract_integer) (emacs_env *env, emacs_value value)
  234. EMACS_ATTRIBUTE_NONNULL(1);
  235. emacs_value (*make_integer) (emacs_env *env, intmax_t value)
  236. EMACS_ATTRIBUTE_NONNULL(1);
  237. double (*extract_float) (emacs_env *env, emacs_value value)
  238. EMACS_ATTRIBUTE_NONNULL(1);
  239. emacs_value (*make_float) (emacs_env *env, double value)
  240. EMACS_ATTRIBUTE_NONNULL(1);
  241. /* Copy the content of the Lisp string VALUE to BUFFER as an utf8
  242. null-terminated string.
  243. SIZE must point to the total size of the buffer. If BUFFER is
  244. NULL or if SIZE is not big enough, write the required buffer size
  245. to SIZE and return true.
  246. Note that SIZE must include the last null byte (e.g. "abc" needs
  247. a buffer of size 4).
  248. Return true if the string was successfully copied. */
  249. bool (*copy_string_contents) (emacs_env *env,
  250. emacs_value value,
  251. char *buffer,
  252. ptrdiff_t *size_inout)
  253. EMACS_ATTRIBUTE_NONNULL(1, 4);
  254. /* Create a Lisp string from a utf8 encoded string. */
  255. emacs_value (*make_string) (emacs_env *env,
  256. const char *contents, ptrdiff_t length)
  257. EMACS_ATTRIBUTE_NONNULL(1, 2);
  258. /* Embedded pointer type. */
  259. emacs_value (*make_user_ptr) (emacs_env *env,
  260. void (*fin) (void *) EMACS_NOEXCEPT,
  261. void *ptr)
  262. EMACS_ATTRIBUTE_NONNULL(1);
  263. void *(*get_user_ptr) (emacs_env *env, emacs_value uptr)
  264. EMACS_ATTRIBUTE_NONNULL(1);
  265. void (*set_user_ptr) (emacs_env *env, emacs_value uptr, void *ptr)
  266. EMACS_ATTRIBUTE_NONNULL(1);
  267. void (*(*get_user_finalizer) (emacs_env *env, emacs_value uptr))
  268. (void *) EMACS_NOEXCEPT EMACS_ATTRIBUTE_NONNULL(1);
  269. void (*set_user_finalizer) (emacs_env *env,
  270. emacs_value uptr,
  271. void (*fin) (void *) EMACS_NOEXCEPT)
  272. EMACS_ATTRIBUTE_NONNULL(1);
  273. /* Vector functions. */
  274. emacs_value (*vec_get) (emacs_env *env, emacs_value vec, ptrdiff_t i)
  275. EMACS_ATTRIBUTE_NONNULL(1);
  276. void (*vec_set) (emacs_env *env, emacs_value vec, ptrdiff_t i,
  277. emacs_value val)
  278. EMACS_ATTRIBUTE_NONNULL(1);
  279. ptrdiff_t (*vec_size) (emacs_env *env, emacs_value vec)
  280. EMACS_ATTRIBUTE_NONNULL(1);
  281. /* Returns whether a quit is pending. */
  282. bool (*should_quit) (emacs_env *env)
  283. EMACS_ATTRIBUTE_NONNULL(1);
  284. };
  285. /* Every module should define a function as follows. */
  286. extern int emacs_module_init (struct emacs_runtime *ert)
  287. EMACS_NOEXCEPT
  288. EMACS_ATTRIBUTE_NONNULL(1);
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif /* EMACS_MODULE_H */