Commit | Line | Data |
---|---|---|
dbb605f5 | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000, 2001, 2002, 2006, 2008 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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
dbb605f5 LC |
20 | #ifdef HAVE_CONFIG_H |
21 | # include <config.h> | |
22 | #endif | |
0f2d19dd | 23 | |
783e7774 | 24 | #include <string.h> |
9de87eea MV |
25 | #include <stdio.h> |
26 | ||
a0599745 MD |
27 | #include "libguile/_scm.h" |
28 | #include "libguile/stackchk.h" | |
29 | #include "libguile/dynwind.h" | |
30 | #include "libguile/eval.h" | |
31 | #include "libguile/smob.h" | |
32 | #include "libguile/pairs.h" | |
33 | #include "libguile/throw.h" | |
34 | #include "libguile/fluids.h" | |
35 | #include "libguile/ports.h" | |
36 | ||
37 | #include "libguile/root.h" | |
0f2d19dd JB |
38 | \f |
39 | ||
40 | SCM scm_sys_protects[SCM_NUM_PROTECTS]; | |
d564d753 | 41 | |
d564d753 MD |
42 | \f |
43 | ||
1cc91f1b | 44 | /* {call-with-dynamic-root} |
d564d753 MD |
45 | * |
46 | * Suspending the current thread to evaluate a thunk on the | |
47 | * same C stack but under a new root. | |
48 | * | |
1cc91f1b | 49 | * Calls to call-with-dynamic-root return exactly once (unless |
e71575d9 | 50 | * the process is somehow exitted). */ |
d564d753 | 51 | |
e71575d9 MV |
52 | /* cwdr fills out both of these structures, and then passes a pointer |
53 | to them through scm_internal_catch to the cwdr_body and | |
54 | cwdr_handler functions, to tell them how to behave and to get | |
55 | information back from them. | |
650fa1ab JB |
56 | |
57 | A cwdr is a lot like a catch, except there is no tag (all | |
58 | exceptions are caught), and the body procedure takes the arguments | |
e71575d9 MV |
59 | passed to cwdr as A1 and ARGS. The handler is also special since |
60 | it is not directly run from scm_internal_catch. It is executed | |
61 | outside the new dynamic root. */ | |
650fa1ab JB |
62 | |
63 | struct cwdr_body_data { | |
650fa1ab JB |
64 | /* Arguments to pass to the cwdr body function. */ |
65 | SCM a1, args; | |
66 | ||
67 | /* Scheme procedure to use as body of cwdr. */ | |
68 | SCM body_proc; | |
e71575d9 MV |
69 | }; |
70 | ||
71 | struct cwdr_handler_data { | |
72 | /* Do we need to run the handler? */ | |
73 | int run_handler; | |
f032b8a8 | 74 | |
e71575d9 MV |
75 | /* The tag and args to pass it. */ |
76 | SCM tag, args; | |
650fa1ab JB |
77 | }; |
78 | ||
79 | ||
80 | /* Invoke the body of a cwdr, assuming that the throw handler has | |
81 | already been set up. DATA points to a struct set up by cwdr that | |
816a6f06 JB |
82 | says what proc to call, and what args to apply it to. |
83 | ||
84 | With a little thought, we could replace this with scm_body_thunk, | |
85 | but I don't want to mess with that at the moment. */ | |
650fa1ab | 86 | static SCM |
39752bec | 87 | cwdr_body (void *data) |
650fa1ab JB |
88 | { |
89 | struct cwdr_body_data *c = (struct cwdr_body_data *) data; | |
90 | ||
91 | return scm_apply (c->body_proc, c->a1, c->args); | |
92 | } | |
93 | ||
e71575d9 MV |
94 | /* Record the fact that the body of the cwdr has thrown. Record |
95 | enough information to invoke the handler later when the dynamic | |
96 | root has been deestablished. */ | |
650fa1ab | 97 | |
f032b8a8 | 98 | static SCM |
e71575d9 | 99 | cwdr_handler (void *data, SCM tag, SCM args) |
f032b8a8 | 100 | { |
e71575d9 | 101 | struct cwdr_handler_data *c = (struct cwdr_handler_data *) data; |
f032b8a8 | 102 | |
e71575d9 MV |
103 | c->run_handler = 1; |
104 | c->tag = tag; | |
105 | c->args = args; | |
106 | return SCM_UNSPECIFIED; | |
f032b8a8 | 107 | } |
d564d753 | 108 | |
e71575d9 | 109 | SCM |
92c2555f MV |
110 | scm_internal_cwdr (scm_t_catch_body body, void *body_data, |
111 | scm_t_catch_handler handler, void *handler_data, | |
e71575d9 | 112 | SCM_STACKITEM *stack_start) |
d564d753 | 113 | { |
e71575d9 | 114 | struct cwdr_handler_data my_handler_data; |
9de87eea | 115 | SCM answer, old_winds; |
d564d753 | 116 | |
8938d022 MD |
117 | /* Exit caller's dynamic state. |
118 | */ | |
9de87eea MV |
119 | old_winds = scm_i_dynwinds (); |
120 | scm_dowinds (SCM_EOL, scm_ilength (old_winds)); | |
121 | ||
661ae7ab MV |
122 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
123 | scm_dynwind_current_dynamic_state (scm_make_dynamic_state (SCM_UNDEFINED)); | |
e71575d9 | 124 | |
9de87eea MV |
125 | my_handler_data.run_handler = 0; |
126 | answer = scm_i_with_continuation_barrier (body, body_data, | |
43e01b1e NJ |
127 | cwdr_handler, &my_handler_data, |
128 | NULL, NULL); | |
9de87eea | 129 | |
661ae7ab | 130 | scm_dynwind_end (); |
9de87eea MV |
131 | |
132 | /* Enter caller's dynamic state. | |
133 | */ | |
d564d753 | 134 | scm_dowinds (old_winds, - scm_ilength (old_winds)); |
e71575d9 MV |
135 | |
136 | /* Now run the real handler iff the body did a throw. */ | |
137 | if (my_handler_data.run_handler) | |
138 | return handler (handler_data, my_handler_data.tag, my_handler_data.args); | |
139 | else | |
140 | return answer; | |
d564d753 MD |
141 | } |
142 | ||
e71575d9 MV |
143 | /* The original CWDR for invoking Scheme code with a Scheme handler. */ |
144 | ||
145 | static SCM | |
146 | cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start) | |
147 | { | |
148 | struct cwdr_body_data c; | |
149 | ||
150 | c.a1 = a1; | |
151 | c.args = args; | |
152 | c.body_proc = proc; | |
153 | ||
154 | return scm_internal_cwdr (cwdr_body, &c, | |
155 | scm_handle_by_proc, &handler, | |
156 | stack_start); | |
157 | } | |
d564d753 | 158 | |
3b3b36dd | 159 | SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0, |
1bbd0b84 | 160 | (SCM thunk, SCM handler), |
9eaf7f85 MV |
161 | "Call @var{thunk} with a new dynamic state and within" |
162 | "a continuation barrier. The @var{handler} catches all" | |
163 | "otherwise uncaught throws and executes within the same" | |
164 | "dynamic context as @var{thunk}.") | |
1bbd0b84 | 165 | #define FUNC_NAME s_scm_call_with_dynamic_root |
d564d753 MD |
166 | { |
167 | SCM_STACKITEM stack_place; | |
8938d022 | 168 | return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place); |
d564d753 | 169 | } |
1bbd0b84 | 170 | #undef FUNC_NAME |
d564d753 | 171 | |
3b3b36dd | 172 | SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0, |
1bbd0b84 | 173 | (), |
b380b885 | 174 | "Return an object representing the current dynamic root.\n\n" |
9de87eea | 175 | "These objects are only useful for comparison using @code{eq?}.\n") |
1bbd0b84 | 176 | #define FUNC_NAME s_scm_dynamic_root |
d564d753 | 177 | { |
9de87eea | 178 | return SCM_I_CURRENT_THREAD->continuation_root; |
d564d753 | 179 | } |
1bbd0b84 | 180 | #undef FUNC_NAME |
d564d753 | 181 | |
d564d753 | 182 | SCM |
1bbd0b84 | 183 | scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler) |
d564d753 MD |
184 | { |
185 | SCM_STACKITEM stack_place; | |
8938d022 | 186 | return cwdr (proc, a1, args, handler, &stack_place); |
d564d753 | 187 | } |
0f2d19dd JB |
188 | |
189 | \f | |
190 | ||
d564d753 MD |
191 | void |
192 | scm_init_root () | |
193 | { | |
a0599745 | 194 | #include "libguile/root.x" |
d564d753 | 195 | } |
89e00824 ML |
196 | |
197 | /* | |
198 | Local Variables: | |
199 | c-file-style: "gnu" | |
200 | End: | |
201 | */ |