throw.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include "libguile/_scm.h"
  22. #include "libguile/async.h"
  23. #include "libguile/smob.h"
  24. #include "libguile/alist.h"
  25. #include "libguile/eval.h"
  26. #include "libguile/eq.h"
  27. #include "libguile/dynwind.h"
  28. #include "libguile/backtrace.h"
  29. #include "libguile/debug.h"
  30. #include "libguile/continuations.h"
  31. #include "libguile/stackchk.h"
  32. #include "libguile/stacks.h"
  33. #include "libguile/fluids.h"
  34. #include "libguile/ports.h"
  35. #include "libguile/lang.h"
  36. #include "libguile/validate.h"
  37. #include "libguile/throw.h"
  38. #include "libguile/init.h"
  39. /* the jump buffer data structure */
  40. static scm_t_bits tc16_jmpbuffer;
  41. #define SCM_JMPBUFP(OBJ) SCM_TYP16_PREDICATE (tc16_jmpbuffer, OBJ)
  42. #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
  43. #define ACTIVATEJB(x) \
  44. (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) | (1L << 16L))))
  45. #define DEACTIVATEJB(x) \
  46. (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) & ~(1L << 16L))))
  47. #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
  48. #define SETJBJMPBUF(x, v) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (v)))
  49. #define SCM_JBDFRAME(x) ((scm_t_debug_frame *) SCM_CELL_WORD_2 (x))
  50. #define SCM_SETJBDFRAME(x, v) (SCM_SET_CELL_WORD_2 ((x), (scm_t_bits) (v)))
  51. #define SCM_JBPREUNWIND(x) ((struct pre_unwind_data *) SCM_CELL_WORD_3 (x))
  52. #define SCM_SETJBPREUNWIND(x, v) (SCM_SET_CELL_WORD_3 ((x), (scm_t_bits) (v)))
  53. static int
  54. jmpbuffer_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  55. {
  56. scm_puts ("#<jmpbuffer ", port);
  57. scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
  58. scm_uintprint((scm_t_bits) JBJMPBUF (exp), 16, port);
  59. scm_putc ('>', port);
  60. return 1 ;
  61. }
  62. static SCM
  63. make_jmpbuf (void)
  64. {
  65. SCM answer;
  66. SCM_NEWSMOB2 (answer, tc16_jmpbuffer, 0, 0);
  67. SETJBJMPBUF(answer, (jmp_buf *)0);
  68. DEACTIVATEJB(answer);
  69. return answer;
  70. }
  71. /* scm_c_catch (the guts of catch) */
  72. struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
  73. {
  74. jmp_buf buf; /* must be first */
  75. SCM throw_tag;
  76. SCM retval;
  77. };
  78. /* These are the structures we use to store pre-unwind handling (aka
  79. "lazy") information for a regular catch, and put on the wind list
  80. for a "lazy" catch. They store the pre-unwind handler function to
  81. call, and the data pointer to pass through to it. It's not a
  82. Scheme closure, but it is a function with data, so the term
  83. "closure" is appropriate in its broader sense.
  84. (We don't need anything like this to run the normal (post-unwind)
  85. catch handler, because the same C frame runs both the body and the
  86. handler.) */
  87. struct pre_unwind_data {
  88. scm_t_catch_handler handler;
  89. void *handler_data;
  90. int running;
  91. int lazy_catch_p;
  92. };
  93. /* scm_c_catch is the guts of catch. It handles all the mechanics of
  94. setting up a catch target, invoking the catch body, and perhaps
  95. invoking the handler if the body does a throw.
  96. The function is designed to be usable from C code, but is general
  97. enough to implement all the semantics Guile Scheme expects from
  98. throw.
  99. TAG is the catch tag. Typically, this is a symbol, but this
  100. function doesn't actually care about that.
  101. BODY is a pointer to a C function which runs the body of the catch;
  102. this is the code you can throw from. We call it like this:
  103. BODY (BODY_DATA)
  104. where:
  105. BODY_DATA is just the BODY_DATA argument we received; we pass it
  106. through to BODY as its first argument. The caller can make
  107. BODY_DATA point to anything useful that BODY might need.
  108. HANDLER is a pointer to a C function to deal with a throw to TAG,
  109. should one occur. We call it like this:
  110. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  111. where
  112. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  113. same idea as BODY_DATA above.
  114. THROWN_TAG is the tag that the user threw to; usually this is
  115. TAG, but it could be something else if TAG was #t (i.e., a
  116. catch-all), or the user threw to a jmpbuf.
  117. THROW_ARGS is the list of arguments the user passed to the THROW
  118. function, after the tag.
  119. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  120. is just a pointer we pass through to HANDLER. We don't actually
  121. use either of those pointers otherwise ourselves. The idea is
  122. that, if our caller wants to communicate something to BODY or
  123. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  124. HANDLER can then use. Think of it as a way to make BODY and
  125. HANDLER closures, not just functions; MUMBLE_DATA points to the
  126. enclosed variables.
  127. Of course, it's up to the caller to make sure that any data a
  128. MUMBLE_DATA needs is protected from GC. A common way to do this is
  129. to make MUMBLE_DATA a pointer to data stored in an automatic
  130. structure variable; since the collector must scan the stack for
  131. references anyway, this assures that any references in MUMBLE_DATA
  132. will be found. */
  133. SCM
  134. scm_c_catch (SCM tag,
  135. scm_t_catch_body body, void *body_data,
  136. scm_t_catch_handler handler, void *handler_data,
  137. scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data)
  138. {
  139. struct jmp_buf_and_retval jbr;
  140. SCM jmpbuf;
  141. SCM answer;
  142. struct pre_unwind_data pre_unwind;
  143. jmpbuf = make_jmpbuf ();
  144. answer = SCM_EOL;
  145. scm_i_set_dynwinds (scm_acons (tag, jmpbuf, scm_i_dynwinds ()));
  146. SETJBJMPBUF(jmpbuf, &jbr.buf);
  147. SCM_SETJBDFRAME(jmpbuf, scm_i_last_debug_frame ());
  148. pre_unwind.handler = pre_unwind_handler;
  149. pre_unwind.handler_data = pre_unwind_handler_data;
  150. pre_unwind.running = 0;
  151. pre_unwind.lazy_catch_p = 0;
  152. SCM_SETJBPREUNWIND(jmpbuf, &pre_unwind);
  153. if (setjmp (jbr.buf))
  154. {
  155. SCM throw_tag;
  156. SCM throw_args;
  157. #ifdef STACK_CHECKING
  158. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  159. #endif
  160. SCM_CRITICAL_SECTION_START;
  161. DEACTIVATEJB (jmpbuf);
  162. scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
  163. SCM_CRITICAL_SECTION_END;
  164. throw_args = jbr.retval;
  165. throw_tag = jbr.throw_tag;
  166. jbr.throw_tag = SCM_EOL;
  167. jbr.retval = SCM_EOL;
  168. answer = handler (handler_data, throw_tag, throw_args);
  169. }
  170. else
  171. {
  172. ACTIVATEJB (jmpbuf);
  173. answer = body (body_data);
  174. SCM_CRITICAL_SECTION_START;
  175. DEACTIVATEJB (jmpbuf);
  176. scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
  177. SCM_CRITICAL_SECTION_END;
  178. }
  179. return answer;
  180. }
  181. SCM
  182. scm_internal_catch (SCM tag,
  183. scm_t_catch_body body, void *body_data,
  184. scm_t_catch_handler handler, void *handler_data)
  185. {
  186. return scm_c_catch(tag,
  187. body, body_data,
  188. handler, handler_data,
  189. NULL, NULL);
  190. }
  191. /* The smob tag for pre_unwind_data smobs. */
  192. static scm_t_bits tc16_pre_unwind_data;
  193. /* Strictly speaking, we could just pass a zero for our print
  194. function, because we don't need to print them. They should never
  195. appear in normal data structures, only in the wind list. However,
  196. it might be nice for debugging someday... */
  197. static int
  198. pre_unwind_data_print (SCM closure, SCM port, scm_print_state *pstate SCM_UNUSED)
  199. {
  200. struct pre_unwind_data *c = (struct pre_unwind_data *) SCM_CELL_WORD_1 (closure);
  201. char buf[200];
  202. sprintf (buf, "#<pre-unwind-data 0x%lx 0x%lx>",
  203. (long) c->handler, (long) c->handler_data);
  204. scm_puts (buf, port);
  205. return 1;
  206. }
  207. /* Given a pointer to a pre_unwind_data structure, return a smob for it,
  208. suitable for inclusion in the wind list. ("Ah yes, a Château
  209. Gollombiere '72, non?"). */
  210. static SCM
  211. make_pre_unwind_data (struct pre_unwind_data *c)
  212. {
  213. SCM_RETURN_NEWSMOB (tc16_pre_unwind_data, c);
  214. }
  215. #define SCM_PRE_UNWIND_DATA_P(obj) (SCM_TYP16_PREDICATE (tc16_pre_unwind_data, obj))
  216. SCM
  217. scm_c_with_throw_handler (SCM tag,
  218. scm_t_catch_body body,
  219. void *body_data,
  220. scm_t_catch_handler handler,
  221. void *handler_data,
  222. int lazy_catch_p)
  223. {
  224. SCM pre_unwind, answer;
  225. struct pre_unwind_data c;
  226. c.handler = handler;
  227. c.handler_data = handler_data;
  228. c.running = 0;
  229. c.lazy_catch_p = lazy_catch_p;
  230. pre_unwind = make_pre_unwind_data (&c);
  231. SCM_CRITICAL_SECTION_START;
  232. scm_i_set_dynwinds (scm_acons (tag, pre_unwind, scm_i_dynwinds ()));
  233. SCM_CRITICAL_SECTION_END;
  234. answer = (*body) (body_data);
  235. SCM_CRITICAL_SECTION_START;
  236. scm_i_set_dynwinds (SCM_CDR (scm_i_dynwinds ()));
  237. SCM_CRITICAL_SECTION_END;
  238. return answer;
  239. }
  240. /* Exactly like scm_internal_catch, except:
  241. - It does not unwind the stack (this is the major difference).
  242. - The handler is not allowed to return. */
  243. SCM
  244. scm_internal_lazy_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data)
  245. {
  246. return scm_c_with_throw_handler (tag, body, body_data, handler, handler_data, 1);
  247. }
  248. /* scm_internal_stack_catch
  249. Use this one if you want debugging information to be stored in
  250. scm_the_last_stack_fluid_var on error. */
  251. static SCM
  252. ss_handler (void *data SCM_UNUSED, SCM tag, SCM throw_args)
  253. {
  254. /* Save the stack */
  255. scm_fluid_set_x (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var),
  256. scm_make_stack (SCM_BOOL_T, SCM_EOL));
  257. /* Throw the error */
  258. return scm_throw (tag, throw_args);
  259. }
  260. struct cwss_data
  261. {
  262. SCM tag;
  263. scm_t_catch_body body;
  264. void *data;
  265. };
  266. static SCM
  267. cwss_body (void *data)
  268. {
  269. struct cwss_data *d = data;
  270. return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
  271. }
  272. SCM
  273. scm_internal_stack_catch (SCM tag,
  274. scm_t_catch_body body,
  275. void *body_data,
  276. scm_t_catch_handler handler,
  277. void *handler_data)
  278. {
  279. struct cwss_data d;
  280. d.tag = tag;
  281. d.body = body;
  282. d.data = body_data;
  283. return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
  284. }
  285. /* body and handler functions for use with any of the above catch variants */
  286. /* This is a body function you can pass to scm_internal_catch if you
  287. want the body to be like Scheme's `catch' --- a thunk.
  288. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  289. contains the Scheme procedure to invoke as the body, and the tag
  290. we're catching. */
  291. SCM
  292. scm_body_thunk (void *body_data)
  293. {
  294. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  295. return scm_call_0 (c->body_proc);
  296. }
  297. /* This is a handler function you can pass to scm_internal_catch if
  298. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  299. applies a handler procedure to (TAG ARGS ...).
  300. If the user does a throw to this catch, this function runs a
  301. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  302. an SCM variable holding the Scheme procedure object to invoke. It
  303. ought to be a pointer to an automatic variable (i.e., one living on
  304. the stack), or the procedure object should be otherwise protected
  305. from GC. */
  306. SCM
  307. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  308. {
  309. SCM *handler_proc_p = (SCM *) handler_data;
  310. return scm_apply_1 (*handler_proc_p, tag, throw_args);
  311. }
  312. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  313. catches all throws that the handler might emit itself. The handler
  314. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  315. struct hbpca_data {
  316. SCM proc;
  317. SCM args;
  318. };
  319. static SCM
  320. hbpca_body (void *body_data)
  321. {
  322. struct hbpca_data *data = (struct hbpca_data *)body_data;
  323. return scm_apply_0 (data->proc, data->args);
  324. }
  325. SCM
  326. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  327. {
  328. SCM *handler_proc_p = (SCM *) handler_data;
  329. struct hbpca_data data;
  330. data.proc = *handler_proc_p;
  331. data.args = scm_cons (tag, throw_args);
  332. return scm_internal_catch (SCM_BOOL_T,
  333. hbpca_body, &data,
  334. scm_handle_by_message_noexit, NULL);
  335. }
  336. /* Derive the an exit status from the arguments to (quit ...). */
  337. int
  338. scm_exit_status (SCM args)
  339. {
  340. if (!SCM_NULL_OR_NIL_P (args))
  341. {
  342. SCM cqa = SCM_CAR (args);
  343. if (scm_is_integer (cqa))
  344. return (scm_to_int (cqa));
  345. else if (scm_is_false (cqa))
  346. return 1;
  347. }
  348. return 0;
  349. }
  350. static void
  351. handler_message (void *handler_data, SCM tag, SCM args)
  352. {
  353. char *prog_name = (char *) handler_data;
  354. SCM p = scm_current_error_port ();
  355. if (scm_ilength (args) == 4)
  356. {
  357. SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  358. SCM subr = SCM_CAR (args);
  359. SCM message = SCM_CADR (args);
  360. SCM parts = SCM_CADDR (args);
  361. SCM rest = SCM_CADDDR (args);
  362. if (SCM_BACKTRACE_P && scm_is_true (stack))
  363. {
  364. SCM highlights;
  365. if (scm_is_eq (tag, scm_arg_type_key)
  366. || scm_is_eq (tag, scm_out_of_range_key))
  367. highlights = rest;
  368. else
  369. highlights = SCM_EOL;
  370. scm_puts ("Backtrace:\n", p);
  371. scm_display_backtrace_with_highlights (stack, p,
  372. SCM_BOOL_F, SCM_BOOL_F,
  373. highlights);
  374. scm_newline (p);
  375. }
  376. scm_i_display_error (stack, p, subr, message, parts, rest);
  377. }
  378. else
  379. {
  380. if (! prog_name)
  381. prog_name = "guile";
  382. scm_puts (prog_name, p);
  383. scm_puts (": ", p);
  384. scm_puts ("uncaught throw to ", p);
  385. scm_prin1 (tag, p, 0);
  386. scm_puts (": ", p);
  387. scm_prin1 (args, p, 1);
  388. scm_putc ('\n', p);
  389. }
  390. }
  391. /* This is a handler function to use if you want scheme to print a
  392. message and die. Useful for dealing with throws to uncaught keys
  393. at the top level.
  394. At boot time, we establish a catch-all that uses this as its handler.
  395. 1) If the user wants something different, they can use (catch #t
  396. ...) to do what they like.
  397. 2) Outside the context of a read-eval-print loop, there isn't
  398. anything else good to do; libguile should not assume the existence
  399. of a read-eval-print loop.
  400. 3) Given that we shouldn't do anything complex, it's much more
  401. robust to do it in C code.
  402. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  403. message header to print; if zero, we use "guile" instead. That
  404. text is followed by a colon, then the message described by ARGS. */
  405. /* Dirk:FIXME:: The name of the function should make clear that the
  406. * application gets terminated.
  407. */
  408. SCM
  409. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  410. {
  411. if (scm_is_true (scm_eq_p (tag, scm_from_locale_symbol ("quit"))))
  412. exit (scm_exit_status (args));
  413. handler_message (handler_data, tag, args);
  414. scm_i_pthread_exit (NULL);
  415. /* this point not reached, but suppress gcc warning about no return value
  416. in case scm_i_pthread_exit isn't marked as "noreturn" (which seemed not
  417. to be the case on cygwin for instance) */
  418. return SCM_BOOL_F;
  419. }
  420. /* This is just like scm_handle_by_message, but it doesn't exit; it
  421. just returns #f. It's useful in cases where you don't really know
  422. enough about the body to handle things in a better way, but don't
  423. want to let throws fall off the bottom of the wind list. */
  424. SCM
  425. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  426. {
  427. if (scm_is_true (scm_eq_p (tag, scm_from_locale_symbol ("quit"))))
  428. exit (scm_exit_status (args));
  429. handler_message (handler_data, tag, args);
  430. return SCM_BOOL_F;
  431. }
  432. SCM
  433. scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
  434. {
  435. scm_ithrow (tag, args, 1);
  436. return SCM_UNSPECIFIED; /* never returns */
  437. }
  438. /* the Scheme-visible CATCH, WITH-THROW-HANDLER and LAZY-CATCH functions */
  439. SCM_DEFINE (scm_catch_with_pre_unwind_handler, "catch", 3, 1, 0,
  440. (SCM key, SCM thunk, SCM handler, SCM pre_unwind_handler),
  441. "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
  442. "exceptions matching @var{key}. If thunk throws to the symbol\n"
  443. "@var{key}, then @var{handler} is invoked this way:\n"
  444. "@lisp\n"
  445. "(handler key args ...)\n"
  446. "@end lisp\n"
  447. "\n"
  448. "@var{key} is a symbol or @code{#t}.\n"
  449. "\n"
  450. "@var{thunk} takes no arguments. If @var{thunk} returns\n"
  451. "normally, that is the return value of @code{catch}.\n"
  452. "\n"
  453. "Handler is invoked outside the scope of its own @code{catch}.\n"
  454. "If @var{handler} again throws to the same key, a new handler\n"
  455. "from further up the call chain is invoked.\n"
  456. "\n"
  457. "If the key is @code{#t}, then a throw to @emph{any} symbol will\n"
  458. "match this call to @code{catch}.\n"
  459. "\n"
  460. "If a @var{pre-unwind-handler} is given and @var{thunk} throws\n"
  461. "an exception that matches @var{key}, Guile calls the\n"
  462. "@var{pre-unwind-handler} before unwinding the dynamic state and\n"
  463. "invoking the main @var{handler}. @var{pre-unwind-handler} should\n"
  464. "be a procedure with the same signature as @var{handler}, that\n"
  465. "is @code{(lambda (key . args))}. It is typically used to save\n"
  466. "the stack at the point where the exception occurred, but can also\n"
  467. "query other parts of the dynamic state at that point, such as\n"
  468. "fluid values.\n"
  469. "\n"
  470. "A @var{pre-unwind-handler} can exit either normally or non-locally.\n"
  471. "If it exits normally, Guile unwinds the stack and dynamic context\n"
  472. "and then calls the normal (third argument) handler. If it exits\n"
  473. "non-locally, that exit determines the continuation.")
  474. #define FUNC_NAME s_scm_catch_with_pre_unwind_handler
  475. {
  476. struct scm_body_thunk_data c;
  477. SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
  478. key, SCM_ARG1, FUNC_NAME);
  479. c.tag = key;
  480. c.body_proc = thunk;
  481. /* scm_c_catch takes care of all the mechanics of setting up a catch
  482. key; we tell it to call scm_body_thunk to run the body, and
  483. scm_handle_by_proc to deal with any throws to this catch. The
  484. former receives a pointer to c, telling it how to behave. The
  485. latter receives a pointer to HANDLER, so it knows who to
  486. call. */
  487. return scm_c_catch (key,
  488. scm_body_thunk, &c,
  489. scm_handle_by_proc, &handler,
  490. SCM_UNBNDP (pre_unwind_handler) ? NULL : scm_handle_by_proc,
  491. &pre_unwind_handler);
  492. }
  493. #undef FUNC_NAME
  494. /* The following function exists to provide backwards compatibility
  495. for the C scm_catch API. Otherwise we could just change
  496. "scm_catch_with_pre_unwind_handler" above to "scm_catch". */
  497. SCM
  498. scm_catch (SCM key, SCM thunk, SCM handler)
  499. {
  500. return scm_catch_with_pre_unwind_handler (key, thunk, handler, SCM_UNDEFINED);
  501. }
  502. SCM_DEFINE (scm_with_throw_handler, "with-throw-handler", 3, 0, 0,
  503. (SCM key, SCM thunk, SCM handler),
  504. "Add @var{handler} to the dynamic context as a throw handler\n"
  505. "for key @var{key}, then invoke @var{thunk}.")
  506. #define FUNC_NAME s_scm_with_throw_handler
  507. {
  508. struct scm_body_thunk_data c;
  509. SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
  510. key, SCM_ARG1, FUNC_NAME);
  511. c.tag = key;
  512. c.body_proc = thunk;
  513. /* scm_c_with_throw_handler takes care of the mechanics of setting
  514. up a throw handler; we tell it to call scm_body_thunk to run the
  515. body, and scm_handle_by_proc to deal with any throws to this
  516. handler. The former receives a pointer to c, telling it how to
  517. behave. The latter receives a pointer to HANDLER, so it knows
  518. who to call. */
  519. return scm_c_with_throw_handler (key,
  520. scm_body_thunk, &c,
  521. scm_handle_by_proc, &handler,
  522. 0);
  523. }
  524. #undef FUNC_NAME
  525. SCM_DEFINE (scm_lazy_catch, "lazy-catch", 3, 0, 0,
  526. (SCM key, SCM thunk, SCM handler),
  527. "This behaves exactly like @code{catch}, except that it does\n"
  528. "not unwind the stack before invoking @var{handler}.\n"
  529. "If the @var{handler} procedure returns normally, Guile\n"
  530. "rethrows the same exception again to the next innermost catch,\n"
  531. "lazy-catch or throw handler. If the @var{handler} exits\n"
  532. "non-locally, that exit determines the continuation.")
  533. #define FUNC_NAME s_scm_lazy_catch
  534. {
  535. struct scm_body_thunk_data c;
  536. SCM_ASSERT (scm_is_symbol (key) || scm_is_eq (key, SCM_BOOL_T),
  537. key, SCM_ARG1, FUNC_NAME);
  538. c.tag = key;
  539. c.body_proc = thunk;
  540. /* scm_internal_lazy_catch takes care of all the mechanics of
  541. setting up a lazy catch key; we tell it to call scm_body_thunk to
  542. run the body, and scm_handle_by_proc to deal with any throws to
  543. this catch. The former receives a pointer to c, telling it how
  544. to behave. The latter receives a pointer to HANDLER, so it knows
  545. who to call. */
  546. return scm_internal_lazy_catch (key,
  547. scm_body_thunk, &c,
  548. scm_handle_by_proc, &handler);
  549. }
  550. #undef FUNC_NAME
  551. /* throwing */
  552. static void toggle_pre_unwind_running (void *data)
  553. {
  554. struct pre_unwind_data *pre_unwind = (struct pre_unwind_data *)data;
  555. pre_unwind->running = !pre_unwind->running;
  556. }
  557. SCM_DEFINE (scm_throw, "throw", 1, 0, 1,
  558. (SCM key, SCM args),
  559. "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
  560. "@var{handler}. \n\n"
  561. "@var{key} is a symbol. It will match catches of the same symbol or of\n"
  562. "@code{#t}.\n\n"
  563. "If there is no handler at all, Guile prints an error and then exits.")
  564. #define FUNC_NAME s_scm_throw
  565. {
  566. SCM_VALIDATE_SYMBOL (1, key);
  567. return scm_ithrow (key, args, 1);
  568. }
  569. #undef FUNC_NAME
  570. SCM
  571. scm_ithrow (SCM key, SCM args, int noreturn SCM_UNUSED)
  572. {
  573. SCM jmpbuf = SCM_UNDEFINED;
  574. SCM wind_goal;
  575. SCM dynpair = SCM_UNDEFINED;
  576. SCM winds;
  577. if (SCM_I_CURRENT_THREAD->critical_section_level)
  578. {
  579. fprintf (stderr, "throw from within critical section.\n");
  580. abort ();
  581. }
  582. rethrow:
  583. /* Search the wind list for an appropriate catch.
  584. "Waiter, please bring us the wind list." */
  585. for (winds = scm_i_dynwinds (); scm_is_pair (winds); winds = SCM_CDR (winds))
  586. {
  587. dynpair = SCM_CAR (winds);
  588. if (scm_is_pair (dynpair))
  589. {
  590. SCM this_key = SCM_CAR (dynpair);
  591. if (scm_is_eq (this_key, SCM_BOOL_T) || scm_is_eq (this_key, key))
  592. {
  593. jmpbuf = SCM_CDR (dynpair);
  594. if (!SCM_PRE_UNWIND_DATA_P (jmpbuf))
  595. break;
  596. else
  597. {
  598. struct pre_unwind_data *c =
  599. (struct pre_unwind_data *) SCM_CELL_WORD_1 (jmpbuf);
  600. if (!c->running)
  601. break;
  602. }
  603. }
  604. }
  605. }
  606. /* If we didn't find anything, print a message and abort the process
  607. right here. If you don't want this, establish a catch-all around
  608. any code that might throw up. */
  609. if (scm_is_null (winds))
  610. {
  611. scm_handle_by_message (NULL, key, args);
  612. abort ();
  613. }
  614. /* If the wind list is malformed, bail. */
  615. if (!scm_is_pair (winds))
  616. abort ();
  617. for (wind_goal = scm_i_dynwinds ();
  618. (!scm_is_pair (SCM_CAR (wind_goal))
  619. || !scm_is_eq (SCM_CDAR (wind_goal), jmpbuf));
  620. wind_goal = SCM_CDR (wind_goal))
  621. ;
  622. /* Is this a throw handler (or lazy catch)? In a wind list entry
  623. for a throw handler or lazy catch, the key is bound to a
  624. pre_unwind_data smob, not a jmpbuf. */
  625. if (SCM_PRE_UNWIND_DATA_P (jmpbuf))
  626. {
  627. struct pre_unwind_data *c =
  628. (struct pre_unwind_data *) SCM_CELL_WORD_1 (jmpbuf);
  629. SCM handle, answer;
  630. /* For old-style lazy-catch behaviour, we unwind the dynamic
  631. context before invoking the handler. */
  632. if (c->lazy_catch_p)
  633. {
  634. scm_dowinds (wind_goal, (scm_ilength (scm_i_dynwinds ())
  635. - scm_ilength (wind_goal)));
  636. SCM_CRITICAL_SECTION_START;
  637. handle = scm_i_dynwinds ();
  638. scm_i_set_dynwinds (SCM_CDR (handle));
  639. SCM_CRITICAL_SECTION_END;
  640. }
  641. /* Call the handler, with framing to set the pre-unwind
  642. structure's running field while the handler is running, so we
  643. can avoid recursing into the same handler again. Note that
  644. if the handler returns normally, the running flag stays
  645. set until some kind of non-local jump occurs. */
  646. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  647. scm_dynwind_rewind_handler (toggle_pre_unwind_running,
  648. c,
  649. SCM_F_WIND_EXPLICITLY);
  650. scm_dynwind_unwind_handler (toggle_pre_unwind_running, c, 0);
  651. answer = (c->handler) (c->handler_data, key, args);
  652. /* There is deliberately no scm_dynwind_end call here. This
  653. means that the unwind handler (toggle_pre_unwind_running)
  654. stays in place until a non-local exit occurs, and will then
  655. reset the pre-unwind structure's running flag. For sample
  656. code where this makes a difference, see the "again but with
  657. two chained throw handlers" test case in exceptions.test. */
  658. /* If the handler returns, rethrow the same key and args. */
  659. goto rethrow;
  660. }
  661. /* Otherwise, it's a normal catch. */
  662. else if (SCM_JMPBUFP (jmpbuf))
  663. {
  664. struct pre_unwind_data * pre_unwind;
  665. struct jmp_buf_and_retval * jbr;
  666. /* Before unwinding anything, run the pre-unwind handler if
  667. there is one, and if it isn't already running. */
  668. pre_unwind = SCM_JBPREUNWIND (jmpbuf);
  669. if (pre_unwind->handler && !pre_unwind->running)
  670. {
  671. /* Use framing to detect and avoid possible reentry into
  672. this handler, which could otherwise cause an infinite
  673. loop. */
  674. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  675. scm_dynwind_rewind_handler (toggle_pre_unwind_running,
  676. pre_unwind,
  677. SCM_F_WIND_EXPLICITLY);
  678. scm_dynwind_unwind_handler (toggle_pre_unwind_running,
  679. pre_unwind,
  680. SCM_F_WIND_EXPLICITLY);
  681. (pre_unwind->handler) (pre_unwind->handler_data, key, args);
  682. scm_dynwind_end ();
  683. }
  684. /* Now unwind and jump. */
  685. scm_dowinds (wind_goal, (scm_ilength (scm_i_dynwinds ())
  686. - scm_ilength (wind_goal)));
  687. jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
  688. jbr->throw_tag = key;
  689. jbr->retval = args;
  690. scm_i_set_last_debug_frame (SCM_JBDFRAME (jmpbuf));
  691. longjmp (*JBJMPBUF (jmpbuf), 1);
  692. }
  693. /* Otherwise, it's some random piece of junk. */
  694. else
  695. abort ();
  696. #ifdef __ia64__
  697. /* On IA64, we #define longjmp as setcontext, and GCC appears not to
  698. know that that doesn't return. */
  699. return SCM_UNSPECIFIED;
  700. #endif
  701. }
  702. void
  703. scm_init_throw ()
  704. {
  705. tc16_jmpbuffer = scm_make_smob_type ("jmpbuffer", 0);
  706. scm_set_smob_print (tc16_jmpbuffer, jmpbuffer_print);
  707. tc16_pre_unwind_data = scm_make_smob_type ("pre-unwind-data", 0);
  708. scm_set_smob_print (tc16_pre_unwind_data, pre_unwind_data_print);
  709. #include "libguile/throw.x"
  710. }
  711. /*
  712. Local Variables:
  713. c-file-style: "gnu"
  714. End:
  715. */