throw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/smob.h"
  44. #include "libguile/alist.h"
  45. #include "libguile/eval.h"
  46. #include "libguile/eq.h"
  47. #include "libguile/dynwind.h"
  48. #include "libguile/backtrace.h"
  49. #ifdef DEBUG_EXTENSIONS
  50. #include "libguile/debug.h"
  51. #endif
  52. #include "libguile/continuations.h"
  53. #include "libguile/stackchk.h"
  54. #include "libguile/stacks.h"
  55. #include "libguile/fluids.h"
  56. #include "libguile/ports.h"
  57. #include "libguile/validate.h"
  58. #include "libguile/throw.h"
  59. /* the jump buffer data structure */
  60. static scm_t_bits tc16_jmpbuffer;
  61. #define SCM_JMPBUFP(OBJ) SCM_TYP16_PREDICATE (tc16_jmpbuffer, OBJ)
  62. #define JBACTIVE(OBJ) (SCM_CELL_WORD_0 (OBJ) & (1L << 16L))
  63. #define ACTIVATEJB(x) \
  64. (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) | (1L << 16L))))
  65. #define DEACTIVATEJB(x) \
  66. (SCM_SET_CELL_WORD_0 ((x), (SCM_CELL_WORD_0 (x) & ~(1L << 16L))))
  67. #define JBJMPBUF(OBJ) ((jmp_buf *) SCM_CELL_WORD_1 (OBJ))
  68. #define SETJBJMPBUF(x,v) (SCM_SET_CELL_WORD_1 ((x), (v)))
  69. #ifdef DEBUG_EXTENSIONS
  70. #define SCM_JBDFRAME(x) ((scm_t_debug_frame *) SCM_CELL_WORD_2 (x))
  71. #define SCM_SETJBDFRAME(x,v) (SCM_SET_CELL_WORD_2 ((x), (v)))
  72. #endif
  73. static int
  74. jmpbuffer_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  75. {
  76. scm_puts ("#<jmpbuffer ", port);
  77. scm_puts (JBACTIVE(exp) ? "(active) " : "(inactive) ", port);
  78. scm_intprint((long) JBJMPBUF (exp), 16, port);
  79. scm_putc ('>', port);
  80. return 1 ;
  81. }
  82. static SCM
  83. make_jmpbuf (void)
  84. {
  85. SCM answer;
  86. SCM_REDEFER_INTS;
  87. {
  88. #ifdef DEBUG_EXTENSIONS
  89. SCM_NEWSMOB2 (answer, tc16_jmpbuffer, 0, 0);
  90. #else
  91. SCM_NEWSMOB (answer, tc16_jmpbuffer, 0);
  92. #endif
  93. SETJBJMPBUF(answer, (jmp_buf *)0);
  94. DEACTIVATEJB(answer);
  95. }
  96. SCM_REALLOW_INTS;
  97. return answer;
  98. }
  99. /* scm_internal_catch (the guts of catch) */
  100. struct jmp_buf_and_retval /* use only on the stack, in scm_catch */
  101. {
  102. jmp_buf buf; /* must be first */
  103. SCM throw_tag;
  104. SCM retval;
  105. };
  106. /* scm_internal_catch is the guts of catch. It handles all the
  107. mechanics of setting up a catch target, invoking the catch body,
  108. and perhaps invoking the handler if the body does a throw.
  109. The function is designed to be usable from C code, but is general
  110. enough to implement all the semantics Guile Scheme expects from
  111. throw.
  112. TAG is the catch tag. Typically, this is a symbol, but this
  113. function doesn't actually care about that.
  114. BODY is a pointer to a C function which runs the body of the catch;
  115. this is the code you can throw from. We call it like this:
  116. BODY (BODY_DATA)
  117. where:
  118. BODY_DATA is just the BODY_DATA argument we received; we pass it
  119. through to BODY as its first argument. The caller can make
  120. BODY_DATA point to anything useful that BODY might need.
  121. HANDLER is a pointer to a C function to deal with a throw to TAG,
  122. should one occur. We call it like this:
  123. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  124. where
  125. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  126. same idea as BODY_DATA above.
  127. THROWN_TAG is the tag that the user threw to; usually this is
  128. TAG, but it could be something else if TAG was #t (i.e., a
  129. catch-all), or the user threw to a jmpbuf.
  130. THROW_ARGS is the list of arguments the user passed to the THROW
  131. function, after the tag.
  132. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  133. is just a pointer we pass through to HANDLER. We don't actually
  134. use either of those pointers otherwise ourselves. The idea is
  135. that, if our caller wants to communicate something to BODY or
  136. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  137. HANDLER can then use. Think of it as a way to make BODY and
  138. HANDLER closures, not just functions; MUMBLE_DATA points to the
  139. enclosed variables.
  140. Of course, it's up to the caller to make sure that any data a
  141. MUMBLE_DATA needs is protected from GC. A common way to do this is
  142. to make MUMBLE_DATA a pointer to data stored in an automatic
  143. structure variable; since the collector must scan the stack for
  144. references anyway, this assures that any references in MUMBLE_DATA
  145. will be found. */
  146. SCM
  147. scm_internal_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data)
  148. {
  149. struct jmp_buf_and_retval jbr;
  150. SCM jmpbuf;
  151. SCM answer;
  152. jmpbuf = make_jmpbuf ();
  153. answer = SCM_EOL;
  154. scm_dynwinds = scm_acons (tag, jmpbuf, scm_dynwinds);
  155. SETJBJMPBUF(jmpbuf, &jbr.buf);
  156. #ifdef DEBUG_EXTENSIONS
  157. SCM_SETJBDFRAME(jmpbuf, scm_last_debug_frame);
  158. #endif
  159. if (setjmp (jbr.buf))
  160. {
  161. SCM throw_tag;
  162. SCM throw_args;
  163. #ifdef STACK_CHECKING
  164. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  165. #endif
  166. SCM_REDEFER_INTS;
  167. DEACTIVATEJB (jmpbuf);
  168. scm_dynwinds = SCM_CDR (scm_dynwinds);
  169. SCM_REALLOW_INTS;
  170. throw_args = jbr.retval;
  171. throw_tag = jbr.throw_tag;
  172. jbr.throw_tag = SCM_EOL;
  173. jbr.retval = SCM_EOL;
  174. answer = handler (handler_data, throw_tag, throw_args);
  175. }
  176. else
  177. {
  178. ACTIVATEJB (jmpbuf);
  179. answer = body (body_data);
  180. SCM_REDEFER_INTS;
  181. DEACTIVATEJB (jmpbuf);
  182. scm_dynwinds = SCM_CDR (scm_dynwinds);
  183. SCM_REALLOW_INTS;
  184. }
  185. return answer;
  186. }
  187. /* scm_internal_lazy_catch (the guts of lazy catching) */
  188. /* The smob tag for lazy_catch smobs. */
  189. static scm_t_bits tc16_lazy_catch;
  190. /* This is the structure we put on the wind list for a lazy catch. It
  191. stores the handler function to call, and the data pointer to pass
  192. through to it. It's not a Scheme closure, but it is a function
  193. with data, so the term "closure" is appropriate in its broader
  194. sense.
  195. (We don't need anything like this in the "eager" catch code,
  196. because the same C frame runs both the body and the handler.) */
  197. struct lazy_catch {
  198. scm_t_catch_handler handler;
  199. void *handler_data;
  200. };
  201. /* Strictly speaking, we could just pass a zero for our print
  202. function, because we don't need to print them. They should never
  203. appear in normal data structures, only in the wind list. However,
  204. it might be nice for debugging someday... */
  205. static int
  206. lazy_catch_print (SCM closure, SCM port, scm_print_state *pstate SCM_UNUSED)
  207. {
  208. struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (closure);
  209. char buf[200];
  210. sprintf (buf, "#<lazy-catch 0x%lx 0x%lx>",
  211. (long) c->handler, (long) c->handler_data);
  212. scm_puts (buf, port);
  213. return 1;
  214. }
  215. /* Given a pointer to a lazy catch structure, return a smob for it,
  216. suitable for inclusion in the wind list. ("Ah yes, a Château
  217. Gollombiere '72, non?"). */
  218. static SCM
  219. make_lazy_catch (struct lazy_catch *c)
  220. {
  221. SCM_RETURN_NEWSMOB (tc16_lazy_catch, c);
  222. }
  223. #define SCM_LAZY_CATCH_P(obj) (SCM_TYP16_PREDICATE (tc16_lazy_catch, obj))
  224. /* Exactly like scm_internal_catch, except:
  225. - It does not unwind the stack (this is the major difference).
  226. - The handler is not allowed to return. */
  227. SCM
  228. scm_internal_lazy_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data)
  229. {
  230. SCM lazy_catch, answer;
  231. struct lazy_catch c;
  232. c.handler = handler;
  233. c.handler_data = handler_data;
  234. lazy_catch = make_lazy_catch (&c);
  235. SCM_REDEFER_INTS;
  236. scm_dynwinds = scm_acons (tag, lazy_catch, scm_dynwinds);
  237. SCM_REALLOW_INTS;
  238. answer = (*body) (body_data);
  239. SCM_REDEFER_INTS;
  240. scm_dynwinds = SCM_CDR (scm_dynwinds);
  241. SCM_REALLOW_INTS;
  242. return answer;
  243. }
  244. /* scm_internal_stack_catch
  245. Use this one if you want debugging information to be stored in
  246. scm_the_last_stack_fluid_var on error. */
  247. static SCM
  248. ss_handler (void *data SCM_UNUSED, SCM tag, SCM throw_args)
  249. {
  250. /* Save the stack */
  251. scm_fluid_set_x (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var),
  252. scm_make_stack (SCM_BOOL_T, SCM_EOL));
  253. /* Throw the error */
  254. return scm_throw (tag, throw_args);
  255. }
  256. struct cwss_data
  257. {
  258. SCM tag;
  259. scm_t_catch_body body;
  260. void *data;
  261. };
  262. static SCM
  263. cwss_body (void *data)
  264. {
  265. struct cwss_data *d = data;
  266. return scm_internal_lazy_catch (d->tag, d->body, d->data, ss_handler, NULL);
  267. }
  268. SCM
  269. scm_internal_stack_catch (SCM tag,
  270. scm_t_catch_body body,
  271. void *body_data,
  272. scm_t_catch_handler handler,
  273. void *handler_data)
  274. {
  275. struct cwss_data d;
  276. d.tag = tag;
  277. d.body = body;
  278. d.data = body_data;
  279. return scm_internal_catch (tag, cwss_body, &d, handler, handler_data);
  280. }
  281. /* body and handler functions for use with any of the above catch variants */
  282. /* This is a body function you can pass to scm_internal_catch if you
  283. want the body to be like Scheme's `catch' --- a thunk.
  284. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  285. contains the Scheme procedure to invoke as the body, and the tag
  286. we're catching. */
  287. SCM
  288. scm_body_thunk (void *body_data)
  289. {
  290. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  291. return scm_call_0 (c->body_proc);
  292. }
  293. /* This is a handler function you can pass to scm_internal_catch if
  294. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  295. applies a handler procedure to (TAG ARGS ...).
  296. If the user does a throw to this catch, this function runs a
  297. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  298. an SCM variable holding the Scheme procedure object to invoke. It
  299. ought to be a pointer to an automatic variable (i.e., one living on
  300. the stack), or the procedure object should be otherwise protected
  301. from GC. */
  302. SCM
  303. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  304. {
  305. SCM *handler_proc_p = (SCM *) handler_data;
  306. return scm_apply_1 (*handler_proc_p, tag, throw_args);
  307. }
  308. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  309. catches all throws that the handler might emit itself. The handler
  310. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  311. struct hbpca_data {
  312. SCM proc;
  313. SCM args;
  314. };
  315. static SCM
  316. hbpca_body (void *body_data)
  317. {
  318. struct hbpca_data *data = (struct hbpca_data *)body_data;
  319. return scm_apply_0 (data->proc, data->args);
  320. }
  321. SCM
  322. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  323. {
  324. SCM *handler_proc_p = (SCM *) handler_data;
  325. struct hbpca_data data;
  326. data.proc = *handler_proc_p;
  327. data.args = scm_cons (tag, throw_args);
  328. return scm_internal_catch (SCM_BOOL_T,
  329. hbpca_body, &data,
  330. scm_handle_by_message_noexit, NULL);
  331. }
  332. /* Derive the an exit status from the arguments to (quit ...). */
  333. int
  334. scm_exit_status (SCM args)
  335. {
  336. if (SCM_NNULLP (args))
  337. {
  338. SCM cqa = SCM_CAR (args);
  339. if (SCM_INUMP (cqa))
  340. return (SCM_INUM (cqa));
  341. else if (SCM_FALSEP (cqa))
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. static void
  347. handler_message (void *handler_data, SCM tag, SCM args)
  348. {
  349. char *prog_name = (char *) handler_data;
  350. SCM p = scm_cur_errp;
  351. if (scm_ilength (args) >= 3)
  352. {
  353. SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  354. SCM subr = SCM_CAR (args);
  355. SCM message = SCM_CADR (args);
  356. SCM parts = SCM_CADDR (args);
  357. SCM rest = SCM_CDDDR (args);
  358. if (SCM_BACKTRACE_P && SCM_NFALSEP (stack))
  359. {
  360. scm_puts ("Backtrace:\n", p);
  361. scm_display_backtrace (stack, p, SCM_UNDEFINED, SCM_UNDEFINED);
  362. scm_newline (p);
  363. }
  364. scm_i_display_error (stack, p, subr, message, parts, rest);
  365. }
  366. else
  367. {
  368. if (! prog_name)
  369. prog_name = "guile";
  370. scm_puts (prog_name, p);
  371. scm_puts (": ", p);
  372. scm_puts ("uncaught throw to ", p);
  373. scm_prin1 (tag, p, 0);
  374. scm_puts (": ", p);
  375. scm_prin1 (args, p, 1);
  376. scm_putc ('\n', p);
  377. }
  378. }
  379. /* This is a handler function to use if you want scheme to print a
  380. message and die. Useful for dealing with throws to uncaught keys
  381. at the top level.
  382. At boot time, we establish a catch-all that uses this as its handler.
  383. 1) If the user wants something different, they can use (catch #t
  384. ...) to do what they like.
  385. 2) Outside the context of a read-eval-print loop, there isn't
  386. anything else good to do; libguile should not assume the existence
  387. of a read-eval-print loop.
  388. 3) Given that we shouldn't do anything complex, it's much more
  389. robust to do it in C code.
  390. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  391. message header to print; if zero, we use "guile" instead. That
  392. text is followed by a colon, then the message described by ARGS. */
  393. /* Dirk:FIXME:: The name of the function should make clear that the
  394. * application gets terminated.
  395. */
  396. SCM
  397. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  398. {
  399. if (SCM_NFALSEP (scm_eq_p (tag, scm_str2symbol ("quit"))))
  400. {
  401. exit (scm_exit_status (args));
  402. }
  403. handler_message (handler_data, tag, args);
  404. exit (2);
  405. }
  406. /* This is just like scm_handle_by_message, but it doesn't exit; it
  407. just returns #f. It's useful in cases where you don't really know
  408. enough about the body to handle things in a better way, but don't
  409. want to let throws fall off the bottom of the wind list. */
  410. SCM
  411. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  412. {
  413. handler_message (handler_data, tag, args);
  414. return SCM_BOOL_F;
  415. }
  416. SCM
  417. scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
  418. {
  419. scm_ithrow (tag, args, 1);
  420. return SCM_UNSPECIFIED; /* never returns */
  421. }
  422. /* the Scheme-visible CATCH and LAZY-CATCH functions */
  423. SCM_DEFINE (scm_catch, "catch", 3, 0, 0,
  424. (SCM key, SCM thunk, SCM handler),
  425. "Invoke @var{thunk} in the dynamic context of @var{handler} for\n"
  426. "exceptions matching @var{key}. If thunk throws to the symbol\n"
  427. "@var{key}, then @var{handler} is invoked this way:\n"
  428. "@lisp\n"
  429. "(handler key args ...)\n"
  430. "@end lisp\n"
  431. "\n"
  432. "@var{key} is a symbol or @code{#t}.\n"
  433. "\n"
  434. "@var{thunk} takes no arguments. If @var{thunk} returns\n"
  435. "normally, that is the return value of @code{catch}.\n"
  436. "\n"
  437. "Handler is invoked outside the scope of its own @code{catch}.\n"
  438. "If @var{handler} again throws to the same key, a new handler\n"
  439. "from further up the call chain is invoked.\n"
  440. "\n"
  441. "If the key is @code{#t}, then a throw to @emph{any} symbol will\n"
  442. "match this call to @code{catch}.")
  443. #define FUNC_NAME s_scm_catch
  444. {
  445. struct scm_body_thunk_data c;
  446. SCM_ASSERT (SCM_SYMBOLP (key) || SCM_EQ_P (key, SCM_BOOL_T),
  447. key, SCM_ARG1, FUNC_NAME);
  448. c.tag = key;
  449. c.body_proc = thunk;
  450. /* scm_internal_catch takes care of all the mechanics of setting up
  451. a catch key; we tell it to call scm_body_thunk to run the body,
  452. and scm_handle_by_proc to deal with any throws to this catch.
  453. The former receives a pointer to c, telling it how to behave.
  454. The latter receives a pointer to HANDLER, so it knows who to call. */
  455. return scm_internal_catch (key,
  456. scm_body_thunk, &c,
  457. scm_handle_by_proc, &handler);
  458. }
  459. #undef FUNC_NAME
  460. SCM_DEFINE (scm_lazy_catch, "lazy-catch", 3, 0, 0,
  461. (SCM key, SCM thunk, SCM handler),
  462. "This behaves exactly like @code{catch}, except that it does\n"
  463. "not unwind the stack before invoking @var{handler}.\n"
  464. "The @var{handler} procedure is not allowed to return:\n"
  465. "it must throw to another catch, or otherwise exit non-locally.")
  466. #define FUNC_NAME s_scm_lazy_catch
  467. {
  468. struct scm_body_thunk_data c;
  469. SCM_ASSERT (SCM_SYMBOLP (key) || SCM_EQ_P (key, SCM_BOOL_T),
  470. key, SCM_ARG1, FUNC_NAME);
  471. c.tag = key;
  472. c.body_proc = thunk;
  473. /* scm_internal_lazy_catch takes care of all the mechanics of
  474. setting up a lazy catch key; we tell it to call scm_body_thunk to
  475. run the body, and scm_handle_by_proc to deal with any throws to
  476. this catch. The former receives a pointer to c, telling it how
  477. to behave. The latter receives a pointer to HANDLER, so it knows
  478. who to call. */
  479. return scm_internal_lazy_catch (key,
  480. scm_body_thunk, &c,
  481. scm_handle_by_proc, &handler);
  482. }
  483. #undef FUNC_NAME
  484. /* throwing */
  485. SCM_DEFINE (scm_throw, "throw", 1, 0, 1,
  486. (SCM key, SCM args),
  487. "Invoke the catch form matching @var{key}, passing @var{args} to the\n"
  488. "@var{handler}. \n\n"
  489. "@var{key} is a symbol. It will match catches of the same symbol or of\n"
  490. "@code{#t}.\n\n"
  491. "If there is no handler at all, Guile prints an error and then exits.")
  492. #define FUNC_NAME s_scm_throw
  493. {
  494. SCM_VALIDATE_SYMBOL (1,key);
  495. return scm_ithrow (key, args, 1);
  496. }
  497. #undef FUNC_NAME
  498. SCM
  499. scm_ithrow (SCM key, SCM args, int noreturn SCM_UNUSED)
  500. {
  501. SCM jmpbuf = SCM_UNDEFINED;
  502. SCM wind_goal;
  503. SCM dynpair = SCM_UNDEFINED;
  504. SCM winds;
  505. /* Search the wind list for an appropriate catch.
  506. "Waiter, please bring us the wind list." */
  507. for (winds = scm_dynwinds; SCM_CONSP (winds); winds = SCM_CDR (winds))
  508. {
  509. dynpair = SCM_CAR (winds);
  510. if (SCM_CONSP (dynpair))
  511. {
  512. SCM this_key = SCM_CAR (dynpair);
  513. if (SCM_EQ_P (this_key, SCM_BOOL_T) || SCM_EQ_P (this_key, key))
  514. break;
  515. }
  516. }
  517. /* If we didn't find anything, print a message and abort the process
  518. right here. If you don't want this, establish a catch-all around
  519. any code that might throw up. */
  520. if (SCM_NULLP (winds))
  521. {
  522. scm_handle_by_message (NULL, key, args);
  523. abort ();
  524. }
  525. /* If the wind list is malformed, bail. */
  526. /* remember calls added below to fix segfaults on ia64 and (I think)
  527. powerpc */
  528. if (!SCM_CONSP (winds))
  529. {
  530. scm_remember_upto_here_1 (winds);
  531. abort ();
  532. }
  533. scm_remember_upto_here_1 (winds);
  534. jmpbuf = SCM_CDR (dynpair);
  535. for (wind_goal = scm_dynwinds;
  536. !SCM_EQ_P (SCM_CDAR (wind_goal), jmpbuf);
  537. wind_goal = SCM_CDR (wind_goal))
  538. ;
  539. /* Is a lazy catch? In wind list entries for lazy catches, the key
  540. is bound to a lazy_catch smob, not a jmpbuf. */
  541. if (SCM_LAZY_CATCH_P (jmpbuf))
  542. {
  543. struct lazy_catch *c = (struct lazy_catch *) SCM_CELL_WORD_1 (jmpbuf);
  544. SCM handle, answer;
  545. scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
  546. - scm_ilength (wind_goal)));
  547. SCM_REDEFER_INTS;
  548. handle = scm_dynwinds;
  549. scm_dynwinds = SCM_CDR (scm_dynwinds);
  550. SCM_REALLOW_INTS;
  551. answer = (c->handler) (c->handler_data, key, args);
  552. scm_misc_error ("throw", "lazy-catch handler did return.", SCM_EOL);
  553. }
  554. /* Otherwise, it's a normal catch. */
  555. else if (SCM_JMPBUFP (jmpbuf))
  556. {
  557. struct jmp_buf_and_retval * jbr;
  558. scm_dowinds (wind_goal, (scm_ilength (scm_dynwinds)
  559. - scm_ilength (wind_goal)));
  560. jbr = (struct jmp_buf_and_retval *)JBJMPBUF (jmpbuf);
  561. jbr->throw_tag = key;
  562. jbr->retval = args;
  563. #ifdef DEBUG_EXTENSIONS
  564. scm_last_debug_frame = SCM_JBDFRAME (jmpbuf);
  565. #endif
  566. longjmp (*JBJMPBUF (jmpbuf), 1);
  567. }
  568. /* Otherwise, it's some random piece of junk. */
  569. else
  570. abort ();
  571. }
  572. void
  573. scm_init_throw ()
  574. {
  575. tc16_jmpbuffer = scm_make_smob_type ("jmpbuffer", 0);
  576. scm_set_smob_print (tc16_jmpbuffer, jmpbuffer_print);
  577. tc16_lazy_catch = scm_make_smob_type ("lazy-catch", 0);
  578. scm_set_smob_print (tc16_lazy_catch, lazy_catch_print);
  579. #include "libguile/throw.x"
  580. }
  581. /*
  582. Local Variables:
  583. c-file-style: "gnu"
  584. End:
  585. */