Commit | Line | Data |
---|---|---|
d4719ab8 | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000, 2001, 2002 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
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. | |
0f2d19dd | 7 | * |
73be1d9e MV |
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. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd JB |
19 | \f |
20 | ||
783e7774 | 21 | #include <string.h> |
9de87eea MV |
22 | #include <stdio.h> |
23 | ||
a0599745 MD |
24 | #include "libguile/_scm.h" |
25 | #include "libguile/stackchk.h" | |
26 | #include "libguile/dynwind.h" | |
27 | #include "libguile/eval.h" | |
28 | #include "libguile/smob.h" | |
29 | #include "libguile/pairs.h" | |
30 | #include "libguile/throw.h" | |
31 | #include "libguile/fluids.h" | |
32 | #include "libguile/ports.h" | |
33 | ||
34 | #include "libguile/root.h" | |
0f2d19dd JB |
35 | \f |
36 | ||
37 | SCM scm_sys_protects[SCM_NUM_PROTECTS]; | |
d564d753 | 38 | |
d564d753 MD |
39 | \f |
40 | ||
1cc91f1b | 41 | /* {call-with-dynamic-root} |
d564d753 MD |
42 | * |
43 | * Suspending the current thread to evaluate a thunk on the | |
44 | * same C stack but under a new root. | |
45 | * | |
1cc91f1b | 46 | * Calls to call-with-dynamic-root return exactly once (unless |
e71575d9 | 47 | * the process is somehow exitted). */ |
d564d753 | 48 | |
e71575d9 MV |
49 | /* cwdr fills out both of these structures, and then passes a pointer |
50 | to them through scm_internal_catch to the cwdr_body and | |
51 | cwdr_handler functions, to tell them how to behave and to get | |
52 | information back from them. | |
650fa1ab JB |
53 | |
54 | A cwdr is a lot like a catch, except there is no tag (all | |
55 | exceptions are caught), and the body procedure takes the arguments | |
e71575d9 MV |
56 | passed to cwdr as A1 and ARGS. The handler is also special since |
57 | it is not directly run from scm_internal_catch. It is executed | |
58 | outside the new dynamic root. */ | |
650fa1ab JB |
59 | |
60 | struct cwdr_body_data { | |
650fa1ab JB |
61 | /* Arguments to pass to the cwdr body function. */ |
62 | SCM a1, args; | |
63 | ||
64 | /* Scheme procedure to use as body of cwdr. */ | |
65 | SCM body_proc; | |
e71575d9 MV |
66 | }; |
67 | ||
68 | struct cwdr_handler_data { | |
69 | /* Do we need to run the handler? */ | |
70 | int run_handler; | |
f032b8a8 | 71 | |
e71575d9 MV |
72 | /* The tag and args to pass it. */ |
73 | SCM tag, args; | |
650fa1ab JB |
74 | }; |
75 | ||
76 | ||
77 | /* Invoke the body of a cwdr, assuming that the throw handler has | |
78 | already been set up. DATA points to a struct set up by cwdr that | |
816a6f06 JB |
79 | says what proc to call, and what args to apply it to. |
80 | ||
81 | With a little thought, we could replace this with scm_body_thunk, | |
82 | but I don't want to mess with that at the moment. */ | |
650fa1ab | 83 | static SCM |
39752bec | 84 | cwdr_body (void *data) |
650fa1ab JB |
85 | { |
86 | struct cwdr_body_data *c = (struct cwdr_body_data *) data; | |
87 | ||
88 | return scm_apply (c->body_proc, c->a1, c->args); | |
89 | } | |
90 | ||
e71575d9 MV |
91 | /* Record the fact that the body of the cwdr has thrown. Record |
92 | enough information to invoke the handler later when the dynamic | |
93 | root has been deestablished. */ | |
650fa1ab | 94 | |
f032b8a8 | 95 | static SCM |
e71575d9 | 96 | cwdr_handler (void *data, SCM tag, SCM args) |
f032b8a8 | 97 | { |
e71575d9 | 98 | struct cwdr_handler_data *c = (struct cwdr_handler_data *) data; |
f032b8a8 | 99 | |
e71575d9 MV |
100 | c->run_handler = 1; |
101 | c->tag = tag; | |
102 | c->args = args; | |
103 | return SCM_UNSPECIFIED; | |
f032b8a8 | 104 | } |
d564d753 | 105 | |
e71575d9 | 106 | SCM |
92c2555f MV |
107 | scm_internal_cwdr (scm_t_catch_body body, void *body_data, |
108 | scm_t_catch_handler handler, void *handler_data, | |
e71575d9 | 109 | SCM_STACKITEM *stack_start) |
d564d753 | 110 | { |
e71575d9 | 111 | struct cwdr_handler_data my_handler_data; |
9de87eea | 112 | SCM answer, old_winds; |
d564d753 | 113 | |
8938d022 MD |
114 | /* Exit caller's dynamic state. |
115 | */ | |
9de87eea MV |
116 | old_winds = scm_i_dynwinds (); |
117 | scm_dowinds (SCM_EOL, scm_ilength (old_winds)); | |
118 | ||
119 | scm_frame_begin (SCM_F_FRAME_REWINDABLE); | |
120 | scm_frame_current_dynamic_state (scm_make_dynamic_state (SCM_UNDEFINED)); | |
e71575d9 | 121 | |
9de87eea MV |
122 | my_handler_data.run_handler = 0; |
123 | answer = scm_i_with_continuation_barrier (body, body_data, | |
124 | cwdr_handler, &my_handler_data); | |
125 | ||
126 | scm_frame_end (); | |
127 | ||
128 | /* Enter caller's dynamic state. | |
129 | */ | |
d564d753 | 130 | scm_dowinds (old_winds, - scm_ilength (old_winds)); |
e71575d9 MV |
131 | |
132 | /* Now run the real handler iff the body did a throw. */ | |
133 | if (my_handler_data.run_handler) | |
134 | return handler (handler_data, my_handler_data.tag, my_handler_data.args); | |
135 | else | |
136 | return answer; | |
d564d753 MD |
137 | } |
138 | ||
e71575d9 MV |
139 | /* The original CWDR for invoking Scheme code with a Scheme handler. */ |
140 | ||
141 | static SCM | |
142 | cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start) | |
143 | { | |
144 | struct cwdr_body_data c; | |
145 | ||
146 | c.a1 = a1; | |
147 | c.args = args; | |
148 | c.body_proc = proc; | |
149 | ||
150 | return scm_internal_cwdr (cwdr_body, &c, | |
151 | scm_handle_by_proc, &handler, | |
152 | stack_start); | |
153 | } | |
d564d753 | 154 | |
3b3b36dd | 155 | SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0, |
1bbd0b84 | 156 | (SCM thunk, SCM handler), |
1bee0e70 | 157 | "Evaluate @code{(thunk)} in a new dynamic context, returning its value.\n\n" |
b380b885 MD |
158 | "If an error occurs during evaluation, apply @var{handler} to the\n" |
159 | "arguments to the throw, just as @code{throw} would. If this happens,\n" | |
160 | "@var{handler} is called outside the scope of the new root -- it is\n" | |
161 | "called in the same dynamic context in which\n" | |
162 | "@code{call-with-dynamic-root} was evaluated.\n\n" | |
163 | "If @var{thunk} captures a continuation, the continuation is rooted at\n" | |
164 | "the call to @var{thunk}. In particular, the call to\n" | |
165 | "@code{call-with-dynamic-root} is not captured. Therefore,\n" | |
166 | "@code{call-with-dynamic-root} always returns at most one time.\n\n" | |
167 | "Before calling @var{thunk}, the dynamic-wind chain is un-wound back to\n" | |
168 | "the root and a new chain started for @var{thunk}. Therefore, this call\n" | |
169 | "may not do what you expect:\n\n" | |
1e6808ea | 170 | "@lisp\n" |
b380b885 MD |
171 | ";; Almost certainly a bug:\n" |
172 | "(with-output-to-port\n" | |
173 | " some-port\n\n" | |
174 | " (lambda ()\n" | |
175 | " (call-with-dynamic-root\n" | |
176 | " (lambda ()\n" | |
177 | " (display 'fnord)\n" | |
178 | " (newline))\n" | |
179 | " (lambda (errcode) errcode))))\n" | |
1e6808ea | 180 | "@end lisp\n\n" |
2a2a730b | 181 | "The problem is, on what port will @samp{fnord} be displayed? You\n" |
b380b885 MD |
182 | "might expect that because of the @code{with-output-to-port} that\n" |
183 | "it will be displayed on the port bound to @code{some-port}. But it\n" | |
184 | "probably won't -- before evaluating the thunk, dynamic winds are\n" | |
185 | "unwound, including those created by @code{with-output-to-port}.\n" | |
186 | "So, the standard output port will have been re-set to its default value\n" | |
187 | "before @code{display} is evaluated.\n\n" | |
188 | "(This function was added to Guile mostly to help calls to functions in C\n" | |
189 | "libraries that can not tolerate non-local exits or calls that return\n" | |
190 | "multiple times. If such functions call back to the interpreter, it should\n" | |
191 | "be under a new dynamic root.)") | |
1bbd0b84 | 192 | #define FUNC_NAME s_scm_call_with_dynamic_root |
d564d753 MD |
193 | { |
194 | SCM_STACKITEM stack_place; | |
8938d022 | 195 | return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place); |
d564d753 | 196 | } |
1bbd0b84 | 197 | #undef FUNC_NAME |
d564d753 | 198 | |
3b3b36dd | 199 | SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0, |
1bbd0b84 | 200 | (), |
b380b885 | 201 | "Return an object representing the current dynamic root.\n\n" |
9de87eea | 202 | "These objects are only useful for comparison using @code{eq?}.\n") |
1bbd0b84 | 203 | #define FUNC_NAME s_scm_dynamic_root |
d564d753 | 204 | { |
9de87eea | 205 | return SCM_I_CURRENT_THREAD->continuation_root; |
d564d753 | 206 | } |
1bbd0b84 | 207 | #undef FUNC_NAME |
d564d753 | 208 | |
d564d753 | 209 | SCM |
1bbd0b84 | 210 | scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler) |
d564d753 MD |
211 | { |
212 | SCM_STACKITEM stack_place; | |
8938d022 | 213 | return cwdr (proc, a1, args, handler, &stack_place); |
d564d753 | 214 | } |
0f2d19dd JB |
215 | |
216 | \f | |
217 | ||
d564d753 MD |
218 | void |
219 | scm_init_root () | |
220 | { | |
a0599745 | 221 | #include "libguile/root.x" |
d564d753 | 222 | } |
89e00824 ML |
223 | |
224 | /* | |
225 | Local Variables: | |
226 | c-file-style: "gnu" | |
227 | End: | |
228 | */ |