Merge from origin/emacs-25
[emacs.git] / src / emacs-module.c
blob5075263edffcdfc06742aafd639c0a4ca67c92ab
1 /* emacs-module.c - Module loading and runtime implementation
3 Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #include "emacs-module.h"
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdio.h>
28 #include "lisp.h"
29 #include "dynlib.h"
30 #include "coding.h"
32 #include <intprops.h>
33 #include <verify.h>
36 /* Feature tests. */
38 #if __has_attribute (cleanup)
39 enum { module_has_cleanup = true };
40 #else
41 enum { module_has_cleanup = false };
42 #endif
44 /* Handle to the main thread. Used to verify that modules call us in
45 the right thread. */
46 #ifdef HAVE_PTHREAD
47 # include <pthread.h>
48 static pthread_t main_thread;
49 #elif defined WINDOWSNT
50 #include <windows.h>
51 #include "w32term.h"
52 static DWORD main_thread;
53 #endif
55 /* True if Lisp_Object and emacs_value have the same representation.
56 This is typically true unless WIDE_EMACS_INT. In practice, having
57 the same sizes and alignments and maximums should be a good enough
58 proxy for equality of representation. */
59 enum
61 plain_values
62 = (sizeof (Lisp_Object) == sizeof (emacs_value)
63 && alignof (Lisp_Object) == alignof (emacs_value)
64 && INTPTR_MAX == EMACS_INT_MAX)
67 /* Function prototype for the module init function. */
68 typedef int (*emacs_init_function) (struct emacs_runtime *);
70 /* Function prototype for the module Lisp functions. */
71 typedef emacs_value (*emacs_subr) (emacs_env *, ptrdiff_t,
72 emacs_value [], void *);
74 /* Function prototype for module user-pointer finalizers. These
75 should not throw C++ exceptions, so emacs-module.h declares the
76 corresponding interfaces with EMACS_NOEXCEPT. There is only C code
77 in this module, though, so this constraint is not enforced here. */
78 typedef void (*emacs_finalizer_function) (void *);
81 /* Private runtime and environment members. */
83 /* The private part of an environment stores the current non local exit state
84 and holds the `emacs_value' objects allocated during the lifetime
85 of the environment. */
86 struct emacs_env_private
88 enum emacs_funcall_exit pending_non_local_exit;
90 /* Dedicated storage for non-local exit symbol and data so that
91 storage is always available for them, even in an out-of-memory
92 situation. */
93 Lisp_Object non_local_exit_symbol, non_local_exit_data;
96 /* The private parts of an `emacs_runtime' object contain the initial
97 environment. */
98 struct emacs_runtime_private
100 /* FIXME: Ideally, we would just define "struct emacs_runtime_private"
101 as a synonym of "emacs_env", but I don't know how to do that in C. */
102 emacs_env pub;
106 /* Forward declarations. */
108 struct module_fun_env;
110 static Lisp_Object module_format_fun_env (const struct module_fun_env *);
111 static Lisp_Object value_to_lisp (emacs_value);
112 static emacs_value lisp_to_value (Lisp_Object);
113 static enum emacs_funcall_exit module_non_local_exit_check (emacs_env *);
114 static void check_main_thread (void);
115 static void finalize_environment (struct emacs_env_private *);
116 static void initialize_environment (emacs_env *, struct emacs_env_private *priv);
117 static void module_handle_signal (emacs_env *, Lisp_Object);
118 static void module_handle_throw (emacs_env *, Lisp_Object);
119 static void module_non_local_exit_signal_1 (emacs_env *, Lisp_Object, Lisp_Object);
120 static void module_non_local_exit_throw_1 (emacs_env *, Lisp_Object, Lisp_Object);
121 static void module_out_of_memory (emacs_env *);
122 static void module_reset_handlerlist (const int *);
124 /* We used to return NULL when emacs_value was a different type from
125 Lisp_Object, but nowadays we just use Qnil instead. Although they
126 happen to be the same thing in the current implementation, module
127 code should not assume this. */
128 verify (NIL_IS_ZERO);
129 static emacs_value const module_nil = 0;
131 /* Convenience macros for non-local exit handling. */
133 /* FIXME: The following implementation for non-local exit handling
134 does not support recovery from stack overflow, see sysdep.c. */
136 /* Emacs uses setjmp and longjmp for non-local exits, but
137 module frames cannot be skipped because they are in general
138 not prepared for long jumps (e.g., the behavior in C++ is undefined
139 if objects with nontrivial destructors would be skipped).
140 Therefore, catch all non-local exits. There are two kinds of
141 non-local exits: `signal' and `throw'. The macros in this section
142 can be used to catch both. Use macros to avoid additional variants
143 of `internal_condition_case' etc., and to avoid worrying about
144 passing information to the handler functions. */
146 /* Place this macro at the beginning of a function returning a number
147 or a pointer to handle non-local exits. The function must have an
148 ENV parameter. The function will return the specified value if a
149 signal or throw is caught. */
150 // TODO: Have Fsignal check for CATCHER_ALL so we only have to install
151 // one handler.
152 #define MODULE_HANDLE_NONLOCAL_EXIT(retval) \
153 MODULE_SETJMP (CONDITION_CASE, module_handle_signal, retval); \
154 MODULE_SETJMP (CATCHER_ALL, module_handle_throw, retval)
156 #define MODULE_SETJMP(handlertype, handlerfunc, retval) \
157 MODULE_SETJMP_1 (handlertype, handlerfunc, retval, \
158 internal_handler_##handlertype, \
159 internal_cleanup_##handlertype)
161 /* It is very important that pushing the handler doesn't itself raise
162 a signal. Install the cleanup only after the handler has been
163 pushed. Use __attribute__ ((cleanup)) to avoid
164 non-local-exit-prone manual cleanup.
166 The do-while forces uses of the macro to be followed by a semicolon.
167 This macro cannot enclose its entire body inside a do-while, as the
168 code after the macro may longjmp back into the macro, which means
169 its local variable C must stay live in later code. */
171 // TODO: Make backtraces work if this macros is used.
173 #define MODULE_SETJMP_1(handlertype, handlerfunc, retval, c, dummy) \
174 if (module_non_local_exit_check (env) != emacs_funcall_exit_return) \
175 return retval; \
176 struct handler *c = push_handler_nosignal (Qt, handlertype); \
177 if (!c) \
179 module_out_of_memory (env); \
180 return retval; \
182 verify (module_has_cleanup); \
183 int dummy __attribute__ ((cleanup (module_reset_handlerlist))); \
184 if (sys_setjmp (c->jmp)) \
186 (handlerfunc) (env, c->val); \
187 return retval; \
189 do { } while (false)
192 /* Function environments. */
194 /* A function environment is an auxiliary structure used by
195 `module_make_function' to store information about a module
196 function. It is stored in a save pointer and retrieved by
197 `internal--module-call'. Its members correspond to the arguments
198 given to `module_make_function'. */
200 struct module_fun_env
202 ptrdiff_t min_arity, max_arity;
203 emacs_subr subr;
204 void *data;
208 /* Implementation of runtime and environment functions.
210 These should abide by the following rules:
212 1. The first argument should always be a pointer to emacs_env.
214 2. Each function should first call check_main_thread. Note that
215 this function is a no-op unless Emacs was built with
216 --enable-checking.
218 3. The very next thing each function should do is check that the
219 emacs_env object does not have a non-local exit indication set,
220 by calling module_non_local_exit_check. If that returns
221 anything but emacs_funcall_exit_return, the function should do
222 nothing and return immediately with an error indication, without
223 clobbering the existing error indication in emacs_env. This is
224 needed for correct reporting of Lisp errors to the Emacs Lisp
225 interpreter.
227 4. Any function that needs to call Emacs facilities, such as
228 encoding or decoding functions, or 'intern', or 'make_string',
229 should protect itself from signals and 'throw' in the called
230 Emacs functions, by placing the macro
231 MODULE_HANDLE_NONLOCAL_EXIT right after the above 2 tests.
233 5. Do NOT use 'eassert' for checking validity of user code in the
234 module. Instead, make those checks part of the code, and if the
235 check fails, call 'module_non_local_exit_signal_1' or
236 'module_non_local_exit_throw_1' to report the error. This is
237 because using 'eassert' in these situations will abort Emacs
238 instead of reporting the error back to Lisp, and also because
239 'eassert' is compiled to nothing in the release version. */
241 /* Use MODULE_FUNCTION_BEGIN to implement steps 2 through 4 for most
242 environment functions. On error it will return its argument, which
243 should be a sentinel value. */
245 #define MODULE_FUNCTION_BEGIN(error_retval) \
246 check_main_thread (); \
247 if (module_non_local_exit_check (env) != emacs_funcall_exit_return) \
248 return error_retval; \
249 MODULE_HANDLE_NONLOCAL_EXIT (error_retval)
251 static void
252 CHECK_USER_PTR (Lisp_Object obj)
254 CHECK_TYPE (USER_PTRP (obj), Quser_ptrp, obj);
257 /* Catch signals and throws only if the code can actually signal or
258 throw. If checking is enabled, abort if the current thread is not
259 the Emacs main thread. */
261 static emacs_env *
262 module_get_environment (struct emacs_runtime *ert)
264 check_main_thread ();
265 return &ert->private_members->pub;
268 /* To make global refs (GC-protected global values) keep a hash that
269 maps global Lisp objects to reference counts. */
271 static emacs_value
272 module_make_global_ref (emacs_env *env, emacs_value ref)
274 MODULE_FUNCTION_BEGIN (module_nil);
275 struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash);
276 Lisp_Object new_obj = value_to_lisp (ref);
277 EMACS_UINT hashcode;
278 ptrdiff_t i = hash_lookup (h, new_obj, &hashcode);
280 if (i >= 0)
282 Lisp_Object value = HASH_VALUE (h, i);
283 EMACS_INT refcount = XFASTINT (value) + 1;
284 if (MOST_POSITIVE_FIXNUM < refcount)
285 xsignal0 (Qoverflow_error);
286 value = make_natnum (refcount);
287 set_hash_value_slot (h, i, value);
289 else
291 hash_put (h, new_obj, make_natnum (1), hashcode);
294 return lisp_to_value (new_obj);
297 static void
298 module_free_global_ref (emacs_env *env, emacs_value ref)
300 /* TODO: This probably never signals. */
301 /* FIXME: Wait a minute. Shouldn't this function report an error if
302 the hash lookup fails? */
303 MODULE_FUNCTION_BEGIN ();
304 struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash);
305 Lisp_Object obj = value_to_lisp (ref);
306 EMACS_UINT hashcode;
307 ptrdiff_t i = hash_lookup (h, obj, &hashcode);
309 if (i >= 0)
311 Lisp_Object value = HASH_VALUE (h, i);
312 EMACS_INT refcount = XFASTINT (value) - 1;
313 if (refcount > 0)
315 value = make_natnum (refcount);
316 set_hash_value_slot (h, i, value);
318 else
319 hash_remove_from_table (h, value);
323 static enum emacs_funcall_exit
324 module_non_local_exit_check (emacs_env *env)
326 check_main_thread ();
327 return env->private_members->pending_non_local_exit;
330 static void
331 module_non_local_exit_clear (emacs_env *env)
333 check_main_thread ();
334 env->private_members->pending_non_local_exit = emacs_funcall_exit_return;
337 static enum emacs_funcall_exit
338 module_non_local_exit_get (emacs_env *env, emacs_value *sym, emacs_value *data)
340 check_main_thread ();
341 struct emacs_env_private *p = env->private_members;
342 if (p->pending_non_local_exit != emacs_funcall_exit_return)
344 /* FIXME: lisp_to_value can exit non-locally. */
345 *sym = lisp_to_value (p->non_local_exit_symbol);
346 *data = lisp_to_value (p->non_local_exit_data);
348 return p->pending_non_local_exit;
351 /* Like for `signal', DATA must be a list. */
352 static void
353 module_non_local_exit_signal (emacs_env *env, emacs_value sym, emacs_value data)
355 check_main_thread ();
356 if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
357 module_non_local_exit_signal_1 (env, value_to_lisp (sym),
358 value_to_lisp (data));
361 static void
362 module_non_local_exit_throw (emacs_env *env, emacs_value tag, emacs_value value)
364 check_main_thread ();
365 if (module_non_local_exit_check (env) == emacs_funcall_exit_return)
366 module_non_local_exit_throw_1 (env, value_to_lisp (tag),
367 value_to_lisp (value));
370 /* A module function is lambda function that calls
371 `internal--module-call', passing the function pointer of the module
372 function along with the module emacs_env pointer as arguments.
374 (function (lambda (&rest arglist)
375 (internal--module-call envobj arglist))) */
377 static emacs_value
378 module_make_function (emacs_env *env, ptrdiff_t min_arity, ptrdiff_t max_arity,
379 emacs_subr subr, const char *documentation,
380 void *data)
382 MODULE_FUNCTION_BEGIN (module_nil);
384 if (! (0 <= min_arity
385 && (max_arity < 0
386 ? max_arity == emacs_variadic_function
387 : min_arity <= max_arity)))
388 xsignal2 (Qinvalid_arity, make_number (min_arity), make_number (max_arity));
390 /* FIXME: This should be freed when envobj is GC'd. */
391 struct module_fun_env *envptr = xmalloc (sizeof *envptr);
392 envptr->min_arity = min_arity;
393 envptr->max_arity = max_arity;
394 envptr->subr = subr;
395 envptr->data = data;
397 Lisp_Object envobj = make_save_ptr (envptr);
398 Lisp_Object doc = Qnil;
399 if (documentation)
401 AUTO_STRING (unibyte_doc, documentation);
402 doc = code_convert_string_norecord (unibyte_doc, Qutf_8, false);
405 /* FIXME: Use a bytecompiled object, or even better a subr. */
406 Lisp_Object ret = list4 (Qlambda,
407 list2 (Qand_rest, Qargs),
408 doc,
409 list4 (Qapply,
410 list2 (Qfunction, Qinternal__module_call),
411 envobj,
412 Qargs));
414 return lisp_to_value (ret);
417 static emacs_value
418 module_funcall (emacs_env *env, emacs_value fun, ptrdiff_t nargs,
419 emacs_value args[])
421 MODULE_FUNCTION_BEGIN (module_nil);
423 /* Make a new Lisp_Object array starting with the function as the
424 first arg, because that's what Ffuncall takes. */
425 Lisp_Object *newargs;
426 USE_SAFE_ALLOCA;
427 ptrdiff_t nargs1;
428 if (INT_ADD_WRAPV (nargs, 1, &nargs1))
429 xsignal0 (Qoverflow_error);
430 SAFE_ALLOCA_LISP (newargs, nargs1);
431 newargs[0] = value_to_lisp (fun);
432 for (ptrdiff_t i = 0; i < nargs; i++)
433 newargs[1 + i] = value_to_lisp (args[i]);
434 emacs_value result = lisp_to_value (Ffuncall (nargs1, newargs));
435 SAFE_FREE ();
436 return result;
439 static emacs_value
440 module_intern (emacs_env *env, const char *name)
442 MODULE_FUNCTION_BEGIN (module_nil);
443 return lisp_to_value (intern (name));
446 static emacs_value
447 module_type_of (emacs_env *env, emacs_value value)
449 MODULE_FUNCTION_BEGIN (module_nil);
450 return lisp_to_value (Ftype_of (value_to_lisp (value)));
453 static bool
454 module_is_not_nil (emacs_env *env, emacs_value value)
456 check_main_thread ();
457 if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
458 return false;
459 return ! NILP (value_to_lisp (value));
462 static bool
463 module_eq (emacs_env *env, emacs_value a, emacs_value b)
465 check_main_thread ();
466 if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
467 return false;
468 return EQ (value_to_lisp (a), value_to_lisp (b));
471 static intmax_t
472 module_extract_integer (emacs_env *env, emacs_value n)
474 MODULE_FUNCTION_BEGIN (0);
475 Lisp_Object l = value_to_lisp (n);
476 CHECK_NUMBER (l);
477 return XINT (l);
480 static emacs_value
481 module_make_integer (emacs_env *env, intmax_t n)
483 MODULE_FUNCTION_BEGIN (module_nil);
484 if (FIXNUM_OVERFLOW_P (n))
485 xsignal0 (Qoverflow_error);
486 return lisp_to_value (make_number (n));
489 static double
490 module_extract_float (emacs_env *env, emacs_value f)
492 MODULE_FUNCTION_BEGIN (0);
493 Lisp_Object lisp = value_to_lisp (f);
494 CHECK_TYPE (FLOATP (lisp), Qfloatp, lisp);
495 return XFLOAT_DATA (lisp);
498 static emacs_value
499 module_make_float (emacs_env *env, double d)
501 MODULE_FUNCTION_BEGIN (module_nil);
502 return lisp_to_value (make_float (d));
505 static bool
506 module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer,
507 ptrdiff_t *length)
509 MODULE_FUNCTION_BEGIN (false);
510 Lisp_Object lisp_str = value_to_lisp (value);
511 CHECK_STRING (lisp_str);
513 Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str);
514 ptrdiff_t raw_size = SBYTES (lisp_str_utf8);
515 ptrdiff_t required_buf_size = raw_size + 1;
517 eassert (length != NULL);
519 if (buffer == NULL)
521 *length = required_buf_size;
522 return true;
525 eassert (*length >= 0);
527 if (*length < required_buf_size)
529 *length = required_buf_size;
530 xsignal0 (Qargs_out_of_range);
533 *length = required_buf_size;
534 memcpy (buffer, SDATA (lisp_str_utf8), raw_size + 1);
536 return true;
539 static emacs_value
540 module_make_string (emacs_env *env, const char *str, ptrdiff_t length)
542 MODULE_FUNCTION_BEGIN (module_nil);
543 AUTO_STRING_WITH_LEN (lstr, str, length);
544 return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false));
547 static emacs_value
548 module_make_user_ptr (emacs_env *env, emacs_finalizer_function fin, void *ptr)
550 MODULE_FUNCTION_BEGIN (module_nil);
551 return lisp_to_value (make_user_ptr (fin, ptr));
554 static void *
555 module_get_user_ptr (emacs_env *env, emacs_value uptr)
557 MODULE_FUNCTION_BEGIN (NULL);
558 Lisp_Object lisp = value_to_lisp (uptr);
559 CHECK_USER_PTR (lisp);
560 return XUSER_PTR (lisp)->p;
563 static void
564 module_set_user_ptr (emacs_env *env, emacs_value uptr, void *ptr)
566 /* FIXME: This function should return bool because it can fail. */
567 MODULE_FUNCTION_BEGIN ();
568 Lisp_Object lisp = value_to_lisp (uptr);
569 CHECK_USER_PTR (lisp);
570 XUSER_PTR (lisp)->p = ptr;
573 static emacs_finalizer_function
574 module_get_user_finalizer (emacs_env *env, emacs_value uptr)
576 MODULE_FUNCTION_BEGIN (NULL);
577 Lisp_Object lisp = value_to_lisp (uptr);
578 CHECK_USER_PTR (lisp);
579 return XUSER_PTR (lisp)->finalizer;
582 static void
583 module_set_user_finalizer (emacs_env *env, emacs_value uptr,
584 emacs_finalizer_function fin)
586 /* FIXME: This function should return bool because it can fail. */
587 MODULE_FUNCTION_BEGIN ();
588 Lisp_Object lisp = value_to_lisp (uptr);
589 CHECK_USER_PTR (lisp);
590 XUSER_PTR (lisp)->finalizer = fin;
593 static void
594 check_vec_index (Lisp_Object lvec, ptrdiff_t i)
596 CHECK_VECTOR (lvec);
597 if (! (0 <= i && i < ASIZE (lvec)))
598 args_out_of_range_3 (make_fixnum_or_float (i),
599 make_number (0), make_number (ASIZE (lvec) - 1));
602 static void
603 module_vec_set (emacs_env *env, emacs_value vec, ptrdiff_t i, emacs_value val)
605 /* FIXME: This function should return bool because it can fail. */
606 MODULE_FUNCTION_BEGIN ();
607 Lisp_Object lvec = value_to_lisp (vec);
608 check_vec_index (lvec, i);
609 ASET (lvec, i, value_to_lisp (val));
612 static emacs_value
613 module_vec_get (emacs_env *env, emacs_value vec, ptrdiff_t i)
615 MODULE_FUNCTION_BEGIN (module_nil);
616 Lisp_Object lvec = value_to_lisp (vec);
617 check_vec_index (lvec, i);
618 return lisp_to_value (AREF (lvec, i));
621 static ptrdiff_t
622 module_vec_size (emacs_env *env, emacs_value vec)
624 /* FIXME: Return a sentinel value (e.g., -1) on error. */
625 MODULE_FUNCTION_BEGIN (0);
626 Lisp_Object lvec = value_to_lisp (vec);
627 CHECK_VECTOR (lvec);
628 return ASIZE (lvec);
632 /* Subroutines. */
634 DEFUN ("module-load", Fmodule_load, Smodule_load, 1, 1, 0,
635 doc: /* Load module FILE. */)
636 (Lisp_Object file)
638 dynlib_handle_ptr handle;
639 emacs_init_function module_init;
640 void *gpl_sym;
642 CHECK_STRING (file);
643 handle = dynlib_open (SSDATA (file));
644 if (!handle)
645 error ("Cannot load file %s: %s", SDATA (file), dynlib_error ());
647 gpl_sym = dynlib_sym (handle, "plugin_is_GPL_compatible");
648 if (!gpl_sym)
649 error ("Module %s is not GPL compatible", SDATA (file));
651 module_init = (emacs_init_function) dynlib_func (handle, "emacs_module_init");
652 if (!module_init)
653 error ("Module %s does not have an init function.", SDATA (file));
655 struct emacs_runtime_private rt; /* Includes the public emacs_env. */
656 struct emacs_env_private priv;
657 initialize_environment (&rt.pub, &priv);
658 struct emacs_runtime pub =
660 .size = sizeof pub,
661 .private_members = &rt,
662 .get_environment = module_get_environment
664 int r = module_init (&pub);
665 finalize_environment (&priv);
667 if (r != 0)
669 if (FIXNUM_OVERFLOW_P (r))
670 xsignal0 (Qoverflow_error);
671 xsignal2 (Qmodule_load_failed, file, make_number (r));
674 return Qt;
677 DEFUN ("internal--module-call", Finternal_module_call, Sinternal_module_call, 1, MANY, 0,
678 doc: /* Internal function to call a module function.
679 ENVOBJ is a save pointer to a module_fun_env structure.
680 ARGLIST is a list of arguments passed to SUBRPTR.
681 usage: (module-call ENVOBJ &rest ARGLIST) */)
682 (ptrdiff_t nargs, Lisp_Object *arglist)
684 Lisp_Object envobj = arglist[0];
685 /* FIXME: Rather than use a save_value, we should create a new object type.
686 Making save_value visible to Lisp is wrong. */
687 CHECK_TYPE (SAVE_VALUEP (envobj), Qsave_value_p, envobj);
688 struct Lisp_Save_Value *save_value = XSAVE_VALUE (envobj);
689 CHECK_TYPE (save_type (save_value, 0) == SAVE_POINTER, Qsave_pointer_p, envobj);
690 /* FIXME: We have no reason to believe that XSAVE_POINTER (envobj, 0)
691 is a module_fun_env pointer. If some other part of Emacs also
692 exports save_value objects to Elisp, than we may be getting here this
693 other kind of save_value which will likely hold something completely
694 different in this field. */
695 struct module_fun_env *envptr = XSAVE_POINTER (envobj, 0);
696 EMACS_INT len = nargs - 1;
697 eassume (0 <= envptr->min_arity);
698 if (! (envptr->min_arity <= len
699 && len <= (envptr->max_arity < 0 ? PTRDIFF_MAX : envptr->max_arity)))
700 xsignal2 (Qwrong_number_of_arguments, module_format_fun_env (envptr),
701 make_number (len));
703 emacs_env pub;
704 struct emacs_env_private priv;
705 initialize_environment (&pub, &priv);
707 USE_SAFE_ALLOCA;
708 emacs_value *args;
709 if (plain_values)
710 args = (emacs_value *) arglist + 1;
711 else
713 args = SAFE_ALLOCA (len * sizeof *args);
714 for (ptrdiff_t i = 0; i < len; i++)
715 args[i] = lisp_to_value (arglist[i + 1]);
718 emacs_value ret = envptr->subr (&pub, len, args, envptr->data);
719 SAFE_FREE ();
721 eassert (&priv == pub.private_members);
723 switch (priv.pending_non_local_exit)
725 case emacs_funcall_exit_return:
726 finalize_environment (&priv);
727 return value_to_lisp (ret);
728 case emacs_funcall_exit_signal:
730 Lisp_Object symbol = priv.non_local_exit_symbol;
731 Lisp_Object data = priv.non_local_exit_data;
732 finalize_environment (&priv);
733 xsignal (symbol, data);
735 case emacs_funcall_exit_throw:
737 Lisp_Object tag = priv.non_local_exit_symbol;
738 Lisp_Object value = priv.non_local_exit_data;
739 finalize_environment (&priv);
740 Fthrow (tag, value);
742 default:
743 eassume (false);
748 /* Helper functions. */
750 static void
751 check_main_thread (void)
753 #ifdef HAVE_PTHREAD
754 eassert (pthread_equal (pthread_self (), main_thread));
755 #elif defined WINDOWSNT
756 eassert (GetCurrentThreadId () == main_thread);
757 #endif
760 static void
761 module_non_local_exit_signal_1 (emacs_env *env, Lisp_Object sym,
762 Lisp_Object data)
764 struct emacs_env_private *p = env->private_members;
765 if (p->pending_non_local_exit == emacs_funcall_exit_return)
767 p->pending_non_local_exit = emacs_funcall_exit_signal;
768 p->non_local_exit_symbol = sym;
769 p->non_local_exit_data = data;
773 static void
774 module_non_local_exit_throw_1 (emacs_env *env, Lisp_Object tag,
775 Lisp_Object value)
777 struct emacs_env_private *p = env->private_members;
778 if (p->pending_non_local_exit == emacs_funcall_exit_return)
780 p->pending_non_local_exit = emacs_funcall_exit_throw;
781 p->non_local_exit_symbol = tag;
782 p->non_local_exit_data = value;
786 /* Signal an out-of-memory condition to the caller. */
787 static void
788 module_out_of_memory (emacs_env *env)
790 /* TODO: Reimplement this so it works even if memory-signal-data has
791 been modified. */
792 module_non_local_exit_signal_1 (env, XCAR (Vmemory_signal_data),
793 XCDR (Vmemory_signal_data));
797 /* Value conversion. */
799 /* Unique Lisp_Object used to mark those emacs_values which are really
800 just containers holding a Lisp_Object that does not fit as an emacs_value,
801 either because it is an integer out of range, or is not properly aligned.
802 Used only if !plain_values. */
803 static Lisp_Object ltv_mark;
805 /* Convert V to the corresponding internal object O, such that
806 V == lisp_to_value_bits (O). Never fails. */
807 static Lisp_Object
808 value_to_lisp_bits (emacs_value v)
810 intptr_t i = (intptr_t) v;
811 if (plain_values || USE_LSB_TAG)
812 return XIL (i);
814 /* With wide EMACS_INT and when tag bits are the most significant,
815 reassembling integers differs from reassembling pointers in two
816 ways. First, save and restore the least-significant bits of the
817 integer, not the most-significant bits. Second, sign-extend the
818 integer when restoring, but zero-extend pointers because that
819 makes TAG_PTR faster. */
821 EMACS_UINT tag = i & (GCALIGNMENT - 1);
822 EMACS_UINT untagged = i - tag;
823 switch (tag)
825 case_Lisp_Int:
827 bool negative = tag & 1;
828 EMACS_UINT sign_extension
829 = negative ? VALMASK & ~(INTPTR_MAX >> INTTYPEBITS): 0;
830 uintptr_t u = i;
831 intptr_t all_but_sign = u >> GCTYPEBITS;
832 untagged = sign_extension + all_but_sign;
833 break;
837 return XIL ((tag << VALBITS) + untagged);
840 /* If V was computed from lisp_to_value (O), then return O.
841 Exits non-locally only if the stack overflows. */
842 static Lisp_Object
843 value_to_lisp (emacs_value v)
845 Lisp_Object o = value_to_lisp_bits (v);
846 if (! plain_values && CONSP (o) && EQ (XCDR (o), ltv_mark))
847 o = XCAR (o);
848 return o;
851 /* Attempt to convert O to an emacs_value. Do not do any checking or
852 or allocate any storage; the caller should prevent or detect
853 any resulting bit pattern that is not a valid emacs_value. */
854 static emacs_value
855 lisp_to_value_bits (Lisp_Object o)
857 EMACS_UINT u = XLI (o);
859 /* Compress U into the space of a pointer, possibly losing information. */
860 uintptr_t p = (plain_values || USE_LSB_TAG
862 : (INTEGERP (o) ? u << VALBITS : u & VALMASK) + XTYPE (o));
863 return (emacs_value) p;
866 #ifndef HAVE_STRUCT_ATTRIBUTE_ALIGNED
867 enum { HAVE_STRUCT_ATTRIBUTE_ALIGNED = 0 };
868 #endif
870 /* Convert O to an emacs_value. Allocate storage if needed; this can
871 signal if memory is exhausted. Must be an injective function. */
872 static emacs_value
873 lisp_to_value (Lisp_Object o)
875 emacs_value v = lisp_to_value_bits (o);
877 if (! EQ (o, value_to_lisp_bits (v)))
879 /* Package the incompressible object pointer inside a pair
880 that is compressible. */
881 Lisp_Object pair = Fcons (o, ltv_mark);
883 if (! HAVE_STRUCT_ATTRIBUTE_ALIGNED)
885 /* Keep calling Fcons until it returns a compressible pair.
886 This shouldn't take long. */
887 while ((intptr_t) XCONS (pair) & (GCALIGNMENT - 1))
888 pair = Fcons (o, pair);
890 /* Plant the mark. The garbage collector will eventually
891 reclaim any just-allocated incompressible pairs. */
892 XSETCDR (pair, ltv_mark);
895 v = (emacs_value) ((intptr_t) XCONS (pair) + Lisp_Cons);
898 eassert (EQ (o, value_to_lisp (v)));
899 return v;
903 /* Environment lifetime management. */
905 /* Must be called before the environment can be used. */
906 static void
907 initialize_environment (emacs_env *env, struct emacs_env_private *priv)
909 priv->pending_non_local_exit = emacs_funcall_exit_return;
910 env->size = sizeof *env;
911 env->private_members = priv;
912 env->make_global_ref = module_make_global_ref;
913 env->free_global_ref = module_free_global_ref;
914 env->non_local_exit_check = module_non_local_exit_check;
915 env->non_local_exit_clear = module_non_local_exit_clear;
916 env->non_local_exit_get = module_non_local_exit_get;
917 env->non_local_exit_signal = module_non_local_exit_signal;
918 env->non_local_exit_throw = module_non_local_exit_throw;
919 env->make_function = module_make_function;
920 env->funcall = module_funcall;
921 env->intern = module_intern;
922 env->type_of = module_type_of;
923 env->is_not_nil = module_is_not_nil;
924 env->eq = module_eq;
925 env->extract_integer = module_extract_integer;
926 env->make_integer = module_make_integer;
927 env->extract_float = module_extract_float;
928 env->make_float = module_make_float;
929 env->copy_string_contents = module_copy_string_contents;
930 env->make_string = module_make_string;
931 env->make_user_ptr = module_make_user_ptr;
932 env->get_user_ptr = module_get_user_ptr;
933 env->set_user_ptr = module_set_user_ptr;
934 env->get_user_finalizer = module_get_user_finalizer;
935 env->set_user_finalizer = module_set_user_finalizer;
936 env->vec_set = module_vec_set;
937 env->vec_get = module_vec_get;
938 env->vec_size = module_vec_size;
939 Vmodule_environments = Fcons (make_save_ptr (env), Vmodule_environments);
942 /* Must be called before the lifetime of the environment object
943 ends. */
944 static void
945 finalize_environment (struct emacs_env_private *env)
947 Vmodule_environments = XCDR (Vmodule_environments);
951 /* Non-local exit handling. */
953 /* Must be called after setting up a handler immediately before
954 returning from the function. See the comments in lisp.h and the
955 code in eval.c for details. The macros below arrange for this
956 function to be called automatically. DUMMY is ignored. */
957 static void
958 module_reset_handlerlist (const int *dummy)
960 handlerlist = handlerlist->next;
963 /* Called on `signal'. ERR is a pair (SYMBOL . DATA), which gets
964 stored in the environment. Set the pending non-local exit flag. */
965 static void
966 module_handle_signal (emacs_env *env, Lisp_Object err)
968 module_non_local_exit_signal_1 (env, XCAR (err), XCDR (err));
971 /* Called on `throw'. TAG_VAL is a pair (TAG . VALUE), which gets
972 stored in the environment. Set the pending non-local exit flag. */
973 static void
974 module_handle_throw (emacs_env *env, Lisp_Object tag_val)
976 module_non_local_exit_throw_1 (env, XCAR (tag_val), XCDR (tag_val));
980 /* Function environments. */
982 /* Return a string object that contains a user-friendly
983 representation of the function environment. */
984 static Lisp_Object
985 module_format_fun_env (const struct module_fun_env *env)
987 /* Try to print a function name if possible. */
988 const char *path, *sym;
989 static char const noaddr_format[] = "#<module function at %p>";
990 char buffer[sizeof noaddr_format + INT_STRLEN_BOUND (intptr_t) + 256];
991 char *buf = buffer;
992 ptrdiff_t bufsize = sizeof buffer;
993 ptrdiff_t size
994 = (dynlib_addr (env->subr, &path, &sym)
995 ? exprintf (&buf, &bufsize, buffer, -1,
996 "#<module function %s from %s>", sym, path)
997 : sprintf (buffer, noaddr_format, env->subr));
998 AUTO_STRING_WITH_LEN (unibyte_result, buffer, size);
999 Lisp_Object result = code_convert_string_norecord (unibyte_result,
1000 Qutf_8, false);
1001 if (buf != buffer)
1002 xfree (buf);
1003 return result;
1007 /* Segment initializer. */
1009 void
1010 syms_of_module (void)
1012 if (!plain_values)
1013 ltv_mark = Fcons (Qnil, Qnil);
1014 eassert (NILP (value_to_lisp (module_nil)));
1016 DEFSYM (Qmodule_refs_hash, "module-refs-hash");
1017 DEFVAR_LISP ("module-refs-hash", Vmodule_refs_hash,
1018 doc: /* Module global reference table. */);
1020 Vmodule_refs_hash
1021 = make_hash_table (hashtest_eq, make_number (DEFAULT_HASH_SIZE),
1022 make_float (DEFAULT_REHASH_SIZE),
1023 make_float (DEFAULT_REHASH_THRESHOLD),
1024 Qnil);
1025 Funintern (Qmodule_refs_hash, Qnil);
1027 DEFSYM (Qmodule_environments, "module-environments");
1028 DEFVAR_LISP ("module-environments", Vmodule_environments,
1029 doc: /* List of active module environments. */);
1030 Vmodule_environments = Qnil;
1031 /* Unintern `module-environments' because it is only used
1032 internally. */
1033 Funintern (Qmodule_environments, Qnil);
1035 DEFSYM (Qmodule_load_failed, "module-load-failed");
1036 Fput (Qmodule_load_failed, Qerror_conditions,
1037 listn (CONSTYPE_PURE, 2, Qmodule_load_failed, Qerror));
1038 Fput (Qmodule_load_failed, Qerror_message,
1039 build_pure_c_string ("Module load failed"));
1041 DEFSYM (Qinvalid_module_call, "invalid-module-call");
1042 Fput (Qinvalid_module_call, Qerror_conditions,
1043 listn (CONSTYPE_PURE, 2, Qinvalid_module_call, Qerror));
1044 Fput (Qinvalid_module_call, Qerror_message,
1045 build_pure_c_string ("Invalid module call"));
1047 DEFSYM (Qinvalid_arity, "invalid-arity");
1048 Fput (Qinvalid_arity, Qerror_conditions,
1049 listn (CONSTYPE_PURE, 2, Qinvalid_arity, Qerror));
1050 Fput (Qinvalid_arity, Qerror_message,
1051 build_pure_c_string ("Invalid function arity"));
1053 /* Unintern `module-refs-hash' because it is internal-only and Lisp
1054 code or modules should not access it. */
1055 Funintern (Qmodule_refs_hash, Qnil);
1057 DEFSYM (Qsave_value_p, "save-value-p");
1058 DEFSYM (Qsave_pointer_p, "save-pointer-p");
1060 defsubr (&Smodule_load);
1062 DEFSYM (Qinternal__module_call, "internal--module-call");
1063 defsubr (&Sinternal_module_call);
1066 /* Unlike syms_of_module, this initializer is called even from an
1067 initialized (dumped) Emacs. */
1069 void
1070 module_init (void)
1072 /* It is not guaranteed that dynamic initializers run in the main thread,
1073 therefore detect the main thread here. */
1074 #ifdef HAVE_PTHREAD
1075 main_thread = pthread_self ();
1076 #elif defined WINDOWSNT
1077 /* The 'main' function already recorded the main thread's thread ID,
1078 so we need just to use it . */
1079 main_thread = dwMainThreadId;
1080 #endif