root.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright (C) 1995,1996,1997,1998,1999,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 <string.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/stackchk.h"
  44. #include "libguile/dynwind.h"
  45. #include "libguile/eval.h"
  46. #include "libguile/smob.h"
  47. #include "libguile/pairs.h"
  48. #include "libguile/throw.h"
  49. #include "libguile/fluids.h"
  50. #include "libguile/ports.h"
  51. #include "libguile/root.h"
  52. SCM scm_sys_protects[SCM_NUM_PROTECTS];
  53. scm_t_bits scm_tc16_root;
  54. #ifndef USE_THREADS
  55. struct scm_root_state *scm_root;
  56. #endif
  57. static SCM
  58. root_mark (SCM root)
  59. {
  60. scm_root_state *s = SCM_ROOT_STATE (root);
  61. scm_gc_mark (s->rootcont);
  62. scm_gc_mark (s->dynwinds);
  63. scm_gc_mark (s->continuation_stack);
  64. scm_gc_mark (s->continuation_stack_ptr);
  65. scm_gc_mark (s->progargs);
  66. scm_gc_mark (s->exitval);
  67. scm_gc_mark (s->cur_inp);
  68. scm_gc_mark (s->cur_outp);
  69. scm_gc_mark (s->cur_errp);
  70. scm_gc_mark (s->def_inp);
  71. scm_gc_mark (s->def_outp);
  72. scm_gc_mark (s->def_errp);
  73. /* No need to gc mark def_loadp */
  74. scm_gc_mark (s->fluids);
  75. return SCM_ROOT_STATE (root) -> parent;
  76. }
  77. static int
  78. root_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  79. {
  80. scm_puts ("#<root ", port);
  81. scm_intprint(SCM_SEQ (SCM_ROOT_STATE (exp) -> rootcont), 16, port);
  82. scm_putc('>', port);
  83. return 1;
  84. }
  85. SCM
  86. scm_make_root (SCM parent)
  87. {
  88. SCM root;
  89. scm_root_state *root_state;
  90. root_state = (scm_root_state *) scm_must_malloc (sizeof (scm_root_state),
  91. "scm_make_root");
  92. if (SCM_ROOTP (parent))
  93. {
  94. memcpy (root_state, SCM_ROOT_STATE (parent), sizeof (scm_root_state));
  95. scm_copy_fluids (root_state);
  96. root_state->parent = parent;
  97. }
  98. else
  99. {
  100. root_state->parent = SCM_BOOL_F;
  101. /* Initialize everything right now, in case a GC happens early. */
  102. root_state->rootcont
  103. = root_state->dynwinds
  104. = root_state->continuation_stack
  105. = root_state->continuation_stack_ptr
  106. = root_state->progargs
  107. = root_state->exitval
  108. = root_state->cur_inp
  109. = root_state->cur_outp
  110. = root_state->cur_errp
  111. = root_state->def_inp
  112. = root_state->def_outp
  113. = root_state->def_errp
  114. = root_state->cur_loadp
  115. = root_state->fluids
  116. = root_state->handle
  117. = root_state->parent
  118. = SCM_BOOL_F;
  119. }
  120. SCM_REDEFER_INTS;
  121. SCM_NEWSMOB (root, scm_tc16_root, root_state);
  122. root_state->handle = root;
  123. SCM_REALLOW_INTS;
  124. return root;
  125. }
  126. /* {call-with-dynamic-root}
  127. *
  128. * Suspending the current thread to evaluate a thunk on the
  129. * same C stack but under a new root.
  130. *
  131. * Calls to call-with-dynamic-root return exactly once (unless
  132. * the process is somehow exitted). */
  133. /* Some questions about cwdr:
  134. Couldn't the body just be a closure? Do we really need to pass
  135. args through to it?
  136. The semantics are a lot like catch's; in fact, we call
  137. scm_internal_catch to take care of that part of things. Wouldn't
  138. it be cleaner to say that uncaught throws just disappear into the
  139. ether (or print a message to stderr), and let the caller use catch
  140. themselves if they want to?
  141. -JimB */
  142. #if 0
  143. SCM scm_exitval; /* INUM with return value */
  144. #endif
  145. static long n_dynamic_roots = 0;
  146. /* cwdr fills out both of these structures, and then passes a pointer
  147. to them through scm_internal_catch to the cwdr_body and
  148. cwdr_handler functions, to tell them how to behave and to get
  149. information back from them.
  150. A cwdr is a lot like a catch, except there is no tag (all
  151. exceptions are caught), and the body procedure takes the arguments
  152. passed to cwdr as A1 and ARGS. The handler is also special since
  153. it is not directly run from scm_internal_catch. It is executed
  154. outside the new dynamic root. */
  155. struct cwdr_body_data {
  156. /* Arguments to pass to the cwdr body function. */
  157. SCM a1, args;
  158. /* Scheme procedure to use as body of cwdr. */
  159. SCM body_proc;
  160. };
  161. struct cwdr_handler_data {
  162. /* Do we need to run the handler? */
  163. int run_handler;
  164. /* The tag and args to pass it. */
  165. SCM tag, args;
  166. };
  167. /* Invoke the body of a cwdr, assuming that the throw handler has
  168. already been set up. DATA points to a struct set up by cwdr that
  169. says what proc to call, and what args to apply it to.
  170. With a little thought, we could replace this with scm_body_thunk,
  171. but I don't want to mess with that at the moment. */
  172. static SCM
  173. cwdr_body (void *data)
  174. {
  175. struct cwdr_body_data *c = (struct cwdr_body_data *) data;
  176. return scm_apply (c->body_proc, c->a1, c->args);
  177. }
  178. /* Record the fact that the body of the cwdr has thrown. Record
  179. enough information to invoke the handler later when the dynamic
  180. root has been deestablished. */
  181. static SCM
  182. cwdr_handler (void *data, SCM tag, SCM args)
  183. {
  184. struct cwdr_handler_data *c = (struct cwdr_handler_data *) data;
  185. c->run_handler = 1;
  186. c->tag = tag;
  187. c->args = args;
  188. return SCM_UNSPECIFIED;
  189. }
  190. /* This is the basic code for new root creation.
  191. *
  192. * WARNING! The order of actions in this routine is in many ways
  193. * critical. E. g., it is essential that an error doesn't leave Guile
  194. * in a messed up state. */
  195. SCM
  196. scm_internal_cwdr (scm_t_catch_body body, void *body_data,
  197. scm_t_catch_handler handler, void *handler_data,
  198. SCM_STACKITEM *stack_start)
  199. {
  200. int old_ints_disabled = scm_ints_disabled;
  201. SCM old_rootcont, old_winds;
  202. struct cwdr_handler_data my_handler_data;
  203. SCM answer;
  204. /* Create a fresh root continuation. */
  205. {
  206. SCM new_rootcont;
  207. SCM_REDEFER_INTS;
  208. {
  209. scm_t_contregs *contregs = scm_must_malloc (sizeof (scm_t_contregs),
  210. "inferior root continuation");
  211. contregs->num_stack_items = 0;
  212. contregs->dynenv = SCM_EOL;
  213. contregs->base = stack_start;
  214. contregs->seq = ++n_dynamic_roots;
  215. contregs->throw_value = SCM_BOOL_F;
  216. #ifdef DEBUG_EXTENSIONS
  217. contregs->dframe = 0;
  218. #endif
  219. SCM_NEWSMOB (new_rootcont, scm_tc16_continuation, contregs);
  220. }
  221. old_rootcont = scm_rootcont;
  222. scm_rootcont = new_rootcont;
  223. SCM_REALLOW_INTS;
  224. }
  225. /* Exit caller's dynamic state.
  226. */
  227. old_winds = scm_dynwinds;
  228. scm_dowinds (SCM_EOL, scm_ilength (scm_dynwinds));
  229. #ifdef DEBUG_EXTENSIONS
  230. SCM_DFRAME (old_rootcont) = scm_last_debug_frame;
  231. scm_last_debug_frame = 0;
  232. #endif
  233. {
  234. my_handler_data.run_handler = 0;
  235. answer = scm_internal_catch (SCM_BOOL_T,
  236. body, body_data,
  237. cwdr_handler, &my_handler_data);
  238. }
  239. scm_dowinds (old_winds, - scm_ilength (old_winds));
  240. SCM_REDEFER_INTS;
  241. #ifdef DEBUG_EXTENSIONS
  242. scm_last_debug_frame = SCM_DFRAME (old_rootcont);
  243. #endif
  244. scm_rootcont = old_rootcont;
  245. SCM_REALLOW_INTS;
  246. scm_ints_disabled = old_ints_disabled;
  247. /* Now run the real handler iff the body did a throw. */
  248. if (my_handler_data.run_handler)
  249. return handler (handler_data, my_handler_data.tag, my_handler_data.args);
  250. else
  251. return answer;
  252. }
  253. /* The original CWDR for invoking Scheme code with a Scheme handler. */
  254. static SCM
  255. cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
  256. {
  257. struct cwdr_body_data c;
  258. c.a1 = a1;
  259. c.args = args;
  260. c.body_proc = proc;
  261. return scm_internal_cwdr (cwdr_body, &c,
  262. scm_handle_by_proc, &handler,
  263. stack_start);
  264. }
  265. SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
  266. (SCM thunk, SCM handler),
  267. "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n"
  268. "If an error occurs during evaluation, apply @var{handler} to the\n"
  269. "arguments to the throw, just as @code{throw} would. If this happens,\n"
  270. "@var{handler} is called outside the scope of the new root -- it is\n"
  271. "called in the same dynamic context in which\n"
  272. "@code{call-with-dynamic-root} was evaluated.\n\n"
  273. "If @var{thunk} captures a continuation, the continuation is rooted at\n"
  274. "the call to @var{thunk}. In particular, the call to\n"
  275. "@code{call-with-dynamic-root} is not captured. Therefore,\n"
  276. "@code{call-with-dynamic-root} always returns at most one time.\n\n"
  277. "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n"
  278. "the root and a new chain started for @var{thunk}. Therefore, this call\n"
  279. "may not do what you expect:\n\n"
  280. "@lisp\n"
  281. ";; Almost certainly a bug:\n"
  282. "(with-output-to-port\n"
  283. " some-port\n\n"
  284. " (lambda ()\n"
  285. " (call-with-dynamic-root\n"
  286. " (lambda ()\n"
  287. " (display 'fnord)\n"
  288. " (newline))\n"
  289. " (lambda (errcode) errcode))))\n"
  290. "@end lisp\n\n"
  291. "The problem is, on what port will @samp{fnord} be displayed? You\n"
  292. "might expect that because of the @code{with-output-to-port} that\n"
  293. "it will be displayed on the port bound to @code{some-port}. But it\n"
  294. "probably won't -- before evaluating the thunk, dynamic winds are\n"
  295. "unwound, including those created by @code{with-output-to-port}.\n"
  296. "So, the standard output port will have been re-set to its default value\n"
  297. "before @code{display} is evaluated.\n\n"
  298. "(This function was added to Guile mostly to help calls to functions in C\n"
  299. "libraries that can not tolerate non-local exits or calls that return\n"
  300. "multiple times. If such functions call back to the interpreter, it should\n"
  301. "be under a new dynamic root.)")
  302. #define FUNC_NAME s_scm_call_with_dynamic_root
  303. {
  304. SCM_STACKITEM stack_place;
  305. return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
  306. }
  307. #undef FUNC_NAME
  308. SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
  309. (),
  310. "Return an object representing the current dynamic root.\n\n"
  311. "These objects are only useful for comparison using @code{eq?}.\n"
  312. "They are currently represented as numbers, but your code should\n"
  313. "in no way depend on this.")
  314. #define FUNC_NAME s_scm_dynamic_root
  315. {
  316. return scm_ulong2num (SCM_SEQ (scm_root->rootcont));
  317. }
  318. #undef FUNC_NAME
  319. SCM
  320. scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
  321. {
  322. SCM_STACKITEM stack_place;
  323. return cwdr (proc, a1, args, handler, &stack_place);
  324. }
  325. #if (SCM_DEBUG_DEPRECATED == 0)
  326. /* Call thunk(closure) underneath a top-level error handler.
  327. * If an error occurs, pass the exitval through err_filter and return it.
  328. * If no error occurs, return the value of thunk.
  329. */
  330. #ifdef _UNICOS
  331. typedef int setjmp_type;
  332. #else
  333. typedef long setjmp_type;
  334. #endif
  335. SCM
  336. scm_call_catching_errors (SCM (*thunk)(), SCM (*err_filter)(), void *closure)
  337. {
  338. SCM answer;
  339. setjmp_type i;
  340. #ifdef DEBUG_EXTENSIONS
  341. SCM_DFRAME (scm_rootcont) = scm_last_debug_frame;
  342. #endif
  343. i = setjmp (SCM_JMPBUF (scm_rootcont));
  344. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  345. if (!i)
  346. {
  347. scm_gc_heap_lock = 0;
  348. answer = thunk (closure);
  349. }
  350. else
  351. {
  352. scm_gc_heap_lock = 1;
  353. answer = err_filter (scm_exitval, closure);
  354. }
  355. return answer;
  356. }
  357. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  358. void
  359. scm_init_root ()
  360. {
  361. scm_tc16_root = scm_make_smob_type ("root", sizeof (struct scm_root_state));
  362. scm_set_smob_mark (scm_tc16_root, root_mark);
  363. scm_set_smob_print (scm_tc16_root, root_print);
  364. #include "libguile/root.x"
  365. }
  366. /*
  367. Local Variables:
  368. c-file-style: "gnu"
  369. End:
  370. */