throw.c 20 KB

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