~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to eval_jump.ci

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-c-*- */
 
2
/*
 
3
 * from eval.c
 
4
 */
 
5
 
 
6
#include "eval_intern.h"
 
7
 
 
8
NORETURN(static VALUE rb_f_throw _((int, VALUE *)));
 
9
 
 
10
/*
 
11
 *  call-seq:
 
12
 *     throw(symbol [, obj])
 
13
 *
 
14
 *  Transfers control to the end of the active +catch+ block
 
15
 *  waiting for _symbol_. Raises +NameError+ if there
 
16
 *  is no +catch+ block for the symbol. The optional second
 
17
 *  parameter supplies a return value for the +catch+ block,
 
18
 *  which otherwise defaults to +nil+. For examples, see
 
19
 *  <code>Kernel::catch</code>.
 
20
 */
 
21
 
 
22
static VALUE
 
23
rb_f_throw(int argc, VALUE *argv)
 
24
{
 
25
    VALUE tag, value;
 
26
    rb_thread_t *th = GET_THREAD();
 
27
    struct rb_vm_tag *tt = th->tag;
 
28
 
 
29
    rb_scan_args(argc, argv, "11", &tag, &value);
 
30
    tag = ID2SYM(rb_to_id(tag));
 
31
 
 
32
    while (tt) {
 
33
        if (tt->tag == tag) {
 
34
            tt->retval = value;
 
35
            break;
 
36
        }
 
37
        tt = tt->prev;
 
38
    }
 
39
    if (!tt) {
 
40
        rb_name_error(SYM2ID(tag), "uncaught throw `%s'",
 
41
                      rb_id2name(SYM2ID(tag)));
 
42
    }
 
43
    rb_trap_restore_mask();
 
44
    th->errinfo = tag;
 
45
 
 
46
    JUMP_TAG(TAG_THROW);
 
47
#ifndef __GNUC__
 
48
    return Qnil;                /* not reached */
 
49
#endif
 
50
}
 
51
 
 
52
void
 
53
rb_throw(const char *tag, VALUE val)
 
54
{
 
55
    VALUE argv[2];
 
56
 
 
57
    argv[0] = ID2SYM(rb_intern(tag));
 
58
    argv[1] = val;
 
59
    rb_f_throw(2, argv);
 
60
}
 
61
 
 
62
/*
 
63
 *  call-seq:
 
64
 *     catch(symbol) {| | block }  > obj
 
65
 *
 
66
 *  +catch+ executes its block. If a +throw+ is
 
67
 *  executed, Ruby searches up its stack for a +catch+ block
 
68
 *  with a tag corresponding to the +throw+'s
 
69
 *  _symbol_. If found, that block is terminated, and
 
70
 *  +catch+ returns the value given to +throw+. If
 
71
 *  +throw+ is not called, the block terminates normally, and
 
72
 *  the value of +catch+ is the value of the last expression
 
73
 *  evaluated. +catch+ expressions may be nested, and the
 
74
 *  +throw+ call need not be in lexical scope.
 
75
 *
 
76
 *     def routine(n)
 
77
 *       puts n
 
78
 *       throw :done if n <= 0
 
79
 *       routine(n-1)
 
80
 *     end
 
81
 *
 
82
 *
 
83
 *     catch(:done) { routine(3) }
 
84
 *
 
85
 *  <em>produces:</em>
 
86
 *
 
87
 *     3
 
88
 *     2
 
89
 *     1
 
90
 *     0
 
91
 */
 
92
 
 
93
static VALUE
 
94
rb_f_catch(VALUE dmy, VALUE tag)
 
95
{
 
96
    int state;
 
97
    VALUE val = Qnil;           /* OK */
 
98
    rb_thread_t *th = GET_THREAD();
 
99
 
 
100
    tag = ID2SYM(rb_to_id(tag));
 
101
    PUSH_TAG();
 
102
 
 
103
    th->tag->tag = tag;
 
104
 
 
105
    if ((state = EXEC_TAG()) == 0) {
 
106
        val = rb_yield_0(1, &tag);
 
107
    }
 
108
    else if (state == TAG_THROW && th->errinfo == tag) {
 
109
        val = th->tag->retval;
 
110
        th->errinfo = Qnil;
 
111
        state = 0;
 
112
    }
 
113
    POP_TAG();
 
114
    if (state)
 
115
        JUMP_TAG(state);
 
116
 
 
117
    return val;
 
118
}
 
119
 
 
120
static VALUE
 
121
catch_i(VALUE tag)
 
122
{
 
123
    return rb_funcall(Qnil, rb_intern("catch"), 1, tag);
 
124
}
 
125
 
 
126
VALUE
 
127
rb_catch(const char *tag, VALUE (*func)(), VALUE data)
 
128
{
 
129
    return rb_iterate((VALUE (*)_((VALUE)))catch_i, ID2SYM(rb_intern(tag)),
 
130
                      func, data);
 
131
}
 
132
 
 
133
 
 
134
/* exit */
 
135
 
 
136
static void call_end_proc _((VALUE data));
 
