Debian Codegraph @

src/doc/manual/user-guide/embedding.txi

raw
1 @node Embedding ECL
2 @section Embedding ECL
3
4 @menu
5 * Embedding ECL - Embedding Reference::
6 @end menu
7
8 @subsection Minimal Example
9
10 An example project is included in the ECL source distribution in the
11 @code{examples/embed/} directory.
12
13 This example consists of a Common Lisp library (@code{hello-lisp.lisp}) and a
14 system definition (@code{hello-lisp.asd}, @xref{Compiling with ASDF}) that is
15 called from a C program (@code{hello.c}). The example @code{Makefile} shows how
16 to build a static library from the Lisp library and link it with the C program.
17
18 @node Embedding ECL - Embedding Reference
19 @subsection Embedding Reference
20
21 @node Starting and Stopping
22 @subsubsection Starting and Stopping
23
24 @cppdef cl_boot
25 @deftypefun int cl_boot (int argc, char **argv);
26 Setup the lisp environment.
27 @table @var
28 @item argc
29 An integer with the number of arguments to this program.
30 @item argv
31 A vector of strings with the arguments to this program.
32 @end table
33
34 @paragraph Description
35 This function must be called before any other function from the ECL library, including the creation of any lisp object or evaluating any lisp code. The only exception are @coderef{ecl_set_option} and @coderef{ecl_get_option}.
36
37 @end deftypefun
38
39
40 @cppdef cl_shutdown
41 @deftypefun int cl_shutdown (void);
42 Close the lisp environment.
43
44 @paragraph Description
45 This function must be called before exiting a program that uses the ECL environment. It performs some cleaning, including the execution of any finalizers, unloading shared libraries and deleting temporary files that were created by the compiler.
46 @end deftypefun
47
48 @cppdef ecl_set_option
49 @deftypefun void ecl_set_option (int option, cl_fixnum value);
50 Set a boot option.
51
52 @table @var
53 @item option
54 An integer from @ref{tab:boot-options}.
55 @item value
56 A @code{cl_index} value for this option
57 @end table
58
59 @paragraph Description
60 This functions sets the value of different options that have to be customized @emph{before} ECL boots. The table of options and default values [@ref{tab:boot-options}] shows that some of them are boolean, and some of them are unsigned integers.
61
62 We distinguish three sets of values. The first set determines whether ECL handles certain exceptions, such as access to forbidden regions of memory, interrupts via , floating point exceptions, etc.
63
64 The second set is related to the sizes of different stacks. Currently ECL uses four stacks: a bind stack for keeping assignments to special variables; a frame stack for implementing blocks, tagbodys and catch points; an interpreter stack for evaluating bytecodes, and finally the machine or C stack, of the computer we run in. We can set the expected size of these stacks, together with the size of a safety area which, if penetrated, will lead to the generation of a correctable error.
65
66 @float Table,tab:boot-options
67 @caption{Boot options for embedded ECL}
68 @multitable @columnfractions .3 .15 .16 .39
69 @headitem Name @code{(ECL_OPT_*)} @tab Type @tab Default @tab Description
70 @item @code{INCREMENTAL_GC} @tab boolean @tab TRUE @tab Activate generational garbage collector.
71 @item @code{TRAP_SIGSEGV} @tab boolean @tab TRUE @tab Capture @code{SIGSEGV} signals.
72 @item @code{TRAP_SIGFPE} @tab boolean @tab TRUE @tab Capture floating point exceptions.
73 @item @code{TRAP_SIGINT} @tab boolean @tab TRUE @tab Capture user interrupts.
74 @item @code{TRAP_SIGILL} @tab boolean @tab TRUE @tab Capture @code{SIGILL} exception.
75 @item @code{TRAP_INTERRUPT_SIGNAL} @tab boolean @tab TRUE @tab Capture the signal that implements @coderef{mp:interrupt-process}.
76 @item @code{SIGNAL_HANDLING_THREAD} @tab boolean @tab TRUE @tab Create a signal to capture and process asynchronous threads (@xref{Signals and Interrupts - Asynchronous signals}).
77 @item @code{BOOTED} @tab boolean @tab TRUE/FALSE @tab Has ECL booted (read only).
78 @item @code{BIND_STACK_SIZE} @tab cl_index @tab 8192 @tab Size of stack for binding special variables.
79 @item @code{BIND_STACK_SAFETY_AREA} @tab cl_index @tab 128 @tab
80 @item @code{FRAME_STACK_SIZE} @tab cl_index @tab 2048 @tab Size of stack for nonlocal jumps.
81 @item @code{FRAME_STACK_SAFETY_AREA} @tab cl_index @tab 128 @tab
82 @item @code{LISP_STACK_SIZE} @tab cl_index @tab 32768 @tab Size of interpreter stack.
83 @item @code{LISP_STACK_SAFETY_AREA} @tab cl_index @tab 128 @tab
84 @item @code{C_STACK_SIZE} @tab cl_index @tab 0 or 1048576 @tab Size of C stack in bytes. The effect and default value of this option depends on the operating system. On Unix, the default is 0 which means that ECL will use the stack size provided by the OS. If set to a non-default value, ECL will set the stack size to the given value unless the stack size provided by the OS is already large enough. On Windows, the stack size is set at build time and cannot be changed at runtime. Here, we use a default of 1 MiB. For other operating systems, it is up to the user to set this value to the available stack size so that ECL can reliably detect stack overflows.
85 @item @code{C_STACK_SAFETY_AREA} @tab cl_index @tab 4192 @tab
86 @item @code{THREAD_INTERRUPT_SIGNAL} @tab unsigned int @tab 0 @tab If nonzero, specify the unix signal which is used to communicate between different Lisp threads.
87 @end multitable
88 @end float
89 @end deftypefun
90
91
92 @cppdef ecl_get_option
93 @deftypefun cl_fixnum ecl_get_option (int option);
94 Read the value of a boot option.
95 @table @var
96 @item option
97 An integer from @ref{tab:boot-options}.
98 @end table
99
100 @paragraph Description
101 This functions reads the value of different options that have to be customized @emph{before} ECL boots. The table of options and default values is @ref{tab:boot-options}.
102 @end deftypefun
103
104 @cppdef ecl_import_current_thread
105 @deftypefun bool ecl_import_current_thread (cl_object name, cl_object bindings);
106 Import an external thread in the Lisp environment.
107 @table @var
108 @item name
109 Thread name.
110 @item bindings
111 Unused (specifying initial bindings for external threads is not supported currently)
112 @item returns
113 True if the thread was successfully imported, false otherwise.
114 @end table
115
116 @paragraph Description
117 External threads, i.e. threads which are not created in the Lisp world using the routines described in @ref{Processes (native threads)}, need to be imported with @coderef{ecl_import_current_thread} before Lisp code can be executed.
118
119 @seealso{ecl_release_current_thread}
120 @end deftypefun
121
122 @cppdef ecl_release_current_thread
123 @deftypefun void ecl_release_current_thread (void);
124 Release an external thread imported with @coderef{ecl_import_current_thread}. Must be called before thread exit to prevent memory leaks.
125 @end deftypefun
126
127 @defvr {Environment variable} ECLDIR
128 Specify a non-standard installation directory.
129
130 @paragraph Description
131 ECL includes various files for external modules (e.g. asdf, sockets), character encodings or documentation strings. The installation directory for these files is chosen during build time by the configure script. If the directory is moved to a different place, the @code{ECLDIR} environment variable should be updated accordingly. Note that the contents of the variable are parsed as a Common Lisp pathname, thus it must end with a slash.
132 @end defvr
133
134 @node Catching Errors and Managing Interrupts
135 @subsubsection Catching Errors and Managing Interrupts
136
137 @cppdef ECL_CATCH_ALL
138 @defmac ECL_CATCH_ALL
139 Create a protected region.
140
141 @paragraph C Macro
142 @example
143 @verbatim
144 cl_env_ptr env = ecl_process_env();
145 ECL_CATCH_ALL_BEGIN(env) {
146 /*
147 * Code that is protected. Uncaught lisp conditions, THROW,
148 * signals such as SIGSEGV and SIGBUS may cause jump to
149 * this region.
150 */
151 } ECL_CATCH_ALL_IF_CAUGHT {
152 /*
153 * If the exception, lisp condition or other control transfer
154 * is caught, this code is executed.
155 */
156 } ECL_CATCH_ALL_END
157 /*
158 * In all cases we exit here.
159 */
160 @end verbatim
161 @end example
162
163 @paragraph Description
164 This is a set of three macros that create an @code{unwind-protect} region that prevents any nonlocal transfer of control to outer loops. In the Lisp speak, the previous code is equivalent to
165
166 @lisp
167 (block nil
168 (unwind-protect
169 (progn
170 ;; Code that is protected
171 )
172 (return nil)))
173 @end lisp
174
175 As explained in @coderef{ECL_UNWIND_PROTECT}, it is normally advisable to set up an unwind-protect frame to avoid the embedded lisp code to perform arbitrary transfers of control.
176
177 @seealso{ECL_UNWIND_PROTECT}
178 @end defmac
179
180
181 @cppdef ECL_UNWIND_PROTECT
182 @defmac ECL_UNWIND_PROTECT
183 Create a protected region.
184
185 @paragraph C Macro
186 @example
187 @verbatim
188 cl_env_ptr env = ecl_process_env();
189 ECL_UNWIND_PROTECT_BEGIN(env) {
190 /*
191 * Code that is protected. Uncaught lisp conditions, THROW,
192 * signals such as SIGSEGV and SIGBUS may cause jump to
193 * this region.
194 */
195 } ECL_UNWIND_PROTECT_EXIT {
196 /*
197 * If the exception, lisp condition or other control transfer
198 * is caught, this code is executed. After this code, the
199 * process will jump to the original destination of the
200 * THROW, GOTO or other control statement that was interrupted.
201 */
202 } ECL_UNWIND_PROTECT_END
203 /*
204 * We only exit here if NO nonlocal jump was interrupted.
205 */
206 @end verbatim
207 @end example
208
209 @paragraph Description
210 When embedding ECL it is normally advisable to set up an @code{unwind-protect} frame to avoid the embedded lisp code to perform arbitrary transfers of control. Furthermore, the unwind protect form will be used in at least in the following occasions:
211
212 @itemize
213 @item In a normal program exit, caused by @coderef{ext:quit}, ECL unwinds up to the outermost frame, which may be an @coderef{ECL_CATCH_ALL} or @coderef{ECL_UNWIND_PROTECT} macro.
214 @end itemize
215
216 Besides this, normal mechanisms for exit, such as @coderef{ext:quit}, and uncaught exceptions, such as serious signals (@xref{Signals and Interrupts - Synchronous signals}), are best handled using @code{unwind-protect} blocks.
217
218 @seealso ECL_CATCH_ALL
219 @end defmac
220
221 @cppdef ecl_clear_interrupts
222 @defmac ecl_clear_interrupts ()
223 Clear all pending signals and exceptions.
224
225 @paragraph Description
226 This macro clears all pending interrupts.
227
228 @paragraph See also
229 @coderef{ecl_disable_interrupts} and @coderef{ecl_enable_interrupts}.
230 @end defmac
231
232
233 @cppdef ecl_disable_interrupts
234 @defmac ecl_disable_interrupts ()
235 Postpone handling of signals and exceptions.
236
237 @paragraph Description
238 This macro sets a thread-local flag indicating that all received signals should be queued for later processing. Note that it is not possible to execute lisp code while interrupts are disabled in this way. For this purpose, use the @coderef{mp:without-interrupts} macro. Every call to @coderef{ecl_disable_interrupts} must be followed by a corresponding call to @coderef{ecl_enable_interrupts}, otherwise race conditions will appear.
239
240 @paragraph See also
241 @coderef{ecl_enable_interrupts} and @coderef{ecl_clear_interrupts}.
242 @end defmac
243
244
245 @cppdef ecl_enable_interrupts
246 @defmac ecl_enable_interrupts ();
247 Activate handling of signals and exceptions.
248
249 @paragraph Description
250 This macro sets a thread-local flag indicating that all received signals can be handled. If there are any pending signals, they will be immediately processed.
251
252 @paragraph See also
253 @coderef{ecl_disable_interrupts} and @coderef{ecl_clear_interrupts}.
254 @end defmac
255
256 @cppdef ECL_WITH_LISP_FPE
257 @defmac ECL_WITH_LISP_FPE
258 Execute Lisp code with correct floating point environment
259
260 @paragraph Description
261 Unless floating point exceptions are disabled (via the
262 @code{--without-fpe} configure option or @code{ECL_OPT_TRAP_SIGFPE}
263 runtime option), ECL will change the floating point environment when
264 booting. This macro allows for execution of Lisp code while saving and
265 later restoring the floating point environment of surrounding C code
266 so that changes in the floating point environment don't leak outside.
267
268 @coderef{ECL_WITH_LISP_FPE} can be also used before ECL has booted.
269
270 @exindex Safely executing Lisp code with floating point exceptions in embedding program
271 @paragraph Example
272 @example
273 @verbatim
274 #include <ecl/ecl.h>
275 #include <stdio.h>
276
277 int main(int argc, char **argv) {
278 ECL_WITH_LISP_FPE_BEGIN {
279 cl_boot(argc, argv);
280 } ECL_WITH_LISP_FPE_END;
281
282 double a = 1.0 / 0.0;
283 double b;
284
285 ECL_WITH_LISP_FPE_BEGIN {
286 cl_object form = ecl_read_from_cstring("(handler-case"
287 "(/ 1d0 0d0)"
288 "(division-by-zero () 0d0))");
289 b = ecl_to_double(si_safe_eval(3, form, ECL_NIL, ECL_NIL));
290 } ECL_WITH_LISP_FPE_END;
291
292 printf("%g %g\n", a, b);
293
294 cl_shutdown();
295 return 0;
296 }
297 @end verbatim
298 @end example
299 will output
300 @verbatim
301 inf 0
302 @end verbatim
303
304 @seealso{ext:trap-fpe}
305
306 @end defmac