throw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include <stdio.h>
  23. #include <unistdio.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/smob.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/eq.h"
  28. #include "libguile/control.h"
  29. #include "libguile/deprecation.h"
  30. #include "libguile/backtrace.h"
  31. #include "libguile/debug.h"
  32. #include "libguile/stackchk.h"
  33. #include "libguile/stacks.h"
  34. #include "libguile/fluids.h"
  35. #include "libguile/ports.h"
  36. #include "libguile/validate.h"
  37. #include "libguile/vm.h"
  38. #include "libguile/throw.h"
  39. #include "libguile/init.h"
  40. #include "libguile/strings.h"
  41. #include "libguile/private-options.h"
  42. /* Pleasantly enough, the guts of catch are defined in Scheme, in terms
  43. of prompt, abort, and the %exception-handler fluid. Check boot-9 for
  44. the definitions.
  45. Still, it's useful to be able to throw unwind-only exceptions from C,
  46. for example so that we can recover from stack overflow. We also need
  47. to have an implementation of catch and throw handy before boot time.
  48. For that reason we have a parallel implementation of "catch" that
  49. uses the same fluids here. Throws from C still call out to Scheme
  50. though, so that pre-unwind handlers can be run. Getting the dynamic
  51. environment right for pre-unwind handlers is tricky, and it's
  52. important to have all of the implementation in one place.
  53. All of these function names and prototypes carry a fair bit of historical
  54. baggage. */
  55. static SCM throw_var;
  56. static SCM exception_handler_fluid;
  57. static SCM
  58. catch (SCM tag, SCM thunk, SCM handler, SCM pre_unwind_handler)
  59. {
  60. struct scm_vm *vp;
  61. SCM eh, prompt_tag;
  62. SCM res;
  63. scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
  64. SCM dynamic_state = SCM_I_CURRENT_THREAD->dynamic_state;
  65. scm_i_jmp_buf registers;
  66. scm_t_ptrdiff saved_stack_depth;
  67. if (!scm_is_eq (tag, SCM_BOOL_T) && !scm_is_symbol (tag))
  68. scm_wrong_type_arg ("catch", 1, tag);
  69. if (SCM_UNBNDP (handler))
  70. handler = SCM_BOOL_F;
  71. else if (!scm_is_true (scm_procedure_p (handler)))
  72. scm_wrong_type_arg ("catch", 3, handler);
  73. if (SCM_UNBNDP (pre_unwind_handler))
  74. pre_unwind_handler = SCM_BOOL_F;
  75. else if (!scm_is_true (scm_procedure_p (pre_unwind_handler)))
  76. scm_wrong_type_arg ("catch", 4, pre_unwind_handler);
  77. prompt_tag = scm_cons (SCM_INUM0, SCM_EOL);
  78. eh = scm_c_make_vector (4, SCM_BOOL_F);
  79. scm_c_vector_set_x (eh, 0, scm_fluid_ref (exception_handler_fluid));
  80. scm_c_vector_set_x (eh, 1, tag);
  81. scm_c_vector_set_x (eh, 2, prompt_tag);
  82. scm_c_vector_set_x (eh, 3, pre_unwind_handler);
  83. vp = scm_the_vm ();
  84. saved_stack_depth = vp->stack_top - vp->sp;
  85. /* Push the prompt and exception handler onto the dynamic stack. */
  86. scm_dynstack_push_prompt (dynstack,
  87. SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY,
  88. prompt_tag,
  89. vp->stack_top - vp->fp,
  90. saved_stack_depth,
  91. vp->ip,
  92. &registers);
  93. scm_dynstack_push_fluid (dynstack, exception_handler_fluid, eh,
  94. dynamic_state);
  95. if (SCM_I_SETJMP (registers))
  96. {
  97. /* A non-local return. */
  98. SCM args;
  99. scm_gc_after_nonlocal_exit ();
  100. /* FIXME: We know where the args will be on the stack; we could
  101. avoid consing them. */
  102. args = scm_i_prompt_pop_abort_args_x (vp, saved_stack_depth);
  103. /* Cdr past the continuation. */
  104. args = scm_cdr (args);
  105. return scm_apply_0 (handler, args);
  106. }
  107. res = scm_call_0 (thunk);
  108. scm_dynstack_unwind_fluid (dynstack, dynamic_state);
  109. scm_dynstack_pop (dynstack);
  110. return res;
  111. }
  112. static void
  113. default_exception_handler (SCM k, SCM args)
  114. {
  115. static int error_printing_error = 0;
  116. static int error_printing_fallback = 0;
  117. if (error_printing_fallback)
  118. fprintf (stderr, "\nFailed to print exception.\n");
  119. else if (error_printing_error)
  120. {
  121. fprintf (stderr, "\nError while printing exception:\n");
  122. error_printing_fallback = 1;
  123. fprintf (stderr, "Key: ");
  124. scm_write (k, scm_current_error_port ());
  125. fprintf (stderr, ", args: ");
  126. scm_write (args, scm_current_error_port ());
  127. scm_newline (scm_current_error_port ());
  128. }
  129. else
  130. {
  131. fprintf (stderr, "Uncaught exception:\n");
  132. error_printing_error = 1;
  133. scm_handle_by_message (NULL, k, args);
  134. }
  135. /* Normally we don't get here, because scm_handle_by_message will
  136. exit. */
  137. fprintf (stderr, "Aborting.\n");
  138. abort ();
  139. }
  140. /* A version of scm_abort_to_prompt_star that avoids the need to cons
  141. "tag" to "args", because we might be out of memory. */
  142. static void
  143. abort_to_prompt (SCM prompt_tag, SCM tag, SCM args)
  144. {
  145. SCM *argv;
  146. size_t i;
  147. long n;
  148. n = scm_ilength (args) + 1;
  149. argv = alloca (sizeof (SCM)*n);
  150. argv[0] = tag;
  151. for (i = 1; i < n; i++, args = scm_cdr (args))
  152. argv[i] = scm_car (args);
  153. scm_c_abort (scm_the_vm (), prompt_tag, n, argv, NULL);
  154. /* Oh, what, you're still here? The abort must have been reinstated. Actually,
  155. that's quite impossible, given that we're already in C-land here, so...
  156. abort! */
  157. abort ();
  158. }
  159. static SCM
  160. throw_without_pre_unwind (SCM tag, SCM args)
  161. {
  162. SCM eh;
  163. /* This function is not only the boot implementation of "throw", it is
  164. also called in response to resource allocation failures such as
  165. stack-overflow or out-of-memory. For that reason we need to be
  166. careful to avoid allocating memory. */
  167. for (eh = scm_fluid_ref (exception_handler_fluid);
  168. scm_is_true (eh);
  169. eh = scm_c_vector_ref (eh, 0))
  170. {
  171. SCM catch_key, prompt_tag;
  172. catch_key = scm_c_vector_ref (eh, 1);
  173. if (!scm_is_eq (catch_key, SCM_BOOL_T) && !scm_is_eq (catch_key, tag))
  174. continue;
  175. if (scm_is_true (scm_c_vector_ref (eh, 3)))
  176. {
  177. const char *key_chars;
  178. if (scm_i_is_narrow_symbol (tag))
  179. key_chars = scm_i_symbol_chars (tag);
  180. else
  181. key_chars = "(wide symbol)";
  182. fprintf (stderr, "Warning: Unwind-only `%s' exception; "
  183. "skipping pre-unwind handler.\n", key_chars);
  184. }
  185. prompt_tag = scm_c_vector_ref (eh, 2);
  186. if (scm_is_true (prompt_tag))
  187. abort_to_prompt (prompt_tag, tag, args);
  188. }
  189. default_exception_handler (tag, args);
  190. return SCM_UNSPECIFIED;
  191. }
  192. SCM
  193. scm_catch (SCM key, SCM thunk, SCM handler)
  194. {
  195. return catch (key, thunk, handler, SCM_UNDEFINED);
  196. }
  197. SCM
  198. scm_catch_with_pre_unwind_handler (SCM key, SCM thunk, SCM handler,
  199. SCM pre_unwind_handler)
  200. {
  201. return catch (key, thunk, handler, pre_unwind_handler);
  202. }
  203. SCM
  204. scm_with_throw_handler (SCM key, SCM thunk, SCM handler)
  205. {
  206. return catch (key, thunk, SCM_UNDEFINED, handler);
  207. }
  208. SCM
  209. scm_throw (SCM key, SCM args)
  210. {
  211. return scm_apply_1 (scm_variable_ref (throw_var), key, args);
  212. }
  213. /* Now some support for C bodies and catch handlers */
  214. static scm_t_bits tc16_catch_closure;
  215. enum {
  216. CATCH_CLOSURE_BODY,
  217. CATCH_CLOSURE_HANDLER
  218. };
  219. static SCM
  220. make_catch_body_closure (scm_t_catch_body body, void *body_data)
  221. {
  222. SCM ret;
  223. SCM_NEWSMOB2 (ret, tc16_catch_closure, body, body_data);
  224. SCM_SET_SMOB_FLAGS (ret, CATCH_CLOSURE_BODY);
  225. return ret;
  226. }
  227. static SCM
  228. make_catch_handler_closure (scm_t_catch_handler handler, void *handler_data)
  229. {
  230. SCM ret;
  231. SCM_NEWSMOB2 (ret, tc16_catch_closure, handler, handler_data);
  232. SCM_SET_SMOB_FLAGS (ret, CATCH_CLOSURE_HANDLER);
  233. return ret;
  234. }
  235. static SCM
  236. apply_catch_closure (SCM clo, SCM args)
  237. {
  238. void *data = (void*)SCM_SMOB_DATA_2 (clo);
  239. switch (SCM_SMOB_FLAGS (clo))
  240. {
  241. case CATCH_CLOSURE_BODY:
  242. {
  243. scm_t_catch_body body = (void*)SCM_SMOB_DATA (clo);
  244. return body (data);
  245. }
  246. case CATCH_CLOSURE_HANDLER:
  247. {
  248. scm_t_catch_handler handler = (void*)SCM_SMOB_DATA (clo);
  249. return handler (data, scm_car (args), scm_cdr (args));
  250. }
  251. default:
  252. abort ();
  253. }
  254. }
  255. /* TAG is the catch tag. Typically, this is a symbol, but this
  256. function doesn't actually care about that.
  257. BODY is a pointer to a C function which runs the body of the catch;
  258. this is the code you can throw from. We call it like this:
  259. BODY (BODY_DATA)
  260. where:
  261. BODY_DATA is just the BODY_DATA argument we received; we pass it
  262. through to BODY as its first argument. The caller can make
  263. BODY_DATA point to anything useful that BODY might need.
  264. HANDLER is a pointer to a C function to deal with a throw to TAG,
  265. should one occur. We call it like this:
  266. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  267. where
  268. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  269. same idea as BODY_DATA above.
  270. THROWN_TAG is the tag that the user threw to; usually this is
  271. TAG, but it could be something else if TAG was #t (i.e., a
  272. catch-all), or the user threw to a jmpbuf.
  273. THROW_ARGS is the list of arguments the user passed to the THROW
  274. function, after the tag.
  275. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  276. is just a pointer we pass through to HANDLER. We don't actually
  277. use either of those pointers otherwise ourselves. The idea is
  278. that, if our caller wants to communicate something to BODY or
  279. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  280. HANDLER can then use. Think of it as a way to make BODY and
  281. HANDLER closures, not just functions; MUMBLE_DATA points to the
  282. enclosed variables.
  283. Of course, it's up to the caller to make sure that any data a
  284. MUMBLE_DATA needs is protected from GC. A common way to do this is
  285. to make MUMBLE_DATA a pointer to data stored in an automatic
  286. structure variable; since the collector must scan the stack for
  287. references anyway, this assures that any references in MUMBLE_DATA
  288. will be found. */
  289. SCM
  290. scm_c_catch (SCM tag,
  291. scm_t_catch_body body, void *body_data,
  292. scm_t_catch_handler handler, void *handler_data,
  293. scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data)
  294. {
  295. SCM sbody, shandler, spre_unwind_handler;
  296. sbody = make_catch_body_closure (body, body_data);
  297. shandler = make_catch_handler_closure (handler, handler_data);
  298. if (pre_unwind_handler)
  299. spre_unwind_handler = make_catch_handler_closure (pre_unwind_handler,
  300. pre_unwind_handler_data);
  301. else
  302. spre_unwind_handler = SCM_UNDEFINED;
  303. return scm_catch_with_pre_unwind_handler (tag, sbody, shandler,
  304. spre_unwind_handler);
  305. }
  306. SCM
  307. scm_internal_catch (SCM tag,
  308. scm_t_catch_body body, void *body_data,
  309. scm_t_catch_handler handler, void *handler_data)
  310. {
  311. return scm_c_catch (tag,
  312. body, body_data,
  313. handler, handler_data,
  314. NULL, NULL);
  315. }
  316. SCM
  317. scm_c_with_throw_handler (SCM tag,
  318. scm_t_catch_body body,
  319. void *body_data,
  320. scm_t_catch_handler handler,
  321. void *handler_data,
  322. int lazy_catch_p)
  323. {
  324. SCM sbody, shandler;
  325. if (lazy_catch_p)
  326. scm_c_issue_deprecation_warning
  327. ("The LAZY_CATCH_P argument to `scm_c_with_throw_handler' is no longer.\n"
  328. "supported. Instead the handler will be invoked from within the dynamic\n"
  329. "context of the corresponding `throw'.\n"
  330. "\nTHIS COULD CHANGE YOUR PROGRAM'S BEHAVIOR.\n\n"
  331. "Please modify your program to pass 0 as the LAZY_CATCH_P argument,\n"
  332. "and adapt it (if necessary) to expect to be within the dynamic context\n"
  333. "of the throw.");
  334. sbody = make_catch_body_closure (body, body_data);
  335. shandler = make_catch_handler_closure (handler, handler_data);
  336. return scm_with_throw_handler (tag, sbody, shandler);
  337. }
  338. /* body and handler functions for use with any of the above catch variants */
  339. /* This is a body function you can pass to scm_internal_catch if you
  340. want the body to be like Scheme's `catch' --- a thunk.
  341. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  342. contains the Scheme procedure to invoke as the body, and the tag
  343. we're catching. */
  344. SCM
  345. scm_body_thunk (void *body_data)
  346. {
  347. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  348. return scm_call_0 (c->body_proc);
  349. }
  350. /* This is a handler function you can pass to scm_internal_catch if
  351. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  352. applies a handler procedure to (TAG ARGS ...).
  353. If the user does a throw to this catch, this function runs a
  354. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  355. an SCM variable holding the Scheme procedure object to invoke. It
  356. ought to be a pointer to an automatic variable (i.e., one living on
  357. the stack), or the procedure object should be otherwise protected
  358. from GC. */
  359. SCM
  360. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  361. {
  362. SCM *handler_proc_p = (SCM *) handler_data;
  363. return scm_apply_1 (*handler_proc_p, tag, throw_args);
  364. }
  365. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  366. catches all throws that the handler might emit itself. The handler
  367. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  368. struct hbpca_data {
  369. SCM proc;
  370. SCM args;
  371. };
  372. static SCM
  373. hbpca_body (void *body_data)
  374. {
  375. struct hbpca_data *data = (struct hbpca_data *)body_data;
  376. return scm_apply_0 (data->proc, data->args);
  377. }
  378. SCM
  379. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  380. {
  381. SCM *handler_proc_p = (SCM *) handler_data;
  382. struct hbpca_data data;
  383. data.proc = *handler_proc_p;
  384. data.args = scm_cons (tag, throw_args);
  385. return scm_internal_catch (SCM_BOOL_T,
  386. hbpca_body, &data,
  387. scm_handle_by_message_noexit, NULL);
  388. }
  389. /* Derive the an exit status from the arguments to (quit ...). */
  390. int
  391. scm_exit_status (SCM args)
  392. {
  393. if (scm_is_pair (args))
  394. {
  395. SCM cqa = SCM_CAR (args);
  396. if (scm_is_integer (cqa))
  397. return (scm_to_int (cqa));
  398. else if (scm_is_false (cqa))
  399. return EXIT_FAILURE;
  400. else
  401. return EXIT_SUCCESS;
  402. }
  403. else if (scm_is_null (args))
  404. return EXIT_SUCCESS;
  405. else
  406. /* A type error. Strictly speaking we shouldn't get here. */
  407. return EXIT_FAILURE;
  408. }
  409. static int
  410. should_print_backtrace (SCM tag, SCM stack)
  411. {
  412. return SCM_BACKTRACE_P
  413. && scm_is_true (stack)
  414. && scm_initialized_p
  415. /* It's generally not useful to print backtraces for errors reading
  416. or expanding code in these fallback catch statements. */
  417. && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
  418. && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
  419. }
  420. static void
  421. handler_message (void *handler_data, SCM tag, SCM args)
  422. {
  423. SCM p, stack, frame;
  424. p = scm_current_error_port ();
  425. /* Usually we get here via a throw to a catch-all. In that case
  426. there is the throw frame active, and the catch closure, so narrow by
  427. two frames. It is possible for a user to invoke
  428. scm_handle_by_message directly, though, so it could be this
  429. narrows too much. We'll have to see how this works out in
  430. practice. */
  431. stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
  432. frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
  433. if (should_print_backtrace (tag, stack))
  434. {
  435. scm_puts ("Backtrace:\n", p);
  436. scm_display_backtrace_with_highlights (stack, p,
  437. SCM_BOOL_F, SCM_BOOL_F,
  438. SCM_EOL);
  439. scm_newline (p);
  440. }
  441. scm_print_exception (p, frame, tag, args);
  442. }
  443. /* This is a handler function to use if you want scheme to print a
  444. message and die. Useful for dealing with throws to uncaught keys
  445. at the top level.
  446. At boot time, we establish a catch-all that uses this as its handler.
  447. 1) If the user wants something different, they can use (catch #t
  448. ...) to do what they like.
  449. 2) Outside the context of a read-eval-print loop, there isn't
  450. anything else good to do; libguile should not assume the existence
  451. of a read-eval-print loop.
  452. 3) Given that we shouldn't do anything complex, it's much more
  453. robust to do it in C code.
  454. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  455. message header to print; if zero, we use "guile" instead. That
  456. text is followed by a colon, then the message described by ARGS. */
  457. /* Dirk:FIXME:: The name of the function should make clear that the
  458. * application gets terminated.
  459. */
  460. SCM
  461. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  462. {
  463. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  464. exit (scm_exit_status (args));
  465. handler_message (handler_data, tag, args);
  466. scm_i_pthread_exit (NULL);
  467. /* this point not reached, but suppress gcc warning about no return value
  468. in case scm_i_pthread_exit isn't marked as "noreturn" (which seemed not
  469. to be the case on cygwin for instance) */
  470. return SCM_BOOL_F;
  471. }
  472. /* This is just like scm_handle_by_message, but it doesn't exit; it
  473. just returns #f. It's useful in cases where you don't really know
  474. enough about the body to handle things in a better way, but don't
  475. want to let throws fall off the bottom of the wind list. */
  476. SCM
  477. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  478. {
  479. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  480. exit (scm_exit_status (args));
  481. handler_message (handler_data, tag, args);
  482. return SCM_BOOL_F;
  483. }
  484. SCM
  485. scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
  486. {
  487. scm_ithrow (tag, args, 1);
  488. return SCM_UNSPECIFIED; /* never returns */
  489. }
  490. SCM
  491. scm_ithrow (SCM key, SCM args, int no_return SCM_UNUSED)
  492. {
  493. return scm_throw (key, args);
  494. }
  495. SCM_SYMBOL (scm_stack_overflow_key, "stack-overflow");
  496. SCM_SYMBOL (scm_out_of_memory_key, "out-of-memory");
  497. static SCM stack_overflow_args = SCM_BOOL_F;
  498. static SCM out_of_memory_args = SCM_BOOL_F;
  499. /* Since these two functions may be called in response to resource
  500. exhaustion, we have to avoid allocating memory. */
  501. void
  502. scm_report_stack_overflow (void)
  503. {
  504. if (scm_is_false (stack_overflow_args))
  505. abort ();
  506. throw_without_pre_unwind (scm_stack_overflow_key, stack_overflow_args);
  507. /* Not reached. */
  508. abort ();
  509. }
  510. void
  511. scm_report_out_of_memory (void)
  512. {
  513. if (scm_is_false (out_of_memory_args))
  514. abort ();
  515. throw_without_pre_unwind (scm_out_of_memory_key, out_of_memory_args);
  516. /* Not reached. */
  517. abort ();
  518. }
  519. void
  520. scm_init_throw ()
  521. {
  522. tc16_catch_closure = scm_make_smob_type ("catch-closure", 0);
  523. scm_set_smob_apply (tc16_catch_closure, apply_catch_closure, 0, 0, 1);
  524. exception_handler_fluid = scm_make_fluid_with_default (SCM_BOOL_F);
  525. /* This binding is later removed when the Scheme definitions of catch,
  526. throw, and with-throw-handler are created in boot-9.scm. */
  527. scm_c_define ("%exception-handler", exception_handler_fluid);
  528. scm_c_define ("catch", scm_c_make_gsubr ("catch", 3, 1, 0, catch));
  529. throw_var = scm_c_define ("throw", scm_c_make_gsubr ("throw", 1, 0, 1,
  530. throw_without_pre_unwind));
  531. /* Arguments as if from:
  532. scm_error (stack-overflow, NULL, "Stack overflow", #f, #f);
  533. We build the arguments manually because we throw without running
  534. pre-unwind handlers. (Pre-unwind handlers could rewind the
  535. stack.) */
  536. stack_overflow_args = scm_list_4 (SCM_BOOL_F,
  537. scm_from_latin1_string ("Stack overflow"),
  538. SCM_BOOL_F,
  539. SCM_BOOL_F);
  540. out_of_memory_args = scm_list_4 (SCM_BOOL_F,
  541. scm_from_latin1_string ("Out of memory"),
  542. SCM_BOOL_F,
  543. SCM_BOOL_F);
  544. #include "libguile/throw.x"
  545. }
  546. /*
  547. Local Variables:
  548. c-file-style: "gnu"
  549. End:
  550. */