137
 
 
138
static void
 
139
call_end_proc(VALUE data)
 
140
{
 
141
    /* TODO: fix me */
 
142
    proc_invoke(data, rb_ary_new2(0), Qundef, 0);
 
143
}
 
144
 
 
145
/*
 
146
 *  call-seq:
 
147
 *     at_exit { block } -> proc
 
148
 *
 
149
 *  Converts _block_ to a +Proc+ object (and therefore
 
150
 *  binds it at the point of call) and registers it for execution when
 
151
 *  the program exits. If multiple handlers are registered, they are
 
152
 *  executed in reverse order of registration.
 
153
 *
 
154
 *     def do_at_exit(str1)
 
155
 *       at_exit { print str1 }
 
156
 *     end
 
157
 *     at_exit { puts "cruel world" }
 
158
 *     do_at_exit("goodbye ")
 
159
 *     exit
 
160
 *
 
161
 *  <em>produces:</em>
 
162
 *
 
163
 *     goodbye cruel world
 
164
 */
 
165
 
 
166
static VALUE
 
167
rb_f_at_exit(void)
 
168
{
 
169
    VALUE proc;
 
170
 
 
171
    if (!rb_block_given_p()) {
 
172
        rb_raise(rb_eArgError, "called without a block");
 
173
    }
 
174
    proc = rb_block_proc();
 
175
    rb_set_end_proc(call_end_proc, proc);
 
176
    return proc;
 
177
}
 
178
 
 
179
struct end_proc_data {
 
180
    void (*func) ();
 
181
    VALUE data;
 
182
    int safe;
 
183
    struct end_proc_data *next;
 
184
};
 
185
 
 
186
static struct end_proc_data *end_procs, *ephemeral_end_procs, *tmp_end_procs;
 
187
 
 
188
void
 
189
rb_set_end_proc(void (*func)(VALUE), VALUE data)
 
190
{
 
191
    struct end_proc_data *link = ALLOC(struct end_proc_data);
 
192
    struct end_proc_data **list;
 
193
    rb_thread_t *th = GET_THREAD();
 
194
 
 
195
    if (th->top_wrapper) {
 
196
        list = &ephemeral_end_procs;
 
197
    }
 
198
    else {
 
199
        list = &end_procs;
 
200
    }
 
201
    link->next = *list;
 
202
    link->func = func;
 
203
    link->data = data;
 
204
    link->safe = rb_safe_level();
 
205
    *list = link;
 
206
}
 
207
 
 
208
void
 
209
rb_mark_end_proc(void)
 
210
{
 
211
    struct end_proc_data *link;
 
212
 
 
213
    link = end_procs;
 
214
    while (link) {
 
215
        rb_gc_mark(link->data);
 
216
        link = link->next;
 
217
    }
 
218
    link = ephemeral_end_procs;
 
219
    while (link) {
 
220
        rb_gc_mark(link->data);
 
221
        link = link->next;
 
222
    }
 
223
    link = tmp_end_procs;
 
224
    while (link) {
 
225
        rb_gc_mark(link->data);
 
226
        link = link->next;
 
227
    }
 
228
}
 
229
 
 
230
void
 
231
rb_exec_end_proc(void)
 
232
{
 
233
    struct end_proc_data *link, *tmp;
 
234
    int status;
 
235
    volatile int safe = rb_safe_level();
 
236
 
 
237
    while (ephemeral_end_procs) {
 
238
        tmp_end_procs = link = ephemeral_end_procs;
 
239
        ephemeral_end_procs = 0;
 
240
        while (link) {
 
241
            PUSH_TAG();
 
242
            if ((status = EXEC_TAG()) == 0) {
 
243
                rb_set_safe_level_force(link->safe);
 
244
                (*link->func) (link->data);
 
245
            }
 
246
            POP_TAG();
 
247
            if (status) {
 
248
                error_handle(status);
 
249
            }
 
250
            tmp = link;
 
251
            tmp_end_procs = link = link->next;
 
252
            free(tmp);
 
253
        }
 
254
    }
 
255
    while (end_procs) {
 
256
        tmp_end_procs = link = end_procs;
 
257
        end_procs = 0;
 
258
        while (link) {
 
259
            PUSH_TAG();
 
260
            if ((status = EXEC_TAG()) == 0) {
 
261
                rb_set_safe_level_force(link->safe);
 
262
                (*link->func) (link->data);
 
263
            }
 
264
            POP_TAG();
 
265
            if (status) {
 
266
                error_handle(status);
 
267
            }
 
268
            tmp = link;
 
269
            tmp_end_procs = link = link->next;
 
270
            free(tmp);
 
271
        }
 
272
    }
 
273
    rb_set_safe_level_force(safe);
 
274
}
 
275
 
 
276
void
 
277
Init_jump(void)
 
278
{
 
279
    rb_define_global_function("catch", rb_f_catch, 1);
 
280
    rb_define_global_function("throw", rb_f_throw, -1);
 
281
    rb_define_global_function("at_exit", rb_f_at_exit, 0);
 
282
}